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.

Thank you for your fantastic idea.

I have a few questions as I'm very new to ToS scripting. I'm trying to replicate this script in Python to do some backtesting.

So I have a few questions to clarify what some steps are doing. I understand the HMA is a list of HMA values.

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;

// here is where I'm getting confused. How are comparing a list of values (HMA) to one singular value (next_bar)? .. Can you explain what's exactly happening in this step? Are you saying if next bar is great than the last HMA value then its concave up?
 
Thank you for your fantastic idea.

I have a few questions as I'm very new to ToS scripting. I'm trying to replicate this script in Python to do some backtesting.

So I have a few questions to clarify what some steps are doing. I understand the HMA is a list of HMA values.

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;

// here is where I'm getting confused. How are comparing a list of values (HMA) to one singular value (next_bar)? .. Can you explain what's exactly happening in this step? Are you saying if next bar is great than the last HMA value then its concave up?

Good question, and I hope you're enjoying the mathematics of this one. :)

The idea is this, and it's important to know that without a bracket thhinscript references the value at [0] by default (the most recent value in the series) not the series itself. Very unpythonic, I know.

So Here's an illustration of what we're doing...
cjNzfl8.png

HMA is the black line in both graphs. The Red dashed line is a forward projection of the slope that we calculate from A (the lookback) and B the price difference over that lookback divided by the length of the lookback. You got that right above.

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 it 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.

Good fun, I think. Hope that helps with what you're trying to sort out. I'd love to see your python code when you've got it sorted (that's my day job btw... python and sql mostly).

happy trading and have fun with the math!

Teaser for anyone reading this far... next up from me is an intraday volume projection indicator... seems interesting.

-mashume
 
Is it possible to please add orders so we could backtest? For example, with a long only bias, the position would simply close when we get a sell signal... or after user defined X bars = auto close - thanks for the great work @mashume !
 
Hi @mashume how do you backtest this strategy P&L ? I tried AddOrder(OrderType.BUY_AUTO,buy,tickColor = GetColor(6), arrowColor = GetColor(6),name = "test"); and also with sell to exit but it look that when I put this information nothing happens. Thank you!
 
Hi @mashume how do you backtest this strategy P&L ? I tried AddOrder(OrderType.BUY_AUTO,buy,tickColor = GetColor(6), arrowColor = GetColor(6),name = "test"); and also with sell to exit but it look that when I put this information nothing happens. Thank you!
There is some back testing early in this thread. And some discussion of methodology.

Good luck and have fun.
-mashume
 
great job @mashume
is there a way to tweak the scan so it'll give me stocks that are in the orange/green areas only? I'm trying to scan for stocks that are starting to curve upwards.
 
Last edited:
Scan code. I'm not posting how to edit scanners right now. This is code to put in the scanner custom filter.

@tenacity11:
Code:
plot signal = !IsNaN(Concavity().MA_Min) within 2 bars;

@kelso:
if you've got the most recent version (with mobile support) this is pretty straight forward:
Code:
plot orange = !isNaN(Concavity().CCD_I);

Code:
plot green = !isNaN(Concavity().CCU_I);
or combine them:

Code:
plot orange_or_green = !isNaN(Concavity().CCD_I) or !isNaN(Concavity().CCU_I);

if you don't have the most mobile version, you'll need to hack the script to have the 'concavity' be a plot rather than a def, that way you can call it in the condition on your scan...
Code:
plot signal =  if Concavity().concavity == 1
    and Concavity().HMA > Concavity().HMA[1]
    then 1 else double.nan;

or something like that.

Don't forget to set your timeframes.

@chewie76 and @newtoazure
Can you tell me what you read as a 'buy confirmed' and I can help you with a scanner. if it's just the 'buy' condition then this may work...

Code:
plot signal = !isNaN(Concavity().buy);

-mashume
 
Scan code. I'm not posting how to edit scanners right now. This is code to put in the scanner custom filter.

@tenacity11:
Code:
plot signal = !IsNaN(Concavity().MA_Min) within 2 bars;

@kelso:
if you've got the most recent version (with mobile support) this is pretty straight forward:
Code:
plot orange = !isNaN(Concavity().CCD_I);

Code:
plot green = !isNaN(Concavity().CCU_I);
or combine them:

Code:
plot orange_or_green = !isNaN(Concavity().CCD_I) or !isNaN(Concavity().CCU_I);

if you don't have the most mobile version, you'll need to hack the script to have the 'concavity' be a plot rather than a def, that way you can call it in the condition on your scan...
Code:
plot signal =  if Concavity().concavity == 1
    and Concavity().HMA > Concavity().HMA[1]
    then 1 else double.nan;

or something like that.

Don't forget to set your timeframes.

@chewie76 and @newtoazure
Can you tell me what you read as a 'buy confirmed' and I can help you with a scanner. if it's just the 'buy' condition then this may work...

Code:
plot signal = !isNaN(Concavity().buy);

-mashume
Thanks...greatly appreciated...I'm getting an error message
Expected double at 1:16
Invalid statement: Unexpected part of function call at 1:34
 
Last edited:
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.
 
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
 
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?
 
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
475 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