```
# Show total volume in gray. Buying volume in green. Sell Volume in red.
# Volume average is gray line.
# Specified percent over average volume is cyan triangles.
# Horserider 12/30/2019 derived from some already existing studies.
# Change 20211012 mcdon030
#1. data choice, add percent, trend, scale volume and Correlation of trend
declare lower;
#Inputs
input ShowTodayVolumeLabel = yes;
input UnusualVolumePercentLabel = 200;
input ShowCurrentBarLabel = yes;
input ShowSellVolumePercentLabel = yes;
input ShowHotVolumeArrows = yes;
input ShowVol = yes;
input ShowMultiBarVol5 = yes;
input ShowMultiBarVol10 = yes;
input ShowAllMultiBar = yes;
def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def tcnt = tick_count ;
def tickS= if(!IsNaN(TickSize()),TickSize(),.01);
def bn = BarNumber();
def nan = Double.NaN;
Input sLength = 3;
Input trLength = 7;
Input CorrelationThreshold = .65;
Input Upbound = .65;
Input Dnbound = .35;
def Data;
input Voltype = {TrRange,TickTrRange,isVolume, default Shares};
switch (Voltype) {
case TrRange:
Data = (v / Max(TrueRange(h, c, l), tickS));
case TickTrRange:
Data = (v / tcnt / Max(TrueRange(h, c, l),tickS));
case isVolume:
Data = v;
case Shares:
Data = (v/tcnt);
}
def buying = Data * (C - L) / (H - L);
def selling = Data * (H - C) / (H - L);
def buprec = buying / (buying + selling );
def beprec = selling / (buying + selling );
def PrimeData = if beprec > buprec then beprec else buprec;
def Balance = If(Between(PrimeData,Dnbound, Upbound), 1, 0);
# TRend
def primesum = Sum(if PrimeData == beprec then -1 else 1,sLength);
def primeTrend = inertia(primesum,trLength);
# Confirm Trend's correlation to price
def x1 = if !isNaN(c) then c else x1[1];
def x2 = if !isNaN(c) then primeTrend else x2[1] ;
def avx = Average(x1, trLength );
def avy = Average(x2, trLength );
def ax = x1 - avx;
def bx = x2 - avy;
def ax2 = sum(sqr(ax), trLength );
def bx2 = sum(sqr(bx), trLength );
def ab = sum(ax * bx, trLength );
def correlation = sum(ab, trLength ) / Sqrt(sum(ax2, trLength ) * sum(bx2, trLength ));
script Scale {
input c = close;
input Min = 0;
input Max = 1;
def hh = HighestAll(c);
def ll = LowestAll(c);
plot Range = (((Max - Min) * (c - ll)) / (hh - ll)) + Min;
}
# Selling Volume
plot BuyVol = if showVol then scale(buying,-10,1) else double.NaN;
BuyVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
BuyVol.SetDefaultColor(Color.DARK_GREEN);
BuyVol.HideTitle();
BuyVol.HideBubble();
BuyVol.SetLineWeight(5);
plot SellVol = if showVol then scale(selling,-10,1) else double.NaN;
SellVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
SellVol.SetDefaultColor(Color.DARK_RED);
SellVol.HideTitle();
SellVol.HideBubble();
SellVol.SetLineWeight(5);
plot pPrime =PrimeData;
pPrime.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
pPrime.AssignValueColor( if PrimeData == beprec then Color.green else Color.red);
#SellVol.HideTitle();
#SellVol.HideBubble();
pPrime.SetLineWeight(1);
plot pTrend = if !isnan(c) then primeTrend else nan;
pTrend.assignValueColor(
if Balance && Correlation>=CorrelationThreshold then color.cyan else
if Balance && Correlation<CorrelationThreshold then color.gray else
if pTrend>pTrend[1] && Correlation>=CorrelationThreshold then color.green else
if pTrend>pTrend[1] && Correlation<CorrelationThreshold then color.dark_green else
if pTrend<pTrend[1] && Correlation>=CorrelationThreshold then color.red else
if pTrend<pTrend[1] && Correlation<CorrelationThreshold then color.dark_red else
color.red);
pTrend.SetLineWeight(2);
pTrend.HideTitle();
pTrend.HideBubble();
addCloud(pTrend,0,color.green,color.red);
```