MQL5 Stop not working on EA

 

Hi all

I got an issue with the Stop Loss and can't find out the cause - I got the Mt4 version running just fine however, mt5 is not able to run. 

Mql5 can't HELP ..... and suggest to ask the Forum - Does any one has this issue (anoying...) and how to solved it 


EA on mt4 works fine - Mt5 ...!!! 


Validation state: Validation completed with errors

  • Errors count21
  • Started2019.03.27 02:10:24
  • Finished2019.03.27 02:11:23
  • TypeExpert Advisor
test on EURUSD,H1 (netting)
 2018.04.03 04:36:55   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:36:57   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:37:15   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:37:18   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:37:21   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:38:00   failed modify #2 sell 1.00 EURUSD sl: 1.23281, tp: 1.22633 -> sl: 1.23281, tp: 1.22633 [Invalid stops]
 2018.04.03 04:40:00   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:40:03   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:40:07   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:40:10   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:40:14   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:40:17   failed modify #2 sell 1.00 EURUSD sl: 1.23282, tp: 1.22633 -> sl: 1.23282, tp: 1.22633 [Invalid stops]
 2018.04.03 04:42:00   failed modify #2 sell 1.00 EURUSD sl: 1.23272, tp: 1.22633 -> sl: 1.23272, tp: 1.22633 [Invalid stops]
 2018.04.03 15:46:00   failed modify #4 sell 1.00 EURUSD sl: 1.23236, tp: 1.22665 -> sl: 1.23236, tp: 1.22665 [Invalid stops]
 2018.04.03 15:48:00   failed modify #4 sell 1.00 EURUSD sl: 1.23267, tp: 1.22665 -> sl: 1.23267, tp: 1.22665 [Invalid stops]
 2018.04.03 15:50:00   failed modify #4 sell 1.00 EURUSD sl: 1.23251, tp: 1.22665 -> sl: 1.23251, tp: 1.22665 [Invalid stops]
 2018.04.03 15:52:00   failed modify #4 sell 1.00 EURUSD sl: 1.23244, tp: 1.22665 -> sl: 1.23244, tp: 1.22665 [Invalid stops]
 2018.04.03 15:57:00   failed modify #4 sell 1.00 EURUSD sl: 1.23260, tp: 1.22665 -> sl: 1.23260, tp: 1.22665 [Invalid stops]
 2018.04.03 15:59:00   failed modify #4 sell 1.00 EURUSD sl: 1.23262, tp: 1.22665 -> sl: 1.23262, tp: 1.22665 [Invalid stops]
 2018.04.03 16:00:00   failed modify #4 sell 1.00 EURUSD sl: 1.23259, tp: 1.22665 -> sl: 1.23259, tp: 1.22665 [Invalid stops]
 2018.04.03 16:02:00   failed modify #4 sell 1.00 EURUSD sl: 1.23256, tp: 1.22665 -> sl: 1.23256, tp: 1.22665 [Invalid stops]
strategy tester report 34 total trades

 
There are no telepaths here. Show the code. Then someone can help you.
 
Konstantin Nikitin:
There are no telepaths here. Show the code. Then someone can help you.

of course, Here is part of the code. 

thanks you in advance. 

EDIT: code in normal text removed as reposted later.

 
 
Konstantin Nikitin:
/+------------------------------------------------------------------+

//|  Normalize Price                                                 |

//+------------------------------------------------------------------+

double NormalizePrice(double price)

{

   double mTickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);

   return(NormalizeDouble(MathRound(price/mTickSize)*mTickSize,_Digits));

}



//+------------------------------------------------------------------+

//|  Modify an Open Position                                         |

//+------------------------------------------------------------------+

void ModifyPosition(ulong ticket, string symbol, double stopLoss, double takeProfit) 

