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

##### Member
@tradebyday , I was actually hoping to see both the legacy EMA and Adaptive MA together on the same study....along with the divergence line in lime color. As presented, the three lines above are a bit confusing. Sorry for not being clear in my first request

Update: I used a workaround.....I added the adaptive EMA to the top study and then dragged it down to the lower study together with the legacy EMA u created. But I'm thinking, if I keep it this way it may present scaling issues.....we'll see when I run it tomorrow. From what I see in the 5 minute, it shows potential.

Last edited:
T

##### Active member
@madeinnyc here is a rough generation of it having all 3 averages (hull, EMA, and AEMA). Simply uncheck the ones you don't want plotted and change their colors. I change the divergence line to dotted for easier visual of when it crosses.
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
#
# ----------------------

declare lower;

input price = CLOSE;

input Divergencelength = 200;
input lookback = 50;

input HMAlength = 200;
input displace = 0;
input EMAlength = 100;
input AEMAlength = 100;

def HMA = HullMovingAvg(length = Divergencelength, 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 divergence = HMA - next_bar;

plot HMA2 = MovingAverage(AverageType.HULL, divergence, HMAlength)[-displace];

assert(HMAlength > 0, "'length' must be positive: " + HMAlength);

def EMA = compoundValue(1, EMA[1] + 2 / (EMAlength + 1) * (divergence[-displace] - EMA[1]), divergence[-displace]);

plot LegacyEMA = EMA;
LegacyEMA.SetDefaultColor(GetColor(1));

input highLowLength = 10;

def multiplier1 = 2 / (AEMAlength + 1);
def multiplier2 = AbsValue((close - Lowest(low, highLowLength)) - (Highest(high, highLowLength) - close)) / (Highest(high, highLowLength) - Lowest(low, highLowLength));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (divergence - ma[1]), Average(divergence, AEMAlength));

plot AEMA = ma;
AEMA.SetDefaultColor(GetColor(1));

plot cx_up = if divergence 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 divergence crosses below zero then 0 else double.nan;
cx_down.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_down.SetDefaultColor(Color.RED);
cx_down.SetLineWeight(4);

divergence.DefineColor("Up", GetColor(1));
divergence.DefineColor("Down", GetColor(0));
divergence.AssignValueColor(if divergence > LegacyEMA then divergence.color("Up") else divergence.color("Down"));
# END``````

##### Member
@tradebyday : Thank you! I modified the above code a little to show me the view format I needed for comparison purposes. I'll post the code below so u can view any changes.

What I'm interested to know pertains to the Adaptive EMA.....notice the first lower study....I had dragged down the AEMA vs. the second study where it's place right into the code. I'm almost sure the inputs are identical however, I think the first lower study AEMA may be affected by scaling issues. What are your thoughts? Do you see scaling issues between the AEMA's when comparing both?

With regards to the code, what MA does input length = 100 control? What does displace = 0 mean?

Watching the yellow divergence line cross the AEMA today was very insightful. It provided exit signals that were previously hard to see.

See Screenshot:

Revised Code:

Code:
``````declare lower;

input price = CLOSE;

input HMA_length = 34;
input lookback = 2;

input length = 100;
input displace = 0;
input EMAlength = 100;
input AEMAlength = 20;

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 divergence = HMA - next_bar;

def HMA2 = MovingAverage(AverageType.HULL, divergence, HMA_length)[-displace];

divergence.setDefaultColor(Color.LIME);
divergence.SetLineweight(2);

assert(HMA_length > 0, "'length' must be positive: " + HMA_length);

def EMA = compoundValue(1, EMA[1] + 2 / (EMAlength + 1) * (divergence[-displace] - EMA[1]), divergence[-displace]);

plot LegacyEMA = EMA;
LegacyEMA.SetDefaultColor(GetColor(1));

input highLowLength = 10;

def multiplier1 = 2 / (AEMAlength + 1);
def multiplier2 = AbsValue((close - Lowest(low, highLowLength)) - (Highest(high, highLowLength) - close)) / (Highest(high, highLowLength) - Lowest(low, highLowLength));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (divergence - ma[1]), Average(divergence, AEMAlength));

plot AEMA = ma;
AEMA.SetDefaultColor(GetColor(1));

plot cx_up = if divergence 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 divergence crosses below zero then 0 else double.nan;
cx_down.SetPaintingStrategy(PaintingStrategy.POINTS);
cx_down.SetDefaultColor(Color.RED);
cx_down.SetLineWeight(4);
# END``````

Last edited by a moderator:

##### Member
@tradebyday .......looking at the last lower study with the code above, there is definitely an error. I'm new to thinkscript coding (just learning), and I can't figure out why the AEMA is tracing different than the higher lower study. Any thoughts are appreciated, thks!

J

#### jay2

##### New member
is version 4 no longer valid? i could have sworn i saw it updated on the first page.

T

##### Active member
@madeinnyc can you post both codes so I can take a look at it? Please note which is the one you believe there to be an error in

##### Member
@tradebyday....it's the code I posted above that may have an error. It's applied to the 2nd lower study. If u compare the AEMA traces between both lower studies, there is something not right with the 2nd lower study AEMA. It may just be a scaling issue.

T

##### Active member
Code posted above seems fine to me, I think your settings are too low for use of the 2line cross aspect. Try putting HMA above 100 and AEMA anything above 50 and it should look better, just for a visual reference

S

#### Schminga

##### New member
I had the same issue. Make sure your script matches the same time frame as watchlist. I found some of my watchlist were calculating close and not H/L 2. I believe the backrest showed results of daily with close.
I have personally found good signals using 21/2 on all time frames.
kshires4: Can you please provide the script for a Thinkorswim Watchlist for your Hull Moving Avg Turning Points which shows the different colors of light green, dark green, red and orange? Of particular interest to me is the orange arrows sell/buy arrows (as a suggestion, change these colors to red and green). Need and example? Look no further than Welkin's example at this link... https://usethinkscript.com/threads/obv-histogram-with-paintbars-for-thinkorswim.1668/#post-24293