Indicators: The "New bar" event handler for the indicators

 

The "New bar" event handler for the indicators:

This indicator will allow you to perform the recalculation of the indicator's data only when the new bar on the chart has appeared.

The idea of this approach is described in the "New Bar" Event Handler article. The example of its use in the Expert Advisors is presented in the article. Here you will find the solution for the indicators. Idea is the same: it will allow to perform recalculations only when the new bar has appeared. It's a convenient alternative to the direct use of the OnCalculate() function.

Here is the simple indicator, it prints a line when the new bar has appeared.

Author: Константин

 
datetime new_time=TimeCurrent()/period_seconds*period_seconds; // Bar opening time on the current chart

There's a mistake here. If the period is a week or a month.

If the period is a week, then the beginning of the week is Wednesday, because 1970.01.01 is Thursday.

If the period is a month and the current month does not have 30 days, it counts incorrectly too, because period_seconds corresponds to 30 days.

 
Maratori:

There's a mistake here. If the period is a week or a month.

If the period is a week, the beginning of the week is Wednesday, since 1970.01.01 is Thursday.

If the period is a month, and the current month does not have 30 days, it counts incorrectly too, since period_seconds corresponds to 30 days.

Agreed. You can use this in OnNewBarCalculate.mqh for these periods:

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
//---
   // When a new bar appears, run the NewBar event handler
   if(current_chart.isNewBar()>0) 
      OnNewBarCalculate(rates_total,prev_calculated,time,open,high,low,close,tick_volume,volume,spread);
   return(rates_total);
  }
 

A bit for other purposes, but I'm using a function like this for now:

datetime getHTFTime(datetime time, ENUM_TIMEFRAMES highTimeFrame) {
  int periodSeconds = PeriodSeconds(highTimeFrame);
  if(periodSeconds <= 60*60*24) return datetime(time/periodSeconds*periodSeconds);
  
  MqlDateTime sTime;
  TimeToStruct(time, sTime);
  
  if(highTimeFrame == PERIOD_W1) {
    TimeToStruct(time - datetime(sTime.day_of_week * 24 * 60 * 60), sTime);
    sTime.hour = 0;
    sTime.min = 0;
    sTime.sec = 0;
  } else if(highTimeFrame == PERIOD_MN1) {
      sTime.day = 1;
      sTime.hour = 0;
      sTime.min = 0;
      sTime.sec = 0;
    }
  return StructToTime(sTime);
}

A date/time and a timeframe are given as input. On the output we get the start time of the bar on this timeframe, which contains this time.

If you have any suggestions for optimising the code, I will be glad to listen.

Of course you can do it this way:

datetime timeOut[1];
CopyTime(_Symbol, highTimeFrame, timeIn, 1, timeOut);

But in this case I had a synchronisation problem. I have to wait for the history to load.

 

Thank for your custom indicator but when i use this, i just alert once and stop debuging! Please help me! 

If i use OnTick instead (and remove OnInit and OnCaculate functions) like this article https://www.mql5.com/en/articles/159 it run good but the indicator not display in navigator windows. And it not run when i add OnInit and OnCaculate functions. If i add  OnInit and OnCaculate functions, the indicator is display in navigator windows?

Thank