Invalid stops

 

I'm new to mql5, I don't know why it gives me an error, [invalid stops], it's a simple EA you must open a buy trade according to the condition and sell on stop loss or sell all trades on the next candle.


I am thankful for any kind of help....


#include  <Trade/Trade.mqh>
CTrade trade;


ulong trade_ticket = 0;
bool time_passed = true;

int prev_num_velas = 0;


void OnTick() {

   int num_velas = Bars(_Symbol,_Period);
   
   if (num_velas > prev_num_velas) {
   
      //Print(PositionsTotal(), "                                       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
      for (int i=PositionsTotal()-1;i>=0; i--){
      
         if(!trade.PositionClose(PositionGetSymbol(i))) {
   
               //--- failure message
   
               Print(PositionGetSymbol(i), "                                                      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  PositionClose() method failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription());
   
            } else {
   
               Print(PositionGetSymbol(i), "                                                      ///////////////////////////////////////////////////////////////////////////  PositionClose() method executed successfully. Return code=",trade.ResultRetcode(), " (",trade.ResultRetcodeDescription(),")");
   
            }
   
      }
      
      //---- Candle7 OHLC
      double O9=NormalizeDouble(iOpen(Symbol(),_Period,9),4);
      double C9=NormalizeDouble(iClose(Symbol(),_Period,9),4);
  
      //---- Candle7 OHLC
      double O8=NormalizeDouble(iOpen(Symbol(),_Period,8),4);
      double C8=NormalizeDouble(iClose(Symbol(),_Period,8),4);
        
      //---- Candle7 OHLC
      double O7=NormalizeDouble(iOpen(Symbol(),_Period,7),4);
      double C7=NormalizeDouble(iClose(Symbol(),_Period,7),4);
      
      //---- Candle6 OHLC
      double O6=NormalizeDouble(iOpen(Symbol(),_Period,6),4);   
      double C6=NormalizeDouble(iClose(Symbol(),_Period,6),4);
      
      //---- Candle5 OHLC
      double O5=NormalizeDouble(iOpen(Symbol(),_Period,5),4);
      double C5=NormalizeDouble(iClose(Symbol(),_Period,5),4);
      
      //---- Candle4 OHLC
      double O4=NormalizeDouble(iOpen(Symbol(),_Period,4),4);
      double C4=NormalizeDouble(iClose(Symbol(),_Period,4),4);
      
      //---- Candle3 OHLC
      double O3=NormalizeDouble(iOpen(Symbol(),_Period,3),4);
      double C3=NormalizeDouble(iClose(Symbol(),_Period,3),4);
      
      //---- Candle2 OHLC
      double O2=NormalizeDouble(iOpen(Symbol(),_Period,2),4);
      double C2=NormalizeDouble(iClose(Symbol(),_Period,2),4);
      double L2=NormalizeDouble(iLow(Symbol(),_Period,2),4);
      
      //---- Candle1 OHLC
      double O1=NormalizeDouble(iOpen(Symbol(),_Period,1),4);
      double C1=NormalizeDouble(iClose(Symbol(),_Period,1),4);     
      
            
      
      
      if (O1 > C1 && O2 < C2 && O3 > C3 && O4 > C4 && O5 > C5 && O6 > C6 && O7 > C7 && time_passed == true) {
         
         for(int x=8; x<=500; x++) {
         
            double O10=NormalizeDouble(iOpen(Symbol(),_Period,x),4);
            double C10=NormalizeDouble(iClose(Symbol(),_Period,x),4);
            
            if (O10 < C10 && C10 < C4) {
            
               double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
               double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
               //Print("ACA: ", Ask, "    ACA: ", Bid-5000*_Point);

               trade.Buy(1, _Symbol, Ask, Ask-5000*_Point, 0, NULL);
               
               
               trade_ticket = trade.ResultOrder();
               time_passed = false;
               EventSetTimer(60*1*2);
               
               //Print("###########################################################################################    ",_Period, "   NUEVO");
               break;              
            
            }
         }
         
      }
       
      //Print(PositionsTotal(), "                                        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); 
      prev_num_velas = num_velas;
   }
}
//+------------------------------------------------------------------+


void OnTimer() {
   time_passed = true;
}
 

The cycle of enumeration of positions must be written correctly. 1. Cycle bypass. 2. Choosing a position - this step is the most important!!!

See example:

//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
int CalculateAllPositions()
  {
   int total=0;
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
            total++;
//---
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      ulong ticket=PositionGetTicket(index);
      if(ticket>0);
        {
         if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic)
           {
            total++
           }
        }
     }
