How to use a percentage of your account balance per trade? Binance-python

556 views Asked by At

As far as I am aware the only way to create a new futures order is with client.futures_create_order(). The issue is this requires you to give it the exact amount of contracts you would like to buy. I am using BUSD to make all my trades, so I am wondering if there is any way to give it a BUSD amount or a percentage of the BUSD I have, instead of the amount of order contracts? If the answer is no then is there any way to calculate how many contracts to buy with a specified amount of BUSD or a percentage of the BUSD you have in your wallet.

I am currently using client.futures_create_order() to execute trades. I am using Binance hedge mode so to place a Long I use, side = 'BUY',positionSide: 'LONG'. Then to sell I keep the positionSide as 'LONG' but switch the side to 'SELL' and opposite for shorts. One of the requirements of using the create order is listing the amount of order contracts you want to buy or sell. But I would like to use 5% of my BUSD account balance per trade. I found a way to pull the price of the symbol I want to trade and then calculate how much 5% of my account balance is into BUSD then finally multiply it by the symbol price to get the number of order contracts to buy. But even that comes with its own issues, one being rounding. If I am buying DOGE Binance wont let me buy less than one contract so would need it to round to the nearest whole number. But if I am buying BTC obviously rounding to a whole number isn't helpful. Even if we can some how get around this issue there is one more. I am making more than one trade at a time, so lets say I place a Long with 5% of my BUSD balance which at the time lets say is $5. Within the time of buying and selling my account balance will change. So when it goes to place the sell order it again uses 5% but now that only $4.5 so there is still $0.5 in the trade open. If using a percentage isn't possible, being able to use a fixed amount of BUSD would still be a huge benefit over using a contract amount.

1

There are 1 answers

0
Edog On BEST ANSWER

for people that are having this issue I figured out a solution, it's not perfect but it works for me.

FIL3MIN_original_quantity=None

@app.route('/test', methods = ['POST'])
def test():
    data = json.loads(request.data)
    side = data['order_action']
    positionSide = data['positionSide']
    command = data['command']

global FIL3MIN_original_quantity

    if command == "FIL3MINBUY":
            key = "https://api.binance.com/api/v3/ticker/price?symbol=FILBUSD"
            hey = requests.get(key)  
            hey = hey.json()
            sym_price =(f"{hey['price']}")   
            ticker_price = float(sym_price)
            acc_balance = client.futures_account_balance()[8]
            balance_value = acc_balance['balance']
            balance_value = float(balance_value)
            BUSD_quantity = balance_value * 0.02
            quantitys = BUSD_quantity/ticker_price
            quantityss =quantitys*20  
            quantityss = round(quantityss, 1) 
            executed_order = client.futures_create_order(symbol='FILBUSD',side=side,positionSide=positionSide,
            type=ORDER_TYPE_MARKET, quantity=quantityss)   
            FIL3MIN_original_quantity = executed_order['origQty']
            if executed_order: return {"code": "success", "message": "order executed BUY",
            "amount bought": FIL3MIN_original_quantity}
            else: return{"code": "error","message": "order failed"}
        if command == "FIL3MINSELL":
            if FIL3MIN_original_quantity == None:return("No open position to close")
            else:executed_order2 = client.futures_create_order(symbol='FILBUSD',side=side,positionSide = positionSide ,
            type=ORDER_TYPE_MARKET, quantity=FIL3MIN_original_quantity)
            FIL3MIN_original_quantity=None
            if executed_order2: return {"code": "success","message": "order executed SELL"}
            else:return {"code": "error","message": "order failed"}

I know this looks like a complete mess but it works for me. to simplify what is going on the progragam first looks at the message it is give in which looks something like this. place the buy order.

{"passphrase": "abc123","order_action": "BUY","positionSide": "LONG", "command": "FIL3MINBUY"}

place the sell order.

{"passphrase": "abc123","order_action": "SELL","positionSide": "LONG", "command": "FIL3MINSELL"}

from that line of code it receives form trading view it then calculated the current price of the symbol which in this case is FILBUSD. Then gets my current BUSD account balance to figure out what 2% is. It then converts 2% of BUSD into the amount of contracts then rounds it. Next it places the buy order and remembers the amount of contracts that were bought and stores it in FIL3MIN_original_quantity. Then once it receives the sell order from trading view it pulls the amount of contracts and places the sell order.