Problem with Trailing SL

 

Hi guys! 


I created a simple trading algo which opens long positions when it received BUY signal from an indicator and closes all long positions and opens Short positions when it received SELL signal.

Everything works and I decided to add a trailing SL feature. Unfortunately although it prints no errors, it doesnt work. 

Could you please have a look at me code and provide me with some guidance on what to chance in my project? (Trailing SL lines start at the bottom from " void Ontick() " )

double RenkoStreet_channelHandle0,RenkoStreet_channelHandle1,RenkoStreet_channelHandle2, RenkoStreet_channelHandle3,RenkoStreet_channelHandle4,RenkoStreet_channelHandle5,RenkoStreet_channelHandle6,RenkoStreet_channelHandle7; 


double LastHigh; 
double LastLow; 

int OnInit() 
   {
   Print("Version 1.15");

   return(INIT_SUCCEEDED);
   }                   
int start()
  {  
  LastHigh = iHigh (NULL,0,1);
  LastLow = iLow(NULL,0,1);
  
  Print("Previous High/Low =" ,  iTime(NULL,0,1),",",
                                 iHigh(NULL, 0,1),",",
                                 iLow(NULL,0,1));
                                
   RenkoStreet_channelHandle0=iCustom(NULL,0,"RenkoStreet_channel",4,500,0,1);
   RenkoStreet_channelHandle1=iCustom(NULL,0,"RenkoStreet_channel",4,500,1,1);
   RenkoStreet_channelHandle2=iCustom(NULL,0,"RenkoStreet_channel",4,500,2,1);
   RenkoStreet_channelHandle3=iCustom(NULL,0,"RenkoStreet_channel",4,500,3,1);
   RenkoStreet_channelHandle4=iCustom(NULL,0,"RenkoStreet_channel",4,500,4,1);
   RenkoStreet_channelHandle5=iCustom(NULL,0,"RenkoStreet_channel",4,500,5,1);
   RenkoStreet_channelHandle6=iCustom(NULL,0,"RenkoStreet_channel",4,500,6,1);
   RenkoStreet_channelHandle7=iCustom(NULL,0,"RenkoStreet_channel",4,500,7,1);     
   Print("RenkoStreet_channel = ",RenkoStreet_channelHandle0," ",RenkoStreet_channelHandle1," ",RenkoStreet_channelHandle2," ",RenkoStreet_channelHandle3," ",RenkoStreet_channelHandle4,"",RenkoStreet_channelHandle5," ",RenkoStreet_channelHandle6,"",RenkoStreet_channelHandle7);  

   CheckForOpen();
   Print ("InsideStartAfterCheckForOpen");
   return (0);
   }
void CheckForOpen()
{
   int res;
   Print ("InsideCheckForOpen");   
   if(Volume[0]>1) return;
   if ((RenkoStreet_channelHandle0 !=0)&&(RenkoStreet_channelHandle1 !=0)&&(RenkoStreet_channelHandle3 !=0))
   {
      res=OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-400*Point,Ask+100000*Point,"SEMA",0,0,Blue);
      CloseSellPositions();
   } 
   for (int b= OrdersTotal()-1; b>=0; b--)
   
   
   
                            
   if ((RenkoStreet_channelHandle0 !=0)&&(RenkoStreet_channelHandle1 !=0)&&(RenkoStreet_channelHandle2 !=0))
   {  
      res=OrderSend(Symbol(),OP_SELL,1,Bid,3,Ask+400*Point,Ask-100000*Point,"SEMA",0,0,Blue);
      CloseBuyPositions();
   }
}      
void CloseBuyPositions()   
{ 
   int res;  
   for (int i=OrdersTotal()-1; i >= 0; i--)
   {    
      res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);      
      string CurrencyPair=OrderSymbol();
      if ((OrderType ()==OP_BUY)&& (_Symbol== CurrencyPair))       
      {
         res=OrderClose(OrderTicket(), OrderLots(),Bid,3,NULL);
         Print ("Pozycje Buy Zamknięte");
      }
   }
}  



