declare Lower;
input period1 = 3;
input period2 = 5;
input period3 = 8;
input period4 = 10;
input period5 = 12;
input period6 = 15;
input period7 = 30;
input period8 = 35;
input period9 = 40;
input period10 = 45;
input period11 = 50;
input period12 = 60;
input paintbars = 0;
Input dotsize = 3;
Def price = (Open + Close)/2;
def a1A = exp(-1.414 * 3.14159 / Period1);
def b1A = 2 * a1A * Cos(1.414 * 3.14159 / Period1);
def coef2A =b1A;
def coef3A = -a1A * a1A;
def coef1A = (1 – b1A + a1A * a1A) / 4;
rec ButterA = if barNumber() < 3 then Price else coef1A * (Price + 2 * Price[1] + Price[2]) + coef2A * ButterA[1] + coef3A * ButterA[2];
def a1B = exp(-1.414 * 3.14159 / Period2);
def b1B = 2 * a1B * Cos(1.414 * 3.14159 / Period2);
def coef2B =b1B;
def coef3B = -a1B * a1B;
def coef1B = (1 – b1B + a1B * a1B) / 4;
rec ButterB = if barNumber() < 3 then Price else coef1B * (Price + 2 * Price[1] + Price[2]) + coef2B * ButterB[1] + coef3B * ButterB[2];
def a1C = exp(-1.414 * 3.14159 / Period3);
def b1C = 2 * a1C * Cos(1.414 * 3.14159 / Period3);
def coef2C =b1C;
def coef3C = -a1C * a1C;
def coef1C = (1 – b1C + a1C * a1C) / 4;
rec ButterC = if barNumber() < 3 then Price else coef1C * (Price + 2 * Price[1] + Price[2]) + coef2C * ButterC[1] + coef3C * ButterC[2];
def a1D = exp(-1.414 * 3.14159 / Period4);
def b1D = 2 * a1D * Cos(1.414 * 3.14159 / Period4);
def coef2D =b1D;
def coef3D = -a1D * a1D;
def coef1D = (1 – b1D + a1D * a1D) / 4;
rec ButterD= if barNumber() < 3 then Price else coef1D * (Price + 2 * Price[1] + Price[2]) + coef2D * ButterD[1] + coef3D * ButterD[2];
def a1E = exp(-1.414 * 3.14159 / Period5);
def b1E = 2 * a1E * Cos(1.414 * 3.14159 / Period5);
def coef2E =b1E;
def coef3E = -a1E * a1E;
def coef1E = (1 – b1E + a1E * a1E) / 4;
rec ButterE = if barNumber() < 3 then Price else coef1E * (Price + 2 * Price[1] + Price[2]) + coef2E * ButterE[1] + coef3E * ButterE[2];
def a1F = exp(-1.414 * 3.14159 / Period6);
def b1F = 2 * a1F * Cos(1.414 * 3.14159 / Period6);
def coef2F =b1F;
def coef3F = -a1F * a1F;
def coef1F = (1 – b1F + a1F * a1F) / 4;
rec ButterF = if barNumber() < 3 then Price else coef1F * (Price + 2 * Price[1] + Price[2]) + coef2F * ButterF[1] + coef3F * ButterF[2];
def a1G = exp(-1.414 * 3.14159 / Period7);
def b1G = 2 * a1G * Cos(1.414 * 3.14159 / Period7);
def coef2G =b1G;
def coef3G = -a1G * a1G;
def coef1G = (1 – b1G + a1G * a1G) / 4;
rec ButterG = if barNumber() < 3 then Price else coef1G * (Price + 2 * Price[1] + Price[2]) + coef2G * ButterG[1] + coef3G * ButterG[2];
def a1H = exp(-1.414 * 3.14159 / Period8);
def b1H = 2 * a1H * Cos(1.414 * 3.14159 / Period8);
def coef2H =b1H;
def coef3H = -a1H * a1H;
def coef1H = (1 – b1H + a1H * a1H) / 4;
rec ButterH = if barNumber() < 3 then Price else coef1H * (Price + 2 * Price[1] + Price[2]) + coef2H * ButterH[1] + coef3H * ButterH[2];
def a1I = exp(-1.414 * 3.14159 / Period9);
def b1I = 2 * a1I * Cos(1.414 * 3.14159 / Period9);
def coef2I =b1I;
def coef3I = -a1I * a1I;
def coef1I = (1 – b1I + a1I * a1I) / 4;
rec ButterI = if barNumber() < 3 then Price else coef1I * (Price + 2 * Price[1] + Price[2]) + coef2I * ButterI[1] + coef3I * ButterI[2];
def a1J = exp(-1.414 * 3.14159 / Period10);
def b1J = 2 * a1J * Cos(1.414 * 3.14159 / Period10);
def coef2J =b1J;
def coef3J = -a1J * a1J;
def coef1J = (1 – b1J + a1J * a1J) / 4;
rec ButterJ = if barNumber() < 3 then Price else coef1J * (Price + 2 * Price[1] + Price[2]) + coef2J * ButterJ[1] + coef3J * ButterJ[2];
def a1K = exp(-1.414 * 3.14159 / Period11);
def b1K = 2 * a1K * Cos(1.414 * 3.14159 / Period11);
def coef2K =b1K;
def coef3K = -a1K * a1K;
def coef1K = (1 – b1K + a1K * a1K) / 4;
rec ButterK = if barNumber() < 3 then Price else coef1K * (Price + 2 * Price[1] + Price[2]) + coef2K * ButterK[1] + coef3K * ButterK[2];
def a1L = exp(-1.414 * 3.14159 / Period12);
def b1L = 2 * a1L * Cos(1.414 * 3.14159 / Period12);
def coef2L =b1L;
def coef3L = -a1L * a1L;
def coef1L = (1 – b1L + a1L * a1L) / 4;
rec ButterL = if barNumber() < 3 then Price else coef1L * (Price + 2 * Price[1] + Price[2]) + coef2L * ButterL[1] + coef3L * ButterL[2];
def sigmaA = StDev(price, Period1);
def sigmaB = StDev(price, Period2);
def sigmaC = StDev(price, Period3);
def sigmaD = StDev(price, Period4);
def sigmaE = StDev(price, Period5);
def sigmaF = StDev(price, Period6);
def sigmaG = StDev(price, Period7);
def sigmaH = StDev(price, Period8);
def sigmaI = StDev(price, Period9);
def sigmaJ = StDev(price, Period10);
def sigmaK = StDev(price, Period11);
def sigmaL = StDev(price, Period12);
def zA = (price - ButterA) / sigmaA;
def zB = (price - ButterB) / sigmaB;
def zC = (price - ButterC) / sigmaC;
def zD = (price - ButterD) / sigmaD;
def zE = (price - ButterE) / sigmaE;
def zF = (price - ButterF) / sigmaF;
def zG = (price - ButterG) / sigmaG;
def zH = (price - ButterH) / sigmaH;
def zI = (price - ButterI) / sigmaI;
def zJ = (price - ButterJ) / sigmaJ;
def zK = (price - ButterK) / sigmaK;
def Zl = (price - ButterL) / sigmaL;
def p = 0.3275911;
def a1 = 0.254829592;
def a2 = -0.284496736;
def a3 = 1.421413741;
def a4 = -1.453152027;
def a5 = 1.061405429;
def signA = If((zA < 0.0), -1, 1);
def signB = If((zB < 0.0), -1, 1);
def signC = If((zC < 0.0), -1, 1);
def signD = If((zD < 0.0), -1, 1);
def signE = If((zE < 0.0), -1, 1);
def signF = If((zF < 0.0), -1, 1);
def signG = If((zG < 0.0), -1, 1);
def signH = If((zH < 0.0), -1, 1);
def signI = If((zI < 0.0), -1, 1);
def signJ = If((zJ < 0.0), -1, 1);
def signK = If((zK < 0.0), -1, 1);
def signL = If((zL < 0.0), -1, 1);
def xA = AbsValue(zA) / Sqrt(2.0);
def xB = AbsValue(zB) / Sqrt(2.0);
def xC = AbsValue(zC) / Sqrt(2.0);
def xD = AbsValue(zD) / Sqrt(2.0);
def xE = AbsValue(zE) / Sqrt(2.0);
def xF = AbsValue(zF) / Sqrt(2.0);
def xG = AbsValue(zG) / Sqrt(2.0);
def xH = AbsValue(zH) / Sqrt(2.0);
def xI = AbsValue(zI) / Sqrt(2.0);
def xJ = AbsValue(zJ) / Sqrt(2.0);
def xK = AbsValue(zK) / Sqrt(2.0);
def xL = AbsValue(zL) / Sqrt(2.0);
def tA = 1.0 / (1.0 + p * xA);
def tB = 1.0 / (1.0 + p * xB);
def tC = 1.0 / (1.0 + p * xC);
def tD = 1.0 / (1.0 + p * xD);
def tE = 1.0 / (1.0 + p * xE);
def tF = 1.0 / (1.0 + p * xF);
def tG = 1.0 / (1.0 + p * xG);
def tH = 1.0 / (1.0 + p * xH);
def tI = 1.0 / (1.0 + p * xI);
def tJ = 1.0 / (1.0 + p * xJ);
def tK = 1.0 / (1.0 + p * xK);
def tL = 1.0 / (1.0 + p * xL);
def erfA = 1.0 - (((((a5 * tA + a4) * tA) + a3) * tA + a2) * tA + a1) * tA * Exp(-xA * xA);
def erfB = 1.0 - (((((a5 * tB + a4) * tB) + a3) * tB + a2) * tB + a1) * tB * Exp(-xB * xB);
def erfC = 1.0 - (((((a5 * tC + a4) * tC) + a3) * tC + a2) * tC + a1) * tC * Exp(-xC * xC);
def erfD = 1.0 - (((((a5 * tD + a4) * tD) + a3) * tD + a2) * tD + a1) * tD * Exp(-xD * xD);
def erfE = 1.0 - (((((a5 * tE + a4) * tE) + a3) * tE + a2) * tE + a1) * tE * Exp(-xE * xE);
def erfF = 1.0 - (((((a5 * tF + a4) * tF) + a3) * tF + a2) * tF + a1) * tF * Exp(-xF * xF);
def erfG = 1.0 - (((((a5 * tG + a4) * tG) + a3) * tG + a2) * tG + a1) * tG * Exp(-xG * xG);
def erfH = 1.0 - (((((a5 * tH + a4) * tH) + a3) * tH + a2) * tH + a1) * tH * Exp(-xH * xH);
def erfI = 1.0 - (((((a5 * tI + a4) * tI) + a3) * tI + a2) * tI + a1) * tI * Exp(-xI * xI);
def erfJ = 1.0 - (((((a5 * tJ + a4) * tJ) + a3) * tJ + a2) * tJ + a1) * tJ * Exp(-xJ * xJ);
def erfK = 1.0 - (((((a5 * tK + a4) * tK) + a3) * tK + a2) * tK + a1) * tK * Exp(-xK * xK);
def erfL = 1.0 - (((((a5 * tL + a4) * tL) + a3) * tL + a2) * tL + a1) * tL * Exp(-xL * xL);
DEF CDFA = 0.5 * (1.0 + signA * erfA);
DEF CDFB = 0.5 * (1.0 + signB * erfB);
DEF CDFC = 0.5 * (1.0 + signC * erfC);
DEF CDFD = 0.5 * (1.0 + signD * erfD);
DEF CDFE = 0.5 * (1.0 + signE * erfE);
DEF CDFF = 0.5 * (1.0 + signF * erfF);
DEF CDFG = 0.5 * (1.0 + signG * erfG);
DEF CDFH = 0.5 * (1.0 + signH * erfH);
DEF CDFI = 0.5 * (1.0 + signI * erfI);
DEF CDFJ = 0.5 * (1.0 + signJ * erfJ);
DEF CDFK = 0.5 * (1.0 + signK * erfK);
DEF CDFL = 0.5 * (1.0 + signL * erfL);
plot A1_Dot = if IsNaN(Close) then Double.NaN else 1;
A1_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A1_Dot.SetLineWeight(DotSize);
A1_Dot.AssignValueColor(if cdfA >= .5 then Color.Cyan else Color.Magenta);
plot A2_Dot = if IsNaN(Close) then Double.NaN else 2;
A2_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A2_Dot.SetLineWeight(DotSize);
A2_Dot.AssignValueColor(if cdfB >= .5 then Color.Cyan else Color.Magenta);
plot A3_Dot = if IsNaN(Close) then Double.NaN else 3;
A3_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A3_Dot.SetLineWeight(DotSize);
A3_Dot.AssignValueColor(if cdfC >= .5 then Color.Cyan else Color.Magenta);
plot A4_Dot = if IsNaN(Close) then Double.NaN else 4;
A4_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A4_Dot.SetLineWeight(DotSize);
A4_Dot.AssignValueColor(if cdfD >= .5 then Color.Cyan else Color.Magenta);
plot A5_Dot = if IsNaN(Close) then Double.NaN else 5;
A5_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A5_Dot.SetLineWeight(DotSize);
A5_Dot.AssignValueColor(if cdfE >= .5 then Color.Cyan else Color.Magenta);
plot A6_Dot = if IsNaN(Close) then Double.NaN else 6;
A6_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A6_Dot.SetLineWeight(DotSize);
A6_Dot.AssignValueColor(if cdfF >= .5 then Color.Cyan else Color.Magenta);
plot A8_Dot = if IsNaN(Close) then Double.NaN else 8;
A8_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A8_Dot.SetLineWeight(DotSize);
A8_Dot.AssignValueColor(if cdfG >= .5 then Color.Blue else Color.Red);
plot A9_Dot = if IsNaN(Close) then Double.NaN else 9;
A9_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A9_Dot.SetLineWeight(DotSize);
A9_Dot.AssignValueColor(if cdfH >= .5 then Color.Blue else Color.Red);
plot A10_Dot = if IsNaN(Close) then Double.NaN else 10;
A10_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A10_Dot.SetLineWeight(DotSize);
A10_Dot.AssignValueColor(if cdfI >= .5 then Color.Blue else Color.Red);
plot A11_Dot = if IsNaN(Close) then Double.NaN else 11;
A11_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A11_Dot.SetLineWeight(DotSize);
A11_Dot.AssignValueColor(if cdfJ >= .5 then Color.Blue else Color.Red);
plot A12_Dot = if IsNaN(Close) then Double.NaN else 12;
A12_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A12_Dot.SetLineWeight(DotSize);
A12_Dot.AssignValueColor(if cdfK >= .5 then Color.Blue else Color.Red);
plot A13_Dot = if IsNaN(Close) then Double.NaN else 13;
A13_Dot.SetPaintingStrategy(PaintingStrategy.POINTS);
A13_Dot.SetLineWeight(DotSize);
A13_Dot.AssignValueColor(if cdfL >= .5 then Color.Blue else Color.Red);
AssignPriceColor(if paintbars == 1 and cdfA >= .5 then Color.Cyan else if paintbars == 1 and cdfA < .5 then Color.Magenta else if paintbars == 2 and cdfB >= .5 then Color.Cyan else if paintbars == 2 and cdfB < .5 then Color.Magenta else if paintbars == 3 and cdfC >= .5 then Color.Cyan else if paintbars == 3 and cdfC < .5 then Color.Magenta else if paintbars == 4 and cdfD >= .5 then Color.Cyan else if paintbars == 4 and cdfD < .5 then Color.Magenta else
if paintbars == 5 and cdfE >= .5 then Color.Cyan else if paintbars == 5 and cdfE < .5 then Color.Magenta else if paintbars == 6 and cdfF >= .5 then Color.Cyan else if paintbars == 6 and cdfF < .5 then Color.Magenta else if paintbars == 8 and cdfG >= .5 then Color.Blue else if paintbars == 8 and cdfG <.5 then Color.Red else if paintbars == 9 and cdfH >= .5 then Color.Blue else if paintbars == 9 and cdfH <.5 then Color.Red else
if paintbars == 10 and cdfI >= .5 then Color.Blue else if paintbars == 10 and cdfI < .5 then Color.Red else if paintbars == 11 and cdfJ >=.5 then Color.Blue else if paintbars == 11 and cdfJ < .5 then Color.Red else if paintbars == 12 and cdfK >= .5 then Color.Blue else if paintbars == 12 and cdfK < .5 then Color.Red else if paintbars == 13 and cdfL >= .5 then Color.Blue else if paintbars == 13 and cdfL < .5 then Color.Red else
color.Current);