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

Repaints
Status
Not open for further replies.

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@tradegeek oh okay, are you using a tick chart or 1 min? and do you leave your hull length @ 20?
Also is this the swing high lo indicator you were referring to?

I use both tick and time charts but for larger time-frames or tick, I wait for the price to pull back enough so it's within my stop loss parameter, usually at the 50% of the signal candle. Quite often this will reduce my stop to 1-point/4-ticks or less. You can also throw your favorite MA on the chart and when it's near your MA, get in as close as possible to that.

You can use that Zigzag Highs Lows indicator or you can find the Swing High Swing Lo indicator below. Basically any that will give you an idea of a reversal would work.

https://usethinkscript.com/threads/...ows-indefinitely-in-thinkorswim.930/post-9509
 
@mashume

In the lower study, you define "concavity" but I don't see it used anywhere in the study. Is it meant to be a plot or is it an orphaned code that isn't needed?
 
Last edited:
Well, I'm embarrassed to say that it seems I can't locate the original version of the lower script. I can try to recreate it, but you'll know it if you find one that uses a displace function.

Perhaps someone in the community at large still has it.
Hello, I played around with the settings to line it up perfect with the chart, look at the results on attached screenshot

Cant figure out setting for lower code, currently set to 55.. Upper code is set 1- lookback, 1- HMA length for 15 in chart.. Any suggestion to make lower code to work with it cant figure out hma lenght to use in 15 min

Please look at the charts below:

NVDA
TSLA
 
Hello, I played around with the settings to line it up perfect with the chart, look at the results on attached screenshot

Cant figure out setting for lower code, currently set to 55.. Upper code is set 1- lookback, 1- HMA length for 15 in chart.. Any suggestion to make lower code to work with it cant figure out hma lenght to use in 15 min

Please look at the charts below:

NVDA
TSLA
Is this what you are looking for? @goofy @mashume
#
# Hull Moving Average Concavity Divergence
# or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mahsume)
# Version: 2020-02-21 Initial Public
#
# This code is licensed (as applicable) under the GPL v3
#
# ----------------------

declare lower;

input price = HL2;

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 divergence = displacer(-1, HMA - next_bar);
divergence.setDefaultColor(Color.LIME);
divergence.SetLineweight(2);

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);
 
@inhouselr Thank you for this script ! Having the same problem with the Upper Script not working with the alerts. When i delete them Upper works.

These are the errors displayed in the Edit Script window.
Expected double at 51:1
Expected double at 52:1
No such variable: buy at 51:19
No such variable: sell at 52:19
No such variable: buy at 51:19
No such variable: sell at 52:19
 
Last edited:
NVM really liking the script..thank you for the awesome work

I have hma length set at 1 .. It overlays perfectly with daily 15 minchart. Is it bad to have hma length set at 1?
 
NVM really liking the script..thank you for the awesome work

I have hma length set at 1 .. It overlays perfectly with daily 15 minchart. Is it bad to have hma length set at 1?

It's not how I would use it, but if it gives you trades you like, use it. :) There is no 'right' or 'wrong' way to use it.

@stocksniper

Code:
AssignPriceColor(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);

or something like that. Just drop that into your concavity upper study code.

Happy Trading
 
Hi @mashume ,
Upper code scanner is not working for me. I have imputed
AB_UpperCode_Mashume("hma length" = 21)."buy" is true within 1 bars
Please excuse as i have changed the name to be top of my list and i have same name on my study.
Please help here. what stupid mistake am i making ? Please correct
Not sure how can i attach image here.
 
As I was looking at the Hull Moving Average, it occurred to me that taking the local minimums and maximums might not be the most effective use of it's smooth nature. Nor did it seem that a moving average cross over would provide timely signals.

And then I remembered that I used to teach physics and calculus and decided to look at rates of change and that lead me to...

CONCAVITY

which is nothing but the second derivative of the function (if you remember or not) :) and so this indicator was born.

