Volume Divergence VN for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
E70ADQv.png

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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@samer800.. Appreciate your generosity in converting these studies from the TV platform-which I also use as well as Tradovate. I am following your contributions via usethinkscript.com. Any progress on what I call the missing link - cumulative delta volume- Bookmap order flows and Delta Histograms you come across is welcomed! . I fear that the old TOS is aging and simply not able to provide the order flow nuances that move the the needle... - with Schwabb taking over I foresee that the time invested in other platforms will come in handy. Regards this particular study, experimented placing on the upper price panel,and shortening it to 5 fast and 21 minute 2nd Mov Ave. on a 1 t- 5 minute chart it is interesting visually stricking see the the short term divergences and reversals- that is only for the brave that do high speed scalping and is not a recommendation that I stand .. all bets off as it might just be another Quick and Dead Replay! Guns Abalzing and Again thanks
I am sure you are aware of all these permutations.. Best wishes MagicQuotes.
 
Last edited:
E70ADQv.png

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
I like this study! Can you offer a small user narrative or a description which I can research? I am a day trader and from the looks of this it will let me soothe some of the edges off. I really would like to understand more about the indicator that is under the hood, without having to try to reverse engineer it. Thanks StkJocky
 
E70ADQv.png

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
I have to refresh or reload the indicator to get bubbles to show up. is there any way to improve that? i do like it.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
537 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top