Checking all running positions and move the SL to breakeven when critera are met

 

Hello guys,

can you help me with this problem? I always receive an invalid request (error 4756 invalid stops) when the SL should be moved to breakeven. But in the log file I can see that the SL is correct.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
   if (PositionsTotal()>0) {
      MqlTradeRequest   m_request;              // request data
      MqlTradeResult    m_result;               // result data
      for (int i=0;i<PositionsTotal();i++) {
         if (PositionGetSymbol(i)!="") {
            string symbol=PositionGetString(POSITION_SYMBOL);
            ulong ticket=PositionGetInteger(POSITION_TICKET);
            double op=PositionGetDouble(POSITION_PRICE_OPEN);
            double sl=PositionGetDouble(POSITION_SL);
            double tp=PositionGetDouble(POSITION_TP);
            long type=PositionGetInteger(POSITION_TYPE);
            if (type==POSITION_TYPE_BUY) {
               if (sl<op) {
                  if (SymbolInfoDouble(symbol,SYMBOL_BID)>op+(op-sl)) {
                     m_request.action  =TRADE_ACTION_SLTP;
                     m_request.position=ticket;
                     m_request.symbol  =symbol;
                     m_request.sl      =op;
                     m_request.tp      =tp;
                     OrderSend(m_request,m_result);
                  }
               }
            }
            else if (type==POSITION_TYPE_SELL) {
               if (sl>op) {
                  if (SymbolInfoDouble(symbol,SYMBOL_ASK)<op-(sl-op)) {
                     m_request.action  =TRADE_ACTION_SLTP;
                     m_request.position=ticket;
                     m_request.symbol  =symbol;
                     m_request.sl      =op;
                     m_request.tp      =tp;
                     OrderSend(m_request,m_result);
                  }
               }
            }
         }
      }
   }
}
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Marbo:

Hello guys,

can you help me with this problem? I always receive an invalid request (error 4756 invalid stops) when the SL should be moved to breakeven. But in the log file I can see that the SL is correct.

No idea? :(

 
Marbo #:

No idea? :(

Learn how to track possible errors!

Check the return code, print the relevant variables like SL, TP, ask and bid ...

Learn to use the debugger: https://www.mql5.com/en/articles/654.

Debugging MQL5 Programs
Debugging MQL5 Programs
  • www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It reveals some debugging techniques and presents a combined experience of the author and many other programmers.
 
Carl Schreiber #:

Learn how to track possible errors!

Check the return code, print the relevant variables like SL, TP, ask and bid ...

Learn to use the debugger: https://www.mql5.com/en/articles/654.

Thanks for your advice!

My problem is that I am not sure about the code itself. The trade management is so different from MQL4 (positions, deals...) and I didn't find any code which modifies a stoploss in a running trade which I could learn from.

 
Marbo: can you help me with this problem? I always receive an invalid request (error 4756 invalid stops) when the SL should be moved to breakeven. But in the log file I can see that the SL is correct.

Your code shows no signs of you checking for Stops Level. Always check the broker's contract specifications, like the Stops Levels and Freeze Levels.

Also your logic seems strange ...

bid > op + ( op - sl ) ... is equivalent to ... bid > 2 * op - sl

ask < op - ( sl - op ) ... is equivalent to ... ask < 2 * op - sl
 
Marbo #:

Thanks for your advice!

My problem is that I am not sure about the code itself. The trade management is so different from MQL4 (positions, deals...) and I didn't find any code which modifies a stoploss in a running trade which I could learn from.

Then use MT4 style for MT5 https://www.mql5.com/ru/code/16006

 
Check the articles of Enrico Lambino: https://www.mql5.com/en/users/iceron/publications
Enrico Lambino
Enrico Lambino
  • www.mql5.com
Trader's profile
 

@all thanks for your help!

FYI: I just needed to add the magic number as a parameter and then my code worked. I didn't know that this magic number is needed.