void CloseSellPositions()   
{ 
   int res;  
   for (int i=OrdersTotal()-1; i >= 0; i--)
   {
      res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);      
      string CurrencyPair=OrderSymbol();   
      if ((OrderType ()==OP_SELL)&& (_Symbol== CurrencyPair))    
      {
         res=OrderClose(OrderTicket(), OrderLots(),Ask,3,NULL);
         Print ("Pozycje Sell Zamknięte");   
      } 
   }
} 


void OnTick()
{
   for (int b= OrdersTotal()-1; b >= 0; b--)
   {
      if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol() == Symbol())
      if (OrderType() == OP_BUY)
      if (OrderStopLoss() < Ask - (150*_Point))
      {
         OrderModify (OrderTicket(),OrderOpenPrice(),Ask - (150*_Point),OrderTakeProfit(),0,CLR_NONE);
         Print ("SL Przesuniety");
      }
   }
}
                                    


Thanks! 

 
int start()

⋮

void OnTick()

You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
          Event Handling Functions - MQL4 Reference
          How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

You can not have both.

 
William Roeder #:

You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
          Event Handling Functions - MQL4 Reference
          How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

You can not have both.

Thank you for your reply!

Unfortunately I dont think its the problem which impacts the code as other versions of this strategy with fixed SL work correctly. I will definitely apply your suggestions, however currently I am trying to determine what is the reason of my code not working properly.. I guess it has to do something with the last part of the code after:

void OnTick(

but I am not sure what exactly..

 
Sebastian Woźniczka #: but I am not sure what exactly..

What part of “You can not have both” is unclear?

 
William Roeder #:

What part of “You can not have both” is unclear?

everything is clear however even tho I changed those old event handlers, the strategy is still not working..
 
Sebastian Woźniczka #: everything is clear however even tho I changed those old event handlers, the strategy is still not working..

You have not shown your NEW code with those changes. So, we cannot see if you applied those changes correctly or not.

 
Fernando Carreiro #:

You have not shown your NEW code with those changes. So, we cannot see if you applied those changes correctly or not.

Yes you are right I apologise, my bad, I am new to MQL and this forum. 

My new code looks as follows:


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//input int TakeProfit = 10;
input int StopLoss = 400;
//input double volume = 0.1;
//

double RenkoStreet_channelHandle0,RenkoStreet_channelHandle1,RenkoStreet_channelHandle2, RenkoStreet_channelHandle3,RenkoStreet_channelHandle4,RenkoStreet_channelHandle5,RenkoStreet_channelHandle6,RenkoStreet_channelHandle7; 


double LastHigh; 
double LastLow; 

int OnInit() 
   {
   Print("Version 1.15");

   return(INIT_SUCCEEDED);
   }                   
int OnStart()
  {  
  LastHigh = iHigh (NULL,0,1);
  LastLow = iLow(NULL,0,1);
  
  Print("Previous High/Low =" ,  iTime(NULL,0,1),",",
                                 iHigh(NULL, 0,1),",",
                                 iLow(NULL,0,1));
                                
   RenkoStreet_channelHandle0=iCustom(NULL,0,"RenkoStreet_channel",4,500,0,1);
   RenkoStreet_channelHandle1=iCustom(NULL,0,"RenkoStreet_channel",4,500,1,1);
   RenkoStreet_channelHandle2=iCustom(NULL,0,"RenkoStreet_channel",4,500,2,1);
   RenkoStreet_channelHandle3=iCustom(NULL,0,"RenkoStreet_channel",4,500,3,1);
   RenkoStreet_channelHandle4=iCustom(NULL,0,"RenkoStreet_channel",4,500,4,1);
   RenkoStreet_channelHandle5=iCustom(NULL,0,"RenkoStreet_channel",4,500,5,1);
   RenkoStreet_channelHandle6=iCustom(NULL,0,"RenkoStreet_channel",4,500,6,1);
   RenkoStreet_channelHandle7=iCustom(NULL,0,"RenkoStreet_channel",4,500,7,1);     
   Print("RenkoStreet_channel = ",RenkoStreet_channelHandle0," ",RenkoStreet_channelHandle1," ",RenkoStreet_channelHandle2," ",RenkoStreet_channelHandle3," ",RenkoStreet_channelHandle4,"",RenkoStreet_channelHandle5," ",RenkoStreet_channelHandle6,"",RenkoStreet_channelHandle7);  

   CheckForOpen();
   Print ("InsideStartAfterCheckForOpen");
   return (0);
   }
void CheckForOpen()
{
   int res;
   Print ("InsideCheckForOpen");   
   if(Volume[0]>1) return;
   if ((RenkoStreet_channelHandle0 !=0)&&(RenkoStreet_channelHandle1 !=0)&&(RenkoStreet_channelHandle3 !=0))
   {
      res=OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-400*Point,Ask+100000*Point,"SEMA",0,0,Blue);
      CloseSellPositions();
   } 
   for (int b= OrdersTotal()-1; b>=0; b--)
   
   
   
                            
   if ((RenkoStreet_channelHandle0 !=0)&&(RenkoStreet_channelHandle1 !=0)&&(RenkoStreet_channelHandle2 !=0))
   {  
      res=OrderSend(Symbol(),OP_SELL,1,Bid,3,Ask+400*Point,Ask-100000*Point,"SEMA",0,0,Blue);
      CloseBuyPositions();
   }
}      
void CloseBuyPositions()   
{ 
   int res;  
   for (int i=OrdersTotal()-1; i >= 0; i--)
   {    
      res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);      
      string CurrencyPair=OrderSymbol();
      if ((OrderType ()==OP_BUY)&& (_Symbol== CurrencyPair))       
      {
         res=OrderClose(OrderTicket(), OrderLots(),Bid,3,NULL);
         Print ("Pozycje Buy Zamknięte");
      }
   }
}  



