Ored Modify Starts as soon as a second currency is added - page 2

 

I went through the code and had some difficulty with the "continue;" as I don't have cycle code for the Trailing stop. I added the magic Number as the the message returned "symbol doesnt equal" (Symbol(0 to OrderSymbol() ) .Now I get messgage "Symbol Equals" when using the magic No.

My Trailing stop is moving back and forth, Is there anything I should add to my code ?

Please let me know if you can see any other issues with my code ? Thanks . 

 // Trailing Stop
   
     TS1 = Ask+ New_Stop(trailingstop)*Point; 
     TS2 = Bid - New_Stop(trailingstop)*Point;                          
      RefreshRates();  
      
      
      
      for(int iPos =OrdersTotal()-1; iPos >=0 ; iPos--)
      if(OrderSelect(iPos,SELECT_BY_POS))
      {
      if(trailingstop > 0)
      {                                                                          
   if (OrderType() == OP_BUY)
      {
      if(OrderMagicNumber() == magicnoa)
      {
      if(OrderSymbol() == Symb)
      {
           Alert("Symbol Equals",Symbol()," ",Symb);
           }
         else
           {
           Alert("Doesnt equal symbol",Symbol()," " ,Symb);
           }                                                                                                
         if ((Bid - OrderOpenPrice()) > trailingstop*Point)
         {
          if(!OrderModify(Ticket, OrderOpenPrice(),TS2,TP, 0, Blue))
          {
          Alert("TStop Failed",GetLastError());            
            Alert("Ticket",OrderTicket(), Digits);
            }
            else
            {
            Alert("OrderModified OK","ModifiedPriceat",MarketInfo(Symbol(),MODE_BID));
            }           
          
                  
          
                        return(0);
                   }
               }
            }
         }
       }
 
barnacle7:

I went through the code and had some difficulty with the "continue;" as I don't have cycle code for the Trailing stop. 

What about the for loop ?

Without the continue you try to do the OrderModify() regardless of a match on the Symbol or not . . .

 

I tried  "While(trailingstop > 0) " code in and it caused the MT4 to freeze, When ever I see examples of continue; being used the  "While statement is being used. If I don't add the while statement I get errors . It seems like it is almost working. 

I've been looking at https://www.mql5.com/en/articles/1404 but I can't get rid of the errors in my code.  But, I can't seem to get rid of the errors ,

 Any assistance you can provide to get me over this hurdle would be welcome. Thanks.

 // Trailing Stop
     TS1 = Ask+ New_Stop(trailingstop)*Point; 
     TS2 = Bid - New_Stop(trailingstop)*Point; 
     RefreshRates();                      
       
      
      
      
      for(int iPos =OrdersTotal()-1; iPos >=0 ; iPos--)
      if(OrderSelect(iPos,SELECT_BY_POS))
   
      
      if(trailingstop > 0)
                                                                              
   if (OrderType() == OP_BUY)
      
      if(OrderMagicNumber() != magicno) continue;
      {
      if(OrderSymbol()!= Symb) 
           {
           Alert("Symbol Equals",Symbol()," ",Symb);
               
           }
         else
           {
           Alert("Doesnt equal symbol",Symbol()," " ,Symb); 
                
            continue;
                                                                                   
             }
      
         if ((Bid - OrderOpenPrice()) > trailingstop*Point)
         {
         
       if(!OrderModify(OrderTicket(), OrderOpenPrice(),TS2,TP, 0, Blue))
                        
            {
            Alert("TStop Failed",GetLastError());            
            Alert("Ticket",OrderTicket(), Digits);
            }
            else
            {
            Alert("OrderModified","PriceModified",MarketInfo(Symbol(),MODE_BID));
        
            }                                     
               
               
                return(0);
                   }
              }
           
        
       
      
      if(trailingstop > 0)
      
      if (OrderType() == OP_SELL)
      
      if(OrderMagicNumber() ! = magicno) continue;
      {
      if(OrderSymbol() != Symb)
          {
           Alert("Symbol Equals",Symbol()," ",Symb);
         
           }
         else
           {
           Alert("Doesnt equal symbol",Symbol()," " ,Symb);                 
                
           continue;
            }                                                                          
       if ((OrderOpenPrice() - Ask) > trailingstop * Point)
         {
                       
     if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS1, TP, 0,Blue))
           {
            Alert("TStop Failed",GetLastError());            
            Alert("Ticket",OrderTicket(),  Digits);
           }
           else
           {
            Alert("OrderModified","PriceModified",MarketInfo(Symbol(),MODE_BID));
        
            }            
                       
           return(0);
             }
          }
                         
        } 
 
