#Companion indicator to the Advanced Volume Study for Volume Spread Analysis
#[email protected]
declare upper;
input PaintPriceAsVol = no;
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowEthTotalVolLabel = no;
input ShowTickLabel = yes;
input VolAverageType = AverageType.SIMPLE;
input PaintAboveAvgVolBars = yes;
input PaintAccordingToRelPrevVol = yes;
input RelativetoPrevVolTolerance = 1.25; #if volume is 1.25x greater than previous bar it will paint even if it is still below the average/sigma2/sigma3
input PaintBelowAvgVol = yes;
input ShowVerticalTickLines = yes;
def ShowVertLines = if ShowVerticalTickLines and GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
input TickLevel = 1000;
def NA = Double.NaN;
def DayVolAgg = if GetAggregationPeriod() < AggregationPeriod.DAY then AggregationPeriod.DAY else GetAggregationPeriod();
def DayVol = volume("period" = DayVolAgg);
def AvgDayVol = Average(DayVol, AvgDayVolLength);
def Start = 0930;
def End = 1600;
def conf = SecondsFromTime(Start) >= 0 and SecondsFromTime(End) <= 0;
plot VolColor = NA;
VolColor.DefineColor("Bullish", Color.GREEN);
VolColor.DefineColor("Bearish", Color.RED);
VolColor.DefineColor("VolAvg", CreateColor(0, 100, 200));
VolColor.DefineColor("VolSigma2", Color.DARK_ORANGE);
VolColor.DefineColor("VolSigma3", Color.MAGENTA);
VolColor.DefineColor("Relative to Prev", Color.YELLOW);
VolColor.DefineColor("Below Average", Color.GRAY);
VolColor.DefineColor("ETH TVOL", Color.GRAY);
VolColor.DefineColor("TICK Vert", Color.GRAY);
def Vol = volume;
def VolAvg = MovingAverage(VolAverageType, volume, AvgVolLength);
#2Sigma and 3Sigma Vol Filter
def Num_Dev1 = 2.0;
def Num_Dev2 = 3.0;
def averageType = AverageType.SIMPLE;
def sDev = StDev(data = Vol, length = AvgVolLength);
def VolSigma2 = VolAvg + Num_Dev1 * sDev;
def VolSigma3 = VolAvg + Num_Dev2 * sDev;
#####
#VSA start test
#####
def VSig3H = if volume > VolSigma3 then high else VSig3H[1];
def VSig2H = if volume > VolSigma2 and volume < VolSigma3 then high else VSig2H[1];
def VSigAH = if volume > VolAvg and volume < VolSigma2 then high else VSigAH[1];
def VSig3L = if volume > VolSigma3 then low else VSig3L[1];
def VSig2L = if volume > VolSigma2 and volume < VolSigma3 then low else VSig2L[1];
def VSigAL = if volume > VolAvg and volume < VolSigma2 then low else VSigAL[1];
plot VSig3Hc = Vsig3H;
plot VSig2Hc = Vsig2H;
plot VSigAHc = VSigAH;
plot VSig3Lc = Vsig3L;
plot VSig2Lc = Vsig2L;
plot VSigALc = VSigAL;
VSig3Hc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VSig2Hc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VSigAHc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VSig3Lc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VSig2Lc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VSigALc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
VSig3Hc.SetDefaultColor(Color.MAGENTA);
VSig2Hc.SetDefaultColor(Color.DARK_ORANGE);
VSigAHc.SetDefaultColor(CreateColor(0,125,225));
VSig3Lc.SetDefaultColor(Color.MAGENTA);
VSig2Lc.SetDefaultColor(Color.DARK_ORANGE);
VSigALc.SetDefaultColor(CreateColor(0,125,225));
#####
def RelDayVol = DayVol / AvgDayVol[1];
def RelPrevDayVol = DayVol / volume("period" = DayVolAgg)[1];
#Daily aggregation volume labels
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if ShowDayVolLabel then 1 else 0, "DayVol: " + DayVol + " / " + Round(RelDayVol, 2) + "x Avg(" + AvgDayVolLength + ") / " + Round(RelPrevDayVol, 2) + "x Prev", if DayVol > AvgDayVol then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));
def RelVol = Vol / VolAvg[1];
def RelPrevVol = volume / volume[1];
#Triangle Vol Signal
def VolSignal = if Vol > VolSigma3 then volume else if Vol > VolSigma2 then volume else if Vol > VolAvg then volume else if RelPrevVol >= RelativetoPrevVolTolerance then volume else NA;
#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + volume + " / " + Round(RelVol, 2) + "x Avg(" + AvgVolLength + ") / " + Round(RelPrevVol, 2) + "x Prev", if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else VolColor.Color("Below Average"));
#ETH Total Vol Label
def ETH_VOL = if !conf and conf[1] then volume else if !conf then CompoundValue(1, ETH_VOL[1] + volume, volume) else ETH_VOL[1];
AddLabel(if !ShowEthTotalVolLabel then 0 else if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else 1, "ETH TVOL: " + ETH_VOL, VolColor.Color("ETH TVOL"));
#$TICK Vertical Lines
def tickc = close("$TICK");
def tickh = high("$TICK");
def tickl = low("$TICK");
AddVerticalLine(if ShowVertLines and (tickh > TickLevel) or ShowVertLines and (tickl < -TickLevel) then 1 else 0, if (tickh > TickLevel) then tickh else if (tickl < -TickLevel) then tickl else Double.NaN, VolColor.Color("TICK Vert"));
#$TICK Label
AddLabel(if ShowTickLabel then 1 else 0,"$TICK: " + tickc, if tickc > 0 then Color.GREEN else Color.RED);
AssignPriceColor(if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma3 then VolColor.Color("VolSigma3") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolSigma2 then VolColor.Color("VolSigma2") else if PaintPriceAsVol and PaintAboveAvgVolBars and volume > VolAvg then VolColor.Color("VolAvg") else if PaintPriceAsVol and PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintPriceAsVol and PaintBelowAvgVol and volume < VolAvg then VolColor.Color("Below Average") else Color.CURRENT);