When trying to place sl/tp orders on Binance (in this case using testnet), I constantly get an error. The implementation logic has already been described on the forum. I take and place an order to open a limit position. I’m waiting through the websocket for it to fill up and trying to place sl/tp orders. For the price I use the last opening price of the candle, for take profit tp_price = open_price + (open_price * 0.01)
, for stop loss sl_price = open_price - (open_price * 0.01)
. In real terms, open_price = 2299,96, tp_price = 2322,95, sl_price = 2292.84, mark_price = 2265.81
. SL order is placed on Binance, tp order is not APIError(code=-2021): Order would immediately trigger
, sometimes this error appears for both sl orders and tp orders at the same time. Im using python-binance
lib. My implementation look like this, for open position:
async def place_limit_order(
self, api_key, api_secret, symbol, quantity, price, side
):
try:
client = AsyncClient(api_key=api_key, api_secret=api_secret, testnet=True)
order_params = {
"symbol": symbol,
"side": side,
"type": "LIMIT",
"timeInForce": "GTC",
"positionSide": "LONG",
"quantity": quantity,
"price": price,
# "reduceOnly": True,
}
order = await client.futures_create_order(**order_params)
print(f"Limit order placed successfully: {order}")
return order
except Exception as e:
print(f"Failed to place limit order: {e}")
return None
finally:
await client.close_connection()
for placing tp_order
async def place_tp_order(
self,
api_key,
api_secret,
symbol,
quantity,
entry_price,
stop_price,
testnet=True,
):
client = await AsyncClient.create(api_key, api_secret, testnet=testnet)
try:
order = await client.futures_create_order(
symbol=symbol,
side="BUY",
type="TAKE_PROFIT",
positionSide="LONG",
timeInForce="GTC",
quantity=quantity,
price=entry_price,
stopPrice=stop_price,
workingType="CONTRACT_PRICE",
# closePosition="true",
# reduceOnly=True,
)
print("Take profit is placed:")
print(order)
except Exception as e:
print(f"Error for Take profit order: {e}")
finally:
await client.close_connection()
for sl order im using same code but type="STOP"
Can someone help, handle this error?