{

   // Validate stops level

   int type = (int) PositionGetInteger(POSITION_TYPE); 

   double bid = SymbolInfoDouble(symbol,SYMBOL_BID);

   double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);

   int stopLevel = (int)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL);   

      

   if(type == POSITION_TYPE_BUY)

   {

      double dif = NormalizeDouble((bid - stopLoss) / Poin, _Digits);

      if (dif < stopLevel)

         stopLoss = NormalizeDouble(stopLoss - ((stopLevel - dif) * Poin),_Digits);

   }

   else

   {

      double dif = NormalizeDouble((stopLoss - ask) / Poin, _Digits); 

      if (dif < stopLevel)

         stopLoss = NormalizeDouble(stopLoss + ((stopLevel - dif) * Poin),_Digits);

   }   

           

   // Prepare a request 

   MqlTradeRequest request={0}; 

   request.action          =  TRADE_ACTION_SLTP;   // Type of trade operation

   request.position        =  ticket;              // Ticket of the position

   request.symbol          =  symbol;              // Symbol 

   request.sl              =  stopLoss;            // Stop Loss of the position

   request.tp              =  takeProfit;          // Take Profit of the position

   request.magic           =  MagicNumber;         // MagicNumber of the position

         

   // Send a trade request 

   MqlTradeResult result={0}; 

   bool val = OrderSend(request,result); 

         

   // Write the server reply to log   

   Print(__FUNCTION__,":",result.comment); 

   if(result.retcode==10016) 

      Print(result.bid,result.ask,result.price); 

   

   if (val && ShowAlert)

      Alert(Symbol(), " M", Period(), ": Stop Loss Order Modified successfully.");

   if (!val && ShowAlert)

   {

      int error = GetLastError();

      Alert("OrderModify failed with error # ", error , " : ", ErrorDescription(error));

   }   

}

//+------------------------------------------------------------------+

//|  Trailing positions                                              |

//+------------------------------------------------------------------+

void TrailingPositions()

{

   // ***** Go through all positions *****

   for(int i=0; i<PositionsTotal(); i++) 

   {

      // ***** Select order by position *****

      if (PositionSelectByTicket(PositionGetTicket(i)))

      {

         // ***** Check Trail all positions and Symbol *****

         if (TrailAllPositions || PositionGetSymbol(i)==Symbol()) 

         {

            double bid, ask;            

            double open = PositionGetDouble(POSITION_PRICE_OPEN);

            double sl = PositionGetDouble(POSITION_SL); 

            double tp = PositionGetDouble(POSITION_TP);

               

            // ***** Trailing Buy *****

            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) 

            {

               bid = SymbolInfoDouble(Symbol(),SYMBOL_BID); 

               if (!ProfitTrailing || (sl >= open && bid - open >= TrailingStop * Poin)) 

               {

                  if (sl < bid - (TrailingStop + TrailingStep) * Poin) 

                  {  

                     double newStopLoss = NormalizeDouble(bid - TrailingStep * Poin, _Digits);                              

                     ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss , tp);

                     Print("Trail Buy ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(bid, _Digits));

                  }                    

               }

            }

            // ***** End *****

            

            // ***** Trailing Sell *****

            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) 

            {

               ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);               

               if (!ProfitTrailing || (sl <= open && open - ask >= TrailingStop * Poin)) 

               {

                  if (sl > ask + (TrailingStop + TrailingStep) * Poin) 

                  {        

                     double newStopLoss = NormalizeDouble(ask + TrailingStep * Poin, _Digits);                                 

                     ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss, tp);

                     Print("Trail Sell ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(ask, _Digits));

                  }

               }

            }

            // ***** End *****

         }

         // ***** End *****

      }

      // ***** End *****

   }

   // ***** End *****

}

 
//+------------------------------------------------------------------+
//|  Normalize Price                                                 |
//+------------------------------------------------------------------+
double NormalizePrice(double price)
{
   double mTickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   return(NormalizeDouble(MathRound(price/mTickSize)*mTickSize,_Digits));
}

