Hull Moving Average Turning Points and Concavity (2nd Derivatives)

I

ikpllc

New member
@mashume Sorry to bother, one more question. How can I make this not repaint?
 
M

mashume

Active member
VIP
Sorry to bother, one more question. How can I make this not repaint?
The repainting issue has been discussed at length earlier in this thread.

TL;DR
It will repaint.
You can, perhaps, minimize the repainting by using HL2 and not CLOSE or HLC3 (or even OHLC4) because it doesn't need to wait for a close and highs and lows are often (but not always) set before the end of a bar's time window.

-mashume
 
I

ikpllc

New member
The repainting issue has been discussed at length earlier in this thread.

TL;DR
It will repaint.
You can, perhaps, minimize the repainting by using HL2 and not CLOSE or HLC3 (or even OHLC4) because it doesn't need to wait for a close and highs and lows are often (but not always) set before the end of a bar's time window.

-mashume
Again, thanks much.
 
E

ext99k

Member
@mashume Would really appreciate it if you can please help with the scan code when there's an inflection (white) point on the chart, would be greatly appreciated! Thanks.
 
T

Tiredoflosing

New member
VIP
Hi @kelso,

Im also interested in Ninja trader 8, would you be open to sharing what you have translated over ? I would really appreciate it.
 
D

DirtyGoats

New member
Good Day All. First time poster. Couple months as a lurker.

What an awesome thread! Starting from page 1 and reading through everything has been very beneficial.

I'm a ToS user and solely focus on the SPY trading options (simple Calls and Puts...nothing fancy because i'm still learning).

I've been following the BlackFlagSwingArms thread for some time, learning, and I was wanting to learn more about the Hull Moving average that was part of that strategy. In searching this forum it was pretty cool to find Jose's posts within this thread and learning how that code came to life. Thank you @mashume .

The question I would like to ask everyone is:
When does one use the "extended hours" on their charts? Or what makes one use the "extended hours"?

I've never really come across a clear answer/opinion.

Appreciate everyone's contribution,

~DG
 
moutic777

moutic777

New member
VIP
Thanks. I was also playing with the HMA. But was not able to write the addchartbubble because my limited scripting skill. If anyone can just poste the addchartbubble code for different pivoting points would be great. Appreciate your help.
 
axlerod

axlerod

Member
@mashume So idk why this isn't working but the scan is not working. Simple example SCAN for BLUE ARROW within 2 bars.. Here's the code everything is defined YET it still is not working....

Here's the code and see screenshot.

I defined EVERYTHING in the indicator like you should in the scan and it still is not working... what am I doing wrong been banging my head on the table and can not figure this out. Plz help and send the full code revised back.

you can edit the code here with codeshare - https://codeshare.io/21jlL3

Code:
input price = HL2;
input HMA_Length = 55;
input lookback = 2;

def HMA = HullMovingAvg(price = price, length = HMA_Length);
def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = HMA[1] + delta_per_bar;
def concavity = if HMA > next_bar then 1 else -1;
Def turning_point = if concavity[1] != concavity then 1 else 0;

Def MA_Max = if HMA[-1] < HMA and HMA > HMA[1] then 1 else 0; # white squares
Def MA_Min = if HMA[-1] > HMA and HMA < HMA[1] then 1 else 0; # white triangles
Def sell = if turning_point and concavity == -1 then 1 else 0; # orange arrow down
Def buy = if turning_point and concavity == 1 then 1 else 0; # blue - cyan arrow up
def divergence = HMA - next_bar;

def scan = if

turning_point and concavity == 1

then 1 else 0 ;

plot sc = scan within 1 bars ;

 
M

mashume

Active member
VIP
@axlerod Perhaps, and I'm really no expert on the internals of ThinkScript by any stretch of the imagination, when you define scan you give it a value of either 1 or 0, both of which are values. In the plot for sc, scan may be returning true any time scan has a value, which is always.

Can you try either
A) then 1 else double.nan; as the then clause in def scan
or
B) plot sc = scan == 1 within 1 bars; and explicitly define sc as only when scan is 1

-mashume
 
MerryDay

MerryDay

Member
VIP
@axlerod Because of the look-back/look-forward nature, the variable actually fired 2 candles ago so you have to scan within 2 bars.
 
UsDollarClub

UsDollarClub

New member
@mashume THIS IS AMAZING! Sorry I can see this almost a year late, lol. Please share with me the updated shareable thinkscript link and colored watchlist link too, And since I use Fibonacci extensions to determine my take profit levels, is it possible to plot them on the code too?

the code wont work for me when trying to creat cusytom watchlist , i see an error highlighted in red, plot data=concavityDivergence () .divergence * 10000 help. Thannks
 
axlerod

axlerod

Member
@MerryDay Yes imma DM you idk how to on this see messages at top of my screen new to this usethinkscript hhaa but plz contact me.

@mashume Also is there a reason for using == 1 instead of just doing concavity = 1 ? whats the difference...
 
G

Ginu09

Member
Hi @mashume, excellent study. I've been using it extensively the past week. I have one question - is there a way to also plot a higher timeframe HMA so as to be able to make a judgement on a lower timeframe on whether to stay in a trade longer? Even as a lower study with a series of dots with the same colours so as to be able to quickly view the state without having to switch the chart. What do you think?

I've been overlaying a higher timeframe HMA on my chart using a higher aggregationPeriod on the price but it's very choppy and very difficult to follow.
 
surferdude

surferdude

New member
VIP
Hi @mashume, excellent study. I've been using it extensively the past week. I have one question - is there a way to also plot a higher timeframe HMA so as to be able to make a judgement on a lower timeframe on whether to stay in a trade longer? Even as a lower study with a series of dots with the same colours so as to be able to quickly view the state without having to switch the chart. What do you think?

I've been overlaying a higher timeframe HMA on my chart using a higher aggregationPeriod on the price but it's very choppy and very difficult to follow.
Try this: https://usethinkscript.com/threads/...and-concavity-2nd-derivatives.1803/post-26455
 
T

thebewb

New member
VIP
@mashume excellent work on this. Very intriguing to me and something I have been imitating just by checking one bar back for MAs... which obviously has its issues. I added the following code to the lower Divergence study... very basic no frills that shows two standard deviation bands. It is interesting to look at as a lower study. It seems to offer the best clues of a turning point when the Std Dev bands are not compressed and the divergence is close to its historical highs (obvious), but still I wonder if it could be used to fine tune this study. This is going to be my main focus going forward.

Also, you mentioned a mathematically intensive way to make the original Hull Concavity script faster by solving for the unknown variable using the source math equation for the Hull MA. Could you expand on that a bit? Much appreciated.

I am trying to figure out a way to add it and/or the Hull script to my proprietary algo script to get better entrances... will keep everyone updated on it. See below:

Code:
#
# Hull Moving Average Concavity Divergence
#  or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mahsume)
# Version: 2020-02-23 V3
#
# This code is licensed (as applicable) under the GPL v3
#
# ----------------------
# TheBewb - Added Standard Deviation bars for divergence amounts.
declare lower;

input price = OPEN;



input HMA_length = 34;
input lookback = 2;

def HMA = HullMovingAvg(length = HMA_length, price = price);

def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;

def next_bar = HMA[1] + delta_per_bar;

def concavity = if HMA > next_bar then 1 else -1;

plot zero = 0;
zero.setdefaultcolor(color.gray);
zero.setpaintingstrategy(PaintingStrategy.DASHES);

plot diffave = HMA - next_bar;
diffave.setdefaultColor(getColor(5));
diffave.SetPaintingStrategy(PaintingStrategy.line);
diffave.SetLineWeight(3);
diffave.DefineColor("Positive and Up", Color.green);
diffave.DefineColor("Positive and Down", Color.dark_green);
diffave.DefineColor("Negative and Down", Color.dark_red);
diffave.DefineColor("Negative and Up", Color.red);
diffave.AssignValueColor(if diffave >= 0 then if diffave > diffave[1] then diffave.Color("Positive and Up") else diffave.Color("Positive and Down") else if diffave < diffave[1] then diffave.Color("Negative and Down") else diffave.Color("Negative and Up"));

