How to fix the data offset with Timeframeset

Hello,

I am trying to have a strategy where i want to access daily/hourly TF data on a 5 mins chart.
While doing so, i am seeing incorrect data its fetching.
Can you let me know how can i fix it within the code?

image

	AddColumn(H			,"High"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(L			,"Low"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(C			,"Close" 		,  1.2 , colorDefault , colorDefault);
	AddColumn(Cc		,"Pot Close"	,  1.2 , barcolor2	  , colorDefault);
	
	AddColumn(TimeFrameExpand(H, tf)			,"TF High"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(L, tf)			,"TF Low"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(C, tf)			,"TF Close" 		,  1.2 , colorDefault , colorDefault);

Below is how the hourly data is looking
image

I want the same to be working when referencing daily timeframe on 5 mins chart.
High has to be hhv of current day trading
Low has to be llv of current day trading
Close has to be lastest 5 min candle close.

Thanks
Pradeep

It would be helpful if you would post all of your code rather than just a snippet. However, to get hourly bars from 5 minute bars you should be using TimeFrameCompress() first and then TimeFrameExpand(). Even simpler would be to just call TimeFrameGetPrice().

Also, it doesn't look like you've verified your AmiBroker license, so you'll want to do that soon or you won't have the ability to continue posting questions.

1 Like

Thanks @mradtke

Sorry about just providing snippet instead of full code.

I did try the TimeFrameSet function for this, and tried to fetch data with Timeframeexpand to verify data in explorations. I might be missing lot of things while doing so, so wanted some help to get it right.

Here is full code.
Descriptions: having the code to display strength of the price action. Added custom timeframe calculation to enamle higher timeframe plot on 5 mins chart, but its not showing me same data when i used custom timeframe(hourly/daily) in 5 mins chart,as i use custom(in hourly/ in daily) timeframe on separate chart or standalone



SetOption( "WarningLevel", 1 );

Plot(100,"Upper Limit",colorBlack,styleDashed);
Plot(-100,"Lower Limit",colorBlack,styleDashed);
debug			= ParamToggle( "Enable Debug Mode"	, "False|True"	, 1);
show_mtf		= ParamToggle( "Show both Mutiple-Timeframe + Current TF plots"		, "False|True"	, 1);
use_same_period		= ParamToggle( "Use same period for all timeframes"	, "False|True"	, 1);

_SECTION_BEGIN("Current Timeframe");
potential_period = Param      ("Periods", 9, 1, 200, 1 );
current_style 	 = ParamStyle ("Style of Current TF");
gainPotential 	 = ParamColor ("Gaining Potential" , colorGreen);
loosPotential 	 = ParamColor ("Loosing Potential" , colorRed  );
changePotential  = ParamColor ("Changing Potential", colorBlue );

ys1	=	(High+Low+Close*2)/4;
rk3	=	EMA(ys1,potential_period);
rk4	=	StDev(ys1,potential_period);
rk5	=	(ys1-rk3)*100/rk4;
rk6	=	EMA(rk5,potential_period);
UP	=	EMA(rk6,potential_period);
DOWN 	=  	EMA(up,potential_period);
Oo	=	IIf(up<down,up,down);
Hh	=	Oo;
Ll	=	IIf(up<down,down,up);
Cc	=	Ll;
barcolor2		=IIf(Ref(oo,-1)<Oo AND Cc<Ref(Cc,-1),changePotential,  IIf(up>down,gainPotential,loosPotential ));
ribbon_color 	=IIf(Ref(oo,-1)<Oo AND Cc<Ref(Cc,-1),changePotential , IIf(up>down,gainPotential,IIf( down>up, loosPotential , changePotential )));

if (show_mtf) {
	PlotOHLC(Oo,hh,ll,Cc, "Current" + Name(), barcolor2, current_style );
	Plot(1,"ribbon",ribbon_color,styleOwnScale|styleArea|styleNoLabel, -01, 50 );
}

_SECTION_END();

_SECTION_BEGIN("Custom Timeframe");

potential_tf 	 	= ParamList  ("Custom TimeFrames (default is Daily)", "in1Minute|in5Minute|in15Minute|in30Minute|inHourly|in75Minute|in90Minute|inDaily|inWeekly|inMonthly", 7);
tf_potential_period = Param      ("Custom TimeFrame Periods", 9, 1, 200, 1);
custom_plot_width   = Param      ("Custom Plot Width"       , 2, 1,  15, 1);
custom_style 	 	= ParamStyle ("Style Custom TF");
tf_gainPotential 	= ParamColor ("Gaining Potential" , colorDarkGreen);
tf_loosPotential 	= ParamColor ("Loosing Potential" , colorDarkRed  );
tf_changePotential 	= ParamColor ("Changing Potential", colorDarkBlue );

switch (potential_tf) {
    case "in1Minute"	: tf = in1Minute	; break;
    case "in5Minute"	: tf = in5Minute	; break;
    case "in15Minute"	: tf = in15Minute	; break;
    case "in30Minute"	: tf = 2*in15Minute	; break;
    case "in45Minute"	: tf = 3*in15Minute	; break;
    case "inHourly"		: tf = inHourly		; break;
    case "in75Minute"	: tf = 5*in15Minute	; break;
    case "in90Minute"	: tf = 6*in15Minute	; break;
    case "inDaily"		: tf = inDaily		; break;
    case "inWeekly"		: tf = inWeekly		; break;
    case "inMonthly"	: tf = inMonthly	; break;
    default				: tf = inDaily		; break;
}

if (use_same_period) {
	tf_potential_period = potential_period;
}

TimeFrameSet( tf ); // switch now to specified timeframes

	tf_close = Ref(C, 1);
	tf_ys1	=	(High+Low+tf_close*2)/4;
	tf_rk3	=	EMA(tf_ys1,		tf_potential_period);
	tf_rk4	=	StDev(tf_ys1,	tf_potential_period);
	tf_rk5	=	(tf_ys1-tf_rk3)*100/tf_rk4;
	tf_rk6	=	EMA(tf_rk5,		tf_potential_period);
	tf_UP	=	EMA(tf_rk6,		tf_potential_period);
	tf_DOWN =   EMA(tf_up,		tf_potential_period);
	tf_Oo	=	IIf(tf_up < tf_down, tf_up, tf_down);
	tf_Hh	=	tf_Oo;
	tf_Ll	=	IIf(tf_up < tf_down, tf_down, tf_up);
	tf_Cc	=	tf_Ll;
	tf_barcolor2 = IIf(Ref(tf_oo,-1)<tf_Oo AND tf_Cc<Ref(tf_Cc,-1), tf_changePotential, IIf( tf_up>tf_down, tf_gainPotential, tf_loosPotential ));
	tf_rib_color = IIf(Ref(tf_oo,-1)<tf_Oo AND tf_Cc<Ref(tf_Cc,-1), tf_changePotential, IIf( tf_up>tf_down, tf_gainPotential, IIf( tf_down>tf_up, tf_loosPotential, tf_changePotential )));
if (debug) {
	AddColumn(H			,"Inside High"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(L			,"Inside Low"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(C			,"Inside Close" 		,  1.2 , colorDefault , colorDefault);
	}
TimeFrameRestore(); 

mtf_Oo 		   	 = TimeFrameExpand (tf_Oo		, tf);
mtf_Hh 		  	 = TimeFrameExpand (tf_Hh		, tf);
mtf_Ll 		  	 = TimeFrameExpand (tf_Ll		, tf);
mtf_Cc 		  	 = TimeFrameExpand (tf_Cc		, tf);
mtf_close	  	 = TimeFrameExpand (Close		, tf);
mtf_barcolor2 	 = TimeFrameExpand (tf_barcolor2, tf);
mtf_ribbon_color = TimeFrameExpand (tf_rib_color, tf);

Plot(mtf_Cc, "TimeFrame" + Name(), mtf_barcolor2, custom_style, Null, Null, 0 , 0, custom_plot_width );
Plot(2,"ribbon", mtf_ribbon_color,styleOwnScale|styleArea|styleNoLabel, -300, 20 );

if (debug) {
	Filter = 1;
	AddColumn(H			,"High"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(L			,"Low"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(C			,"Close" 		,  1.2 , colorDefault , colorDefault);
	AddColumn(Cc		,"Pot Close"	,  1.2 , barcolor2	  , colorDefault);
	
	AddColumn(TimeFrameExpand(H, tf)			,"TF High"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(L, tf)			,"TF Low"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(C, tf)			,"TF Close" 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(H, tf)			,"EL TF High"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(L, tf)			,"EL TF Low"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(C, tf)			,"EL TF Close" 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_Close, tf)		,"TF1 Close" 		,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_ys1, tf)		,"TF ys1"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_rk3, tf)		,"TF rk3"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_rk4, tf)		,"TF rk4"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_rk5, tf)		,"TF rk5"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_rk6, tf)		,"TF rk6"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_UP, tf)		,"TF UP"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_DOWN, tf)		,"TF DOWN"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_Oo, tf)		,"TF Oo"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_Hh, tf)		,"TF Hh"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_Ll, tf)		,"TF Ll"			,  1.2 , colorDefault , colorDefault);
	AddColumn(TimeFrameExpand(tf_Cc, tf)		,"TF Pot Close"	,  1.2 , barcolor2	  , colorDefault);
	AddColumn(TimeFrameExpand(tf_barcolor2, tf)	,"TF Barcolor2"	,  1.2 , barcolor2	  , colorDefault);
	AddColumn(TimeFrameExpand(Ref(C, 1), tf)	,"TF Close"	 		,  1.2 , colorDefault , colorDefault);
	AddColumn(mtf_Cc	,"MTF Pot Close",  1.2 , mtf_barcolor2, colorDefault);
}

