Reputation: 123
I'm playing with Binance API to make my trading bot with Python 3.6. and CCXT library (here you find the docs ).
One very useful thing they have in their site is the capability to place orders for a percentage of your current balance:
for example if I'm lookin at BTC/USDT
crypto coin pair, and I have 50 USDT
on my account, I can choose between buying N
amount of BTC
or using 100%
of my account's USDT
for buying, consequently buying the maximum amount of BTC
I can.
I read the docs many times, but I can't find the option to do these "percentage of balance" orders with the API in any way: the only thing I can do is passing a float
to the order function.
This is how i place orders now:
amount = 0.001
symbol = "BTC/USDT"
def buyorder(amount, symbol): # this makes a market order taking in the amount I defined before, for the pair defined by "symbol"
type = 'market' # or 'limit'
side = 'buy' # or 'sell'
params = {} # extra params and overrides if needed
order = exchange.create_order(symbol, type, side, amount, params)
Do anyone know if there is a built-in capability to do a percentage order? If API gives no way to do so, would you suggest some workarounds?
I want to be able to give to the API percentage of my current balance as an amount
, so I can always use the full of it without having to update when fees are detracted
Upvotes: 9
Views: 16447
Reputation: 51
Use a custom function like this:
def get_max_position_available():
to_use = float(exchange.fetch_balance().get('USDT').get('free'))
price = float(exchange.fetchTicker('BTC/USDT').get('last'))
decide_position_to_use = to_use / price
return decide_position_to_use
Upvotes: 5
Reputation: 313
I don't know of any Binance API function that does that, but you can try something like this:
# You ask for the balance
balance= client.get_asset_balance(asset='USDT')
# set the percentage or fraction you want to invest in each order
portion_balance = float(balance['free']) * 0.35
# you assign the created variable in the quantity of your order
sell_market = client.order_market_sell(
symbol= 'ETHUSDT',
quantity= portion_balance)
Greetings ;)
Upvotes: 3