0xCryp70DaV1D
0xCryp70DaV1D

Reputation: 11

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!

Upvotes: 1

Views: 4789

Answers (4)

user628904
user628904

Reputation: 746

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
            )

Upvotes: 0

NoahVerner
NoahVerner

Reputation: 875

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.

Upvotes: 0

Diego Rojas
Diego Rojas

Reputation: 19

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)

Upvotes: 1

Cyrille Lan Chua
Cyrille Lan Chua

Reputation: 26

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)

Upvotes: 0

Related Questions