_SECTION_END();

Sure will verify the licence

-Thanks
Pradeep

It appears that you have many errors, so I suggest that you create a simplified example and then use the various TimeFrame* functions along with an Exploration until you understand how they work. Reviewing the documentation again wouldn't hurt either.

Here are a couple of things that you need to address. After your TimeFrameSet() call you have:

tf_close = Ref(C, 1);

If tf is set to inDaily, then this code will place the NEXT DAY'S close into tf_close. Therefore, you are peeking into the future, and all of the tf_ calculations that follow have been corrupted by this future closing value. Always be cautious about using the Ref() function with a second argument which is positive.

After the TimeFrameRestore() function is called, you have this code:

AddColumn(TimeFrameExpand(H, tf)			,"TF High"	 		,  1.2 , colorDefault , colorDefault);
AddColumn(TimeFrameExpand(L, tf)			,"TF Low"	 		,  1.2 , colorDefault , colorDefault);
AddColumn(TimeFrameExpand(C, tf)			,"TF Close" 		,  1.2 , colorDefault , colorDefault);
AddColumn(TimeFrameExpand(H, tf)			,"EL TF High"	 		,  1.2 , colorDefault , colorDefault);
AddColumn(TimeFrameExpand(L, tf)			,"EL TF Low"	 		,  1.2 , colorDefault , colorDefault);
AddColumn(TimeFrameExpand(C, tf)			,"EL TF Close" 		,  1.2 , colorDefault , colorDefault);