void CloseSellPositions()   
{ 
   int res;  
   for (int i=OrdersTotal()-1; i >= 0; i--)
   {
      res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);      
      string CurrencyPair=OrderSymbol();   
      if ((OrderType ()==OP_SELL)&& (_Symbol== CurrencyPair))    
      {
         res=OrderClose(OrderTicket(), OrderLots(),Ask,3,NULL);
         Print ("Pozycje Sell Zamknięte");   
      } 
   }
} 


void OnTick()
{
   for (int b= OrdersTotal()-1; b >= 0; b--)
   {
      if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if (OrderSymbol() == Symbol())
      if (OrderType() == OP_BUY)
      if (OrderStopLoss() < Ask - (300*_Point))
      {
         OrderModify (OrderTicket(),OrderOpenPrice(),Ask - (300*_Point),OrderTakeProfit(),0,CLR_NONE);
         Print ("SL Przesuniety");
      }
   }
}
                                    
 
Sebastian Woźniczka #: Yes you are right I apologise, my bad, I am new to MQL and this forum. My new code looks as follows:

Again the same mistake. You cannot use both OnTick() and OnStart() at the same time! You are are coding an Expert Advisor (EA), so you cannot use OnStart() which is for Scripts, not EAs. You should use ONLY the OnTick() for EAs.

EDIT: The only event handlers allowed in MQL4 Expert Advisors (EAs) is OnInit(), OnDeinit(), OnTick(), OnTimer(), OnChartEvent(), OnTester().

See the following documentation: Program Running - MQL4 programs - MQL4 Reference

Program Running - MQL4 programs - MQL4 Reference
Program Running - MQL4 programs - MQL4 Reference
  • docs.mql4.com
Program Running - MQL4 programs - MQL4 Reference
 
      if (OrderType() == OP_BUY)
      if (OrderStopLoss() < Ask - (300*_Point))

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
William Roeder #:

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

I applied your advices and it all works correctly now, thank you very much for your help guys, I really appreciate this! 

Reason: