Standardization - ZScore Calculation

Dear Seniors,

I would like to fetch ZScore of OpenInt in order to standardize the OI. ZScore Calculation requires Average of OI and StDev of OI of total bars till the current time.

Since AVG is not a function in amibroker, I am confused on its usage to the above stated purpose. Also the case with StDev of OI.

Kindly suggest on how to fetch zscore value of OpenInt.
Thanks in advance.

You can have a go at this.

AB has almost everything, sometimes we need to change how we look at it.

With respect to Time series, you are having variable periods and that's maybe why you are looking for a static formula.

But with hint from here How to make any function accepting variable period

we can have variable periods for all the data with something like BarIndex()

bi = BarIndex();
ZScore = ( OI - MA( OI, bi ) ) / StDev( OI, bi );

This way you get a moving everything so for each new bar you get adjusted calculations.

1 Like

Hi Thanks for the suggestion.

I have run the analysis part only to see wrong data. I have used excel to generate formula based Zscore for OI and compared to AB generated OI Zscore both are very much different.

Attached is a reference image to showcase Zscore generated using Excel ( on the left side ) ad Ami..

Screenshot 2021-02-20 200955

What could have gone wrong considering input data of OI is same for both but different output.. Any Idea?

If you notice the below image -

image_2021-02-20_203108

  1. How is AVG OI calculated for the first bar as highlighted in Red? Instead perhaps it should be as is cause of the fresh day start.

  2. When you add up 2 candle period's Oi and average it , it is not actually tallying to the correct math as highlighted on right side..

Perhaps, we should hv avoided the very first candle of each fresh day to start with ..

I am sorry but I cannot notice anything if you just share screenshots.
It is up to you to share all the data, settings and formula. My formula code was to give you an idea of how one can move from some static code to a Time Series based dynamic one and AB is very capable of doing such stuff.
Also in your first post you do not say if it is daily aggregate or intraday etc and whether one should reset everyday or not.

Obviously you have set some from to range other than All Quotes! So there are bars of before 19/02/21 9:15 taken into account.

x = Close;
period = Cum(Status("barinrange"))-1;

mean = MA(x,period);
stdv = StDev(x,period);// is STDEV.P in EXCEL!!!!!
ZScore = ( x - mean ) / (stdv+1e-9);

// exploration
format = 1.2;
Filter = NOT IsNull(Zscore);// skip first bar of array

AddColumn( x, "X", format );
AddColumn( mean, "Mean", format );
AddColumn( stdv, "StDev", format );
AddColumn( zscore, "Z-Score", 1.3 );
AddColumn( period, "Count", 1);

Additonal note: since AB 6.19 array period has to be <= current barindex for functions such as Sum, MA, StDev.

  1. AFL: variable period Sum() now returns NULL if range is greater than current bar index (previously returned partial sum)

http://www.amibroker.com/devlog/2016/12/16/amibroker-6-19-0-beta-released/

Besides AB default StDev works like Excel's StDev.P function. So you should compare apples to apples.
If you want to compare to Excel default StDev then you have to set AB Stdev's third argument to FALSE.

So if you follow these then results between AB and Excel are same ones:

23


BTW you were not careful in your last picture. Instead of 61475 you have used 64475 in your calculation on the right.

4 Likes

Change to

cs = Cum(Status("barinrange"));
period = Min(cs, BarIndex());

If counting per day then change to

period = BarsSince(DateNum()!=Ref(DateNum(),-1))+1;
4 Likes

Dear @fxshrat,

Apologies for the type error. Thanks a lot for the explanation. I will look into it.

As always, you are spot with the best solution !! Thanks a ton.

This went into a loop of results while performing analysis. After the last candle of the day it started again frim the day start. It went on displaying continuity of results...

No, it doesn't !!!

If counter restarts at new day then you used this one

period = BarsSince(DateNum()!=Ref(DateNum(),-1))+1;

I even wrote what that line does:

In my previous post I meant to use either one or the other depending on how you want to count. I clearly wrote "if ... then...". So I don't think it is too hard to understand.
And quite frankly you should learn to tell what you actually want, because no one know what you actually want to count from where to where! So far you just posted useless pictures.

You should post entire reproducible code plus analysis settings plus results!

3 Likes

Dear @fxshrat ,

I was just curious to know the z-score values of Volume, open Interest as they usually have higher scales compared to any other indicators. As a result I sought some help from the forum to plot the z-score values and in the process, I tried to compare it with Excel generated Z-scores just to ensure correct data being populated.

I really had no intention to to use it for any exploration but it was only to ascertain the accuracy of data points for plotting Zscore. If I may caused any sort of confusion or inconvenience while communicating clearly , I really apologize.

Your suggestions always helped me to achieve what I have intended to. Thanks a lot for being very helpful and most importantly, you never backed away from taking time to give some explanation on the mistakes while making suggestions. Very thankful for all your help.

1 Like

dear @nsm51.

I am extremely sorry for my poor communication in my earlier mentions. You have been kind and helpful on my request. Thanks a lot for all your efforts. Will surely append my way of communication way forward.

Thanks again for your time and valuable suggestions.

2 Likes

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