//+------------------------------------------------------------------+
//|  Modify an Open Position                                         |
//+------------------------------------------------------------------+
void ModifyPosition(ulong ticket, string symbol, double stopLoss, double takeProfit) 
{
   // Validate stops level
   int type = (int)PositionGetInteger(POSITION_TYPE); 
   double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
   double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
   double point = SymbolInfoDouble(symbol,SYMBOL_POINT);
   int stopLevel = (int)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL)+1;

   switch(type)
   {
      case POSITION_TYPE_BUY :
         if (stopLevel > (bid - stopLoss) / point)
            stopLoss = NormalizeDouble(bid - stopLevel * point,_Digits);
            break;
      case POSITION_TYPE_SELL :
         if (stopLevel > (stopLoss - ask) / point)
            stopLoss = NormalizeDouble(ask + stopLevel * point,_Digits);
            break;
   }
   
   // Prepare a request 
   MqlTradeRequest request={0}; 
   request.action          =  TRADE_ACTION_SLTP;   // Type of trade operation
   request.position        =  ticket;              // Ticket of the position
   request.symbol          =  symbol;              // Symbol 
   request.sl              =  stopLoss;            // Stop Loss of the position
   request.tp              =  takeProfit;          // Take Profit of the position
   request.magic           =  MagicNumber;         // MagicNumber of the position

   // Send a trade request 
   MqlTradeResult result={0}; 
   bool val = OrderSend(request,result); 

   // Write the server reply to log   
   Print(__FUNCTION__,":",result.comment); 
   if(result.retcode==10016) 
      Print(result.bid,result.ask,result.price); 

   if (val && ShowAlert)
      Alert(_Symbol, " M", Period(), ": Stop Loss Order Modified successfully.");
   if (!val && ShowAlert)
   {
      int error = GetLastError();
      Alert("OrderModify failed with error # ", error , " : ", ErrorDescription(error));
   }
}

//+------------------------------------------------------------------+
//|  Trailing positions                                              |
//+------------------------------------------------------------------+
void TrailingPositions()
{
   // ***** Go through all positions *****
   for(int i=0; i<PositionsTotal(); i++) 
   {
      // ***** Select order by position *****
      if (PositionSelectByTicket(PositionGetTicket(i)))
      {
         // ***** Check Trail all positions and Symbol *****
         if (TrailAllPositions || PositionGetSymbol(i)==_Symbol) 
         {
            double bid, ask;            
            double open = PositionGetDouble(POSITION_PRICE_OPEN);
            double sl = PositionGetDouble(POSITION_SL); 
            double tp = PositionGetDouble(POSITION_TP);
            double point = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
            
            // ***** Trailing Buy *****
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) 
            {
               bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); 
               if (!ProfitTrailing || (sl >= open && bid - open >= TrailingStop * point)) 
               {
                  if (sl < bid - (TrailingStop + TrailingStep) * point) 
                  {
                     double newStopLoss = NormalizeDouble(bid - TrailingStep * point, _Digits);                              
                     ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss , tp);
                     Print("Trail Buy ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(bid, _Digits));
                  }
               }
            }
            // ***** End *****

            // ***** Trailing Sell *****
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) 
            {
               ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);               
               if (!ProfitTrailing || (sl <= open && open - ask >= TrailingStop * point)) 
               {
                  if (sl > ask + (TrailingStop + TrailingStep) * point) 
                  {
                     double newStopLoss = NormalizeDouble(ask + TrailingStep * point, _Digits);
                     ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss, tp);
                     Print("Trail Sell ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(ask, _Digits));
                  }
               }
            }
            // ***** End *****
         }
         // ***** End *****
      }
      // ***** End *****
   }
   // ***** End *****
}
 
Konstantin Nikitin:

Thank you Konstantin it solved the SL. 

now Is the volume issue with par bgp/usd on the 'testing loading EA'  - can validate de EA. 

Will search  to limit the trade and adjust de volume. 

test on EURUSD,H1 (netting)
strategy tester report 86 total trades
test on XAUUSD,D1 (netting)
there are no trading operations
test on GBPUSD,M30 (netting)
 2017.02.06 08:00:40   failed request sell 15.00 GBPUSD at 1.24869  sl: 1.25069 tp: 1.24469 [Volume limit reached]
 2017.02.06 08:30:00   failed request sell 15.00 GBPUSD at 1.24871  sl: 1.25071 tp: 1.24471 [Volume limit reached]
 2017.02.06 09:06:59   failed request sell 15.00 GBPUSD at 1.24869  sl: 1.25069 tp: 1.24469 [Volume limit reached]
Reason: