Binance Futures Python API - Placing limit order with TP/SL

327 Views Asked by At

How can I place TP/SL at the time of placing a limit order. The code below only places the limit order and not TP/SL.

from binance.client import Client

# Replace these with your Binance API Key and Secret Key
api_key = BINANCE_API_KEY
api_secret = BINANCE_SECRET_KEY


# Initialize the Binance client
client = Client(api_key, api_secret)

def place_stop_loss_and_take_profit_order(symbol, side, quantity, price, sl, tp):
    try:
        order = client.futures_create_order(
            symbol=symbol,
            side=side,
            type='LIMIT',
            timeInForce='GTC',  # Good 'til cancelled
            quantity=quantity,
            price=price,
            STOP_MARKET=sl,
            TAKE_PROFIT_MARKET=tp
        )
        print("Stop loss and take profit order placed successfully!")
        print(order)
    except Exception as e:
        print(f"Error placing stop loss and take profit order: {e}")

# Replace with your desired parameters
symbol = 'KNCUSDT'
side = 'BUY'  # 'BUY' for buying, 'SELL' for selling
quantity = 11  # Amount of BTC you want to buy/sell
price = 0.71  # The price at which you want to buy/sell BTC
sl = 0.68  # Stop loss price
tp = 0.75  # Take profit price

place_stop_loss_and_take_profit_order(symbol, side, quantity, price, sl, tp)

I want to place a limit order with TP/SL predefined. when I run this code, it just places a limit order but does not place TP/SL

1

There are 1 best solutions below

2
On

I don't think it's possible to place TP/SL order via API as it's not supported.