Need help to spot some slight error . - page 4

 
If that's so then I'm right in my 1st code also then , I call OrderSelect() right away after the 1st for loop ..... The problem now is that the trailing stop loss is not working properly and I still have no clue where the problem is .
 
juniorlcq: uses count down loop but with x-- . I don't understand why you suggest --x though .
  1. x++ reads the value, saves a copy, increments the value, stores the new value in x, and retrieves the copy for subsequent use.
  2. ++x reads the value, increments the value, stores the new value in x
  3. For integers it make little difference. On some platforms the latter is a single instruction making it several times faster.
  4. For classes (like iterators) you have an extra constructor and destructor call, making it many times faster.
    Prefix operator++
    Postfix operator++
    RaRev* RaRev::operator++(void){         // prefix
       ++mPos;
       return GetPointer(this); // Allow (++It).Get()
    }
    
    RaRev RaRev::operator++(int){               // postfix
       RaRev  orig(this); // Make a copy to return.
       ++this;            // Increment myself.
       return orig;       // Return the original position.
    }
    
    In most STL implementations you'll frequenty see "iterator last=end; --end;" rather than "iterator last=end--"

 

Say the price goes up 600 point from OrderOpenprice(), =  StartTraillingStop (200) + 8(xai)*50

  the code is here :

  if ( xai >= 8 ) 
    {
    if ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xai ) ) )
        {
        if ( OrderStopLoss() == ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * ( xai - 1 ) ) ) * Figure ) ) )
           {
            Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * xai ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
       }
    }

but the code will do all the xai from 0 to 8, say the code is at xai = 4; do this condition is true ?

if ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xai ) ) )

yes then the SL will be set at : StartTraillinStop (200) + 4(xai)*50 ( going down from previous )

then xai  5, the SL will be set at : StartTraillinStop (200) + 5(xai)*50

and so on, so it is not possible to put a break once the SL is set;  

 

juniorlcq Your code is much easier to read now you have reformatted it. I reformatted it a little bit more to make it smaller to post, I have highlighted possible issues. I am thinking you might have "double == calculated double" problem that is preventing your code from executing some of the trails. Have you ever read the thread about can price != price ?

I am most suspicious of the 2nd and 4th ones as they are more complicated calculations.

Put Print() statements after those calculations to see if they really do == or not.

The error known as floating point error could make them != even when it looks like they should be ==.

== is really only 100% safe to use with integers so you might change the ==  to <= or >= as a test.

  void ProcessTrailingStop()
 {
  bool Result;
  for(int x=(OrdersTotal()-1); x>=0; x--)
  {if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==True)
   {PointsDetection();
    if(OrderType()==OP_BUY)
    {for(int xai=0; xai<NumberOfTrail; xai++)
     {if(xai==0)
      {if(((OrderClosePrice()-OrderOpenPrice())/Figure)>=StartTrailingStop)
       {if(( OrderStopLoss()==0) || (OrderStopLoss()<(OrderOpenPrice()+(TrailingStop*Figure))))
        {Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+(TrailingStop*Figure)),OrderTakeProfit(),0,Color);
      }}}
      if(xai==1)
      {if(((OrderClosePrice()-OrderOpenPrice())/Figure)>=(StartTrailingStop+CummulativeValue))
       {if(OrderStopLoss()==(OrderOpenPrice()+(TrailingStop*Figure)))
        {Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+((TrailingStop+CummulativeValue)*Figure)),OrderTakeProfit(),0,Color);
      }}}
      if(xai>=2)
      {if(((OrderClosePrice()-OrderOpenPrice())/Figure)>=(StartTrailingStop+(CummulativeValue*xai)))
       {if(OrderStopLoss()==(OrderOpenPrice()+(( TrailingStop+(CummulativeValue *(xai-1)))*Figure)))
        {Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+(( TrailingStop+(CummulativeValue*xai))*Figure)),OrderTakeProfit(),0,Color);
    }}}}}else
    {if(OrderType()==OP_SELL)
     {for(int xaii=0; xaii<NumberOfTrail; xaii++)
      {if(xaii==0)
       {if(((OrderOpenPrice()-OrderClosePrice())/Figure)>=StartTrailingStop)
        {if(( OrderStopLoss()==0) || (OrderStopLoss()>(OrderOpenPrice() -(TrailingStop*Figure))))
         {Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice() -(TrailingStop*Figure)),OrderTakeProfit(),0,Color);
       }}}
       if(xaii==1)
       {if(((OrderOpenPrice()-OrderClosePrice())/Figure)>=(StartTrailingStop+CummulativeValue))
        {if(OrderStopLoss()==(OrderOpenPrice() -(TrailingStop*Figure)))
         {Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice() -(( TrailingStop+CummulativeValue)*Figure)),OrderTakeProfit(),0,Color);
       }}}
       if(xaii>=2)
       {if(((OrderOpenPrice()-OrderClosePrice())/Figure)>=(StartTrailingStop+(CummulativeValue*xaii)))
        {if(OrderStopLoss()==(OrderOpenPrice() -(( TrailingStop+(CummulativeValue *(xaii-1)))*Figure)))
         {Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice() -(( TrailingStop+(CummulativeValue*xaii))*Figure)),OrderTakeProfit(),0,Color);
 }}}}}}}}}


