StaticVar Set/Get confusion

Hello,

I have read on the forum that StaticVar through the Amibroker engine automatically compress to the different timeframes as long as they are a multiple of the same base time the Var is set in, so 1 min can be read in 5/15min timeframes. but am having a bit of trouble executing it.
At the moment the only way i can get it to work is by creating 2 different Staticvars set to the different timeframes (-20 and -260) IE the following way
in the Tic window i have this

_SECTION_BEGIN("Just collecting Tick bids volume");
Ticker = Name();

TF = -20;
TF2 = -260;
BuyVolume = IIf( Close >= Aux2 AND (Aux2 != Aux1), Volume, 0 );
SellVolume = IIf( Close <= Aux1 AND (Aux2 != Aux1), Volume, 0 );

BuyVolComp = TimeFrameCompress( BuyVolume, TF, compressVolume );
BuyVolExp	= TimeFrameExpand( BuyVolComp, TF,  expandFirst );
StaticVarSet( "~BuyVolume" + Ticker, BuyVolexp, False );

SellVolComp = TimeFrameCompress( SellVolume, TF, compressVolume );
SellVolExp	 = TimeFrameExpand( SellVolComp, TF,  expandFirst );
StaticVarSet( "~SellVolume" + Ticker, SellVolexp, False );



BuyVolComp2 = TimeFrameCompress( BuyVolume2, TF2, compressVolume );
BuyVolExp2	= TimeFrameExpand( BuyVolComp2, TF2,  expandFirst );
StaticVarSet( "~BuyVolume2" + Ticker, BuyVolexp2, False );

SellVolComp2 = TimeFrameCompress( SellVolume2, TF2, compressVolume );
SellVolExp2	 = TimeFrameExpand( SellVolComp2, TF2,  expandFirst );
StaticVarSet( "~SellVolume2" + Ticker, SellVolexp2, False );

Then in each chart set of 20 tics or 260 tics i have the following
20 tic

BuyVolume =  StaticVarGet( "~BuyVolume"+ Ticker + -20  );
SellVolume =  StaticVarGet( "~SellVolume" + Ticker + -20 );

260 tic

BuyVolume =  StaticVarGet( "~BuyVolume"+ Ticker + -260  );
SellVolume =  StaticVarGet( "~SellVolume" + Ticker + -260 );

I would just like to remove excess code or duplication where i can.

Any help would be appreciated.

Cheers
Jon

Can you clear some doubts?

Are these Tick charts or Time Charts like 1-minute or 5-minute etc ?
In both cases when setting variable and while calling it back.

static array variables do not work well for non-time based intervals (tick/n-volume/n-tick) because timestamps in those intervals may not be unique (i.e. several bars may have same time stamp), so time synchronization is not reliable.

Also from from the Manual:

static array variables work best if you read them using the same interval as they were written to. I.e when you create static array variables using 5-minute chart, for best results read them in some other 5-minute chart. Reading in different intervals is possible, but subject to limitations of timestamping (see below)

when you read static variable in a different interval that it was originally stored, static variables perform padding/synchronization and time compression/decompression automatically in a similar way as foreign, however Foreign compresses data always from base-time interval, while static variables operate on previously stored interval, hence result may differ. For example, if previously stored data was in daily interval, and you read such static variable in intraday chart, you will see essentially flat lines for each day, representing static data from daily interval.

TimeFrameCompress expand should be used in conjunction with TimeFrameSet() so is there some part of code you have not posted ?

My version:
Lets say, you have 1-minute exploration running for example, you will call StaticVarSet() and store variable called SV1min
Now, in 5min chart, if you call same variable, then you can do
myvar1 = TimeFrameExpand( getSV1m, in5Minute); // After using StaticVarGet() of course
so here, it will expand correctly without problems. or you can use 15m or hourly as needed.

From section I quoted, you can see TJ mentions going from Higher TF to lower will create holes etc.

2 Likes

Sorry, Yes it's Tic charts, the first bit of code i posted is running in a chart interval of 1 tic, and i just use that to collect and set all the static Vars.

the timeframes that i draw charts in are 20tic and 260 tic, i use a few of the same static Vars on both charts, but at the moment i can only get it to work buy doubling up on each var and compressing/expanding it to each timeframe in the tic window.

what i would like to do is be able to display the 20tic variable on the 260 tic and therefor remove the duplicated staticvar and associated 260 tic compression/expand from the 1 tic window.

Are you suggesting scrap the collection from 1 tic and set all the Vars in the 20tic window?

Probably those having expert experience with Tick charts can give better answer, but using will different N-Tick chart will obviously mess up the array because time synchronization is not available.

