Trailing fails

 

Hi all,

The code works fine, but sometimes it fail to start trailing. Could anyone get why ?

The Trailing Stop should start to trail once MinimumProfit is reached.

I've even change the position of line that makes the Trail start. 

The ea is to work with an ECN broker. 

Here is the code that works with TrailingStop ;

 

 

OTLastTick = OTCurrentTick;                      
    OTCurrentTick = OrdersTotal(); 
                     
    if(OTCurrentTick == 0 && OTLastTick > 0)
      {      
      BuyTrigger = Ask + OpenDistance * pips2dbl;
      SellTrigger = Bid - OpenDistance * pips2dbl;
      }             
    //if(OTCurrentTick > 0) Trail();   //<---------------------------------- just to confirm I've change this call to point 1(next block of code)                
    if(OTLastTick >= 2                                                
      && OTCurrentTick < OTLastTick
      && OTCurrentTick > 0)
      {
      CloseAllOnSL();return;

 

 MLots = 0;
   for(int cnt = OrdersTotal()-1; cnt >= 0; cnt--)
      {//11
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))continue;
         {//12
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//13 
            LastOrderProfit = OrderProfit();            
             OrderLots();
         if(MLots <= OrderLots())MLots = NormalizeDouble(OrderLots() * Multiplier,2);                                                    
            }//13
         }//12      
      }//11
         if(LastOrderProfit > 0)Trail();  //<------------------------------------------------------------ point 1        
   return(0);
   }//0 

 

void Trail()
    {        
     double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
     double TrailingStop = StopLevel + MinStop * pips2dbl;
                 
      for(int OrderCounter = OrdersTotal()-1; OrderCounter >= 0; OrderCounter--)                   
         {//23
        if(!OrderSelect(OrderCounter,SELECT_BY_POS))continue;
         if(OrderType()== OP_BUY && OrderSymbol()== Symbol() 
            && OrderMagicNumber()== MagicNumber)                                                                        
            {//24
               RefreshRates();
            if(OrderStopLoss() < Bid - (TrailingStop * pips2dbl) && Bid - OrderOpenPrice() > (MinimumProfit * pips2dbl)) 
               {//25
                  RefreshRates();
               if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (TrailingStop * pips2dbl), OrderTakeProfit(),0,Yellow))
                Print("Buy TrailingStop Failed, order number:",OrderTicket(),"Error:",GetLastError());
               }//25
             }//24
           }//23          
           if(OrderType()==OP_SELL && OrderSymbol()== Symbol() 
              && OrderMagicNumber()== MagicNumber)
              {//26
                 RefreshRates();
              if(OrderStopLoss() > Ask + (TrailingStop * pips2dbl) && OrderOpenPrice() - Ask > (MinimumProfit * pips2dbl))  
                 {//2
                    RefreshRates();
                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (TrailingStop * pips2dbl) ,OrderTakeProfit(),0,Yellow))              
                 Print("Sell TrailingStop Failed, order number:",OrderTicket(),"Error:",GetLastError());
               }//27                                         
            return(0);     
          }//26   
    }

In attach is the complete code.

Tanks in advance for any help.

Luis 

 

Tell me , will this work in every possible situation ?? 

OTLastTick = OTCurrentTick;                      
    OTCurrentTick = OrdersTotal(); 
                     
    if(OTCurrentTick == 0 && OTLastTick > 0)
      {      
      BuyTrigger = Ask + OpenDistance * pips2dbl;
      SellTrigger = Bid - OpenDistance * pips2dbl;
      }             

 or do you know yourself situations when it will fail ??

 
deVries:

Tell me , will this work in every possible situation ?? 

 or do you know yourself situations when it will fail ??

 


Hi deVries,

Thank you to keep attention to my issue.

That part of code to this moment have not fail. Every time I run the ea the initial orders have always open. 

Because I do not know how to evaluate this line of code

if(OTCurrentTick > 0) Trail();

and because that sometimes the trailing stop do not start to trail when  Minimumprofit reach the value(in this case 1.7)  I thought to put the function call were in my point of view  is always present a valid signal (sorry for my ignorance).

On the Trailing Stop code and as far I know a problem with stop level could arrive, I've put this code to avoid that issue, but really I'm not sure is id this the way to do that...

 

 void Trail()
    {        
     double StopLevel = MarketInfo(Symbol(),MODE_STOPLEVEL) * Point;
     double TrailingStop = StopLevel + MinStop * pips2dbl;

 

 Some times I receive error modification of order failed [invalid S/L or T/P] on journal tab and Trailing Stop failed Error :130.

 This moment (I'm testing on demo) seems to work fine but other times (2 hours ago) it not trail....

So, I dot not know is the code as it is is right or  due to my ignorance some things should be done other way...

Any support you could provide is of up most importance for me

Best Regards

Luis 

 

Then explain what this part

OTLastTick = OTCurrentTick;                      
    OTCurrentTick = OrdersTotal(); 
                     
    if(OTCurrentTick == 0 && OTLastTick > 0)
      {      
      BuyTrigger = Ask + OpenDistance * pips2dbl;
      SellTrigger = Bid - OpenDistance * pips2dbl;
      }             

 of code has to do ....

And what do you think will happen if you use other systems and trading other charts ??

 
deVries:

Then explain what this part

 of code has to do ....

And what do you think will happen if you use other systems and trading other charts ??

 


Hi deVries,

 Yes, seems that I have an issue here. As it is and as far I understand, if other orders are active in the pool that will cause a conflict. To be honest I do not know how to handle a way to open orders without  indicators. For instance, I think to get say a variable that is 0 and the after some time or ticks take the Ask or Aid at present moment and open an order  , but is just an idea..I've no clue how to do it....

Back to trailing stop some times it start to trail before reach Minimumprofit.....and here I can't see what can do this. 

 
luisneves:


Hi deVries,

 Yes, seems that I have an issue here. As it is and as far I understand, if other orders are active in the pool that will cause a conflict. To be honest I do not know how to handle a way to open orders without  indicators. For instance, I think to get say a variable that is 0 and the after some time or ticks take the Ask or Aid at present moment and open an order  , but is just an idea..I've no clue how to do it....

Back to trailing stop some times it start to trail before reach Minimumprofit.....and here I can't see what can do this. 

 


Why go back to trailing stop if the issue is not solved ???     

I think if there is no open trade running your EA you place an BUYSTOP   and a SELLSTOP   trade

First you have to do is make a loop checking trades counting down all trades and count the trades of your EA

to do this select your magicnumber and symbol EA is trading and perhaps count the different ordertypes 

 
deVries:


Why go back to trailing stop if the issue is not solved ???     

I think if there is no open trade running your EA you place an BUYSTOP   and a SELLSTOP   trade

First you have to do is make a loop checking trades counting down all trades and count the trades of your EA

to do this select your magicnumber and symbol EA is trading and perhaps count the different ordertypes 


Thank you for your clues to help me. I will do my best to get this part of code work properly.

Once again tank you for your support

Luis 

Reason: