Author Message:
This indicator was created using different weighted moving averages of trading volume to find the difference between trading volume and price.
Specifically, this code uses the pine_wma(x, y) function to calculate a weighted moving average of the trading volume. It then uses these moving averages to calculate the indicator and gives a signal for holding divergence and volume in stock market.
CODE:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © baymucuk
#study(title="Volume Divergence VN", shorttitle="Volume VN"
# converted by Sam4Cok@Samer800 - 04/2023
declare lower;
input ColorBars = yes;
input FirstMovingAverageLength = 14; # "First Moving Average length"
input SecondMovingAverageLength = 21; # "Second Moving Average length"
def na = Double.NaN;
def last = isNaN(close);
def vl1 = FirstMovingAverageLength;
def vl2 = SecondMovingAverageLength;
#pine_wma(x, y) =>
script pine_wma {
input x = close;
input y = 14;
def c = ohlc4;
def o = if o[1]== 0 then hl2 else
CompoundValue(1, (o[1] + c[1]) / 2, hl2);
def norm = fold i = 0 to y - 1 with p do
p + ((y - i) * y);
def sum = fold i1 = 0 to y - 1 with p1 do
p1 + (x[i1] * ((y - i1) * y) * (If(c[i1] < o[i1], -1, 1)));
def wma = sum / norm;
plot out = wma;
}
def vl3 = vl1 + vl2;
def vl4 = vl2 + vl3;
def vl5 = vl3 + vl4;
def v1 = pine_wma(volume, vl1);
def v2 = pine_wma(v1, vl2);
def v3 = pine_wma(v2, vl3);
def v4 = pine_wma(v3, vl4);
def vol = pine_wma(v4, vl5);
plot hline = if last then na else 0; # "Baseline"
hline.SetDefaultColor(Color.GRAY);
hline.SetStyle(Curve.SHORT_DASH);
plot volHist = vol; # "Volume"
volHist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
volHist.AssignValueColor(if vol>0 then if vol>vol[1] then Color.GREEN else Color.DARK_GREEN else
if vol<vol[1] then Color.RED else Color.DARK_RED);
#---- Bar Colors
AssignPriceColor( if !ColorBars then Color.CURRENT else
if vol>0 then if vol>vol[1] then Color.GREEN else Color.DARK_GREEN else
if vol<vol[1] then Color.RED else Color.DARK_RED);
#----Div-----------
input LookBackRight = 5; # "Pivot Lookback Right"
input LookBackLeft = 5; # "Pivot Lookback Left"
input MaxLookback = 60; # "Max of Lookback Range"
input MinLookback = 5; # "Min of Lookback Range"
input DivBull = yes; # "Plot Bullish"
input DivBear = yes; # "Plot Bearish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivHiddenBear = no; # "Plot Hidden Bearish"
def divSrc = vol;
def h = high;
def l = low;
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 5; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = AbsValue(CompoundValue(1, BarNumber(), 0));
_nan = Double.NaN;
_VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
input cond = yes;
input src = close;
input MinLookback = 5;
input MaxLookback = 60;
input occurrence = 0;
def n = occurrence + 1;
def offset = fold j = MinLookback to MaxLookback + MinLookback 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);
}
#_inRange(cond) =>
script _inRange {
input cond = yes;
input rangeLower = 5;
input rangeUpper = 60;
def bars = if cond==yes then 1 else bars[1] + 1;
def inrange = (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
def pl = findpivots(divSrc,-1, LookBackLeft, LookBackRight);
def ph = findpivots(divSrc, 1, LookBackLeft, LookBackRight);
def plFound = if isNaN(pl) then 0 else 1;
def phFound = if isNaN(ph) then 0 else 1;
def vlFound = valuewhen(plFound, divSrc, MinLookback, MaxLookback, 1);
def vhFound = valuewhen(phFound, divSrc, MinLookback, MaxLookback, 1);
def plPrice = valuewhen(plFound, l, MinLookback, MaxLookback, 1);
def phPrice = valuewhen(phFound, h, MinLookback, MaxLookback, 1);
#// Regular Bullish
def oscHL = divSrc > vlFound and _inRange(plFound[1],MinLookback, MaxLookback);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc < vlFound and _inRange(plFound[1],MinLookback, MaxLookback);
def priceHL = l > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;
#// Regular Bearish
def oscLH = divSrc < vhFound and _inRange(phFound[1],MinLookback, MaxLookback);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and _inRange(phFound[1], MinLookback, MaxLookback);
def priceLH = h < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;
#------ Bubbles
addchartbubble(bullCond, divSrc, "R", Color.CYAN, no);
addchartbubble(bearCond, divSrc, "R", Color.MAGENTA, yes);
addchartbubble(hiddenBullCond, divSrc, "H", Color.VIOLET, no);
addchartbubble(hiddenBearCond, divSrc, "H", Color.PLUM, yes);
#--- END of CODE