How to bootstrap Amibroker array variable

mastermind007

Well-Known Member
#1
Recently came across a document that describes NSE's formula to compute volatility which is a kind of weighted average of two values.

The most accurate AFL (that I can come up with :)) for the same is

Code:
absoluteReturn = log(C/Ref(C,-1));
nseDailyVolatility= sqrt(0.06 * absoluteReturn * absoluteReturn + 0.94 * Ref(nseDailyVolatility,-1) * Ref(nseDailyVolatility,-1));
Just as I had expected, Ami does not like this and whines about having to use variable without initializing it first . Fair Enough !!!

One minor correction transforms Ami into being happy,

Code:
absoluteReturn   = log(C/Ref(C,-1));
[B]nseDailyVolatility= absoluteReturn;[/B]
nseDailyVolatility= sqrt(0.06 * absoluteReturn * absoluteReturn + 0.94 * Ref(nseDailyVolatility,-1) * Ref(nseDailyVolatility,-1));
Nevertheless, this correction leaves me wondering on how I could actually bootstrap the nseDailyVolatility array.

I want to initialize the array correctly without having to use the loop. How do I do it?

BTW, without the correction, formula is slightly inaccurate.
 

KelvinHand

Well-Known Member
#2
Recently came across a document that describes NSE's formula to compute volatility which is a kind of weighted average of two values.

The most accurate AFL (that I can come up with :)) for the same is

Code:
absoluteReturn = log(C/Ref(C,-1));
nseDailyVolatility= sqrt(0.06 * absoluteReturn * absoluteReturn + 0.94 * Ref(nseDailyVolatility,-1) * Ref(nseDailyVolatility,-1));
Just as I had expected, Ami does not like this and whines about having to use variable without initializing it first . Fair Enough !!!

One minor correction transforms Ami into being happy,

Code:
absoluteReturn   = log(C/Ref(C,-1));
[B]nseDailyVolatility= absoluteReturn;[/B]
nseDailyVolatility= sqrt(0.06 * absoluteReturn * absoluteReturn + 0.94 * Ref(nseDailyVolatility,-1) * Ref(nseDailyVolatility,-1));
Nevertheless, this correction leaves me wondering on how I could actually bootstrap the nseDailyVolatility array.

I want to initialize the array correctly without having to use the loop. How do I do it?

BTW, without the correction, formula is slightly inaccurate.
Why nseDailyVolatility is not initialize to 0 ?
 

mastermind007

Well-Known Member
#3
Why nseDailyVolatility is not initialize to 0 ?
Logically, that should work too. However dear old Ami seems to assign entire array variable in one pass, so if I set it to 0, all past values would get ignored. I would be left with sqrt(0.06 * absoluteReturn * absoluteReturn). In other words, I'd be ignore 0.94th fraction. Very Very Wrong!!!

Oddly enough, metastock may not have this problem because it can use Prev.
 

KelvinHand

Well-Known Member
#4
Logically, that should work too. However dear old Ami seems to assign entire array variable in one pass, so if I set it to 0, all past values would get ignored. I would be left with sqrt(0.06 * absoluteReturn * absoluteReturn). In other words, I'd be ignore 0.94th fraction. Very Very Wrong!!!

Oddly enough, metastock may not have this problem because it can use Prev.

If the formula come from metastock using PREV or it work using PREV.
No need to think too much, the looping is needed.
 
#8
Mr. Nut

what about this one

Code:
absoluteReturn = log( C / Ref( C, -1 ) );

xx = sqrt( AMA2( absoluteReturn ^ 2, 0.06, 0.94 ) );

Plot( xx, "volatility", colorRed, styleLine);
 
#9
And this is the loop version

Code:
absoluteReturn = log( C / Ref( C, -1 ) );

xx = 0; 
for ( i = 1; i < BarCount; i++ )
{
    prev = xx[ i - 1 ]; 
    xx[ i ] = sqrt( absoluteReturn[ i ] ^ 2 * 0.06 + 0.94 * prev ^ 2  );
}

Plot( xx, "", colorRed, styleLine);
So who is the expert here?


Just as I had expected, Ami does not like this and whines about having to use variable without initializing it first .
However dear old Ami seems to assign entire array variable in one pass, so if I set it to 0, all past values would get ignored. I would be left with sqrt(0.06 * absoluteReturn * absoluteReturn). In other words, I'd be ignore 0.94th fraction. Very Very Wrong!!!

Oddly enough, metastock may not have this problem because it can use Prev.
blah, blah, blah.
 
Last edited:

mastermind007

Well-Known Member
#10
//============ Metastock Indicator NSE Annual Volatility as %
absoluteReturn := Log(C/Ref(C,-1));
dailyVolatility := Sqrt(0.06 * absoluteReturn * absoluteReturn + 0.94 * PREV * PREV);
dailyVolatility * Sqrt(365) * 100;

MS formula seems to work just as I had expected it to and values match with those downloaded from NSE site.

Amibroker's AMA2 approach works However, for ACC on 7th May 13 it was off by about 1% differential. NSE sheet quotes it to be 27.97%. AB computes it at 28.25

Please do realize that I have only checked data of one day and for few scrips in NIFTY at this point in time.
 
Last edited:

Similar threads