barnacle7:

I tried  "While(trailingstop > 0) " code in and it caused the MT4 to freeze, When ever I see examples of continue; being used the  "While statement is being used. If I don't add the while statement I get errors .

What errors are you getting ?
 
RaptorUK:


Why have these 2 lines when they do exactly the same thing ?


         if ((Bid - OrderOpenPrice()) > trailingstop*Point)
         {
            if (OrderStopLoss() < (Bid - trailingstop*Point)) 

They don't do exactly the same thing....  

OrderStopLoss()  can be smaller, equal or higher value then the OrderOpenPrice()

It makes the trailing happen if BreakEvenPoint has past..... 

 
deVries:

They don't do exactly the same thing....  

OrderStopLoss()  can be smaller, equal or higher value then the OrderOpenPrice()

It makes the trailing happen if BreakEvenPoint has past..... 

Qops,  sorry,  you are quite right,  for some reason I saw the same function in both cases. 
 

 If I remove this block of code from the EA  it compiles with NO Errors and and  it works.

So the Error is due to this block of code in the // TrailingStop.

I am getting 3 Errors;  Error 1/ ' continue' - 'break' or 'continue' used with some cycle only

      (Same as Error 1) Error 2/   'continue' - 'break' or 'continue' used with some cycle only

                               Error  3/ 'Error' - internal error

The above Errors are as per the code in SRC.

If I put:  While (trailingstop > 0)  it causes the MT4 platform to freeze, even though it does compile with "Zero Errors".

 

 

 // Trailing Stop
     TS1 = Ask+ New_Stop(trailingstop)*Point; 
     TS2 = Bid - New_Stop(trailingstop)*Point; 
     RefreshRates();                      
       
      
      
      
      for(int iPos =OrdersTotal()-1; iPos >=0 ; iPos--)
     
      if(OrderSelect(iPos,SELECT_BY_POS))
   
      {        //1
      if(trailingstop > 0)
      {         //2                                                               
   if (OrderType() == OP_BUY)
      {      //3
      if(OrderMagicNumber() != magicno) continue;
      {     //4
      if(OrderSymbol()!= Symb) 
           {
          
           Alert("Doesnt equal symbol",Symbol()," " ,Symb);     
           }
         else
           {
            Alert("Symbol Equals",Symbol()," ",Symb);
            }
             continue;
         
         
               
         if ((Bid - OrderOpenPrice()) > trailingstop*Point)
                {     //5
                
          if (OrderStopLoss() < (Bid - trailingstop*Point))
            {        //6
            
       if(!OrderModify(OrderTicket(), OrderOpenPrice(),TS2,TP, 0, Blue))
                        
            {
            Alert("TStop Failed",GetLastError());            
            Alert("Ticket",OrderTicket(), Digits);
            }
            else
            {
            Alert("OrderModified","PriceModified",MarketInfo(Symbol(),MODE_BID));
        
            }                                     
               
               
                return(0);
                   }
              }                     //6 Parenthesis
           }
        }
      } 
    }  
      if(trailingstop > 0)
      {     //1
      if (OrderType() == OP_SELL)
      {     //2
      if(OrderMagicNumber() != magicno) continue;
      {     //3
      if(OrderSymbol() != Symb)
          {
           Alert("Symbol Equals",Symbol()," ",Symb);
         
           }
         else
           {
           Alert("Doesnt equal symbol",Symbol()," " ,Symb);                 
               }   
       
         continue;
           
              
                                                                                      
       if ((OrderOpenPrice() - Ask) > trailingstop * Point)
         {        //4
       if (OrderStopLoss() > (Ask + trailingstop * Point)) 
            {   //5
                       
     if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS1, TP, 0,Blue))
           {
            Alert("TStop Failed",GetLastError());            
            Alert("Ticket",OrderTicket(),  Digits);
           }
           else
           {
            Alert("OrderModified","PriceModified",MarketInfo(Symbol(),MODE_BID));
        
            }            
                       
           return(0);
                 }
             }             //5  Parenthesis
                         
           }       
         }
       }
 
