MT4 New Bar in indicator

 

Hi,

Could someone please show me how I can detect and only execute something at the open of a new bar please?

I know how to do it in an EA but struggling with an indicator (it's the first indicator I've written!)

Thanks in advance. 

 
imamushroom:

Hi,

Could someone please show me how I can detect and only execute something at the open of a new bar please?

I know how to do it in an EA but struggling with an indicator (it's the first indicator I've written!)

Thanks in advance. 

If you know how to do it in an EA, then just do the same in an indicator.
 
imamushroom:

Hi,

Could someone please show me how I can detect and only execute something at the open of a new bar please?

I know how to do it in an EA but struggling with an indicator (it's the first indicator I've written!)

Thanks in advance. 

int prevbars;

//////////////////////////////////////////////////////////////////
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[])
  {
  //new bar
if(Bars==prevbars) return(rates_total);
prevbars=Bars;
Reason: