python binance api order_market_buy 和 order_market_sell 的问题

发布于 2025-01-16 19:39:34 字数 370 浏览 1 评论 0原文

我正在尝试使用 order_market_buy 和 order_market_sell 来买入/卖出, 以BTCUSDT为例,买入时,我想用掉所有的USDT,卖出时,我想卖出所有的BTC。

我使用

order_buy = Client.order_market_buy(symbol='BTCUSDT', quoteOrderQty=my_USDT_position)
order_sell = Client.order_market_sell(symbol='BTCUSDT', quoteOrderQty=my_BTC_position)

它不起作用,并弹出“缺少 1 个必需的位置参数:'self'”

请帮助我解决问题,谢谢!

I am trying to use order_market_buy and order_market_sell to buy/sell,
taking BTCUSDT for example, when buying, I want to use all my usdt, when selling, I want to sell all the BTC.

I use

order_buy = Client.order_market_buy(symbol='BTCUSDT', quoteOrderQty=my_USDT_position)
order_sell = Client.order_market_sell(symbol='BTCUSDT', quoteOrderQty=my_BTC_position)

it's not working and pop"missing 1 required positional argument: 'self'"

Please help me with the problem, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慵挽 2025-01-23 19:39:34

您可以获取特定资产的当前余额,然后将其作为参数传递到 order_market_buy 方法中。

例子:

usdtBalance = client.get_asset_balance(asset='USDT').get('free')
btcBalance = client.get_asset_balance(asset='BTC').get('free')

order_buy = Client.order_market_buy(symbol='BTCUSDT', quantity=usdtBalance)

order_sell = Client.order_market_sell(symbol='BTCUSDT', quantity=btcBalance)

You can get you current balance of a specific asset then passes it as a parameter in the order_market_buy method.

Example:

usdtBalance = client.get_asset_balance(asset='USDT').get('free')
btcBalance = client.get_asset_balance(asset='BTC').get('free')

order_buy = Client.order_market_buy(symbol='BTCUSDT', quantity=usdtBalance)

order_sell = Client.order_market_sell(symbol='BTCUSDT', quantity=btcBalance)
天暗了我发光 2025-01-23 19:39:34

从他们的文档中:
https://python- binance.readthedocs.io/en/latest/binance.html?highlight=order_market_buy#binance.client.Client.order_market_buy

看来您做到了没有在 order_market_buy 和 order_market_sell 的函数调用中输入数量参数,这就是您收到错误的原因。数量和符号是这些函数的必需参数。

所以我认为要解决“缺少 1 个必需的位置参数:'self'”错误,您应该这样做:

order_buy = Client.order_market_buy(symbol='BTCUSDT', quantity=<your quantity>, quoteOrderQty=my_USDT_position)
order_sell = Client.order_market_sell(symbol='BTCUSDT', quantity=<your quantity>, quoteOrderQty=my_BTC_position)

From their documentation:
https://python-binance.readthedocs.io/en/latest/binance.html?highlight=order_market_buy#binance.client.Client.order_market_buy

It seems that you did not input a quantity argument in the function call of order_market_buy and order_market_sell, that is why you are getting an error. quantity and symbol are a required parameter of these functions.

So I think to solve the "missing 1 required positional argument: 'self'" error you should do:

order_buy = Client.order_market_buy(symbol='BTCUSDT', quantity=<your quantity>, quoteOrderQty=my_USDT_position)
order_sell = Client.order_market_sell(symbol='BTCUSDT', quantity=<your quantity>, quoteOrderQty=my_BTC_position)
狂之美人 2025-01-23 19:39:34

你也尝试过类似的事情吗?

usdtBalance = Client.get_asset_balance(asset='USDT').get('free') 

#use param quoteOrderQty instead of param quantity when buying
order_buy = Client.order_market_buy(symbol='BTCUSDT', quoteOrderQty=usdtBalance)

##Some time later##

btcBalance = Client.get_asset_balance(asset='BTC').get('free')

#use param quantity instead of param quoteOrderQty when selling
order_sell = Client.order_market_sell(symbol='BTCUSDT', quantity=btcBalance)

根据您的评论,似乎是Client.order_market_buy(symbol='BTCUSDT', quoteOrderQty=usdtBalance) 用于买入,但Client.order_market_sell(symbol='BTCUSDT',Quantity=btcBalance) 不适用于卖出。