//---
   return(total);
  }
 
Vladimir Karputov #:

The cycle of enumeration of positions must be written correctly. 1. Cycle bypass. 2. Choosing a position - this step is the most important!!!

See example:


I appreciate your help, but this still doesn't resolve the [invalid stops] error

 
Edinson Perez # :


I appreciate your help, but this still doesn't resolve the [invalid stops] error

You didn't show the corrected code - I don't believe the words, I believe only if there is MQL5 code :)

 
Vladimir Karputov #:

You didn't show the corrected code - I don't believe the words, I believe only if there is MQL5 code :)

Corrected code? but the error is not generated by PositionsTotal()

 
Edinson Perez # :

Corrected code? but the error is not generated by PositionsTotal()

Where is your code? You should fix your code (see post) and post your corrected code.

Invalid stops
Invalid stops
  • 2022.03.20
  • www.mql5.com
I'm new to mql5, I don't know why it gives me an error, [invalid stops], it's a simple EA you must open a buy trade according to the condition and...
 

Step 1: ALWAYS use the 'MQL5 Wizard' - this is very important!

Follow step 1 and show your result.


After that, I will show you how to go through step 2.

 
Vladímir Karputov # :

Paso 1: SIEMPRE use el ' Asistente MQL5 ' - ¡esto es muy importante!

Siga el paso 1 y muestre su resultado.


Después de eso, le mostraré cómo realizar el paso 2.

#include  <Trade/Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CTrade trade;
CSymbolInfo *m_symbol;

ulong trade_ticket = 0;
bool time_passed = true;

int prev_num_velas = 0;


