Plotting TimeFrameExpand data

I was trying to Plot 200 Day EMA along with 1% envelope on to 5 min chart. Please find the code below.

TimeFrameSet(inDaily);
	pds = 200;
	bandwidthperc = 1;
	ema200_D = EMA(C, pds);
	lb_ema200_D = ema200_D * (100 - bandwidthperc)/100;
	ub_ema200_D = ema200_D * (100 + bandwidthperc)/100;

TimeFrameRestore();

ema200 = TimeFrameExpand(ema200_D, inDaily);
lb_ema200 = TimeFrameExpand(lb_ema200_D, inDaily);
ub_ema200 = TimeFrameExpand(ub_ema200_D, inDaily);


SetChartOptions(0,chartShowArrows|chartShowDates);  
Plot( C, "Close", colorDefault, styleBar); 
Plot(ema200, "EMA200", colorGreen, styleLine);
PlotOHLC(ub_ema200, ub_ema200, lb_ema200, lb_ema200,  "", ColorBlend( colorAqua, GetChartBkColor(), 0.7 ), 
		styleNoLabel | styleCloud | styleNoRescale, Null, Null, Null, 0 ); 

But when I apply the above code on to a chart with 5m interval, I dont see the EMA200 or the envelope.

Screenshot 2021-07-06 114819

I checked the "Explore" section, the data seem to be calculated properly.

However, If I zoom out or change the periodicity to 1h/1d, then I could see EMA band.
Screenshot 2021-07-06 115126

Can some please advise how should I tweak the code to get the desired result?

Thanks a lot for your time.
Prasanth.

try adding

SetBarsRequired( sbrAll, sbrAll );

on the top of your code

2 Likes

That worked. Thank you very much.

Actually, activating all past and future bars via sbrAll is not proper way.

EMA does not look forward but backward so you just need to add past bars but not future bars.

Also you should only activate as much bars as actually required.
So you should just set 200 daily bars as being assigned to pds variable.

pds = 200;
bandwidthperc = 1;

SetBarsRequired(pds*inDaily/Max(1,Interval()));

Also you do not need to put pds and bandwidthperc inside TimeFrameSet procedure.
Those variables are not arrays.

Also you do not need to expand three times but just one time.

It is all waste of ressources.

pds = 200;
bandwidthperc = 1;

/// only set as much as required ones but not all bars
/// setting future bars not required as EMA looks backward
SetBarsRequired(pds*inDaily/Max(1,Interval()));

TimeFrameSet(inDaily);	
	ema200_D = EMA(C, pds);	
TimeFrameRestore();
ema200 = TimeFrameExpand(ema200_D, inDaily);

lb_ema200 = ema200 * (1 - bandwidthperc/100);
ub_ema200 = ema200 * (1 + bandwidthperc/100);

SetChartOptions(0,chartShowArrows|chartShowDates);  
Plot( C, "Close", colorDefault, styleBar); 
Plot(ema200, "EMA200", colorGreen, styleLine);
PlotOHLC(ub_ema200, ub_ema200, lb_ema200, lb_ema200,  "", ColorBlend( colorAqua, GetChartBkColor(), 0.7 ), 
		styleNoLabel | styleCloud | styleNoRescale, Null, Null, Null, 0 ); 
4 Likes

Thanks a lot for the clear explanation.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.