Volatility zoning

Here is the code to get HHV and LLV of last 100 days of VIX

Split into 3 zones and allocate into separate zones
Can someone review this code, i am not getting correct output in explorer all the records for VIN_Bin is set to 2 (screenshot attached)

VIX_HH = HHV(Foreign("$VIX", "H"), 100); // Higher high of last 100 days 
VIX_LL = LLV(Foreign("$VIX", "L"), 100); // Lower low of last 100 days 


// Split volatility into 3 zones
value1 = (VIX_HH - VIX_LL)/4;

VIX_Level1 =  VIX_LL + value1;
VIX_Level2 =  VIX_LL + (2 * value1);
VIX_Level3 =  VIX_HH + value1;

// Assess current volatility zone
VIX_Bin = 0;

for(i = 0; i < BarCount; i++)
{
	if(VIX[i] <= VIX_Level1[i]){
	VIX_Bin = 1;
	}

	if(VIX[i] > VIX_Level1[i] AND VIX[i] <= VIX_Level2[i]){
	VIX_Bin = 2;
	}

	if(VIX[i] > VIX_Level2[i] AND VIX[i] <= VIX_Level3[i]){
	VIX_Bin = 3;
	}

	if(VIX[i] > VIX_Level3[i]){
	VIX_Bin = 4;
	}
}

AF_2019-06-20_0-43-25

I haven’t had time to really look into your code but I would suggest you consider using PercentRank and create your zones depending upon that. And there is no need for loops to create your zones.

Quick response: I think that you've missed the array indexes in the for loop. Non tested possible correction:

VIX_HH = HHV(Foreign("$VIX", "H"), 100); // Higher high of last 100 days 
VIX_LL = LLV(Foreign("$VIX", "L"), 100); // Lower low of last 100 days 


// Split volatility into 3 zones
value1 = (VIX_HH - VIX_LL)/4;

VIX_Level1 =  VIX_LL + value1;
VIX_Level2 =  VIX_LL + (2 * value1);
VIX_Level3 =  VIX_HH + value1;

// Assess current volatility zone
VIX_Bin = 0;  // OK: You're assigning value 0 to all elements in the array VIX_Bin

for(i = 0; i < BarCount; i++)
{
	if(VIX[i] <= VIX_Level1[i]){
	VIX_Bin[i] = 1;    // Missing [i] !!! You are asiggning an individual value
	}

	if(VIX[i] > VIX_Level1[i] AND VIX[i] <= VIX_Level2[i]){
	VIX_Bin[i] = 2;    // Missing [i] !!!
	}

	if(VIX[i] > VIX_Level2[i] AND VIX[i] <= VIX_Level3[i]){
	VIX_Bin[i] = 3;    // Missing [i] !!!
	}

	if(VIX[i] > VIX_Level3[i]){
	VIX_Bin[i] = 4;    // Missing [i] !!!
	}
}

By the way: You also missed the definition of the VIX variable...

Hint: Using IIf function (not tested):

VIX_HH = HHV(Foreign("$VIX", "H"), 100); // Higher high of last 100 days 
VIX_LL = LLV(Foreign("$VIX", "L"), 100); // Lower low of last 100 days 


// Split volatility into 3 zones
value1 = (VIX_HH - VIX_LL)/4;

VIX_Level1 =  VIX_LL + value1;
VIX_Level2 =  VIX_LL + (2 * value1);
VIX_Level3 =  VIX_HH + value1;

// Assess current volatility zone
VIX_Bin = VIX_Bin = IIf(VIX<=VIX_Level1, 1, IIf(VIX<=VIX_Level2, 2, IIf(VIX<=VIX_Level3, 3, 4)));

Another hint: I suppose that you have other bug:

VIX_Level3 =  VIX_HH + value1;

should be:

VIX_Level3 =  VIX_HH - value1;

or

VIX_Level3 =  VIX_LL + 3*value1;
6 Likes

@LeoCV brilliant thanks for your help :+1:
Without changing anything just by using IIf option worked, at least I can see correct VIX_Bin values.

BTW, VIX variable was set but forgot in include it in code snippet above

VIX = Foreign("$VIX", "C");

Love this forum

Just continuation to above logic, when I try to optimize for Bin/zones type to test longentry i get same net profit result for all bin type, not sure if i am missing anything, here is the code sample:

Bin_Value = Optimize("Bin_Value",1,1,4,1);
Current_BIN = Bin_Value;

LongEntry =  entryRule AND Current_BIN;
LongExit = exitRule;

Buy= LongEntry;
Sell= LongExit;

Buy= ExRem( Buy, Sell);
Sell= ExRem( Sell, Buy);

SetPositionSize(10, spsPercentOfEquity);

I suppose that you want to enter long when your entryRule's value is true AND where the current VIX zone is the one assigned to the variable Bin_Value. So, I think that the lines:

Bin_Value = Optimize("Bin_Value",1,1,4,1);
Current_BIN = Bin_Value;

LongEntry =  entryRule AND Current_BIN;
LongExit = exitRule;

should read like:

Bin_Value = Optimize("Bin_Value",1,1,4,1);

LongEntry =  entryRule AND VIX_Bin == Bin_Value;
LongExit = exitRule;

But due to the lack of information you provides, I'm only guessing...

Moderator comment: typo corrected

2 Likes

@LeoCV,
There is a typo there.
It actually should be equality check operator (==) but not assignment operator (=) there on right side.
So,

LongEntry = entryRule AND VIX_Bin == Bin_Value;

Link to manual about operator types:
https://www.amibroker.com/guide/a_language.html

3 Likes

Oh perfect!.. thank you @fxshrat

@subsoft,

don't forget thanking @LeoCV as he just had a typo and rest of it being in line.

3 Likes