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

K

kelso

New member
VIP
Dear @mashume,

Is there a definition to a curve up of the HMA line within the study? I'm trying to figure a way to exclude stocks that are curving down.
 
M

mashume

Active member
VIP
Dear @mashume,

Is there a definition to a curve up of the HMA line within the study? I'm trying to figure a way to exclude stocks that are curving down.
If you're up for digging into the code, the thing you're looking for is the concavity variable. it's 1 when concavity is upward and -1 when downward. It's defined rather than plotted as the code reads now, but you can change that if you want to use it in a scanner. You'd want to include something along the lines of:
Code:
...
and Concavity().concavity == 1
...
This is, of course, not code that will run but should give you an idea of how to implement the condition. You can only use PLOT lines from functions in scans, not defined variables, which is why you'd need to make it 'plot' to use it in a scan.

-mashume
 
murkr

murkr

Member
VIP
We all have favorite MAs, so, I've been running your script with HMA and EHMA, EHMA seems to lead by 1-2 bars, rarely they give opposing signals, but when they both agree on a bar, its like a NASA ignition. Using a setting of H+L/2 and 8,2 on a 1 day, 2min chart.

I think the wide swings many experience in current conditions can be decreased by using a simple trailing stop system and with such low commissions, maybe it makes sense to take smaller peices of the pie and let them add up.
@codydog Are you still using this with the EHMA?
 
K

kelso

New member
VIP
Hi @mashume
I'd love to take this indicator and 'translate' it to NinjaTrader if you don't mind. In order for me to do so, can you please clarify the math behind it? here's what I learned from other replies in this thread, can you confirm or correct me please?

High level explanation of the process:
1. calculate the slope of a straight line between the value n bars ago and the previous bar
2. project a line on that slope from the previous bar
3. compare the value of the current bar HMA to the projected value
4. if the value from step 3 is negative, and the value from step 3 last time around was negative, there is no change of concavity. Otherwise, it has changed and a signal is generated.

Math:
def delta = HMA[1] - HMA[lookback + 1];
// ^ this line takes the 3rd from last calculated HMA value and subtracts it from last HMA value. so if HMA was [4,6,10], delta would be 6
def delta_per_bar = delta / lookback;
// ^ here we will take 6 and divide it by lookback (2). Our delta_per_bar here is 6/2 = 3
def next_bar = HMA[1] + delta_per_bar;
// ^ next HMA value (one we are predicting) is 10+3 = 13
def concavity = if HMA > next_bar then 1 else -1;
// ^The trick part of this comes by looking to see if the value of the HMA for the current bar is above or below the predicted (future projection of last HMA + the calculated slope as a change per bar. In the illustration, if the shaded region between the red dash and the black HMA is red, we're below the value predicted and the concavity is downward. If it's green, we're above the projected value and concavity is upward. The other calculations are simply to decide if the general trend is upward or downward (check the original image I sent for visual representation.

Have I missed any other calculation?

Thanks again, this indicator is amazing.
 
M

mashume

Active member
VIP
@kelso,

you wrote:
Hi @mashume
...

Have I missed any other calculation?
...
Nope. That's about the size of it. It's not magic, just some math. ;-)

good luck with the ninja trader port. Let me know how the signals compare.
I wonder, as i read what you've reverse-engineered, whether the lookback should really be adjusted, or not. It may still be a legacy of the earliest version -- an artifact. It's certainly correctly compensated for in the calculation, but I wonder at the mathematical necessity now.

-mashume
 
E

ext99k

Member
@mashume do u think i can pay you to create some scans for me? thanks :)
 
Z

Zoomer

New member
VIP
@mashume - I have been using your concavity indicators for the last couple of months and have made some very good results with it. Thanks a lot for it as it has helped me a lot with my trade entry decisions. I am mostly focussed on long entries currently. For my trades, the best ones have been entries when concavity turns positive (dark green) AND the underlying crosses or is above the MA. I have a list of stocks and use the watchlist code you provided to look at their current concavity levels - this however does slow down TOS when moving from one watchlist to another. Would really appreciate it you could provide a scan that looks for +ve concavity (ideally just dark green MA) and where stock price is above the MA? This will greatly help reduce the watchlist with few number of stocks. Thanks a lot again for this really useful indicator.
 
E

ext99k

Member
I cannot agree more @Zoomer. I think a scan would be best and provide us an opportunity to dig deeper on securities that meet the criteria. If possible, scan code that supports the different color levels would be ideal to apply to the most people, if someone could please produce it that would be greatly appreciated. I would if I knew how to code, thanks! :)
 
M

mashume

Active member
VIP
@Zoomer and @ext99k ,

This should get you started. It relies on the mobile-capable (v4 I think) version of the indicator.
Code:
# Dark Green
Concavity("price" = HL2, "hma length" = 55)."CCU_D" is true

# Light Green
# or
# Concavity("price" = HL2, "hma length" = 55)."CCU_I" is true