void OnTick() {

   int num_velas = Bars(_Symbol,_Period);
   
   if (num_velas > prev_num_velas) {
   
      //Print(PositionsTotal(), "                                       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
      /*
      for (int i=PositionsTotal()-1;i>=0; i--){
      
         if(!trade.PositionClose(PositionGetSymbol(i))) {
   
               //--- failure message
   
               Print(PositionGetSymbol(i), "                                                      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  PositionClose() method failed. Return code=",trade.ResultRetcode(),". Code description: ",trade.ResultRetcodeDescription());
   
            } else {
   
               Print(PositionGetSymbol(i), "                                                      ///////////////////////////////////////////////////////////////////////////  PositionClose() method executed successfully. Return code=",trade.ResultRetcode(), " (",trade.ResultRetcodeDescription(),")");
   
            }
   
      }
      */
      int total=0;
      for(int i=PositionsTotal()-1; i>=0; i--) {
         ulong ticket=PositionGetTicket(i);
         if(ticket>0){            
            if(PositionGetString(POSITION_SYMBOL)==Symbol()) {// && PositionGetInteger(POSITION_MAGIC)==InpMagic)
              
               total = total + 1;
              }
           }
      }
      
      Print("Total: ", total);
      
      
      //---- Candle7 OHLC
      double O9=NormalizeDouble(iOpen(Symbol(),_Period,9),4);
      double C9=NormalizeDouble(iClose(Symbol(),_Period,9),4);
  
      //---- Candle7 OHLC
      double O8=NormalizeDouble(iOpen(Symbol(),_Period,8),4);
      double C8=NormalizeDouble(iClose(Symbol(),_Period,8),4);
        
      //---- Candle7 OHLC
      double O7=NormalizeDouble(iOpen(Symbol(),_Period,7),4);
      double C7=NormalizeDouble(iClose(Symbol(),_Period,7),4);
      
      //---- Candle6 OHLC
      double O6=NormalizeDouble(iOpen(Symbol(),_Period,6),4);   
      double C6=NormalizeDouble(iClose(Symbol(),_Period,6),4);
      
      //---- Candle5 OHLC
      double O5=NormalizeDouble(iOpen(Symbol(),_Period,5),4);
      double C5=NormalizeDouble(iClose(Symbol(),_Period,5),4);
      
      //---- Candle4 OHLC
      double O4=NormalizeDouble(iOpen(Symbol(),_Period,4),4);
      double C4=NormalizeDouble(iClose(Symbol(),_Period,4),4);
      
      //---- Candle3 OHLC
      double O3=NormalizeDouble(iOpen(Symbol(),_Period,3),4);
      double C3=NormalizeDouble(iClose(Symbol(),_Period,3),4);
      
      //---- Candle2 OHLC
      double O2=NormalizeDouble(iOpen(Symbol(),_Period,2),4);
      double C2=NormalizeDouble(iClose(Symbol(),_Period,2),4);
      
      
      //---- Candle1 OHLC
      double O1=NormalizeDouble(iOpen(Symbol(),_Period,1),4);
      double C1=NormalizeDouble(iClose(Symbol(),_Period,1),4);     
      
            
      
      
      if (O1 > C1 && O2 < C2 && O3 > C3 && O4 > C4 && O5 > C5 && O6 > C6 && O7 > C7 && time_passed == true) {
         
         for(int x=8; x<=500; x++) {
         
            double O10=NormalizeDouble(iOpen(Symbol(),_Period,x),4);
            double C10=NormalizeDouble(iClose(Symbol(),_Period,x),4);
            
            if (O10 < C10 && C10 < C2) {
            
               double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
               double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
               Print("ACA: ", Ask, "    ACA: ", Bid-5000*_Point);
               //trade.Buy(1, _Symbol, Ask, 0, 0, NULL);
               ///////////////////////////////////////////////////////////trade.Buy(1, _Symbol, Ask, 0, 0, NULL);
               //Print(Ask-Bid);
               trade.Buy(1, _Symbol, Ask, Bid-7000*_Point, Ask+900000*_Point, NULL);
               
               //////////////////////////////////////////////////////////trade_ticket = trade.ResultOrder();
               
               /*
               ulong ticket;
               MqlTradeRequest request = {};
               MqlTradeResult  result  = {};
               double newsl;
               
               for(int i=0; i<PositionsTotal();i++) {
                  ticket=PositionGetTicket(i);
                  
                  if(ticket>0) {
                     
                     request.action   = TRADE_ACTION_SLTP; // type of trade operation
                     request.position = ticket;   // ticket of the position
                     double precio = request.price    = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
                     request.symbol   = PositionGetString(POSITION_SYMBOL);     // symbol 
                     request.sl       = PositionGetDouble(POSITION_SL);                // Stop Loss of the position
                     request.tp       = PositionGetDouble(POSITION_TP);                // Take Profit of the position
                     request.magic    = 100;                                    // MagicNumber of the position
            
                     
                     //newsl = NormalizeDouble(m_symbol.Ask()-5000*m_symbol.Point(),m_symbol.Digits());
                     newsl = NormalizeDouble(Ask-12000*_Point,_Digits);
                     Print("Aca estoy");
                     if(newsl>request.sl) {
                        request.sl = newsl;
                        if(!OrderSend(request,result)) {
                           PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
                          }
                        //--- information about the operation   
                        PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
                       }
                    }
                 }*/
               //trade.
               
               //////ulong ticker = OrderTicket();
               //////double precio = OrderOpenPrice();
               //apertura = OrderOpenPrice();
               
               
              /// if (OrderSelect(trade_ticket) != 0) {
                  //trade.OrderModify(ticker,precio, precio-5000*_Point, NULL);
                  
              ///    trade.OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-5000*_Point, 0, OrderExpiration());
                  //OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-5000*_Point, 0, OrderExpiration());
              /// }
               
               
               
               time_passed = false;
               EventSetTimer(60*2*2);
               //Print(PositionsTotal(), "   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
               //Print("###########################################################################################    ",_Period, "   NUEVO");
               break;              
            
            }
         }
         
      }
       
      //Print(PositionsTotal(), "                                        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); 
      prev_num_velas = num_velas;
   }
}
//+------------------------------------------------------------------+


void OnTimer() {
   time_passed = true;
}
 

Forum on trading, automated trading systems and testing trading strategies

Invalid stops

Vladimir Karputov, 2022.03.21 06:41

Step 1: ALWAYS use the 'MQL5 Wizard' - this is very important!

Follow step 1 and show your result.


After that, I will show you how to go through step 2.


 

Step #2:

 

Step #3: