Step-1: you need to contact your broker.
For more detailed info about errors https://www.mql5.com/ en/docs/constants/ errorswarnings/errorcodes
NOTE: your screenshot got misplaced (not available).

- www.mql5.com
Hello,
I’m experiencing an issue with my MQL5 code where I receive an "invalid request" error when ...

- www.mql5.com
Step-1: you need to contact your broker.
For more detailed info about errors https://www.mql5.com/ en/docs/constants/ errorswarnings/errorcodes
NOTE: your screenshot got misplaced (not available).
CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Calculating ADX value... CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 ADX value copied correctly: 27.184684636478547 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Calculating Bollinger Bands... CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Final SMA value: 1.2716445 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Standard Deviation: 0.0031988052066280223 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Bollinger Bands calculated: Upper: 1.2780421104132562, Middle: 1.2716445, Lower: 1.265246889586744 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 DEBUG: BUY condition met (RSI < RSI_Oversold: true, MACD Main > MACD Signal: false, ADX > ADX Threshold: true, BID Price < Lower Band: 1.26386, ASK Price: 1.26386) CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 An open position already exists for the symbol: GBPUSD CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Calling the ApplyTrailingStop() function CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 DEBUG: Minimum SL/TP distance (stop_level): 0.0 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 DEBUG: Freeze level: 0.0 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 DEBUG: Calculated trailing stop price for BUY: 1.2616399999999999 CS 2 12:01:07.711 Trades 2024.01.02 15:21:35 failed modify #2 buy 0.3 GBPUSD sl: 1.26060, tp: 1.27588 -> sl: 1.26164, tp: 1.27588 [Invalid request] CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 Error updating SL/TP: 4756 CS 0 12:01:07.711 new trend following 1 (GBPUSD,H1) 2024.01.02 15:21:35 DEBUG: Error updating trailing stop BUY.
First of all thank you for your answer. This is the Strategy Tester. The error occurs even when no position is open. The first operation fails as well.
Step-1b: you need to contact your broker and check the minimum stop level (StopLevel).
NOTE: many brokers enforce a minimum distance (in points) between the current price and SL/TP levels.
Step-2: ensure that:
- For a BUY position: SL > Bid + stop_level
- For a SELL position: SL < Ask - stop_level
Sharing your code is always a good step to take.
The moderators are trying to check if you aren't violating a minimum requirement in your SL distance, you can easily find the minimum distance your SL should be using the function SymbolInfoDouble(Symbol(),SYMBOL_MARGIN_STOPLIMIT)), and then add this minimum amount or subtract it, depending on whether the SL is for a long or short position respectively.
By attaching your code, we can give you advice faster.
Make sure to apply the principles detailed in the following article, even if it is not for the Market. They are all valid points to consider in your own EAs.
The checks a trading robot must pass before publication in the Market
MetaQuotes, 2016.08.01 09:30
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
// Function to apply the trailing stop with checks for pending orders and freeze level void ApplyTrailingStop() { // Check if the position is active (not pending) if (!PositionSelect(_Symbol)) { Print("DEBUG: No active position for the symbol ", _Symbol); return; } // Get the current order ticket (for pending orders) ulong ticket = PositionGetInteger(POSITION_TICKET); // Check if the position is pending using OrderSelect() if (OrderSelect(ticket)) { long order_type = OrderGetInteger(ORDER_TYPE); // Order type // If the order is pending (BUY_LIMIT, SELL_LIMIT, BUY_STOP, SELL_STOP) if (order_type == ORDER_TYPE_BUY_LIMIT || order_type == ORDER_TYPE_SELL_LIMIT || order_type == ORDER_TYPE_BUY_STOP || order_type == ORDER_TYPE_SELL_STOP) { Print("DEBUG: The position is a pending order. Trailing stop cannot be applied."); return; } } // Retrieve position information double stop_loss = PositionGetDouble(POSITION_SL); double entry_price = PositionGetDouble(POSITION_PRICE_OPEN); // Entry price double trailing_stop_price = 0.0; long position_type = PositionGetInteger(POSITION_TYPE); // Position type (BUY or SELL) // Get market prices double bid_price = SymbolInfoDouble(_Symbol, SYMBOL_BID); double ask_price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double spread = NormalizeDouble(ask_price - bid_price, _Digits); // Ensure prices are valid if (bid_price == 0.0 || ask_price == 0.0) { Print("DEBUG: Error retrieving market prices. Error code: ", GetLastError()); return; } // Calculate the minimum stop level (minimum distance for SL/TP) double stop_level = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) * _Point; // Check the freeze level double freeze_level = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL) * _Point; // Debugging for stop level and freeze level Print("DEBUG: Minimum distance for SL/TP (stop_level): ", stop_level); Print("DEBUG: Freeze level (freeze_level): ", freeze_level); // Check if the position is too close to the market price (freeze level) if (MathAbs(bid_price - entry_price) < freeze_level) { Print("DEBUG: The position is too close to the market price to apply trailing stop."); return; } // Trailing stop calculation for BUY if (position_type == POSITION_TYPE_BUY) { // Integrate the spread into the calculation and add the trailing stop distance trailing_stop_price = NormalizeDouble(bid_price - TrailingStopDistance * _Point - spread, _Digits); // Debugging for trailing stop calculation Print("DEBUG: Calculated trailing stop price for BUY: ", trailing_stop_price); // Ensure the distance between SL and the new trailing stop is valid if (MathAbs(stop_loss - trailing_stop_price) >= stop_level && trailing_stop_price > stop_loss) { // Modify the trailing stop only if the new distance is valid if (trade.PositionModify(_Symbol, trailing_stop_price, PositionGetDouble(POSITION_TP))) { Print("DEBUG: Trailing stop updated for BUY: SL = ", trailing_stop_price); } else { Print("DEBUG: Failed to update trailing stop for BUY. Error code: ", GetLastError()); } } else { Print("DEBUG: The distance between the new SL and the old SL is insufficient."); } } // Trailing stop calculation for SELL else if (position_type == POSITION_TYPE_SELL) { // Integrate the spread into the calculation and add the trailing stop distance trailing_stop_price = NormalizeDouble(ask_price + TrailingStopDistance * _Point + spread, _Digits); // Debugging for trailing stop calculation Print("DEBUG: Calculated trailing stop price for SELL: ", trailing_stop_price); // Ensure the distance between SL and the new trailing stop is valid if (MathAbs(stop_loss - trailing_stop_price) >= stop_level && trailing_stop_price < stop_loss) { // Modify the trailing stop only if the new distance is valid if (trade.PositionModify(_Symbol, trailing_stop_price, PositionGetDouble(POSITION_TP))) { Print("DEBUG: Trailing stop updated for SELL: SL = ", trailing_stop_price); } else { Print("DEBUG: Failed to update trailing stop for SELL. Error code: ", GetLastError()); } } else { Print("DEBUG: The distance between the new SL and the old SL is insufficient."); } } else { Print("DEBUG: Unrecognized position type. Error code: ", GetLastError()); } }
Is this one ok?
Sharing your code is always a good step to take.
The moderators are trying to check if you aren't violating a minimum requirement in your SL distance, you can easily find the minimum distance your SL should be using the function SymbolInfoDouble(Symbol(),SYMBOL_MARGIN_STOPLIMIT)), and then add this minimum amount or subtract it, depending on whether the SL is for a long or short position respectively.
By attaching your code, we can give you advice faster.
Here is the trailing stop code:
// Function to apply the trailing stop with checks for pending orders, freeze level, and minimum stop levels void ApplyTrailingStop() { // Check if the position is active (not pending) if (!PositionSelect(_Symbol)) { Print("DEBUG: No active position for the symbol ", _Symbol); return; } // Retrieve position information double stop_loss = PositionGetDouble(POSITION_SL); double take_profit = PositionGetDouble(POSITION_TP); double entry_price = PositionGetDouble(POSITION_PRICE_OPEN); // Entry price double trailing_stop_price = 0.0; long position_type = PositionGetInteger(POSITION_TYPE); // Position type (BUY or SELL) // Get market prices double bid_price = SymbolInfoDouble(_Symbol, SYMBOL_BID); double ask_price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double spread = NormalizeDouble(ask_price - bid_price, _Digits); // Ensure prices are valid if (bid_price == 0.0 || ask_price == 0.0) { Print("DEBUG: Error retrieving market prices. Error code: ", GetLastError()); return; } // Calculate the minimum stop level (minimum distance for SL/TP) double stop_level = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) * _Point; // Check the freeze level double freeze_level = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL) * _Point; // Debugging for stop level and freeze level Print("DEBUG: Minimum distance for SL/TP (stop_level): ", stop_level); Print("DEBUG: Freeze level (freeze_level): ", freeze_level); // Trailing stop calculation for BUY if (position_type == POSITION_TYPE_BUY) { // Calculate the new trailing stop price trailing_stop_price = NormalizeDouble(bid_price - TrailingStopDistance * _Point, _Digits); // Check minimum distance for StopLoss and TakeProfit if (take_profit - bid_price < stop_level) { Print("DEBUG: TakeProfit does not meet the minimum stop level for BUY."); return; } if (bid_price - stop_loss < stop_level) { Print("DEBUG: StopLoss does not meet the minimum stop level for BUY."); return; } // Debugging for trailing stop calculation Print("DEBUG: Calculated trailing stop price for BUY: ", trailing_stop_price); // Ensure the trailing stop is valid and greater than the current StopLoss if (trailing_stop_price > stop_loss) { if (trade.PositionModify(_Symbol, trailing_stop_price, take_profit)) { Print("DEBUG: Trailing stop updated for BUY: SL = ", trailing_stop_price); } else { Print("DEBUG: Failed to update trailing stop for BUY. Error code: ", GetLastError()); } } else { Print("DEBUG: New trailing stop price for BUY is not valid."); } } // Trailing stop calculation for SELL else if (position_type == POSITION_TYPE_SELL) { // Calculate the new trailing stop price trailing_stop_price = NormalizeDouble(ask_price + TrailingStopDistance * _Point, _Digits); // Check minimum distance for StopLoss and TakeProfit if (ask_price - take_profit < stop_level) { Print("DEBUG: TakeProfit does not meet the minimum stop level for SELL."); return; } if (stop_loss - ask_price < stop_level) { Print("DEBUG: StopLoss does not meet the minimum stop level for SELL."); return; } // Debugging for trailing stop calculation Print("DEBUG: Calculated trailing stop price for SELL: ", trailing_stop_price); // Ensure the trailing stop is valid and less than the current StopLoss if (trailing_stop_price < stop_loss) { if (trade.PositionModify(_Symbol, trailing_stop_price, take_profit)) { Print("DEBUG: Trailing stop updated for SELL: SL = ", trailing_stop_price); } else { Print("DEBUG: Failed to update trailing stop for SELL. Error code: ", GetLastError()); } } else { Print("DEBUG: New trailing stop price for SELL is not valid."); } } else { Print("DEBUG: Unrecognized position type. Error code: ", GetLastError()); } }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I’m experiencing an issue with my MQL5 code where I receive an "invalid request" error when attempting to modify a position with a trailing stop. I have implemented the logic for applying a trailing stop based on the market prices, but when the PositionModify() function is called, I receive the "invalid request" error.
I have double-checked that the prices and stop loss values are valid and properly calculated, but I’m still receiving the "invalid request" error.
I am encountering a problem when trying to modify the Stop Loss (SL) and Take Profit (TP) for an open position. The code is attempting to update the SL and TP, but it fails with the error "Invalid request" (error code 4756).
Here’s the relevant part of the log showing the issue:
However, when trying to modify the SL and TP, the modification fails:
It seems like the request to modify the position is invalid for some reason. I’ve ensured that the SL and TP values are within reasonable ranges, but I’m still receiving the "Invalid request" error
Could you kindly help me with this issue? I’ve attached a screenshot of the strategy tester output to provide more context.
Any guidance on fixing this problem would be greatly appreciated.
Thank you very much for your time and assistance.