I’ve been struggling to implement a market sell/buy order using the python-binance library through API, despite attempting alternatives like ccxt. My progress has hit a snag.
I retrieve Binance’s order specifications data for a symbol using:
symbol_info = client.get_symbol_info(trading_pair)
So far, I’ve identified the following constraints for executing a market buy/sell order for a given trading pair:
- The quantity must fall within the specified min-max range.
- The quantity must be a multiple of the specified step size.
- The decimal precision of the quantity must not exceed the default precision.
Below is the code I’ve been working on. The objective of this question is to create a functional buy or sell order that sells 95% of the currency.
from binance.client import Client
from decimal import Decimal, ROUND_DOWN
import json
from k.binance import binance_api_key, binance_api_secret
# Load API keys
api_key = binance_api_key
api_secret = binance_api_secret
# Initialize client
client = Client(api_key, api_secret)
# Get trading pair
trading_pair = 'BTCUSDT'
# Get available asset balance
asset = 'USDT'
info = client.get_account()
asset_balance = None
for balance in info['balances']:
if balance['asset'] == asset:
asset_balance = float(balance['free'])
break
if asset_balance is None:
print(f"No balance available for asset: {asset}")
else:
# Calculate order quantity (95% of available balance)
order_quantity = 0.95 * asset_balance
# Get symbol info
symbol_info = client.get_symbol_info(trading_pair)
lot_size_filter = next(filter for filter in symbol_info['filters'] if filter['filterType'] == 'LOT_SIZE')
# Extract the step size, minQty and maxQty from the LOT_SIZE filter
step_size = Decimal(lot_size_filter['stepSize'])
min_qty = Decimal(lot_size_filter['minQty'])
max_qty = Decimal(lot_size_filter['maxQty'])
# Adjust quantity to match Binance standards
order_quantity = Decimal(order_quantity)
if step_size != 0.0:
order_quantity = step_size * round(order_quantity / step_size)
# Check if order quantity is within the minQty and maxQty limits
if order_quantity < min_qty or order_quantity > max_qty:
print(f"Order quantity {order_quantity} is outside the allowed range ({min_qty}, {max_qty})")
else:
# Adjust order quantity to the digit precision given by symbol info
base_asset_precision = int(symbol_info['baseAssetPrecision'])
order_quantity = order_quantity.quantize(Decimal(f'0.{"0" * base_asset_precision}'), rounding=ROUND_DOWN)
# Place market order
order = client.order_market_buy(
symbol=trading_pair,
quantity=float(order_quantity)
)
print(f"Market order placed: {order}")
The error (this time) is:
~/.venv/bin/python ~/myfile.py.py
Traceback (most recent call last):
File "~/myfile.py", line 54, in <module>
order = client.order_market_buy(
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 1614, in order_market_buy
return self.order_market(**params)
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 1586, in order_market
return self.create_order(**params)
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 1448, in create_order
return self._post('order', True, data=params)
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 418, in _post
return self._request_api('post', path, signed, version, **kwargs)
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 378, in _request_api
return self._request(method, uri, signed, **kwargs)
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 359, in _request
return self._handle_response(self.response)
File "~/.venv/lib/python3.10/site-packages/binance/client.py", line 368, in _handle_response
raise BinanceAPIException(response, response.status_code, response.text)
binance.exceptions.BinanceAPIException: APIError(code=-1013): Filter failure: MARKET_LOT_SIZE
Process finished with exit code 1
I’d greatly appreciate any insights or suggestions on how to troubleshoot this issue. Thanks in advance!
`
I am trying to place a real market buy/sell order in Binance and is so impossible!