barnacle7:

 If I remove this block of code from the EA  it compiles with NO Errors and and  it works.

So the Error is due to this block of code in the // TrailingStop.

I am getting 3 Errors;  Error 1/ ' continue' - 'break' or 'continue' used with some cycle only

      (Same as Error 1) Error 2/   'continue' - 'break' or 'continue' used with some cycle only

                               Error  3/ 'Error' - internal error

The above Errors are as per the code in SRC.

If I put:  While (trailingstop > 0)  it causes the MT4 platform to freeze, even though it does compile with "Zero Errors".

You don't need the while . . .

 // Trailing Stop
     TS1 = Ask+ New_Stop(trailingstop)*Point; 
     TS2 = Bid - New_Stop(trailingstop)*Point; 
     RefreshRates();                      
       
      
     for(int iPos =OrdersTotal()-1; iPos >=0 ; iPos--)
         
         if(OrderSelect(iPos,SELECT_BY_POS))
            {        //1
            if(trailingstop > 0)
               {         //2                                                               
               if (OrderType() == OP_BUY)
                  {      //3
                  if(OrderMagicNumber() != magicno) continue;
                     {     //4
                     if(OrderSymbol()!= Symb) 
                        {
                        Alert("Doesnt equal symbol",Symbol()," " ,Symb);     
                        continue;
                        }
                     else
                        {
                        Alert("Symbol Equals",Symbol()," ",Symb);
                        }
                     if ((Bid - OrderOpenPrice()) > trailingstop*Point)
                        {     //5
                        if (OrderStopLoss() < (Bid - trailingstop*Point))
                           {        //6
                           if(!OrderModify(OrderTicket(), OrderOpenPrice(),TS2,TP, 0, Blue))
                              {
                              Alert("TStop Failed",GetLastError());            
                              Alert("Ticket",OrderTicket(), Digits);
                              }
                           else
                              {
                              Alert("OrderModified","PriceModified",MarketInfo(Symbol(),MODE_BID));
                              }                                     
                           return(0);           //  remove or continue
                           }  //  6 - end
                        }   //  5 - end                     //6 Parenthesis  --  Nope ! ! 
                     }  //  4 - end
                  }  //  3 - end
               }  //  2 - end  
            }  //  1 - end

  //   ! ! ! !      For loop ends here     ! ! ! ! 
  
        if(trailingstop > 0)
           {     //1
           if (OrderType() == OP_SELL)
              {     //2
              if(OrderMagicNumber() != magicno) continue;
                 {     //3
                 if(OrderSymbol() != Symb)
                    {
                    Alert("Symbol Equals",Symbol()," ",Symb);
                    }
                 else
                    {
                    Alert("Doesnt equal symbol",Symbol()," " ,Symb);                 
                    continue;
                    }   
                 if ((OrderOpenPrice() - Ask) > trailingstop * Point)
                    {        //4
                    if (OrderStopLoss() > (Ask + trailingstop * Point)) 
                       {   //5
                       if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS1, TP, 0,Blue))
                          {
                          Alert("TStop Failed",GetLastError());            
                          Alert("Ticket",OrderTicket(),  Digits);
                          }
                       else
                          {
                          Alert("OrderModified","PriceModified",MarketInfo(Symbol(),MODE_BID));
                          }            
                       
                       return(0);
                       }
                    }             //5  Parenthesis
                 }
              }
           }

 The problem is the second block of code is outside of the for loop so the  continues   cannot apply to the for loop, hence the errors.

 