plot cx_up = if diffave crosses above zero then 0 else double.nan;
cx_up.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_up.SetDefaultColor(Color.LIGHT_GREEN);
cx_up.SetLineWeight(4);

plot cx_down = if diffave crosses below zero then 0 else double.nan;
cx_down.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_down.SetDefaultColor(Color.RED);
cx_down.SetLineWeight(4);

input stddev_len = 21;
def divergence_stddev = StandardDeviation(price = diffave, length = stddev_len);
plot div_StdDev = divergence_stddev;
plot div_StdDevNeg = -divergence_stddev;
plot div_StdDev2 = divergence_stddev*2;
plot div_StdDevNeg2 = -divergence_stddev*2;
 
D

Daughters Keeper

New member
@mashume I can't figure out which one I took this part out of, but it's super accurate. I have used it in 1 to 5 minute time frames. I use Ma Min for buys and Ma Max for sells. I tried to set onscreen alerts fort those buy and sell spots but it doesn't work. Maybe you could take a look at it?

Code:
#
# Hull Moving Average Concavity and Turning Points
#  or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mahsume)
# Version: 2020-05-01 V4
#
# Now with support for ToS Mobile
#
# This code is licensed (as applicable) under the GPL v3
#
# ----------------------


declare upper;

input price = HL2;
input HMA_Length = 55;
input lookback = 2;

# I read somewhere that it's faster to define nan's and then use the def'd var rather than call double.nan every time.
def nan = double.nan;

plot HMA = HullMovingAvg(price = price, length = HMA_Length);

def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;

def next_bar = HMA[1] + delta_per_bar;

def concavity = if HMA > next_bar then 1 else -1;

plot turning_point = if concavity[1] != concavity then HMA else nan;

HMA.AssignValueColor(color = if concavity[1] == -1 then
    if HMA > HMA[1] then color.dark_orange else color.red else
    if HMA < HMA[1] then color.dark_green else color.green);

HMA.SetLineWeight(3);

turning_point.SetLineWeight(4);
turning_point.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
turning_point.SetDefaultColor(color.white);

plot MA_Max = if HMA[-1] < HMA and HMA > HMA[1] then HMA else NaN;
MA_Max.SetDefaultColor(Color.WHITE);
MA_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);
MA_Max.SetLineWeight(3);

plot MA_Min = if HMA[-1] > HMA and HMA < HMA[1] then HMA else Nan;
MA_Min.SetDefaultColor(Color.WHITE);
MA_Min.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
MA_Min.SetLineWeight(3);

# NOTE: I PREFER TO TURN OFF UP ARROWS WHEN IN DOWN TREND.  IF IN DOWNTRENDING SWINGARM, THE TURN ON ONLY DOWN SELL ARROWS. YOU CAN DO THIS USING THE INPUT SETTINGS SCREEN.

plot sell = if turning_point and concavity == -1 then high else nan;
sell.SetDefaultColor(Color.DARK_ORANGE);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(3);

plot buy = if turning_point and concavity == 1 then low else nan;
buy.SetDefaultColor(Color.CYAN);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetLineWeight(3);

def divergence = HMA - next_bar;

addLabel(yes, concat("DIVERGENCE: " , divergence), color = if concavity < 0 then if divergence[1] > divergence then Color.RED else color.PINK else if divergence[1] < divergence then color.green else color.yellow);

###################
#
# ALERTS
#
###################

Alert(condition = MA_Min, text = "Buy", "alert type" = Alert.BAR, sound = Sound.Bell);

Alert(condition = MA_Max, text = "Sell", "alert type" = Alert.BAR, sound = Sound.Chimes);

###################
#
# 2020-05-01
#
# MOBILE TOS SUPPORT
#
# Each color of the HMA needs to be a separate plot as ToS Mobile
# lacks the ability to assign colors the way ToS Desktop does.
# I recommend a plain colored HMA behind the line
# Set the line color of the HMA above to gray or some neutral
#
# CCD_D -> ConCave Down and Decreasing
# CCD_I -> ConCave Down and Increasing
# CCU_D -> ConCave Up and Decreasing
# CCU_I -> ConCave Up and Increasing
#
###################
plot CCD_D = if concavity == -1 and HMA < HMA[1] then HMA else nan;
CCD_D.SetDefaultColor(Color.RED);
CCD_D.SetLineWeight(3);

plot CCD_I = if concavity == -1 and HMA >= HMA[1] then HMA else nan;
CCD_I.SetDefaultColor(Color.DARK_ORANGE);
CCD_I.SetLineWeight(3);

plot CCU_D = if concavity == 1 and HMA <= HMA[1] then HMA else nan;
CCU_D.SetDefaultColor(COLOR.DARK_GREEN);
CCU_D.SetLineWeight(3);

plot CCU_I = if concavity == 1 and HMA > HMA[1] then HMA else nan;
CCU_I.SetDefaultColor(COLOR.GREEN);
CCU_I.SetLineWeight(3);
 
T

thebewb

New member
VIP
@mashume excellent work on this. Very intriguing to me and something I have been imitating just by checking one bar back for MAs... which obviously has its issues. I added the following code to the lower Divergence study... very basic no frills that shows two standard deviation bands. It is interesting to look at as a lower study. It seems to offer the best clues of a turning point when the Std Dev bands are not compressed and the divergence is close to its historical highs (obvious), but still I wonder if it could be used to fine tune this study. This is going to be my main focus going forward.

Also, you mentioned a mathematically intensive way to make the original Hull Concavity script faster by solving for the unknown variable using the source math equation for the Hull MA. Could you expand on that a bit? Much appreciated.

I am trying to figure out a way to add it and/or the Hull script to my proprietary algo script to get better entrances... will keep everyone updated on it. See below:

Code:
#
# Hull Moving Average Concavity Divergence
#  or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mahsume)
# Version: 2020-02-23 V3
#
# This code is licensed (as applicable) under the GPL v3
#
# ----------------------
# TheBewb - Added Standard Deviation bars for divergence amounts.
declare lower;

input price = OPEN;



input HMA_length = 34;
input lookback = 2;

def HMA = HullMovingAvg(length = HMA_length, price = price);

def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;

def next_bar = HMA[1] + delta_per_bar;

def concavity = if HMA > next_bar then 1 else -1;

plot zero = 0;
zero.setdefaultcolor(color.gray);
zero.setpaintingstrategy(PaintingStrategy.DASHES);

plot diffave = HMA - next_bar;
diffave.setdefaultColor(getColor(5));
diffave.SetPaintingStrategy(PaintingStrategy.line);
diffave.SetLineWeight(3);
diffave.DefineColor("Positive and Up", Color.green);
diffave.DefineColor("Positive and Down", Color.dark_green);
diffave.DefineColor("Negative and Down", Color.dark_red);
diffave.DefineColor("Negative and Up", Color.red);
diffave.AssignValueColor(if diffave >= 0 then if diffave > diffave[1] then diffave.Color("Positive and Up") else diffave.Color("Positive and Down") else if diffave < diffave[1] then diffave.Color("Negative and Down") else diffave.Color("Negative and Up"));

plot cx_up = if diffave crosses above zero then 0 else double.nan;
cx_up.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_up.SetDefaultColor(Color.LIGHT_GREEN);
cx_up.SetLineWeight(4);

plot cx_down = if diffave crosses below zero then 0 else double.nan;
cx_down.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_down.SetDefaultColor(Color.RED);
cx_down.SetLineWeight(4);

input stddev_len = 21;
def divergence_stddev = StandardDeviation(price = diffave, length = stddev_len);
plot div_StdDev = divergence_stddev;
plot div_StdDevNeg = -divergence_stddev;
plot div_StdDev2 = divergence_stddev*2;
plot div_StdDevNeg2 = -divergence_stddev*2;
@mashume Seth, broski... can you help me out on the in depth math required to make this indicator more accurate? Just point me in the right direction as to what you were inferring. Thanks!
 
M

mashume

Active member
VIP
@mashume Seth, broski... can you help me out on the in depth math required to make this indicator more accurate? Just point me in the right direction as to what you were inferring. Thanks!
I've got a conference all week this week... presenter Thursday. I may be able to get back to this stuff at the weekend or so.
-mashume
 

Similar threads

Top