Also as a side note: Your code would be a lot more efficient if you call OrderOpenPrice(), OrderStopLoss(), OrderClosePrice() once at the beginning of your OrderSelect() loop and assign their values to local vars to use throughout the rest of the code. Local vars are much faster access than function calls so you should avoid making repeated function calls for the same result... As a rule of thumb, the more pink writing you have in your code the slower the EA's performance will be.

 

I understand, I can't make it work, this one may work with order buy, SL are gioing down as explained :

   extern double CummulativeValue = 50.0 ;
   extern int    NumberOfTrail = 100 ;



  void ProcessTrailingStop()
   {
       double Figure = Point;
    color Color = Yellow;
  
    bool Result ;
         for ( int x =  OrdersTotal(); x >= 0 ; x-- )
         {
         if ( OrderSelect ( x , SELECT_BY_POS , MODE_TRADES ) == false) continue;
        if ( OrderType() == OP_BUY )
           {
                 for ( int xai = 0 ; xai < NumberOfTrail ; xai++ )
                     {
                        if ( xai == 0 )
                        {
                           if ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= StartTrailingStop )
                           {
                              if ( OrderStopLoss() == 0 ) 
                              {
                               Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( TrailingStop * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                          } } }                        
                  
                        if ( xai == 1 )
                           {
                           if ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + CummulativeValue ) )
                           {
                           Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + CummulativeValue ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                          } }                         
                        
                        if ( xai >= 2 )
                           {
                          if ( ( ( OrderClosePrice() - OrderOpenPrice() ) / Figure ) >= ( StartTrailingStop + ( CummulativeValue * xai ) ) )
                           {
                           Result = OrderModify ( OrderTicket() , OrderOpenPrice() , ( OrderOpenPrice() + ( ( TrailingStop + ( CummulativeValue * xai ) ) * Figure ) ) , OrderTakeProfit() , 0 , Color ) ;
                        } }                         
                     }
                  }
                  
       }}           
 
WHRoeder:
  1. x++ reads the value, saves a copy, increments the value, stores the new value in x, and retrieves the copy for subsequent use.
  2. ++x reads the value, increments the value, stores the new value in x
  3. For integers it make little difference. On some platforms the latter is a single instruction making it several times faster.
  4. For classes (like iterators) you have an extra constructor and destructor call, making it many times faster.
    Prefix operator++
    Postfix operator++
    In most STL implementations you'll frequenty see "iterator last=end; --end;" rather than "iterator last=end--"


Ohhhhhhh I didn't know about this . Thanks WHRoeder :) .

 

So does it mean that ,

let's say OrdersTotal() == 3 , with the count down for loop for ( int x = ( OrdersTotal() - 1 ) ; x >= 0 ; x-- ) , x will save the 1st value as 2 , then it will continue the for loop from 2 onwards without going through OrdersTotal() again ??

 
SDC:

