'Binance API proper way to open a futures trade?

I am currently in the midst of writing myself a little python bot for binance using their API and I find the documentation rather lackluster and wondered whether someone on here might be able to help.

Let's say I want to open up a futures trade in the BTCUSDT pairing with a leverage of 5x and a margin of 100 USDT at market price, with a take profit at 50% and a stop loss at 10%.

from binance.client import Client
import cfg
client = Client(cfg.api_key, cfg.api_secret)
client.futures_create_order(symbol='BNBUSDT', side='LONG', type='MARKET',  quantity = 100 USDT * leverage / asset_price)

This is about as far as I get. I don't see any leverage attribute, however there is another function called futures_change_leverage() which is able to change your leverage, so I do have to initialize a position and then change the leverage? Wouldn't that also just scale my margin down?

I am also rather lost as to how the later attributes work and how I would be able to place a take profit and stop loss order.

Thanks for any help.



Solution 1:[1]

From running a few tests it seems like Binance uses the margin type (cross or isolated) and the leverage you last used on that pairing on the web, desktop or mobile app. If you have not changed it, it defaults to 20x Cross.

So before opening a futures trade you should change the leverage and margin type and only then open the position.

For example:

client.futures_change_margin_type(symbol='BNBUSDT', marginType='ISOLATED')

marginType has to be either 'ISOLATED' or 'CROSSED'.

Since questions came up about leverage and margin types and how to set those:

def adjust_leverage(symbol, client):
    client.futures_change_leverage(symbol=symbol, leverage=10)

def adjust_margintype(symbol, client):
    client.futures_change_margin_type(symbol=symbol, marginType='ISOLATED')

I am still figuring out how to do Stop Losses and Take Profits, potentially even Trailing SL, if I do find them I will keep you posted.

Solution 2:[2]

this is how I'm doing the Close position.

Par = clientR.futures_symbol_ticker(Symbol=ticker)

def CLOSE_POSITIONLONG(ticker):

      par = client.futures_symbol_ticker(Symbol=ticker)
      preciodeventa = float(par.get('price'))
      print("Precio de cierre: " + str(preciodeventa))
      triggerB = float(preciodeventa)+(float(preciodeventa)*0.0005)
      client.futures_create_order(
      symbol = ticker
      ,side='SELL'
      ,closePosition=True
      ,stopPrice= round(triggerB,3)
      ,type='TAKE_PROFIT_MARKET'
      )

      triggerS = float(preciodeventa)-(float(preciodeventa)*0.0005)
      client.futures_create_order(
      symbol = ticker
      ,side='SELL'
      ,closePosition=True
      ,stopPrice= round(triggerS,3)
      ,type='STOP_MARKET'
      )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 EJoshuaS - Stand with Ukraine