If it were me I would combine some of your  if  statements to make your code easier to follow and less prone to this kind of error,  also use { }  braces with your for  loop for clarity.   I have to go out for a couple of hours,  when I get back I'll show you what I mean if you haven't done it already by then.

 
RaptorUK:

  I have to go out for a couple of hours,  when I get back I'll show you what I mean if you haven't done it already by then.

OK,  this is what I would do . . .

 // Trailing Stop
     TS1 = Ask + New_Stop(trailingstop) * Point; 
     TS2 = Bid - New_Stop(trailingstop) * Point; 
     RefreshRates();                      
       
     if(trailingstop > 0)
        {        //1
        for(int iPos = OrdersTotal()-1; iPos >=0 ; iPos--)
           {         //2
           if(OrderSelect(iPos, SELECT_BY_POS))
              {        //3
              if( OrderType() == OP_BUY &&
                 OrderMagicNumber() == magicno &&
                 OrderSymbol() == Symb ) 
                 {        //4
                 if ( Bid - OrderOpenPrice() > trailingstop * Point &&
                    OrderStopLoss() < Bid - ( trailingstop * Point) )
                    {        //5
                    if(!OrderModify(OrderTicket(), OrderOpenPrice(),TS2,TP, 0, Blue))
                       {
                       Alert("TStop Failed, error # ", GetLastError());            
                       Alert("Ticket ", OrderTicket(), " ", Digits);
                       }
                    else
                       {
                       Alert("OrderModified, PriceModified ", Bid );
                       }
                    }  //  5 - end
                 }  //  4 - end

              if( OrderType() == OP_SELL &&
                  OrderMagicNumber() == magicno &&
                  OrderSymbol() == Symb )
                  {  //  6 
                  if ( OrderOpenPrice() - Ask > trailingstop * Point &&
                     OrderStopLoss() > Ask + ( trailingstop * Point) ) 
                     {   //  7
                     if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS1, TP, 0,Blue))
                        {
                        Alert("TStop Failed, error # ", GetLastError() );            
                        Alert("Ticket ", OrderTicket(), " ", Digits);
                        }
                     else
                        {
                        Alert("OrderModified PriceModified ", Bid);
                        }       
                     }  //  7 - end 
                  }  //  6 - end 
              }  //  3 - end  
           }  //  2 - end
        }  //  1 - end

There is no need for continue in this code,  it will apply the trailing stop to all open orders that match your Symbol and Magic Number. 

 

As I said previously you are going to have issues with comparing doubles.

 
RaptorUK:

OK,  this is what I would do . . .

There is no need for continue in this code,  it will apply the trailing stop to all open orders that match your Symbol and Magic Number. 

 

As I said previously you are going to have issues with comparing doubles.

 

Thanks Raptoruk, I now realise the importance of the parenthesis to keep things in the loop. I was suprised that there was no return; , After alert .. , Well , I'll add it to my list of things I need to learn more about.

 If I didn't have a magic number is it better to use Continue; to stop it flowing to the next code if the Symbol() doesn't Equate to Ordersymbol() ? Using or not using continue; is there anything important to consider when deciding ? 

 I looked at the "can price!= price?" , The issues you are refering to would that be for "Orderopen price , Orderstoploss ,Bid and Ask as they are of "Double type" ? 

My understanding is Never compare double type  for equality and that there may be slippage due to the rounding (Early/Late Entry/Exit or Alert on an indicator  ? Are these the main issues ? 

Thanks . 

Reason: