We are encountering an issue while placing a stop market order for the BTCUSDT trading pair using the Binance API. The error details are as follows:
Error Message: Binance API Exception: Order would immediately trigger. (Code: -2021)
Issue Description: This error occurs even though we have verified that at the time of placing the order, the Last Traded Price (LTP) had not crossed the specified stop price. Despite this, the error prevents the order from being placed successfully, which is impacting our objectives.
Code Snippet:
#Get the current price (MARK_PRICE or CONTRACT_PRICE)
current_price = self.get_ltp(symbol)
#Validate stop price
if side == Client.SIDE_BUY and current_price >= stop_price:
raise ValueError("Invalid stop price for BUY: Stop price must be greater than or equal to the latest price.")
if side == Client.SIDE_SELL and current_price <= stop_price:
raise ValueError("Invalid stop price for SELL: Stop price must be less than or equal to the latest price.")
client.futures_create_order(
symbol=symbol,
side=side,
type=Client.FUTURE_ORDER_TYPE_STOP_MARKET,
stopPrice=float(stop_price),
quantity=float(quantity),
timeInForce=Client.TIME_IN_FORCE_GTC
)
We would appreciate it if you could:
- Clarify the specific conditions under which this error (Code: -2021) is triggered.
- Confirm if there are any additional validations required to prevent this error.
- Provide recommendations or alternative approaches to ensure successful execution of stop market orders in similar scenarios.
Your guidance will help us address this issue and align our implementation with Binance’s requirements.
Thank you for your support. We look forward to your response.
Hi @Sathesh_Muppudathi,
The error code -2021
(“Order would immediately trigger”) occurs when the stop price of your stop market order is evaluated as already met or surpassed at the moment the order is placed. This can happen due to market price fluctuations or how the Binance API interprets the stop condition. Here’s a detailed breakdown and some guidance to resolve the issue:
1. Conditions Triggering Error Code -2021
- The
stopPrice
for a buy stop market order is less than or equal to the current market price (MARK_PRICE
or CONTRACT_PRICE
) at the time the order is placed.
- The
stopPrice
for a sell stop market order is greater than or equal to the current market price.
- Even slight price fluctuations between fetching the
current_price
and sending the order can trigger this error.
2. Additional Validations
- Ensure the stop price complies with Binance’s tick size and price precision rules for the BTCUSDT trading pair. Use the
GET /fapi/v1/exchangeInfo
endpoint to verify:
minPrice
and tickSize
for the trading pair.
- Validate the mark price (
MARK_PRICE
) rather than the last traded price (LTP
) when determining the stop price, as the API uses MARK_PRICE
for stop conditions.
3. Recommendations
- Buffer the Stop Price:
- Add a small buffer to your stop price to account for market fluctuations:
if side == Client.SIDE_BUY:
stop_price = current_price * (1 + buffer_percentage)
elif side == Client.SIDE_SELL:
stop_price = current_price * (1 - buffer_percentage)
Example: Use a buffer_percentage
of 0.01% or another value based on volatility.
- Double-Check the Stop Price Before Placing the Order:
- Revalidate the stop price against the current market price just before order placement:
current_price = self.get_ltp(symbol) # Fetch updated price again
if side == Client.SIDE_BUY and current_price >= stop_price:
raise ValueError("Stop price for BUY order must be greater than the current price.")
if side == Client.SIDE_SELL and current_price <= stop_price:
raise ValueError("Stop price for SELL order must be less than the current price.")
- Use
MARK_PRICE
Explicitly:
- If you are not already using
MARK_PRICE
, ensure you retrieve and validate it as part of your stop price calculations.
4. Alternative Approach
- Conditional Orders: If this is a frequent issue, consider using a combination of other order types, such as a limit order with conditions, to achieve your objectives while minimizing immediate triggers.
- Simulate Stop Price in Code: Before placing the actual order, simulate how the stop price would behave given the current
MARK_PRICE
.
I hope this makes things more clear and resolves your issue. Let us know if further clarification is needed!