When I'm trying to place an order on Binance exchange using python-binance library, I'm getting errors like APIError(code=-2021): Order would immediately trigger. What am I doing wrong? So my logic is open position with limit order like this:
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,
}
# Place limit order
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()
Then, when order is placed on exchange, I'm watching user data stream. Then, I'm trying place tp/sl order like this:
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,
# reduceOnly=True,
)
print("Take profit placed:")
print(order)
except Exception as e:
print(f"Error place Take profit order: {e}")
finally:
await client.close_connection()
So what about prices calculation?
I take open price for a candle, for example is price = 2319 and calculate price like for tp order stop_price = price + (price) * 0.1, its up price for one percent and for stop loss order I'm doing the same but on the opposite side. So my orders price look like this: price=2319, take_profit_price=2342.19, stop_loss_price=2295.81. But it's giving me errors like APIError(code=-2021): Order would immediately trigger. When I'm up my percentage its give me same error.