The built-in arrays like O, H, L, C were all expanded back to the base timeframe when you called TimeFrameRestore(), so calling TimeFrameExpand() on these arrays again is incorrect. You only need to call TimeFrameExpand() on your own arrays. Also, it obviously makes no sense to report the same value twice but with different column headings, for example "TF Close" and then "EL TF Close".

Thanks @mradtke for looking into this.

May i had too much of illogical statements in my code (am new to this AFL coding, so was tweaking around with flow sequences),

just to keep it simple, can you let me know how i convert below set of code to be working for multi-time-frame and have same value reflected in exploration tab just to validate what value its getting.

I was trying to debug what values it was getting at each stage and kept some redundant statements.
Which you have pointed out in your last point about error.


_SECTION_BEGIN("Current Timeframe");
potential_period = Param      ("Periods", 9, 1, 200, 1 );
current_style 	 = ParamStyle ("Style of Current TF");
gainPotential 	 = ParamColor ("Gaining Potential" , colorGreen);
loosPotential 	 = ParamColor ("Loosing Potential" , colorRed  );
changePotential  = ParamColor ("Changing Potential", colorBlue );

ys1	=	(High+Low+Close*2)/4;
rk3	=	EMA(ys1,potential_period);
rk4	=	StDev(ys1,potential_period);
rk5	=	(ys1-rk3)*100/rk4;
rk6	=	EMA(rk5,potential_period);
UP	=	EMA(rk6,potential_period);
DOWN 	=  	EMA(up,potential_period);
Oo	=	IIf(up<down,up,down);
Hh	=	Oo;
Ll	=	IIf(up<down,down,up);
Cc	=	Ll;
barcolor2		=IIf(Ref(oo,-1)<Oo AND Cc<Ref(Cc,-1),changePotential,  IIf(up>down,gainPotential,loosPotential ));
ribbon_color 	=IIf(Ref(oo,-1)<Oo AND Cc<Ref(Cc,-1),changePotential , IIf(up>down,gainPotential,IIf( down>up, loosPotential , changePotential )));
PlotOHLC(Oo,hh,ll,Cc, "Current" + Name(), barcolor2, current_style );
Plot(1,"ribbon",ribbon_color,styleOwnScale|styleArea|styleNoLabel, -01, 50 );

_SECTION_END();