我倾向于认为这是因为您存储在 btcBalance 中的信息是在初始化 btcBalance 后立即进行购买之前的,这是有意义的,因为这样您就可以存储< code>0.00 或只是"Dust" 这是基础资产的极少量,不能用于在币安上进行交易,而只能用于转换为 BNB

在此指南中,有一个使用Testnet Binance Vision,尽管它很好地说明了如何使用 Python Binance 包设置 Market Sell Order

from binance.exceptions import BinanceAPIException

api_key = '<testnet api_key>'
api_secret = '<testnet api_secret>'

async def main():

    quantity = '0.000001'
    client = await AsyncClient.create(api_key=api_key, api_secret=api_secret, testnet=True)

    try:
        market_res = await client.order_market_sell(symbol='BTCUSDT', quantity=quantity)
    except BinanceAPIException as e:
        print(e)
    else:
        print(json.dumps(market_res, indent=2))

    await client.close_connection()

它甚至说,如果存储在 quantity 中的值> 不大于MIN_NOTIONAL 值,您将收到以下错误:

APIError(code=-1013):过滤器失败:MIN_NOTIONAL

我建议你检查一下,它可能会帮助你更好地处理这个主题。

Did you also try something like this?

usdtBalance = Client.get_asset_balance(asset='USDT').get('free') 

#use param quoteOrderQty instead of param quantity when buying
order_buy = Client.order_market_buy(symbol='BTCUSDT', quoteOrderQty=usdtBalance)

##Some time later##

btcBalance = Client.get_asset_balance(asset='BTC').get('free')

#use param quantity instead of param quoteOrderQty when selling
order_sell = Client.order_market_sell(symbol='BTCUSDT', quantity=btcBalance)

Based on your comment, it seems that Client.order_market_buy(symbol='BTCUSDT', quoteOrderQty=usdtBalance) worked for buying, but Client.order_market_sell(symbol='BTCUSDT', quantity=btcBalance) did NOT for selling.

I tend to think it's because the information you stored in btcBalance was prior to the purchase you made right after initializing btcBalance, it would make sense since that way you would have stored 0.00 or just "Dust" which is a very low amount of the base asset that can't be used for trading on Binance but instead for just converting to BNB.

In this guide, there's an example using the Testnet Binance Vision, although it illustrates very well how to set a Market Sell Order with Python Binance package:

from binance.exceptions import BinanceAPIException

api_key = '<testnet api_key>'
api_secret = '<testnet api_secret>'

async def main():

    quantity = '0.000001'
    client = await AsyncClient.create(api_key=api_key, api_secret=api_secret, testnet=True)

    try:
        market_res = await client.order_market_sell(symbol='BTCUSDT', quantity=quantity)
    except BinanceAPIException as e:
        print(e)
    else:
        print(json.dumps(market_res, indent=2))

    await client.close_connection()

It even says that if the value stored in quantity is not greater than the MIN_NOTIONAL value, you will get the following error:

APIError(code=-1013): Filter failure: MIN_NOTIONAL

I recommend you to check it out, it may help you better when dealing with this topic.

胡大本事 2025-01-23 19:39:34

交易货币对(BTC 和 USDT)时,您可以在 quantity 参数中指定您想要买入/卖出的 BTC 金额,或者在 quoteOrderQty< 中指定您想要买入/卖出的 USDT /代码> 参数。

btc_balance = client.get_asset_balance(asset='BTC')
btc_balance = btc_balance['free']

usdt_balance = client.get_asset_balance(asset='USDT')
usdt_balance = usdt_balance['free']

# Sell BTC
sell_order = client.order_market_sell(
                 symbol='BTCUSDT', 
                 quantity=btc_balance
             )

# Buy BTC
buy_order = client.order_market_buy(
                 symbol='BTCUSDT', 
                 quoteOrderQty = usdt_balance
            )

When trading a pair (BTC and USDT) you can either specify the BTC amount you want to buy/sell in the quantity parameter, or the USDT you want to buy/sell in the quoteOrderQty parameter.

btc_balance = client.get_asset_balance(asset='BTC')
btc_balance = btc_balance['free']

usdt_balance = client.get_asset_balance(asset='USDT')
usdt_balance = usdt_balance['free']

# Sell BTC
sell_order = client.order_market_sell(
                 symbol='BTCUSDT', 
                 quantity=btc_balance
             )

# Buy BTC
buy_order = client.order_market_buy(
                 symbol='BTCUSDT', 
                 quoteOrderQty = usdt_balance
            )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文