Reputation: 21
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'
)
Upvotes: 1
Views: 6280
Reputation: 357
It is because you set stoploss or take profit less than market price. For example for Buy position, stop loss should be bigger than market price.
Upvotes: 0
Reputation: 92
Seems like you messed up with the parameter that you send
Check the API Docs. It's all there
For TRAILING_STOP_MARKET, if you got such error code.
{"code": -2021, "msg": "Order would immediately trigger."}
means that the parameters you send do not meet the following requirements:
BUY: activationPrice should be smaller than latest price.
SELL: activationPrice should be larger than latest price.
https://binance-docs.github.io/apidocs/futures/en/#new-order-trade
Upvotes: -1