Divergent Bar Indicators (MotiveWave, Williams) for ThinkorSwim

I would love to see thinkscript for divergent bar for motivewave
A bullish (green) divergent bar, signals a trend switch from bear -> bull
-> The current bar has a lower low than the previous bar, but closes in the upper half of the candle.
-> This means the bulls are pushing from below and are trying to take over, potentially resulting in a trend switch to bullish .
-> We also check if this bar is below the three alligator lines to avoid false positives.

A bearish (red) divergent bar, signals a trend switch from bull -> bear
-> The current bar has a higher high than the previous bar, but closes in the lower half of the candle.
-> This means the bears are pushing the price down and are taking over, potentially resulting in a trend switch to bearish .
-> We also check if this bar is above the three alligator lines to avoid false positives.

Best used in combination with the Bill Williams Alligator indicator.
 
Last edited by a moderator:

diazlaz

Well-known member
2019 Donor
VIP
@samiranadhikari - here you go, can you take it for a spin and see if this works for you.
Most Updated Version:
Ruby:
#Divergent Bar from MotiveWave V1.2
# @samiranadhikari request
#
# 2019.12.13 @diazlaz - Bug fix on prevLow and prevHigh
# 2019.12.13 @diazlaz - logic change/feedback from @EMMA
# 2019.12.06 @diazlaz - initial port/interpretation.

def prevLow = Low[1];
def prevHigh = High[1];
def currentAverage = (High + Low) / 2;

def buy = (Low < prevLow AND Close > currentAverage);
def sell = (High > prevHigh AND Close < currentAverage);

def sState = if buy then 100 else if sell then -100 else sState[1];

# ARROWS
input showArrows = yes;
plot pUP = showArrows  and buy;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = showArrows  and sell;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if showColorBars then
if sState > 0 then Color.GREEN
  else Color.RED
else
  COLOR.CURRENT
);

#LABELS
input showLabels = yes;
AddLabel(showLabels, "Buy",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == 100 then COLOR.GREEN else COLOR.DARK_GRAY);

AddLabel(showLabels, "Sell",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == -100 then COLOR.RED else COLOR.DARK_GRAY);

#END of Divergent Bar from MotiveWave V1.2
 
Last edited by a moderator:
DJ864Oz.png
 

Buckbull

Active member
Where can I find info about what this study does ? and am I supposed to see a line and arrows or just arrows ?
 
Last edited:
I would love to see a few indicators aligned in the same direction. This helps me to think in terms of probability of success. However, everything can change with a disappointing market moving event. Having said that, odds are more in favor to rely on the finding of divergent bar supported by 50 day EMA or Anchored VWAP or DMI crossover on a larger time frame.
 

EMMA

New member
DJ864Oz.png

Thanks @diazlaz ! I have been looking for this indicator for a while, so glad to find it here! But after imported it into tos, I found some of the bars are not actually divergent bars. I marked a few bars on the picture that @samiranadhikari uploaded. The lows of the first two bars are not lower than the previous bar, and the high of the third bar is not higher than the previous bar. I am new to thinkscript, can't find any bug in the code. Would someone please take a look? Thanks in advance!

Here is the definition I found for divergent bars: Divergent Bars (Bullish, the current bar must have lower low than previous bar AND current bar must close in the upper half) (Bearish, The current bar must have a higher high than previous bar AND current bar must close in the lower half).

@Buckbull , here is some information https://www.tradingview.com/script/lLgCdjag-Bill-Williams-Divergent-Bars/
 
Last edited:

diazlaz

Well-known member
2019 Donor
VIP
Hi @EMMA,
Most Up-To-Date version:
Here you go! - Its interesting, i'm noticing differences in the candle price data from ToS and Trading View. Some of the candles have different values, but overall, It looks like it's working correctly. This observation on the data differences is when looking at it in the 1 minute candle.

Original:
SGOz4io.png


Port:
P0pWHLs.png


Code:

Ruby:
# Bill William Bull/Bear divergent bars
# See: Book, Trading Chaos by Bill Williams
#
# https://www.tradingview.com/script/lLgCdjag-Bill-Williams-Divergent-Bars/
# Author: polyclick
#
# A bullish (green) divergent bar, signals a trend switch from bear -> bull
# The current bar has a lower low than the previous bar, but closes in the
# upper half of the candle.
# This means the bulls are pushing from below and are trying to take over,
# potentially resulting in a trend switch to bullish.
# We also check if this bar is below the three alligator lines to avoid false positives.
#
# A bearish (red) divergent bar, signals a trend switch
# from bull -> bear
# The current bar has a higher high than the previous bar, but closes in
# the lower half of the candle.
#
# This means the bears are pushing the price down and are taking over,
# potentially resulting in a trend switch to bearish.
# We also check if this bar is above the three alligator lines to avoid false positives.
#
#
# @@EMMA request
#
# 2019.12.13 @diazlaz - logic bug fixes.
# 2019.12.13 @diazlaz - initial port/interpretation.
#

# LOGIC
def price = hl2;
def jawLength = 13;
def teethLength = 8;
def lipsLength = 5;
def jawDisplace = -8;
def teethDisplace = -5;
def lipsDisplace = -3;
def averageType = AverageType.WILDERS;

def lips = MovingAverage(averageType, price[-lipsDisplace], lipsLength);
def jaw = MovingAverage(averageType, price[-jawDisplace], jawLength);
def teeth = MovingAverage(averageType, price[-teethDisplace], teethLength);

def bullDivSignal = low  < low[1]  and close > hl2 and high < lips and high < teeth and high < jaw;
def bearDivSignal = high > high[1] and close < hl2 and low  > lips and low  > teeth and low  > jaw;
def sState = if bullDivSignal then 100 else if bearDivSignal then -100 else sState[1];

# ARROWS
input showArrows = yes;
plot pUP = showArrows  and bullDivSignal;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = showArrows  and bearDivSignal;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);#

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if showColorBars then
if sState > 0 then Color.GREEN
  else Color.RED
else
  COLOR.CURRENT
);

#LABELS
input showLabels = yes;
AddLabel(showLabels, "Buy",
if isNaN(sState) then COLOR.DARK_GRAY else
If bullDivSignal then COLOR.GREEN else COLOR.DARK_GRAY);

AddLabel(showLabels, "Sell",
if isNaN(sState) then COLOR.DARK_GRAY else
If bearDivSignal then COLOR.RED else COLOR.DARK_GRAY);

# END OF Bill William Bull/Bear divergent bars
 
Last edited by a moderator:

mc01439

Well-known member
2019 Donor
VIP
Looks like the code did not include "showArrows in the pUP and pDOWN. Below is with showArrows added to the plot code.

Code:
# ARROWS
input showArrows = no;
plot pUP = showArrows and bullDivSignal;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = showArrows and bearDivSignal;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);#
 

dolomick

Member
Shouldn't Tesla have triggered on 1/16 on a daily chart? It did for the Simpler trading moderator who defines a divergent bar the way it is defined in the header of the tradingview port version on post #17. Can someone take a look and see if I am wrong somehow? I would like a version that matches what they are doing at Simpler ideally. Thanks....

Okay I figured it out - the Simpler Trading version doesn't use the alligator lines to help confirm the signal, once I removed that part of the code it matches the Simpler trading one.
 
Last edited by a moderator:

J-Fearless

New member
I just wanted to mention that this repaints. It's honestly not a huge problem since if you take the signals in the trend direction it has a very high success rate, but thought I'd mention. It repaints both the arrows and the candle color. I had two consecutive 15m bearish candles this morning on AMD. Now that AMD is breaking out nearly an hour later those are both green and the red arrows are gone. This is excellent for suggesting a trade though and I like it a lot, but be careful looking at it historically. It only really has failed me so far on a counter trend move, which are tricky to handle anyway unless that's your thing.

My above statement applies to the code in the second post
 
Last edited by a moderator:

Cindy

New member
@samiranadhikari - here you go, can you take it for a spin and see if this works for you.

Ruby:
#Divergent Bar from MotiveWave
# @samiranadhikari request
#
# 2019.12.06 @diazlaz - initial port/interpretation.
#

def prevLow = Low[-1];
def prevHigh = High[-1];
def currentAverage = (High + Low) / 2;

def buy = (Low < prevLow AND Close > currentAverage);
def sell = (High > prevHigh AND Close < currentAverage);

def sState = if buy then 100 else if sell then -100 else sState[1];

# ARROWS
input showArrows = yes;
plot pUP = sState crosses above 0;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = sState crosses below 0;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if showColorBars then
if sState > 0 then Color.GREEN
  else Color.RED
else
  COLOR.CURRENT
);

#LABELS
input showLabels = yes;
AddLabel(showLabels, "Buy",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == 100 then COLOR.GREEN else COLOR.DARK_GRAY);

AddLabel(showLabels, "Sell",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == -100 then COLOR.RED else COLOR.DARK_GRAY);

#END of Divergent Bar from MotiveWave
Thanks for this code. How can I set up a scan in TOS? In the custom study, what should I make pUp equal to? Thanks!
 

BenTen

Administrative
Staff member
Staff
VIP
The scan would be: pUp is true.
 
Last edited by a moderator:

DeeO

New member
How to print Divergent bar colors in current playing bar rather than at the start of new bar? Example: In daily time frame chart, i am not getting the divergent bar confirmation until the next day open. Is there a fix to it?
 

Cindy

New member
How to print Divergent bar colors in current playing bar rather than at the start of new bar? Example: In daily time frame chart, i am not getting the divergent bar confirmation until the next day open. Is there a fix to it?
I am NOT an expert on TOS but I tweaked one of the scripts form this forum so that I can get 4 colors on the divergent bars. I set Blue as a Bullish buy and pink as a bearish sell. I hope this helps.

Code:
#Divergent Bar from MotiveWave
# [USER=717]@samiranadhikari[/USER] request
#
# 2019.12.06 [USER=258]@diazlaz[/USER] - initial port/interpretation.
#
#Divergent Bar from MotiveWave V1.2
# [USER=717]@samiranadhikari[/USER] request
#
# 2019.12.13 [USER=258]@diazlaz[/USER] - Bug fix on prevLow and prevHigh
# 2019.12.13 [USER=258]@diazlaz[/USER] - logic change/feedback from [USER=1993]@EMMA[/USER]
# 2019.12.06 [USER=258]@diazlaz[/USER] - initial port/interpretation.

def prevLow = low[1];
def prevHigh = high[1];
def currentAverage = (high + low) / 2;

def buyD = (low < prevLow and close > currentAverage);
def sellD = (high > prevHigh and close < currentAverage);
def buy = (low < prevLow and close < currentAverage);
def sell = (high > prevHigh and close > currentAverage);

#def sState = if buy then 100 else if sell then -100 else sState[1];

# ARROWS
input showArrows = yes;
plot pUP = showArrows  and buy;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = showArrows  and sell;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if buyD then Color.BLUE
else
if sellD then Color.PINK
else
  Color.CURRENT
);

#COLORBARS
#input showColorBars = yes;
#AssignPriceColor(if showColorBars then
#if sState > 0 then Color.GREEN
#  else Color.RED
#else
# COLOR.CURRENT
# );




#LABELS
#input showLabels = yes;
#AddLabel(showLabels, "Buy",
#if IsNaN(sState) then Color.DARK_GRAY else
#if sState == 100 then Color.GREEN else Color.DARK_GRAY);

#AddLabel(showLabels, "Sell",
#if IsNaN(sState) then Color.DARK_GRAY else
#if sState == -100 then Color.RED else Color.DARK_GRAY);

#END of Divergent Bar from MotiveWave V1.2
 

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Similar threads

Top