It is in two parts -- the upper which is the Hull Moving Average with the addition of colored segments representing concavity and turning points: maxima, minima and inflection. The last of these are of the greatest interest. The second part is a plot of the calculation used in finding the turning points (which is roughly the second derivative of the HMA function, where zero crosses are the inflection points.

SDcVypw.png


UPPER INDICATOR

Upper HMA colors:
  • Green: Concave Up but HMA decreasing. The 'mood' has changed and the declining trend of the HMA is slowing. Long trades were entered at the turning point
  • Light Green: Concave up and HMA increasing. Price is increasing, and since the curve is still concave up, it is accelerating upward.
  • Orange: Concavity is now downward, and though price is still increasing, the rate has slowed, perhaps the mood has become less enthusiastic. We EXIT the trade (long) when this phase starts. Very little additional upward price movement is likely.
  • Red: Concave down and HMA decreasing. Not good for long trades, but get ready for a turning point to enter long on again.

Upper Label Colors:
these are useful for getting ready to enter a trade, or exit a trade and serve as warnings that a turning point may be reached soon
  • Green: Concave up and divergence (the distance from the expected HMA value to the actual HMA value is increasing). That is, we're moving away from a 2nd derivative zero crossover.
  • Yellow: Concave up but the divergence is decreasing (heading toward a 2nd derivative zero crossover); it may soon be time to exit the trade.
  • Red: Concave down and the absolute value of the divergence is increasing (moving away from crossover)
  • Pink: Concave down but approaching a zero crossover from below (remember that that is the entry signal, so pink means 'get ready').
Arrows are provided as Buy and Sell and could perhaps be scanned against.

LOWER INDICATOR

For those who prefer less cluttered uppers, I offer the plot of the divergence from expected HMA values; analogous to the second derivative in that the zero crossovers are of interest, as is the slope of the line. The further from zero, the stronger the curve of the concavity, and the more likely to reach a local minima or maxima in short order.


Miscelaneous

If you find that there are too many buy and sell signals, you can change the length of the HMA (I find 34 a happy medium, though 55 and sometimes 89 can be appropriate). You can also play with the value of the lookback, though that will slow down signals.

This works well on Daily timeframes as well as intraday candles.

I set it up with High + Low / 2 as the default so that it shouldn't wait for close prices. That may not be appropriate to how you wish to trade.

Comments welcome. Let me know how you use it, how it works for your trades, and whether it made your head hurt trying to remember your calculus.

Happy Trading,
Mashume

UPPER:
https://tos.mx/cgKFdmm

LOWER:
https://tos.mx/p0k7ims

UPPER CODE

Code:
#
# Hull Moving Average Concavity and Turning Points
#  or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mahsume)
# Version: 2020-02-23 V3
# Faster, but not necessarily mathematically as good as the first
#
# This code is licensed (as applicable) under the GPL v3
#
# ----------------------


declare upper;

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

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

# def delta_per_bar =
# (fold n = 0 to lookback with s do s + getValue(HMA, n, lookback - 1)) / lookback;

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 double.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 Double.NaN;
MA_Max.SetDefaultColor(Color.WHITE);
MA_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);

#####
# Added Alerts 2020-02-23
Alert(condition = buy, text = "Buy", "alert type" = Alert.BAR, sound = Sound.Chimes);
Alert(condition = sell, text = "Sell", "alert type" = Alert.BAR, sound = Sound.Chimes);

LOWER CODE

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 = OPEN;

input HMA_length = 55;
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 divergence = HMA - next_bar;
divergence.setDefaultColor(Color.LIME);
divergence.SetLineweight(2);

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);
How do you get the arrows, I dont see them on chart
 
How do you get the arrows, I dont see them on chart
Add this to your upper study somewhere near the end:
Code:
plot sell = if turning_point and concavity == 1 then high else double.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 double.nan;
buy.SetDefaultColor(Color.CYAN);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetLineWeight(3);

It's from another version I had sitting around. Not sure why they weren't in the released version. May or may not do exactly what you're interested in. :)

mashume
 
Hey Hey! Thanks for this!

I've noticed sometimes the arrows paint on the screen live, then are removed later. Is there a way to catch the removal and paint it in another color on the screen so we can see when the script changes its mind?

Thanks again!

Edit:

Also, if anyone else gets sick of staring at the candles and painted arrows live, I highly recommend seting up the study on multiple charts with different candle periods (5m, 10m, etc) and just screen record with Quicktime. It's easy to go over hours of realtime data, kinda like backtesting but in real time, to see how the algo preforms. Quicktime let's you FF 60x. I always knew having 2 macbooks would come in handy some day.
 
Last edited:
Hey Hey! Thanks for this!

I've noticed sometimes the arrows paint on the screen live, then are removed later. Is there a way to catch the removal and paint it in another color on the screen so we can see when the script changes its mind?

Thanks again!
Repainting is, unfortunately, a know issue with the way the calculations work. There are discussions of close vs hl2 elsewhere in this thread.

mashume
 
Repainting is, unfortunately, a know issue with the way the calculations work. There are discussions of close vs hl2 elsewhere in this thread.

mashume

I went back through the thread and saw that. Sorry for the redundant question.

Is there not a way to catch when the condition which painted the disappearing arrow is no longer true and paint the opposite arrow? I'm a developer by trade but new to thinkscript.

Is there a way to set a variable when the current plot is set to a particular direction? That way we could check the variable to see if the current plot (the last one that didnt disappear) is the same as the variable and repaint the current direction again. Does that make sense?
 
I went back through the thread and saw that. Sorry for the redundant question.

Is there not a way to catch when the condition which painted the disappearing arrow is no longer true and paint the opposite arrow? I'm a developer by trade but new to thinkscript.

Is there a way to set a variable when the current plot is set to a particular direction? That way we could check the variable to see if the current plot (the last one that didnt disappear) is the same as the variable and repaint the current direction again. Does that make sense?

ThinkScript is, to a great degree, a functional language rather than a procedural one. There isn't really a way to set variables that persist. Each time it repaints, it is running the current data against the function, and when it returns true, it paints and when false it doesn't. There isn't really a record of when it ran and painted per se.

Hope that helps a bit. Thinkscript is a marvelous language, but as all languages do, it excels at some things and does not at others.

-mashume
 
@mashume Thank you so much, can you add one just for the original Hull without the orange and dark green only 2 colors red and green. I would greatly appreciated.

There are any number of other very good implementations of the Hull Moving average on this site.
This one, for example:
https://usethinkscript.com/threads/hull-moving-average-pack-for-thinkorswim.947/

The point of this flavor of the HMA is the change from curving upward to curving downward, precisely the dark green and orange transitions. So, while there is no wrong way to use it, there are perhaps some it is better suited to than others.

You can always just poke around in the code and change the offending colors to ones you prefer too. ;-)

mashume
 
Status
Not open for further replies.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
306 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top