#//@xinolia
#study(shorttitle="vwBB", title="vwBB", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 01/2023
script nz {
input data = close;
input repl = 0;
def ret_val = if !IsNaN(data) then data else repl;
plot return = ret_val;
}
#vwma(source, length)
script pine_vwma {
input x = close;
input y = 15;
def v = volume;
def VWMA = SimpleMovingAvg(x * nz(v,1), y) / SimpleMovingAvg(nz(v,1), y);
plot result = VWMA;
}
#stdv(src, len, avg, vol)=>
script stdv {
input src = close;
input len = 20;
input avg = 0;
input vol = volume;
def std;
std = fold i = 1 to len - 1 with p = Power(src - avg, 2) * vol do
P + Power(src[i] - avg, 2) * vol[i];
def stdv = Sqrt(std / (SimpleMovingAvg(vol, len) * len));
plot return = stdv;
}
#// Function for outputting the i-th Sigma deviation band
#devBand(flag, i, basis, dev) =>
script devBand {
input flag = yes;
input i = 1;
input basis = 0;
input dev = 0;
def devBand;
if (i > 0 or !(flag)) {
devBand = basis + i * dev;
} else {
devBand = basis * basis / (basis + AbsValue(i) * dev);
}
plot return = devBand;
}
#rms(source, length)=>
script rms {
input source = close;
input length = 14;
def rms = sqrt(sum(power(source, 2), length)/length);
plot return = rms;
}
#Gaussianma(values, length) =>
script Gaussian {
input values = close;
input length = 20;
def stddev = length / 4;
def indices = length - 1;
def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
def sum = Sum(values * weights, length);
def gMA = sum / Sum(weights, length);
plot return = gMA;
}
#pine_swma(source) =>
script swma {
input source = close;
def swma = source[3] * 1 / 6 + source[2] * 2 / 6 + source[1] * 2 / 6 + source[0] * 1 / 6;
plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
input src = close;
input len = 14;
def lsma = Inertia(src, len);
def lsma2 = Inertia(lsma, len);
def eq = lsma - lsma2;
def zlsma = lsma + eq;
plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
input src = close;
input len = 14;
def zdema1 = ExpAverage(src, len);
def zdema2 = ExpAverage(zdema1, len);
def dema1 = 2 * zdema1 - zdema2;
def zdema12 = ExpAverage(dema1, len);
def zdema22 = ExpAverage(zdema12, len);
def zldema = 2 * zdema12 - zdema22;
plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
input src = close;
input len = 14;
def ema1 = ExpAverage(src, len);
def ema2 = ExpAverage(ema1, len);
def ema3 = ExpAverage(ema2, len);
def tema1 = 3 * (ema1 - ema2) + ema3;
def ema1a = ExpAverage(tema1, len);
def ema2a = ExpAverage(ema1a, len);
def ema3a = ExpAverage(ema2a, len);
def zltema = 3 * (ema1a - ema2a) + ema3a;
plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
input series = close;
input windowsize = 9;
input Offset = 0.85;
input Sigma = 6;
def m = Offset * (windowsize - 1);
def s = windowsize / Sigma;
def norm = fold z = 0 to windowsize with CW do
CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
def sum = fold y = 0 to windowsize with WS do
WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);
plot ALMA = sum / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
input src = close;
input len = 14;
def mg;
def t = ExpAverage(src, len);
mg = if IsNaN(mg[1]) then t else
CompoundValue(1 ,mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)),src);
plot return = mg;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
input source = close;
input length = 14;
input type = "SMA";
def multiMa =
if type == "SMA" then SimpleMovingAvg(source, length) else
if type == "EMA" then ExpAverage(source, length) else
if type == "SMMA" then WildersAverage(source, length) else
if type == "WMA" then WMA(source, length) else
if type == "DEMA" then DEMA(source, length) else
if type == "TEMA" then TEMA(source, length) else
if type == "RMS" then RMS(source, length) else
if type == "ZLSMA" then zlSma(source, length) else
if type == "ZLDEMA" then zlDema(source, length) else
if type == "ZLTEMA" then zlTema(source, length) else
if type == "McGinley" then mcginley(source, length) else
if type == "ALMA" then ALMA(source, length) else
if type == "SWMA" then SWMA(source) else
if type == "Gaussian" then Gaussian(source, length) else
if type == "HMA" then HullMovingAvg(source, length ) else Double.NaN;
plot return = multiMa;
}
input ColorBar = yes;
input highLowCrossWedges = yes;
input Smoothing = yes;
input SmoothingLength = 5;
input length = 20;
input src = close;
input VolumeWeighted = yes;
input maType = {default SMA, EMA, SMMA, WMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian};
input deviationBand = yes;#, title = "deviation band Mode")
input Band1 = yes;# title = "display 1st bands")
input Band2 = yes; #(true, title = "display 2nd bands")
input Band3 = yes; #(true, title = "display 3rd bands")
def na = Double.NaN;
#----------Color
DefineGlobalColor("Band", CreateColor(33,150,243));
DefineGlobalColor("basis", CreateColor(255,91,129));
DefineGlobalColor("cloud", CreateColor(3,40,68));
#---Cal
def basis = if VolumeWeighted then pine_vwma(src, length) else multiMa(src, length, maType);
def SmoothBasis = if Smoothing then multiMa(basis,SmoothingLength,maType) else basis;
def dev = stdv(src, length, basis, (if(VolumeWeighted,nz(volume,1.0),basis)));
def DevBand1 = if Smoothing then multiMa(devBand(deviationBand, 1, basis, dev),SmoothingLength,maType) else
devBand(deviationBand,1, basis, dev);
def DevBandN1 = if Smoothing then multiMa(devBand(deviationBand, -1, basis, dev),SmoothingLength,maType) else
devBand(deviationBand,-1, basis, dev);
def DevBand2 = if Smoothing then multiMa(devBand(deviationBand, 2, basis, dev),SmoothingLength,maType) else
devBand(deviationBand, 2, basis, dev);
def DevBandN2 = if Smoothing then multiMa(devBand(deviationBand,-2, basis, dev),SmoothingLength,maType) else
devBand(deviationBand,-2, basis, dev);
def DevBand3 = if Smoothing then multiMa(devBand(deviationBand, 3, basis, dev),SmoothingLength,maType) else
devBand(deviationBand, 3, basis, dev);
def DevBandN3 = if Smoothing then multiMa(devBand(deviationBand,-3, basis, dev),SmoothingLength,maType) else
devBand(deviationBand,-3, basis, dev);
plot BasisLine = SmoothBasis; #"Basis"
BasisLine.SetDefaultColor(GlobalColor("basis"));
plot FstSigma = if Band1 then DevBand1 else na;
FstSigma.SetDefaultColor(GlobalColor("Band"));
plot FstSigmaNeg = if Band1 then DevBandN1 else na;
FstSigmaNeg.SetDefaultColor(GlobalColor("Band"));
plot SecSigma = if Band2 then DevBand2 else na;
SecSigma.SetDefaultColor(GlobalColor("Band"));
plot SecSigmaNeg = if Band2 then DevBandN2 else na;
SecSigmaNeg.SetDefaultColor(GlobalColor("Band"));
plot trdSigma = if Band3 then DevBand3 else na;
trdSigma.SetDefaultColor(GlobalColor("Band"));
plot trdSigmaNeg = if Band3 then DevBandN3 else na;
trdSigmaNeg.SetDefaultColor(GlobalColor("Band"));
#--- PriceColor
plot OverBought = if highLowCrossWedges and src>DevBand3 then high else na;
OverBought.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
OverBought.SetDefaultColor(Color.RED);
plot OverSold = if highLowCrossWedges and src<DevBandN3 then low else na;
OverSold.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
OverSold.SetDefaultColor(Color.GREEN);
def ExHi = src>DevBand2;
def hi = src>DevBand1;
def weakHi = src>SmoothBasis;
def ExLo = src<DevBandN2;
def Lo = src<DevBandN1;
def weakLo = src<SmoothBasis;
AssignPriceColor( if !ColorBar then Color.CURRENT else
if ExHi then Color.GREEN else
if Hi then Color.DARK_GREEN else
if weakHi then Color.LIGHT_GREEN else
if ExLo then Color.RED else
if Lo then Color.DARK_RED else
if weakLo then Color.PINK else Color.GRAY);
AddCloud(SecSigma, SecSigmaNeg, GlobalColor("cloud"));
#-- END CODE