#Advanced Volume Study
#[email protected]
#v5.22.2020
declare on_volume;
input ShowVolumeAsCandlesticks = no;
input ShowBuySellStrengthOnVolumeBars = no;
input ShowBuySellStrength2ndAgg = no;
input AvgDayVolLength = 5;
input AvgVolLength = 20;
input ShowDayVolLabel = yes;
input ShowBarVolLabel = yes;
input ShowEthTotalVol = no;
input ShowBuySellStrength = yes;
#----------------------------------------
input startcount = {default firstbar, rthbegin};
input begin = 0930;
input bars = 5;
def bar = if startcount == startcount.rthbegin and SecondsTillTime(begin) == 0 and
SecondsFromTime(begin) == 0
then 1
else if close == First(close)
then 1
else bar[1] + 1;
def BuySellStrAgg2 = bar % bars == 0;# AggregationPeriod.THIRTY_MIN;
#----------------------------------------
input VolAverageType = AverageType.SIMPLE;
#if ShowBuySellStrengthOnVolumeBars is toggled on then the following volume bar paint options will not show, only the VolSignal Triangle set at the top of the bars will be painting according to volume average levels.
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 PaintPriceAsVol = no;
input ShowVerticalTickLines = yes;
def ShowVertLines = if ShowVerticalTickLines and GetAggregationPeriod() < AggregationPeriod.DAY then 1 else 0;
input TickLevel = 1000;
input ShowTickLabel = yes;
def NA = Double.NaN;
def h = high;
def l = low;
def c = close;
def o = open;
def v = volume;
def PriceRange = h - l;
def TopShadowRange = if o >= c then h - o else h - c;
def BottomShadowRange = if o <= c then o - l else c - l;
def BodyRange = PriceRange - (TopShadowRange + BottomShadowRange);
def VolumeTopShadowValue = (1 - (TopShadowRange / PriceRange)) * v;
def VolumeBottomShadowValue = ((BottomShadowRange / PriceRange) * v);
def BodyRangeVolValue = ((BodyRange + BottomShadowRange) / PriceRange) * v;
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);
#Current Candle Buy and Sell Strength
def BuyStr = ((c - l) / PriceRange) * 100;
def SellStr = ((h - c) / PriceRange) * 100;
def bar2count = if bar % bars == 0
then bar2count[1] + 1
else bar2count[1];
def high2 = if IsNaN(h)
then high2[1]
else if bar2count != bar2count[1]
then Highest(h, bars) else Double.NaN;
def close2 = if IsNaN(c)
then close2[1]
else if bar2count != bar2count[1]
then c else Double.NaN;
def low2 = if IsNaN(l)
then low2[1]
else if bar2count != bar2count[1]
then Lowest(l, bars) else Double.NaN;
def volume2 = if IsNaN(v)
then volume2[1]
else if bar2count != bar2count[1]
then Highest(v, bars) else Double.NaN;
def v2 = volume2;
def BuyStr2 = if IsNaN((close2 - low2) / (high2 - low2) * 100)
then BuyStr2[1]
else (close2 - low2) / (high2 - low2) * 100;
def SellStr2 = if IsNaN((high2 - close2) / (high2 - low2) * 100)
then SellStr2[1]
else (high2 - close2) / (high2 - low2) * 100;
plot BuyVol = if ShowBuySellStrengthOnVolumeBars then (BuyStr / 100) * v else NA;
def SellVol = (SellStr / 100) * v;
def BuyVol2 = (BuyStr2 / 100) * volume2;
def SellVol2 = (SellStr2 / 100) * volume2;
AddCloud(if ShowBuySellStrength2ndAgg then BuyVol2 else NA, 0, VolColor.Color("Bullish"), VolColor.Color("Bullish"), yes);
AddCloud(if ShowBuySellStrength2ndAgg then v2 else NA, BuyVol2, VolColor.Color("Bearish"), VolColor.Color("Bearish"), yes);
BuyVol.SetDefaultColor(VolColor.Color("Bullish"));
BuyVol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#BuyVol2.SetDefaultColor(Color.GREEN);
#BuyVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#SellVol2.SetDefaultColor(Color.GREEN);
#SellVol2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot Vol = v;
plot VolumeBottom = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else VolumeBottomShadowValue;
plot VolumeBody = if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then NA else BodyRangeVolValue;
VolumeBottom.HideTitle();
VolumeBody.HideTitle();
plot VolAvg = MovingAverage(VolAverageType, v, 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);
plot VolSigma2 = VolAvg + Num_Dev1 * sDev;
plot VolSigma3 = VolAvg + Num_Dev2 * sDev;
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 = v / v[1];
#Triangle Vol Signal
plot VolSignal = if Vol > VolSigma3 then v else if Vol > VolSigma2 then v else if Vol > VolAvg then volume else if RelPrevVol >= RelativetoPrevVolTolerance then v else NA;
#current aggregation's volume labels
AddLabel(ShowBarVolLabel, "Vol: " + v + " / " + 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 v else if !conf then CompoundValue(1, ETH_VOL[1] + v, v) else ETH_VOL[1];
AddLabel(if !ShowEthTotalVol 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);
#current candle Buy/Sell strength labels
AddLabel(if ShowBuySellStrength then 1 else 0, " ", Color.BLACK);
AddLabel(if ShowBuySellStrength then 1 else 0, "1", Color.GRAY);
AddLabel(if ShowBuySellStrength then 1 else 0, "Sell " + Round(SellStr, 2) + "%", if SellStr > BuyStr then Color.RED else Color.DARK_RED);
AddLabel(if ShowBuySellStrength then 1 else 0, "Buy " + Round(BuyStr, 2) + "%", if BuyStr > SellStr then Color.GREEN else Color.DARK_GREEN);
#2nd Aggregation Buy/Sell strength labels
#AddLabel(if ShowBuySellStrength then 1 else 0, "Check BuySellAgg2 > Current Agg", Color.YELLOW);
AddLabel(if !ShowBuySellStrength then 0 else 1, " ", Color.BLACK);
AddLabel(if !ShowBuySellStrength then 0 else 1, "2", Color.GRAY);
AddLabel(if !ShowBuySellStrength then 0 else 1, "Sell " + Round(SellStr2, 2) + "%", if SellStr2 > BuyStr2 then Color.RED else Color.DARK_RED);
AddLabel(if !ShowBuySellStrength then 0 else 1, "Buy " + Round(BuyStr2, 2) + "%", if BuyStr2 > SellStr2 then Color.GREEN else Color.DARK_GREEN);
VolumeBottom.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
VolumeBottom.AssignValueColor(Color.BLACK);
VolumeBody.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
VolumeBody.AssignValueColor(if PaintAboveAvgVolBars and Vol > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and Vol > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and Vol > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintBelowAvgVol and Vol < VolAvg then VolColor.Color("Below Average") else if c > o then VolColor.Color("Bullish") else VolColor.Color("Bearish"));
#VolumeTop.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
#VolumeTop.AssignValueColor(if close > open then Color.Black else Color.Black);
VolAvg.SetDefaultColor(VolColor.Color("VolAvg"));
VolSigma2.SetDefaultColor(VolColor.Color("VolSigma2"));
VolSigma3.SetDefaultColor(VolColor.Color("VolSigma3"));
Vol.SetPaintingStrategy(if !ShowVolumeAsCandlesticks or ShowBuySellStrengthOnVolumeBars then PaintingStrategy.SQUARED_HISTOGRAM else PaintingStrategy.HISTOGRAM);
Vol.SetLineWeight(1);
Vol.AssignValueColor(if ShowBuySellStrengthOnVolumeBars then VolColor.Color("Bearish") else if PaintAboveAvgVolBars and v > VolSigma3 then VolColor.Color("VolSigma3") else if PaintAboveAvgVolBars and v > VolSigma2 then VolColor.Color("VolSigma2") else if PaintAboveAvgVolBars and v > VolAvg then VolColor.Color("VolAvg") else if PaintAccordingToRelPrevVol and RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else if PaintBelowAvgVol and v < VolAvg then VolColor.Color("Below Average") else if c > o then VolColor.Color("Bullish") else VolColor.Color("Bearish"));
AssignPriceColor(if PaintPriceAsVol and PaintAboveAvgVolBars and v > VolSigma3 then VolColor.Color("VolSigma3") else if PaintPriceAsVol and PaintAboveAvgVolBars and v > VolSigma2 then VolColor.Color("VolSigma2") else if PaintPriceAsVol and PaintAboveAvgVolBars and v > 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 v < VolAvg then VolColor.Color("Below Average") else Color.CURRENT);
VolSignal.AssignValueColor(if Vol > VolSigma3 then VolColor.Color("VolSigma3") else if Vol > VolSigma2 then VolColor.Color("VolSigma2") else if Vol > VolAvg then VolColor.Color("VolAvg") else if RelPrevVol >= RelativetoPrevVolTolerance then VolColor.Color("Relative to Prev") else VolColor.Color("Below Average"));
VolSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
VolAvg.SetLineWeight(2);
VolSignal.SetLineWeight(1);
########################################
#Buy/Sell Bubbles
input show_buy_sell_bubbles = yes;
input bubblemover = 0;
def b = bubblemover;
def b1 = b + 1;
def periods = bar % bars == 0;
def periodCount = CompoundValue(1, if periods then periodCount[1] + 1 else periodCount[1], 0);
def thisperiod = (HighestAll(periodCount) - periodCount) ;
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1] != thisperiod[b], VolAvg[b], Round(SellStr2) + "%", if Round(SellStr2) > Round(BuyStr2) then Color.RED else Color.GRAY);
AddChartBubble(show_buy_sell_bubbles and thisperiod[b1] != thisperiod[b] , VolAvg[b], Round(BuyStr2) + "%", if Round(SellStr2) > Round(BuyStr2) then Color.GRAY else Color.GREEN);
#######################################