简体   繁体   中英

problem with python binance api order_market_buy and order_market_sell

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

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)

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM