# Designed to achieve Normal Distribution across all aggregations and instruments
# Mobius
# V02.02.2019
#Hint: Plots a Gaussian Mean with Standard deviation Envelope. If Normal Distribution is met, then at minimum, 68.2% of the close values should be inside a One Standard Deviation Envelope and 95.4% of the close values should be inside a 2 Standard Deviation Envelope.

input length = 21; #hint length: length for Moving Average
input price = close; #hint price: iData used for calculations
input SdMult = 2.00; #hint SdMult: Mult. for Standard Deviation Bands
input ShowBands = yes;#hint ShowBands: yes or no
input ShowLabel = yes;#hint ShowLabel: yes or no
input PaintBars = yes;#hint PaintBars: yes or no
input BandsColor = 5; #hint BandsColor: Color Range from 1 to 9
def h;
def l;
def c;
def betaDenom;
def w;
def beta;
def alpha;
def G;
def SD;
def count;
def bars;
def p;
plot mean;
plot upper;
plot lower;
h = high;
l = low;
c = price;
betaDenom = 10*(Log(Sum((Max(h, c[1]) - Min(l, c[1])), length) /
(Highest(h, length) - Lowest(l, length)))
/ Log(length));
w = (2 * Double.Pi / length);
beta = (1 - Cos(w)) / (Power(1.414, 2.0 / betaDenom) - 1 );
alpha = (-beta + Sqrt(beta * beta + 2 * beta));
G = Power(alpha, 4) * c +
4 * (1 – alpha) * G[1] – 6 * Power( 1 - alpha, 2 ) * G[2] +
4 * Power( 1 - alpha, 3 ) * G[3] - Power( 1 - alpha, 4 ) * G[4];
SD = StDev(close, length);
mean = G;
mean.AssignValueColor(if G > G[1] then Color.CYAN else Color.YELLOW);
mean.SetLineWeight(2);
mean.HideBubble();
mean.HideTitle();
upper = mean + (SdMult * SD);
upper.SetDefaultColor(GetColor(BandsColor));
upper.SetHiding(!ShowBands);
upper.HideBubble();
upper.HideTitle();
lower = mean + (-SdMult * SD);
lower.SetDefaultColor(GetColor(BandsColor));
lower.SetHiding(!ShowBands);
lower.HideBubble();
lower.HideTitle();
count = if (close > upper) or (close < lower) and barNumber() >= 1
then compoundValue(1, count[1] + 1, 0)
else count[1];
bars = if barNumber() >= 1
then CompoundValue(1, bars[1] + 1, 1)
else 0;
p = (bars - count) / bars;
AddLabel(ShowLabel, "At " + SDmult +
" Standard Deviations Inside Bell Curve = " + AsPercent(p),
Color.WHITE);
#End Code