I have written a trading bot working on futures account for margin trading. The piece of code that I shared below worked fine for just one time. Then it started to give the following error:
APIError(code=-2021): Order would immediately trigger
The problem is when I copy and paste this piece of code(part related to open position and give take profit and stop loss orders) to a new file and run it, it works fine but gives an error in the bot. I couldn't find the cause of the problem.
if currentdiffer > 0:
buy_order = client.futures_create_order(symbol='SOLBUSD',
side='BUY',
type ='MARKET',
quantity = qty,
positionSide='LONG')
poisiton = "LONG"
stopprice = currentprice - (value*1.2)
takeprofit = currentprice + (value*1.2)
stopprice = round(stopprice,2)
takeprofit = round(takeprofit,2)
print("Take profit : ", takeprofit)
print("Stop price : ", stopprice)
tp_order = client.futures_create_order(symbol='SOLBUSD',
side='SELL',
positionSide='LONG',
type ='TAKE_PROFIT_MARKET',
timeInForce='GTE_GTC',
quantity = qty,
stopPrice=stopprice,
workingType='MARK_PRICE'
)
sl_order = client.futures_create_order(symbol='SOLBUSD',
side='SELL',
positionSide='LONG',
type ='STOP_MARKET',
timeInForce='GTE_GTC',
quantity = qty,
stopPrice=takeprofit,
workingType='MARK_PRICE'
)