TimeFrameSet and staticvars


#21

What matters is what is described in manual for TimeFrameExpand http://www.amibroker.com/f?timeframeexpand
Users guide is the only authoritative source of information and really all you ever needed.
TimeFrameExpand docs clearly say:

Available modes:

expandLast - the compressed value is expanded starting from last bar within given period (so for example weekly close/high/low is available on Friday's bar)
expandFirst - the compressed value is expanded starting from first bar within given period (so for example weekly open is available from Monday's bar)

The default is expandLast. That is why you are getting timestamps like xx:04.
and then you think something is wrong, but it isn't. It is perfectly all right and program operates exactly the way you told it to operate. If you want xx:00 timestamp you need expandFirst as written in manual. In your case:

TimeFrameSet( in5Minute );
.... 
StaticVarSet( "name", TimeFrameExpand( compressed_array, in5Minute, expandFirst ) );

And really as I wrote and as @Milosz wrote, your entire approach is more convoluted that it needs to be. You are builing complex schemes of operations for no gain. Really. Why you need to store high in static variable? Makes no sense to me. Even if you want some indicator. Calculate it on the fly in the formula which consumes it. You seem to be over-engineering the whole thing. Static variables are not really meant to be used the way I see them used. They should be used sparingly for few "state" variables not as some gigantic "virtual hard disk" where you store everything you might need.


#22

You mean this?

HighExpanded = TimeFrameExpand( HighComp, TF, ExpandFirst ) ;

But I am already using it. It is in script1, in my code.

Are you talking about this image?

I am sorry, I could not follow you on this one.

Could you also answer on why the last value of HighExpanded in script2 does not match? It should be 1.38219. Instead it is 1.38225.

I am not saving HIGH in my real project! I tried really hard to make a simple example that I could present to everybody. The staticvars I am saving involve much more calculations.

I am already talking to @milosz and maybe I will change my approach, if I have a chance to present more details about the project and see how I can backtest it. I asked him if we can do it in a different thread though.


#23

Hello
Lets say I have 10 symbols to run in exploration mode.
What is the code that we can use in an exploration to check if we have a new bar?

Bob. I didn’t open a new tread for my question, sorry


#25

Please send the screenshot of Tools->Preferences, "Intraday" page.


#26

settings


#27

Then something else is weird on your end as you won't get 04, 09, 14 timestamps with this. On my end the only way to get such timestamps in watch window is to change the settings to non-standard values in this page. With the default settings I am getting 05, 10, 15 timestamps.

The timestamps are the key. StaticVarGet would give you value from exact matching timestamp and if it is NOT found, it would give you the most recent preceding it.


#28

Here are my database settings, I also think they are ok:

It could be my database. Here is a txt export of the only database (EURUSD) I am using. The prices have only 2 decimal places, I could not find how to control this. Normally they have 5 decimal places. https://ufile.io/k9x6r

Also please send me a small test database, so I can test the script.


#29

You must be refering to this image (script1):

Or this one:

The problem goes away if you put the TimeFrameExpand AFTER the TimeFrameRestore, as @awilson suggested.

The only problem that remains is the last value of HighExpanded in script2 does not match. It should be 1.38219. Instead it is 1.38225.


#30

Hi @Panos

I only have one symbol, but I guess the logic is exactly the same. Here are the resources to take the code you need:
http://www.amibroker.com/kb/2015/11/29/how-to-execute-part-of-the-formula-only-when-new-bar-is-added/


#31

In script1 staticvar HighExpanded does have a timestamp of 16:25:00, when it is stored, with the value 1.38219.

In script2 I can also see the timestamp of 16:25:00. However the value read by staticvarget seems to be "the most recent preceding it" : 1.38225.

But the timestamp of 16:25:00 is there. Why doesn't it read the correct value ( 1.38219 ) ?

script1:
s1

script2:
s2


#32

I tested more by deleting several 1min bars/quotes from the end. Same thing.

In script2, HighExpanded keeps having the same value for the last two 5min bars.

s3

Does this happen to anybody else? Is it only me?


#33

Hi guys

For anybody that wants to test it on their pc, here is the source code. If you test and you find different results, then I would appreciate if you can tell me what I am doing wrong.

The idea is to create an array inside a TimeFrameSet(in5minute);/TimeFrameRestore(); block and then retrieve it from another chart with 5min periodicity.

chart1: 1min period, aa1.afl script.
chart2: 5min period, aa2.afl script.

Situation 1:

Set debugger to CHART INTERVAL:
Tools -> preferences -> debugger -> Bar Interval : Use Current Chart Interval

In script aa2, if you go to the debugger, the last two values of HighExpanded are the same, which seems incorrect to me. (see screenshot)

5min

The output of printf("HighExpanded= "+HighExpanded+"\n"); on the last bar also seems incorrect to me.

The debugger needs to be in CHART INTERVAL:
Tools -> preferences -> debugger -> Bar Interval : Use Current Chart Interval

aa1.afl:

TF = in5Minute;
TimeFrameSet(TF);
HighComp = High;
TimeFrameRestore();

HighExpanded = TimeFrameExpand( HighComp, TF, ExpandFirst ) ;
StaticVarSet( "_HighExpanded", HighExpanded);
SetChartOptions( 0, chartShowArrows | chartShowDates );
//_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

_N( Title = StrFormat( "MyTF - Interval: "+Interval()+ "; DateTime: " + DateTimeToStr(selectedvalue(DateTime())) +
" Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

SetBarsRequired( sbrAll, sbrAll );

aa2.afl:

HighExpanded = StaticVarGet("_HighExpanded");

SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
printf("High= "+High+"\n");
printf("HighExpanded= "+HighExpanded+"\n");

SetBarsRequired( sbrAll, sbrAll );

#34

Hi guys

The problem is solved!

Tomasz explained to me in great detail (through a support ticket) why this is happening:

"if you want to read 1-minute static variable in 5-minute interval, you've got timestamp mismatch
as I explained in the forum, if last bar is incomplete. Reading static variable in such situation
would give you value of last COMPLETED bar."

Knowing this, and since I want it to work in live systems too (where last bar is almost never complete), I can easily create a workaround and store the last bar value in a second static var.

Thank you @Tomasz and @awilson.