# Copyright Stephen Harlin, 2011
# Created Oct 2009
# Information Signal Translation
# Study based upon the work of Jeff Augen (Price Volatility), Ryan Harlin (.TS translation) and myself
# 86% Positive Predictive Value when buying volatility troughs and selling volatility peaks
# Calculate price volatility (Jeff Augen) and display as both a histogram and a running cumulative
# Back-testing found rare instances of spikes greater than 3/-3 and had no advantage over limit of 3 Std Devs
#Converted to a Binary dot system, Added paintbars by changing the APC input to whichever line you designate by Henry Z Kaczmarczyk
declare lower;
input length1 = 5;
input length2 = 8;
input length3 = 13;
input length4 = 21;
input length5 = 34;
input length6 = 55;
input length7 = 89;
input length8 = 144;
input length9 = 233;
Input cumlength1 = 3;
Input cumlength2 = 5;
Input cumlength3 = 8;
Input cumlength4 = 13;
Input cumlength5 = 21;
Input cumlength6 = 34;
Input cumlength7 = 55;
Input cumlength8 = 89;
Input cumlength9 = 144;
Input APC = 0;
Input dotsize = 3;
def closeLog = log(close[1] / close[2]);
def SDev1 = stdev(closeLog, length1) * Sqrt(length1 / (length1 - 1));
def SDev2 = stdev(closeLog, length2) * Sqrt(length2 / (length2 - 1));
def SDev3 = stdev(closeLog, length3) * Sqrt(length3 / (length3 - 1));
def SDev4 = stdev(closeLog, length4) * Sqrt(length4 / (length4 - 1));
def SDev5 = stdev(closeLog, length5) * Sqrt(length5 / (length5 - 1));
def SDev6 = stdev(closeLog, length6) * Sqrt(length6 / (length6 - 1));
def SDev7 = stdev(closeLog, length7) * Sqrt(length7 / (length7 - 1));
def SDev8 = stdev(closeLog, length8) * Sqrt(length8 / (length8 - 1));
def SDev9 = stdev(closeLog, length9) * Sqrt(length9 / (length9 - 1));
def x1 = SDev1 * close[1];
def x2 = SDev2 * close[1];
def x3 = SDev3 * close[1];
def x4 = SDev4 * close[1];
def x5 = SDev5 * close[1];
def x6 = SDev6 * close[1];
def x7 = SDev7 * close[1];
def x8 = SDev8 * close[1];
def x9 = SDev9 * close[1];
def spike1 = (close[0] - close[1]) / x1;
def spike2 = (close[0] - close[1]) / x2;
def spike3 = (close[0] - close[1]) / x3;
def spike4 = (close[0] - close[1]) / x4;
def spike5 = (close[0] - close[1]) / x5;
def spike6 = (close[0] - close[1]) / x6;
def spike7 = (close[0] - close[1]) / x7;
def spike8 = (close[0] - close[1]) / x8;
def spike9 = (close[0] - close[1]) / x9;
def y1 = if spike1 > 0 then spike1 else 0;
def y2 = if spike2 > 0 then spike2 else 0;
def y3 = if spike3 > 0 then spike3 else 0;
def y4 = if spike4 > 0 then spike4 else 0;
def y5 = if spike5 > 0 then spike5 else 0;
def y6 = if spike6 > 0 then spike6 else 0;
def y7 = if spike7 > 0 then spike7 else 0;
def y8 = if spike8 > 0 then spike8 else 0;
def y9 = if spike9 > 0 then spike9 else 0;
def up1 = sum(y1, cumlength1);
def up2 = sum(y2, cumlength2);
def up3 = sum(y3, cumlength3);
def up4 = sum(y4, cumlength4);
def up5 = sum(y5, cumlength5);
def up6 = sum(y6, cumlength6);
def up7 = sum(y7, cumlength7);
def up8 = sum(y8, cumlength8);
def up9 = sum(y9, cumlength9);
def z1 = if spike1 <= 0 then spike1 else 0;
def z2 = if spike2 <= 0 then spike2 else 0;
def z3 = if spike3 <= 0 then spike3 else 0;
def z4 = if spike4 <= 0 then spike4 else 0;
def z5 = if spike5 <= 0 then spike5 else 0;
def z6 = if spike6 <= 0 then spike6 else 0;
def z7 = if spike7 <= 0 then spike7 else 0;
def z8 = if spike8 <= 0 then spike8 else 0;
def z9 = if spike9 <= 0 then spike9 else 0;
def dn1 = sum(AbsValue(z1), cumlength1);
def dn2 = sum(AbsValue(z2), cumlength2);
def dn3 = sum(AbsValue(z3), cumlength3);
def dn4 = sum(AbsValue(z4), cumlength4);
def dn5 = sum(AbsValue(z5), cumlength5);
def dn6 = sum(AbsValue(z6), cumlength6);
def dn7 = sum(AbsValue(z7), cumlength7);
def dn8 = sum(AbsValue(z8), cumlength8);
def dn9 = sum(AbsValue(z9), cumlength9);
def spikeLimit1 = if spike1 > 3 then 3 else if spike1 < -3 then -3 else spike1;
def spikeLimit2 = if spike2 > 3 then 3 else if spike2 < -3 then -3 else spike2;
def spikeLimit3 = if spike3 > 3 then 3 else if spike3 < -3 then -3 else spike3;
def spikeLimit4 = if spike4 > 3 then 3 else if spike4 < -3 then -3 else spike4;
def spikeLimit5 = if spike5 > 3 then 3 else if spike5 < -3 then -3 else spike5;
def spikeLimit6 = if spike6 > 3 then 3 else if spike6 < -3 then -3 else spike6;
def spikeLimit7 = if spike7 > 3 then 3 else if spike7 < -3 then -3 else spike7;
def spikeLimit8 = if spike8 > 3 then 3 else if spike8 < -3 then -3 else spike8;
def spikeLimit9 = if spike9 > 3 then 3 else if spike9 < -3 then -3 else spike9;
def spikeOsc1 = (up1 – dn1)/3;
def spikeOsc2 = (up2 – dn2)/3;
def spikeOsc3 = (up3 – dn3)/3;
def spikeOsc4 = (up4 – dn4)/3;
def spikeOsc5 = (up5 – dn5)/3;
def spikeOsc6 = (up6 – dn6)/3;
def spikeOsc7 = (up7 – dn7)/3;
def spikeOsc8 = (up8 – dn8)/3;
def spikeOsc9 = (up9 – dn9)/3;
plot A1_Dot = if IsNaN(close) then Double.NaN else 1;
A1_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A1_Dot.SetLineWeight(DotSize);
A1_Dot.AssignValueColor(if spike1 < -3 then color.magenta else if spike1 < 0 then Color.Plum else Color.Black);
plot A2_Dot = if IsNaN(close) then Double.NaN else 2;
A2_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A2_Dot.SetLineWeight(DotSize);
A2_Dot.AssignValueColor(if spikeOsc1 > 0 then color.green else if spikeOsc1 < 0 then color.red else Color.DARK_GRAY);
plot A3_Dot = if IsNaN(close) then Double.NaN else 3;
A3_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A3_Dot.SetLineWeight(DotSize);
A3_Dot.AssignValueColor(if spike1 > 3.0 then color.cyan else if spike1 > 0 then Color.Blue else Color.Black);
plot A4_Dot = if IsNaN(close) then Double.NaN else 4;
A4_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A4_Dot.SetLineWeight(DotSize);
A4_Dot.AssignValueColor(if spike2 < -3 then color.magenta else if spike2 < 0 then Color.Plum else Color.Black);
plot A5_Dot = if IsNaN(close) then Double.NaN else 5;
A5_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A5_Dot.SetLineWeight(DotSize);
A5_Dot.AssignValueColor(if spikeOsc2 > 0 then color.green else if spikeOsc2 < 0 then color.red else Color.DARK_GRAY);
plot A6_Dot = if IsNaN(close) then Double.NaN else 6;
A6_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A6_Dot.SetLineWeight(DotSize);
A6_Dot.AssignValueColor(if spike2 > 3.0 then color.cyan else if spike2 > 0 then Color.Blue else Color.Black);
plot A7_Dot = if IsNaN(close) then Double.NaN else 7;
A7_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A7_Dot.SetLineWeight(DotSize);
A7_Dot.AssignValueColor(if spike3 < -3 then color.magenta else if spike3 < 0 then Color.Plum else Color.Black);
plot A8_Dot = if IsNaN(close) then Double.NaN else 8;
A8_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A8_Dot.SetLineWeight(DotSize);
A8_Dot.AssignValueColor(if spikeOsc3 > 0 then color.green else if spikeOsc3 < 0 then color.red else Color.DARK_GRAY);
plot A9_Dot = if IsNaN(close) then Double.NaN else 9;
A9_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A9_Dot.SetLineWeight(DotSize);
A9_Dot.AssignValueColor(if spike3 > 3.0 then color.cyan else if spike3 > 0 then Color.Blue else Color.Black);
plot A10_Dot = if IsNaN(close) then Double.NaN else 10;
A10_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A10_Dot.SetLineWeight(DotSize);
A10_Dot.AssignValueColor(if spike4 < -3 then color.magenta else if spike4 < 0 then Color.Plum else Color.Black);
plot A11_Dot = if IsNaN(close) then Double.NaN else 11;
A11_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A11_Dot.SetLineWeight(DotSize);
A11_Dot.AssignValueColor(if spikeOsc4 > 0 then color.green else if spikeOsc4 < 0 then color.red else Color.DARK_GRAY);
plot A12_Dot = if IsNaN(close) then Double.NaN else 12;
A12_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A12_Dot.SetLineWeight(DotSize);
A12_Dot.AssignValueColor(if spike4 > 3.0 then color.cyan else if spike4 > 0 then Color.Blue else Color.Black);
plot A13_Dot = if IsNaN(close) then Double.NaN else 13;
A13_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A13_Dot.SetLineWeight(DotSize);
A13_Dot.AssignValueColor(if spike5 < -3 then color.magenta else if spike5 < 0 then Color.Plum else Color.Black);
plot A14_Dot = if IsNaN(close) then Double.NaN else 14;
A14_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A14_Dot.SetLineWeight(DotSize);
A14_Dot.AssignValueColor(if spikeOsc5 > 0 then color.green else if spikeOsc5 < 0 then color.red else Color.DARK_GRAY);
plot A15_Dot = if IsNaN(close) then Double.NaN else 15;
A15_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A15_Dot.SetLineWeight(DotSize);
A15_Dot.AssignValueColor(if spike5 > 3.0 then color.cyan else if spike5 > 0 then Color.Blue else Color.Black);
plot A16_Dot = if IsNaN(close) then Double.NaN else 16;
A16_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A16_Dot.SetLineWeight(DotSize);
A16_Dot.AssignValueColor(if spike6 < -3 then color.magenta else if spike6 < 0 then Color.Plum else Color.Black);
plot A17_Dot = if IsNaN(close) then Double.NaN else 17;
A17_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A17_Dot.SetLineWeight(DotSize);
A17_Dot.AssignValueColor(if spikeOsc6 > 0 then color.green else if spikeOsc6 < 0 then color.red else Color.DARK_GRAY);
plot A18_Dot = if IsNaN(close) then Double.NaN else 18;
A18_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A18_Dot.SetLineWeight(DotSize);
A18_Dot.AssignValueColor(if spike6 > 3.0 then color.cyan else if spike6 > 0 then Color.Blue else Color.Black);
plot A19_Dot = if IsNaN(close) then Double.NaN else 19;
A19_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A19_Dot.SetLineWeight(DotSize);
A19_Dot.AssignValueColor(if spike7 < -3 then color.magenta else if spike7 < 0 then Color.Plum else Color.Black);
plot A20_Dot = if IsNaN(close) then Double.NaN else 20;
A20_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A20_Dot.SetLineWeight(DotSize);
A20_Dot.AssignValueColor(if spikeOsc7 > 0 then color.green else if spikeOsc7 < 0 then color.red else Color.DARK_GRAY);
plot A21_Dot = if IsNaN(close) then Double.NaN else 21;
A21_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A21_Dot.SetLineWeight(DotSize);
A21_Dot.AssignValueColor(if spike7 > 3.0 then color.cyan else if spike7 > 0 then Color.Blue else Color.Black);
plot A22_Dot = if IsNaN(close) then Double.NaN else 22;
A22_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A22_Dot.SetLineWeight(DotSize);
A22_Dot.AssignValueColor(if spike8 < -3 then color.magenta else if spike8 < 0 then Color.Plum else Color.Black);
plot A23_Dot = if IsNaN(close) then Double.NaN else 23;
A23_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A23_Dot.SetLineWeight(DotSize);
A23_Dot.AssignValueColor(if spikeOsc8 > 0 then color.green else if spikeOsc8 < 0 then color.red else Color.DARK_GRAY);
plot A24_Dot = if IsNaN(close) then Double.NaN else 24;
A24_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A24_Dot.SetLineWeight(DotSize);
A24_Dot.AssignValueColor(if spike8 > 3.0 then color.cyan else if spike8 > 0 then Color.Blue else Color.Black);
plot A25_Dot = if IsNaN(close) then Double.NaN else 25;
A25_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A25_Dot.SetLineWeight(DotSize);
A25_Dot.AssignValueColor(if spike9 < -3 then color.magenta else if spike9 < 0 then Color.Plum else Color.Black);
plot A26_Dot = if IsNaN(close) then Double.NaN else 26;
A26_Dot.SetPaintingStrategy(PaintingStrategy.Squares);
A26_Dot.SetLineWeight(DotSize);
A26_Dot.AssignValueColor(if spikeOsc9 > 0 then color.green else if spikeOsc9 < 0 then color.red else Color.DARK_GRAY);
plot A27_Dot = if IsNaN(close) then Double.NaN else 27;
A27_Dot.SetPaintingStrategy(PaintingStrategy.Points);
A27_Dot.SetLineWeight(DotSize);
A27_Dot.AssignValueColor(if spike9 > 3.0 then color.cyan else if spike9 > 0 then Color.Blue else Color.Black);
AssignPriceColor( if APC ==1 and spike1 < -3 then color.magenta else if APC ==1 and spike1 < 0 then Color.Plum else If APC ==2 and spikeOsc1 > 0 then color.green else if APC ==2 and spikeOsc1 < 0 then color.red else if APC ==3 and spike1 > 3.0 then color.cyan else if APC ==3 and spike1 > 0 then Color.Blue else
if APC ==4 and spike2 < -3 then color.magenta else if APC ==4 and spike2 < 0 then Color.Plum else If APC ==5 and spikeOsc2 > 0 then color.green else if APC ==5 and spikeOsc2 < 0 then color.red else if APC ==6 and spike2 > 3.0 then color.cyan else if APC ==6 and spike2 > 0 then Color.Blue else
if APC ==7 and spike3 < -3 then color.magenta else if APC ==7 and spike3 < 0 then Color.Plum else If APC ==8 and spikeOsc3 > 0 then color.green else if APC ==8 and spikeOsc3 < 0 then color.red else if APC ==9 and spike3 > 3.0 then color.cyan else if APC ==9 and spike3 > 0 then Color.Blue else
if APC ==10 and spike4 < -3 then color.magenta else if APC ==10 and spike4 < 0 then Color.Plum else If APC ==11 and spikeOsc4 > 0 then color.green else if APC ==11 and spikeOsc4 < 0 then color.red else if APC ==12 and spike4 > 3.0 then color.cyan else if APC ==12 and spike4 > 0 then Color.Blue else
if APC ==13 and spike5 < -3 then color.magenta else if APC ==13 and spike5 < 0 then Color.Plum else If APC ==14 and spikeOsc5 > 0 then color.green else if APC ==14 and spikeOsc5 < 0 then color.red else if APC ==15 and spike5 > 3.0 then color.cyan else if APC ==15 and spike5 > 0 then Color.Blue else
if APC ==16 and spike6 < -3 then color.magenta else if APC ==16 and spike6 < 0 then Color.Plum else If APC ==17 and spikeOsc6 > 0 then color.green else if APC ==17 and spikeOsc6 < 0 then color.red else if APC ==18 and spike6 > 3.0 then color.cyan else if APC ==18 and spike6 > 0 then Color.Blue else
if APC ==19 and spike7 < -3 then color.magenta else if APC ==19 and spike7 < 0 then Color.Plum else If APC ==20 and spikeOsc7 > 0 then color.green else if APC ==20 and spikeOsc7 < 0 then color.red else if APC ==21 and spike7 > 3.0 then color.cyan else if APC ==21 and spike7 > 0 then Color.Blue else
if APC ==22 and spike8 < -3 then color.magenta else if APC ==22 and spike8 < 0 then Color.Plum else If APC ==23 and spikeOsc8 > 0 then color.green else if APC ==23 and spikeOsc8 < 0 then color.red else if APC ==24 and spike8 > 3.0 then color.cyan else if APC ==24 and spike8 > 0 then Color.Blue else
if APC ==25 and spike9 < -3 then color.magenta else if APC ==25 and spike9 < 0 then Color.Plum else If APC ==26 and spikeOsc9 > 0 then color.green else if APC ==26 and spikeOsc9 < 0 then color.red else if APC ==27 and spike9 > 3.0 then color.cyan else if APC ==27 and spike9 > 0 then Color.Blue else Color.Current);