# Orange
# or
# Concavity("price" = HL2, "hma length" = 55)."CCD_I" is true

# RED
# or
# Concavity("price" = HL2, "hma length" = 55)."CCD_D" is true

# CLOSE vs HMA
and
CLOSE > HullMovingAvg(HL2, 55)
Comment out the bits you don't want to scan for. As it's commented now it scans for dark green and close > hma.

-mashume
 
E

ext99k

Member
Thanks for your help @mashume! I tried to paste the code alone into the scan, along with the V4 code in the scan but there's an error: Invalid statement: Concavity

Would appreciate some help as I'm not a programmer, thanks again
 
M

mashume

Active member
VIP
Thanks for your help @mashume! I tried to paste the code alone into the scan, along with the V4 code in the scan but there's an error: Invalid statement: Concavity

Would appreciate some help as I'm not a programmer, thanks again
Can you post up the complete code of your scan and I'll try to sort out where it went wrong
 
Z

Zoomer

New member
VIP
@mashume - This is great. Works really well. Thanks a lot - this will save me a lot of time manually looking at the charts.

@ext99k This is what I have done.
1. https://tos.mx/NXqbYE9 - ver 4 - save it - make sure you name the study as Concavity
2. In the Scan - Only paste the code that mashume provided above (not the entire study). If you only want Light Green in the scan, comment out the 2nd line under Dark Green (#Concavity("price" = HL2, "hma length" = 55)."CCU_D" is true)

It should work
 
Last edited by a moderator:
E

ext99k

Member
Thank you @Zoomer and @mashume the problem was that I had named the custom filter something different, got it to work! appreciate everyone's help on the scan, it will save us all so much time! Cheers!

@mashume thanks for your help, can you possibly provide the scan code we can add to identify turning points as well? Thanks!
 
Last edited:
J

jnitro

New member
VIP
@kelso +1 for the Ninjatrader port. I'm interested as well.

@mashume this indicator is awesome, thanks!
 
T

topmarx

New member
VIP
Has anyone done a conversation to TradingView, had a browse didn't see anything but thought would ask in case?
 
I

ikpllc

New member
If you're up for digging into the code, the thing you're looking for is the concavity variable. it's 1 when concavity is upward and -1 when downward. It's defined rather than plotted as the code reads now, but you can change that if you want to use it in a scanner. You'd want to include something along the lines of:
Code:
...
and Concavity().concavity == 1
...
This is, of course, not code that will run but should give you an idea of how to implement the condition. You can only use PLOT lines from functions in scans, not defined variables, which is why you'd need to make it 'plot' to use it in a scan.

-mashume
Hello...any idea how to add alerts to this?

input price = HL2;
input HMA_Length = 21;
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 HMA else Double.NaN;

AddOrder(condition = turning_point and concavity == -1, tickcolor = Color.white, arrowcolor = Color.green, name = "LONG", tradeSize = 1, price = open[-1]);


AddOrder(OrderType.SELL_TO_CLOSE, turning_point and concavity == 1, tickcolor = Color.white, arrowcolor = Color.red, name = "SHORT", tradeSize = 1, price = open[-1]);
 
I

ikpllc

New member
I'm using part of the awesome Concavity study, but I'm not sure how to add alerts to it? Please see below. Each time a LONG or SHORT appears, I'd like an alert. Any thoughts? Thanks

Code:
input price = HL2;
input HMA_Length = 21;
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 HMA else Double.NaN;

AddOrder(condition = turning_point and concavity == -1, tickcolor = Color.white, arrowcolor = Color.green, name = "LONG", tradeSize = 1, price = open[-1]);


AddOrder(OrderType.SELL_TO_CLOSE, turning_point and concavity == 1, tickcolor = Color.white, arrowcolor = Color.red, name = "SHORT", tradeSize = 1, price = open[-1]);
 
M

mashume

Active member
VIP
from:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/Alert
Code:
def buy_condition = turning_point and concavity == -1;
Alert(buy_condition, "Buy!", Alert.ONCE,  Sound.Chimes);

def sell_condition = turning_point and concavity == 1;
Alert(sell_condition, "Get Out!", Alert.ONCE,  Sound.Chimes);
perhaps. You may want to change sounds, or read about the various Alert types at the link above.

I typed this here in the browser, not in thinkorswim... there may be typos.

-mashume
 
I

ikpllc

New member
from:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/Alert
Code:
def buy_condition = turning_point and concavity == -1;
Alert(buy_condition, "Buy!", Alert.ONCE,  Sound.Chimes);

def sell_condition = turning_point and concavity == 1;
Alert(sell_condition, "Get Out!", Alert.ONCE,  Sound.Chimes);
perhaps. You may want to change sounds, or read about the various Alert types at the link above.

I typed this here in the browser, not in thinkorswim... there may be typos.

-mashume
Nice, thanks sir! I'll try it and let you know.
 

Similar threads

Top