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

Repaints
Status
Not open for further replies.
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.
 
@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
 
@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.
 
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! :)
 
@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
 
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
 
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
 
@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:
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:
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'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]);
 
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
 
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.
 
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
 
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.
 
@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.
 
Status
Not open for further replies.

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
438 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