Mismatch value between indicator and timeframeset

Hi Guys,
I was trying to test the code for backtest. Before backtesting, I use the explore to see if the value are correct by adding column. Setting is in "Daily" mode.
So after exploring, I checked on each result. the chart is on Daily, I then switched to Weekly chart, ADX indicator on the chart is way different from the result. I was looking for ADX up and I was finding ADX was down where the condition was true in weekly chart. How come? Can someone help me please?
Thanks

//Weekly ADX is uptrend
TimeFrameSet(inWeekly);
_Weekly_PDI = PDI(14);
_Weekly_MDI = MDI(14);
_Weekly_ADX = ADX(14);
TimeFrameRestore();
Weekly_ADX_Up = TimeFrameExpand(ADX(14),inWeekly) > TimeFrameExpand(Ref(ADX(14),-1),inWeekly) AND 
				TimeFrameExpand(Ref(ADX(14),-1),inWeekly) > TimeFrameExpand(Ref(ADX(14),-2),inWeekly) AND
				TimeFrameExpand(_Weekly_PDI,inWeekly) > TimeFrameExpand(_Weekly_MDI,inWeekly);

Filter = Weekly_ADX_Up;
AddColumn(TimeFrameExpand(ADX(14),inWeekly),"TimeFrameExpand(ADX(14),inWeekly)");
AddColumn(TimeFrameExpand(Ref(ADX(14),-1),inWeekly),"TimeFrameExpand(Ref(ADX(14),-1),inWeekly)");

It is mandatory rule to apply code tags for code insertion to forum posts. So please do so as shown here.


Please read documentation of AmiBroker. It is all explained in detail there and everything is working correctly.

Default expand mode of TimeFrameExpand is expandLast. There are three expand modes overall.

Ok, silly mistake. using wrong variables in timeframeexpand .

None of the expand mode variables are wrong. They are different and result in different return.

But yes, your code is incorrect (see first argument of your TimeFrameExpand() calls).

And FYI, you should take care to not look forward in backtest.

thanks fxshrat,

In previous code, after TimeFrameRestore(), Instead of using same variable in TimeFramExpand, I used ADX(14) , so it was calculating daily value. Now I changed to below code, it is now working.
But come to another chellange, REF(_Weekly_ADX,-1) value is same as REF(_Weekly_ADX,-2) and REF(_Weekly_ADX,-3) or any previous value.

//Weekly ADX is uptrend and DI+ is above DI-
TimeFrameSet(inWeekly);
_Weekly_PDI = PDI(14);
_Weekly_MDI = MDI(14);
_Weekly_ADX = ADX(14);
TimeFrameRestore();
_Weekly_ADX = TimeFrameExpand(_Weekly_ADX,inWeekly);
_Weekly_PDI = TimeFrameExpand(_Weekly_PDI,inWeekly);
_Weekly_MDI = TimeFrameExpand(_Weekly_MDI,inWeekly);
Weekly_ADX_Up = _Weekly_ADX > Ref(_Weekly_ADX,-1);// AND

AddColumn(_Weekly_ADX,"_Weekly_ADX");
AddColumn(Ref(_Weekly_ADX,-1),"REF(_Weekly_ADX,-1)");
AddColumn(Ref(_Weekly_ADX,-1),"REF(_Weekly_ADX,-2)");
AddColumn(Ref(_Weekly_ADX,-3),"REF(_Weekly_ADX,-3)");

Your third 'AddColumn' should be -2, not -1.

If you look closely at your Daily Explorer result you will see that it is doing what your code is telling AB to do. It is Ref each Daily Bar because you are on a Daily setting. You need to set your Ref's within TimeFrameSet / TimeFrameRestore because that is what you are trying to do, Ref Weeklies not Dailies.

1 Like

Thanks TrendSurfer,

it worked. Thanks for pointing out.

TimeFrameSet(inWeekly);
_Weekly_PDI = PDI(14);
_Weekly_MDI = MDI(14);
_Weekly_ADX = ADX(14);
_Weekly_ADX_1 = Ref(_Weekly_ADX,-1);
_Weekly_ADX_2 = Ref(_Weekly_ADX,-2);
TimeFrameRestore();