Use the same Tick chart to store and retrieve the variables is my best guess, but i'm still not aware of full drawbacks.

1 Like

Clear example, nice :+1:

I tried, using -ve value in TimeFrameSet() and TFExpand and it seemed ok as in my 2nd post.

Could you cite any scenarios where the arrays can mess up because time Sync is not available?

I tried tick charts but with volatile scrips it doesnt suit the trading strategy like time based charts because events tend to occur in daily sync at the same time.

Edit: where did the post go ?

I removed because he wants to look inside bar. My code just plotted longer interval (e.g 20-tick on 1-tick).

AFAIU his post now, all @kastin is after is avoiding code duplication.

So,

// Applied on 1-tick
Ticker = Name();

TF = -20;// or -260 etc

BuyVolume = IIf( Close >= Aux2 AND (Aux2 != Aux1), Volume, 0 );
SellVolume = IIf( Close <= Aux1 AND (Aux2 != Aux1), Volume, 0 );

BuyVolComp = TimeFrameCompress( BuyVolume, TF, compressVolume );
BuyVolExp = TimeFrameExpand( BuyVolComp, TF, expandFirst );

SellVolComp = TimeFrameCompress( SellVolume, TF, compressVolume );
SellVolExp = TimeFrameExpand( SellVolComp, TF, expandFirst );

TFstr = StrFormat( "%g-tick", abs(TF) );

StaticVarSet( "~BuyVolume" + Ticker + TFstr, BuyVolexp, False );
StaticVarSet( "~SellVolume" + Ticker + TFstr, SellVolexp, False );

Then calling StaticVar in either 20 tick or 260-tick

// applied on n-tick e.g. 20T or 260T
TFstr = Interval(2);
BuyVolume = StaticVarGet( "~BuyVolume"+ Ticker + TFstr );
SellVolume = StaticVarGet( "~SellVolume" + Ticker + TFstr );

Or create all longer n-tick intervals at once in 1-tick like so

/// applied on 1-tick interval
/// discussed here
/// @link https://forum.amibroker.com/t/staticvar-set-get-confusion/9547/7
Ticker = Name();

TFmat = MxFromString( "[-20;-260]" );

BuyVolume = IIf( Close >= Aux2 AND (Aux2 != Aux1), Volume, 0 );
SellVolume = IIf( Close <= Aux1 AND (Aux2 != Aux1), Volume, 0 );

for ( i = 0; i < MxGetSize(TFmat, 0); i++ ) {
	TF = TFmat[i][0];

	BuyVolComp = TimeFrameCompress( BuyVolume, TF, compressVolume );
	BuyVolExp = TimeFrameExpand( BuyVolComp, TF,  expandFirst );

	SellVolComp = TimeFrameCompress( SellVolume, TF, compressVolume );
	SellVolExp = TimeFrameExpand( SellVolComp, TF, expandFirst );

	TFstr = StrFormat( "%g-tick", abs(TF) );

	StaticVarSet( "~BuyVolume" + Ticker + TFstr, BuyVolexp, False );
	StaticVarSet( "~SellVolume" + Ticker + TFstr, SellVolexp, False );
	
	//Plot( BuyVolExp, "BuyVolume" + TFstr, colorGreen, styleHistogram );
	//Plot( -SellVolExp, "SellVolume" + TFstr, colorRed, styleHistogram );
}

Then again as in upper first example... calling StaticVar in either 20 tick or 260-tick

// applied on n-tick e.g. 20T or 260T
TFstr = Interval(2);
BuyVolume = StaticVarGet( "~BuyVolume"+ Ticker + TFstr );
SellVolume = StaticVarGet( "~SellVolume" + Ticker + TFstr );
8 Likes
// Applied on 1-tick
Ticker = Name();

TF = -20;// or -260 etc

BuyVolume = IIf( Close >= Aux2 AND (Aux2 != Aux1), Volume, 0 );
SellVolume = IIf( Close <= Aux1 AND (Aux2 != Aux1), Volume, 0 );

BuyVolComp = TimeFrameCompress( BuyVolume, TF, compressVolume );
BuyVolExp = TimeFrameExpand( BuyVolComp, TF, expandFirst );

SellVolComp = TimeFrameCompress( SellVolume, TF, compressVolume );
SellVolExp = TimeFrameExpand( SellVolComp, TF, expandFirst );

TFstr = StrFormat( "%g-tick", abs(TF) );

StaticVarSet( "~BuyVolume" + Ticker + TFstr, BuyVolexp, False );
StaticVarSet( "~SellVolume" + Ticker + TFstr, SellVolexp, False );

Thanks, ill try putting something like this to work and see how it goes!

Cheers

1 Like