Author Message:
This is an experimental study designed using data from Bollinger Bands to determine price squeeze ranges and active levels of support and resistance .
First, a set of Bollinger Bands using a Coefficient of Variation weighted moving average as the basis is calculated.
Then, the relative percentage of current bandwidth to maximum bandwidth over the specified sampling period determines the relative squeeze.
The box is outlined by drawing the current highest and lowest source value over the sampling period whenever a squeeze is active.
I've included the COVWMA in the visualization for additional confirmation of price activity.
Custom Bar color scheme is included.
Code:
CSS:
#//@version=2
#study(title="Squeeze Box [DW]", overlay=true, linktoseries=true)
#https://www.tradingview.com/script/Bin98rqQ-Squeeze-Box-DW/
#//by Donovan Wall
# Converted and mod by Sam4Cok@Samer800 - 10/2022
#//inputs
input ColorBar = no;
input ShowCloud = yes;
input ShowMaLine = yes;
input src = hlc3; # "Source"
input Length = 21; # "Sampling Period"
input maType = {EMA, SMA, SMMA, WMA, VWMA, LSMA, ALMA, HULLMA, default COVWMA, FRAMA, KAMA};
input Deviations = 2.0; # "Number of Deviations for Squeeze Calculation"
input SqueezePer = 50.0; # "Relative Squeeze % Threshold"
input lasmaOffset = 0; # "Offset (if LSMA)"
input almaOffset = 0.85; # "Offset (if ALMA)"
input sigma = 6; # "Sigma (if ALMA)"
input framaCoeff = -4.6; # "Coefficient (if FRAMA)"
input kamaFast = 0.666; # "Smoothing Constant Fast End (if KAMA)"
input kamaSlow = 0.0645; # "Smoothing Constant Slow End (if KAMA)"
def na = Double.NaN;
script nz {
input data = close;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}
def v = volume;
#valuewhen (Cond, source, occurrence)
script valuewhen {
input cond = 0;
input src = close;
input occurrence = 0;
def n = occurrence + 1;
def offset = fold j = 0 to 200 with p=1 while p < n + 1
do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
plot price = GetValue(src, offset-1);
}
#//Definitions
def ema = ExpAverage(src, Length);
def sma = SimpleMovingAvg(src, Length);
def smma;
smma = if IsNaN(smma[1]) then SimpleMovingAvg(src, Length) else (smma[1] * (Length - 1) + src) / Length;
def wma = WMA(src, Length);
def vmp = src * v;
def vwma = Sum(vmp, Length) / Sum(v, Length);
def lsma = Inertia(src[lasmaOffset], Length);
#//ALMA
script ALMA {
input Data = close;
input Window = 9;
input Sigma = 6;
input Offset = 0.85;
def m = (Offset * (Window - 1));
def s = Window / Sigma;
def SumVectorData = fold y = 0 to Window with WS do
WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(Data, (Window - 1) - y);
def SumVector = fold z = 0 to Window with CW do
CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
plot ALMA = SumVectorData / SumVector;
}
#//Hull MA
def hullma = WMA(2 * WMA(src, Length / 2) - WMA(src, Length), Round(Sqrt(Length),0));
#//COVWMA
#covwma(src, per) =>
script covwma {
input src = 0;
input per = 0;
def cov = StDev(src, per) / SimpleMovingAvg(src, per);
def cw = src * cov;
def covwma = Sum(cw, per) / Sum(cov, per);
plot return = covwma;
}
#//FRAMA
#frama(src, per, w) =>
script frama {
input src = close;
input b = 0;
input w = 0;
def n3 = (Highest(high, b) - Lowest(low, b)) / b;
def hd2 = Highest(high, b / 2);
def ld2 = Lowest(low, b / 2);
def n2 = (hd2 - ld2) / (b / 2);
def n1 = (hd2[b / 2] - ld2[b / 2]) / (b / 2);
def dim = if (n1 > 0) and (n2 > 0) and (n3 > 0) then (Log(n1 + n2) - Log(n3)) / Log(2) else 0;
def alpha = Exp(w * (dim - 1));
def sc = (if alpha < 0.01 then 0.01 else (if alpha > 1 then 1 else alpha));
def frama = if TotalSum(1) <= 2 * b then src else (src * sc) + nz(frama[1]) * (1 - sc);
plot return = frama;
}
#//KAMA
#kama(a, b, fast, slow)=>
script kama {
input a = close;
input b = 20;
input fast = 0;
input slow = 0;
def dist = AbsValue(a[0] - a[1]);
def signal = AbsValue(a - a[b]);
def noise = Sum(dist, b);
def effr = if noise != 0 then signal / noise else 1;
def sc = Power(effr * (fast - slow) + slow, 2);
def kama = nz(kama[1], a) + sc * (a - nz(kama[1], a));
plot return = kama;
}
def ma;
switch (matype) {
case EMA:
ma = EMA;
case SMA:
ma = SMA;
case SMMA:
ma = SMMA;
case WMA:
ma = WMA;
case VWMA:
ma = vwma;
case LSMA:
ma = lsma;
case ALMA:
ma = alma(src, Length, Sigma, almaOffset);
case HULLMA:
ma = hullma;
case COVWMA:
ma = covwma(src, Length);
case FRAMA:
ma = frama(src, Length, framaCoeff);
case KAMA:
ma = kama(src, Length, kamaFast, kamaSlow);
}
#//Bollinger Bands
def bu = ma + StDev(src, Length) * Deviations;
def bd = ma - StDev(src, Length) * Deviations;
#//Bandwidth
def bw = (bu - bd);
#//Squeeze Percentage
def buh = Highest(bu, Length);
def bdl = Lowest(bd, Length);
def brng = buh - bdl;
def sqp = 100 * bw / brng;
#//Squeeze Box
def sqz = if (sqp < SqueezePer) then 1 else 0;
def boxh = if sqz then Highest(src, Length) else src;
def boxl = if sqz then Lowest(src, Length) else src;
def bh = valuewhen(sqz, boxh, 1);
def bl = valuewhen(sqz, boxl, 1);
#//Colors
def boxcolor = if src > bh then 1 else
if src < bl then -1 else 0;
def barcolor = if (src > bh) and (src > src[1]) then 2 else
if (src > bh) and (src < src[1]) then 1 else
if (src < bl) and (src < src[1]) then -2 else
if (src < bl) and (src > src[1]) then -1 else 0;
#//Plots
#//Squeeze Box
plot bhplot = bh; # "Box High Level"
bhplot.AssignValueColor(if boxcolor > 0 then Color.GREEN else
if boxcolor < 0 then Color.RED else Color.GRAY);
plot blplot = bl; # "Box Low Level"
blplot.AssignValueColor(if boxcolor > 0 then Color.GREEN else
if boxcolor < 0 then Color.RED else Color.GRAY);
#//MA
plot maplot = ma;
maplot.SetLineWeight(2);
maplot.SetHiding(!ShowMaLine);
maplot.AssignValueColor(if barcolor == 2 then color.GREEN else
if barcolor == 1 then color.DARK_GREEN else
if barcolor ==-2 then Color.RED else
if barcolor ==-1 then Color.DARK_RED else Color.GRAY);;
#//Fill
AddCloud(if !ShowCloud then na else
if boxcolor > 0 then bhplot else
if boxcolor < 0 then blplot else na, if boxcolor > 0 then blplot else bhplot,
color.DARK_green, Color.DARK_RED);
#//Bar Color
AssignPriceColor(if !ColorBar then Color.CURRENT else
if barcolor == 2 then color.GREEN else
if barcolor == 1 then color.DARK_GREEN else
if barcolor ==-2 then Color.RED else
if barcolor ==-1 then Color.DARK_RED else Color.GRAY);
### END
Last edited by a moderator: