CCXT Binance Futures order / python with stop loss and take profit

640 Views Asked by At

I am wondering how to open a Binance futures order (buy or sell) with a stop loss and multiple take profits.

I am able to open an order using CCXT in python: order = binance.create_limit_order(symbol='ETH/USDT', side='buy', amount=0.01, price=1200) order_id = order['id']

But I am not able to either use edit_order to edit the order to add the SL and TP.

Can someone post a small amount of code to push me in the correct direction???

I also have tried this already: how to change SL/TP with CCXT (python) on Binance Futures

1

There are 1 best solutions below

0
Smokey Van Den Berg On

I have the same issue, seems that what ever value you pass breaks the code... but I git mine to work as follows:

def open_trade(symbol, entry, leverage, take_profit, stop_loss, position_type, size):
    # Load the time difference
    exchange.load_time_difference()
    
    # Assuming you already have 'exchange' defined somewhere
    ticker = exchange.fetch_ticker(symbol)
    current_price = ticker['last']

    if position_type == 'LONG' and not (entry[0] < current_price < entry[1]):
        print(f"Current price ({current_price}) is not between entry1 ({entry[0]}) and entry2 ({entry[1]}). Signal NOT executed")
        return None
    elif position_type == 'SHORT' and not (entry[1] < current_price < entry[0]):
        print(f"Current price ({current_price}) is not between entry1 ({entry[1]}) and entry2 ({entry[0]}). Signal NOT executed")
        return None

    # Define additional parameters, including leverage
    additional_params = {
        'leverage': calc_leverage(symbol, leverage),
        #'stopPrice': stop_loss,
        #'stopLossPrice': float(stop_loss),
        #'stopLossTimeInForce': "GTC",
        #'takeProfitPrice': float(take_profit[4]),
        #'takeProfitTimeInForce': "GTC",
    }
    print('Additional parms: ' + str(additional_params))

    # Determine the position side based on the order type
    if position_type == 'LONG':
        additional_params['positionSide'] = 'LONG'
    elif position_type == 'SHORT':
        additional_params['positionSide'] = 'SHORT'
    else:
        print(f"Invalid position_type: {position_type}")
        return None

    # Place market order with additional parameters
    order = exchange.create_order(
        symbol=symbol,
        type='market',
        side='buy' if position_type == 'LONG' else 'sell',
        amount=calc_trade_size(symbol, leverage),
        params=additional_params,
    )
    position_info = order
    print("Futures position information:")
    print(position_info)

    # Add stopPrice to additional_params
    additional_params['stopPrice'] = stop_loss
    

    # Place stop loss order
    stop_loss_order = exchange.create_order(
        symbol=symbol,
        type='STOP_MARKET',
        side='sell' if position_type == 'LONG' else 'buy',
        amount=size,
        price=stop_loss,  # This is the stop loss price
        params=additional_params,
    )

    position_info = stop_loss_order
    print("Futures position information:")
    print(position_info)

    size = round(size / 5, 5)  # Split the size into 5 take profit orders

    for tp in take_profit:
        # Add stopPrice to additional_params
        additional_params['stopPrice'] = tp
        # Place take profit order
        take_profit_order = exchange.create_order(
            symbol=symbol,
            type='TAKE_PROFIT_MARKET',
            side='sell' if position_type == 'LONG' else 'buy',
            amount=size,
            price=tp,  # This is the take profit price
            params=additional_params,
        )

        position_info = take_profit_order
        print("Futures position information:")
        print(position_info)

It creates the orders but its not like the SL/TP on the initial order: enter image description here

enter image description here

I hope this helps