draw n day moving average on intraday charts? with errors!

 

Hello, i am new to mt4 and programmed a very long time ago but not sure how to fix this indicator?

The goal is to have the daily moving average on intraday charts.

errors

1.When i change time frames say 15m to 30m the moving average disappears.

2.When a new candle is formed the ma line shoots straight up.

My strategy uses the daily ma on the smaller time frames.

It would be great if someone could help fix these errors

Thanks

Don

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red


extern int timeframe=1440;  //minutes
extern int period=14;
extern int shift=0;
extern int method=MODE_SMA;
extern int applyToPrice=PRICE_CLOSE;

double movingAverage[];


int init()  {
	SetIndexStyle(0,DRAW_LINE);
	SetIndexShift(0,0);
	SetIndexDrawBegin(0,0);
	SetIndexEmptyValue(0,0.0);
	SetIndexLabel(0,"Moving Average");
	SetIndexBuffer(0,movingAverage);
	
	IndicatorShortName("Moving Average " + timeframe + " minutes, shown on " + (Time[0]-Time[1])/60 + " min chart");
	
return(0);
}



int deinit()   {
	return(0);
}




int start() {
	int limit=0;
	int i=0;	
	int counted_bars=IndicatorCounted();
	static int longerTimeframeBarShift=0;
	
	
	if(counted_bars<0) {
		return(-1);
	}
	if(counted_bars>0){
		counted_bars--;
	}

	limit=Bars-counted_bars;
	for(i=0; i<limit; i++) {
		if(Time[i]%60 == 0 && (Time[i]/60)%timeframe == 0) {
			longerTimeframeBarShift++;
		}
		movingAverage[i]=iMA(NULL, timeframe, period, shift, method, PRICE_CLOSE, longerTimeframeBarShift);
	}
return(0);
}


Files:
 
springdv:

Hello, i am new to mt4 and programmed a very long time ago but not sure how to fix this indicator?

The goal is to have the daily moving average on intraday charts.

errors

1.When i change time frames say 15m to 30m the moving average disappears.

2.When a new candle is formed the ma line shoots straight up.

My strategy uses the daily ma on the smaller time frames.

It would be great if someone could help fix these errors

Thanks

Don

1. It's good that you understand HTML, but around here, to post your code please use SRC button.

So would you please edit your post (click edit link on top right of your post), remove the HTML and use SRC.

2. Below is Bollinger Band on Multi TF as example on how to draw CI with bigger TF on smaller TF chart. Copy paste that onto MetaEditor.

Files:
 
springdv:
1.When i change time frames say 15m to 30m the moving average disappears.
static int longerTimeframeBarShift=0;
  1. When you change periods, the EA/indicator goes through a deinit/init cycle, but it is not reloaded, so global and static variables are not reloaded. longerTimeframeBarShift MUST be reset to zero EACH time. Delete the static keyword.
  2. And you are not changing longerTimeframeBarShift properly (i.e. the first H1/M30.. bar of the day shows the moving average of yesterday's timeframe's TF.
    //if(Time[i]%60 == 0 && (Time[i]/60)%timeframe == 0) {
    //                      longerTimeframeBarShift++;
    //}
    movingAverage[i]=iMA(NULL, timeframe, period, shift, method, PRICE_CLOSE, longerTimeframeBarShift);
    if((Time[i]/60)%timeframe == 0) {
                            longerTimeframeBarShift++;
    }
    

 
WHRoeder:
  1. When you change periods, the EA/indicator goes through a deinit/init cycle, but it is not reloaded, so global and static variables are not reloaded. longerTimeframeBarShift MUST be reset to zero EACH time. Delete the static keyword.
  2. And you are not changing longerTimeframeBarShift properly (i.e. the first H1/M30.. bar of the day shows the moving average of yesterday's timeframe's TF.

WHRoeder,

thank you for your very quick reply. I have commented out the lines you pointed out and now i have the current daily ma value across my entire chart.

With the code left in you get the value for each daily ma (note each day is different),for example on the 4h chart you can see exactly how the 4hr candles interact with the daily ma.

Any other thought?

Thanks again for your help.

Don

 
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Green
//+------------------------------------------------------------------+
//---- indicator parameters
//+------------------------------------------------------------------+
extern int timeframe=1440;  //minutes
extern int period=8;
extern int shift=0;
extern int method=MODE_SMA;
extern int applyToPrice=PRICE_CLOSE;

double movingAverage[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function 
//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
//---- drawing settings
//+------------------------------------------------------------------+
        SetIndexStyle(0,DRAW_LINE);
        SetIndexShift(0,0);
        SetIndexDrawBegin(0,0);
        SetIndexEmptyValue(0,0.0);
        SetIndexLabel(0,"Moving Average");
        SetIndexBuffer(0,movingAverage);
//+------------------------------------------------------------------+
//---- indicator short name
//+------------------------------------------------------------------+  
        IndicatorShortName("Moving Average " + timeframe + " minutes, shown on " + (Time[0]-Time[1])/60 + " min chart");
        
return(0);
}

int deinit()   {
        return(0);
}

int start() {
        int limit=0;
        int i=0;        
        int counted_bars=IndicatorCounted();
        static int longerTimeframeBarShift=0;
        
        if(counted_bars<0) {
                return(-1);
        }
        if(counted_bars>0){
                counted_bars--;
        }

        limit=Bars-counted_bars-2;
        for(i=0; i<limit; i++) {
                if(Time[i]%60 == 0 && (Time[i]/60)%timeframe == 0) {
                        longerTimeframeBarShift++;
                }
                movingAverage[i]=iMA(NULL, timeframe, period, shift, method, PRICE_CLOSE, longerTimeframeBarShift);
        }
return(0);
}
Reason: