Convert Divergent Bar from MotiveWave to thinkscript?

  • Thread starter samiranadhikari
  • Start date
samiranadhikari

samiranadhikari

Member
I would love to see thinkscript for divergent bar. The following script describes the code for motivewave

Code:
//currentAverage = midpoint = (high+low)/2previous
//prev = previous
//LT = lessThan, index = current bar number
//MT = moreThan
prevLow = Low[index-1]);
prevHigh = High[index-1];
currentAverage = (High + Low) / 2;
//Signals
buy = (Low LT prevLow AND Close MT currentAverage);
sell = (High MT prevHigh AND Close LT currentAverage);
 
D

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.

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
 
samiranadhikari

samiranadhikari

Member
AAPL 4 hourly Chart with Anchored VWAP and Divergent Bar Indicator during 1st Week of December, 2019.

 
Last edited by a moderator:
J

johntermotto

New member
2019 Donor
Wow, great indicator! John Carter's team at Simpler Trading is trying to sell this exact indicator for $397. You guys just saved the community a ton of $'s. Thanks!
 
B

Buckbull

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:
samiranadhikari

samiranadhikari

Member
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.
 
E

EMMA

New member

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:
D

diazlaz

Well-known member
2019 Donor
VIP
@EMMA I will look at the tradingview version and will port it over the weekend.
 
D

diazlaz

Well-known member
2019 Donor
VIP
Hi @EMMA,

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:


Port:


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 - initial port/interpretation.
#

# LOGIC
def lips = if(isNaN(lips[1]), Average(hl2, 5), (lips[1] * (5 - 1) + hl2) / 5 );
def teeth = if(isNaN(teeth[1]), Average(hl2, 8), (teeth[1] * (8 - 1) + hl2) / 8 );
def jaw = if(isNaN(jaw[1]), Average(hl2, 13), (jaw[1] * (13 - 1) + hl2) / 13 );

def _lips = lips[3];
def _teeth = teeth[5];
def _jaw = jaw[8];

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 = bullDivSignal;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = 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
When I compare the 1 day / 1 Year chart with trading view, the chart is identical, seems the intra day data feeds are different in Trading View versus ToS at least when comparing it in 1 MIN timeframe.
 
Last edited:
D

diazlaz

Well-known member
2019 Donor
VIP
Hi @EMMA,

I made a small logic change and please let me know if this makes any difference in the original port.

Ruby:
#Divergent Bar from MotiveWave V1.1
# @samiranadhikari request
#
# 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.1
 
Last edited:
E

EMMA

New member
@diazlaz , thank you for the prompt response! I installed and tested both the second and third versions. version 2 misses some signals and version 3 gives some wrong signals. I highlighted some bars with yellow dots. I will look into the codes when I have time this weekend.

Have a good weekend!


 
D

diazlaz

Well-known member
2019 Donor
VIP
thanks @EMMA I think I found a bug and updated, can you please check this 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:
D

diazlaz

Well-known member
2019 Donor
VIP
rebuild the logic of the tradingview port. 100% match to the trading view now:

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:
E

EMMA

New member
thanks @EMMA I think I found a bug and updated, can you please check this 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 = buy;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = 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
This version looks perfect! Thank you so much!!
 
samiranadhikari

samiranadhikari

Member


I find the initial version (motivewave) is better correlated to VWAP Z score. Left panel shows 1st version.

version 1.2 is great (right panel) or equivalent to Motivewave version (left)

 
mc01439

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);#
 

Similar threads

Top