How to call certain void functions only at the beginning of Candle formation

 

Hi Everyone

I have two related questions:

Question 1.

I have tried a number of different ways to limit my EA from running on every tick for certain void functions, but have not had any success.  Some of my void functions only need to get executed when the new candle/bar forms, so it's not necessary to call all the iCustom indicators.  My EA looks something like this:

 

extern bool Trail_Stop=true;

int start(){
   Order_Send();
   Order_Close();
   if(Trail_Stop) TrailingStop();

   return(0);
  } 
void Order_Send(){
   //code
  }
void Order_Close(){
   //code
  }
void TrailingStop(){
   //code
  }

 I tried: 

extern bool Trail_Stop=true;
datetime now=0;

int start(){
   if(now !=Time[0]){
   Order_Send();
   Order_Close();
   now=Time[0];
  }
   if(Trail_Stop) TrailingStop();

   return(0);
  } 
void Order_Send(){
   //code
  }
void Order_Close(){
   //code
  }
void TrailingStop(){
   //code
  }

//OR:

extern bool Trail_Stop=true;
datetime now=0;

int start(){
   Order_Send();
   Order_Close();
   if(Trail_Stop) TrailingStop();

   return(0);
  } 
void Order_Send(){
   if(now !=Time[0]){
   //code
   now=Time[0];
  }
 }
void Order_Close(){
  if(now !=Time[0]){
   //code
   now=Time[0];
  }
 }
void TrailingStop(){
   //code
  }

 The efforts above does not work because no trades are opened.

Question 2.

The reason I am trying to accomplish the above is because my EA calls iCustom indicators.  Since I added this to my EA, it became extremely slow in the Strategytester.  The iCustom indicators use functions such as IndicatorCounted or rates_total to limit the calculations to only new data, but this seems to be insufficient.  So, it is due to this, that I am trying what I asked in question one, but is there perhaps a better way or alternative way to speed up startegy tester when iCustom indicators are used?

 

Thank you very much in advance 

 
Add the below code, calling the isNewBar() function to detect when a new bar is formed and thus only execute your code when necessary. However, the function should only be called once per tick even, so if you need to check its state more than once per tick event, save it to a variable for the duration of the start() or OnTick() event.
extern bool Trail_Stop=true;

int start()
{
   if( isNewBar() )
   {
      Order_Send();
      Order_Close();
   }

   if( Trail_Stop ) TrailingStop();

   return(0);
}

void Order_Send() {};
void Order_Close() {};
void TrailingStop() {};

bool isNewBar()
{
   static datetime last_time = WRONG_VALUE;
   datetime lastbar_time = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE ); // to be compatible with MQL5

   if( last_time != WRONG_VALUE )
   {
      if( last_time != lastbar_time )
      {
         last_time = lastbar_time;
         return( true );
      }
   }
   else
      last_time = lastbar_time;

   return( false );
}
As for your problem of the trades not being opened, you did not provide that logic in your post, so we will not be able to help you there.
 
FMIC:
Add the below code, calling the isNewBar() function to detect when a new bar is formed and thus only execute your code when necessary. However, the function should only be called once per tick even, so if you need to check its state more than once per tick event, save it to a variable for the duration of the start() or OnTick() event.
As for your problem of the trades not being opened, you did not provide that logic in your post, so we will not be able to help you there.
Thank you very much for your help.  I will give this a try.
 
Trader3000:
Thank you very much for your help.  I will give this a try.

You can also use WHRoeder's method which much more compact than my example:

WHRoeder:

  1. Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  2. I disagree with make a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
 
FMIC:

You can also use WHRoeder's method which much more compact than my example:

WHRoeder:

  1. Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  2. I disagree with make a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
Okay thank you again.  I will test both methods
Reason: