Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place percentage orders with Binance API and Python-CCXT?

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

like image 361
kalikantus Avatar asked Jul 31 '19 00:07

kalikantus


People also ask

How does python connect to Binance API?

Connect to Binance Client Using Python To connect to the client just define your API and secret key variable and execute the client function. To verify that your keys are correct and that you're connected to Binance, execute this function that will ping the server.

Does Binance have a REST API?

More specifically, Binance has a RESTful API that uses HTTP requests to send and receive data. Further, there is also a WebSocket available that enables the streaming of data such as price quotes and account updates.


1 Answers

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
like image 190
rsg Avatar answered Sep 20 '22 19:09

rsg