juniorlcq Your code is much easier to read now you have reformatted it. I reformatted it a little bit more to make it smaller to post, I have highlighted possible issues. I am thinking you might have "double == calculated double" problem that is preventing your code from executing some of the trails. Have you ever read the thread about can price != price ?

I am most suspicious of the 2nd and 4th ones as they are more complicated calculations.

Put Print() statements after those calculations to see if they really do == or not.

The error known as floating point error could make them != even when it looks like they should be ==.

== is really only 100% safe to use with integers so you might change the ==  to <= or >= as a test.



Nope I haven't read that thread before . Just only did .

What you suggested do sound logical . On a second thought , sometimes drawing lines on MT4 on the price properties it doesn't really show 5 digits price , instead sometimes show something more than that .

But here's one of my floating trade on the one of the previous account , I printed it out to see what was the double like with a simple print coding . Weird MQL4 I guess @.@ ?

I'm still thinking how should I modify the double == double part ..... 

 

 

 

 

 

 

SDC:


Also as a side note: Your code would be a lot more efficient if you call OrderOpenPrice(), OrderStopLoss(), OrderClosePrice() once at the beginning of your OrderSelect() loop and assign their values to local vars to use throughout the rest of the code. Local vars are much faster access than function calls so you should avoid making repeated function calls for the same result... As a rule of thumb, the more pink writing you have in your code the slower the EA's performance will be.

Ohhh I didn't know there will be a huge difference in speed execution , thanks for the guide SDC . Will modified according to your guide after the trail stop started working .

 

Try replacing the last code block with this debug version. Check my code I didn't compile it.

       if(xaii>=2)
       {if(((OrderOpenPrice()-OrderClosePrice())/Figure)>=(StartTrailingStop+(CummulativeValue*xaii)))
        {Print("debug2.1: first condition true");
         if(OrderStopLoss()==(OrderOpenPrice() -(( TrailingStop+(CummulativeValue *(xaii-1)))*Figure)))
         {Print("debug2.2: second condition true");
          Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice() -(( TrailingStop+(CummulativeValue*xaii))*Figure)),OrderTakeProfit(),0,Color);
         }else
         {Print("debug2.2: second condition false");
          Print("debug2.2: val1=",OrderStopLoss()," val2=",OrderOpenPrice() -(( TrailingStop+(CummulativeValue *(xaii-1)))*Figure));
 }}}}}}}}}

 That might reveal something, you could do something similar with the other == conditions too.

 
SDC:

Try replacing the last code block with this debug version. You could do something similar with the xa

 

That might reveal something, you could do something similar with the other == conditions too.


Yup the problems pops out , it is the " double == calculated double problem " . Thanks for pointing out the problem SDC .

Thank you ffoorr for also pointing out the same problem earlier before , but I didn't edit it the correct way and thought it wasn't the problem . 

 

I have a question need some suggestion though , for the 2nd and 3rd if in the 2nd for loop , is it better to put a " between " statement in it ? Like OrderStopLoss() >= x + 1    &&    x - 1      ?? Or should I just use >= or <=  ??

 

I hope WHR doesn't read this, he will have a fit, but I'm going to tell you to try NormalizeDouble() .... it should make those conditions equal when they are supposed to be equal. It should not be necessary to do it to the OrderStopLoss() side of it too, but if it still doesn't come equal when it should you could try to do it to both sides of the condition....

       if(xaii>=2)
       {if(((OrderOpenPrice()-OrderClosePrice())/Figure)>=(StartTrailingStop+(CummulativeValue*xaii)))
        {Print("debug2.0: condition true");
         if(OrderStopLoss()==NormalizeDouble((OrderOpenPrice() -(( TrailingStop+(CummulativeValue *(xaii-1)))*Figure)),Digits));
         {Print("debug2.1: condition true");
          Result=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice() -(( TrailingStop+(CummulativeValue*xaii))*Figure)),OrderTakeProfit(),0,Color);
         }else
         {Print("debug2.1: condition false");
          Print("debug2.1: val1=",OrderStopLoss()," val2=",OrderOpenPrice() -(( TrailingStop+(CummulativeValue *(xaii-1)))*Figure));
 }}}}}}}}}
Reason: