Create a custom strategy from Hull moving average

Alex

Active member
VIP
Hey! I would like to turn the default thinkorswim Hull moving average into a custom strategy. Whenever the color of the hull moving average slope changes its color from up / down I’d like the strategy to open / close an order only on the close of the candle / bar. In addition I would like an alert to pop up whenever all of the conditions are met. I'm not much of a coder myself so hopefully this isn't too complicated...

ASFxmFe.png


Here is the default thinkorswim HullMovingAvg code:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

input price = close;
input length = 20;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));

HMA.DefineColor("Down", GetColor(0));

HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));
 
@VenB To make a scan:
  1. Save the above code as a study
  2. Scan for HMAup is true
In the scan image where it says: A2_HMAplot, you need to substitute the name you saved your study as.

aaa2.png
Hi MerryDay, I created the study with the code you provided. I would like to enter a trade exactly when it crosses and RSI is greater than 25, I thought by scripting HMAup is true would work, but afraid not. is there away to determine when it crosses?
 

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

Hi MerryDay, I created the study with the code you provided. I would like to enter a trade exactly when it crosses and RSI is greater than 25, I thought by scripting HMAup is true would work, but afraid not. is there away to determine when it crosses?
"when it crosses" is not a helpful mathematical definition.
I "assume" you are looking for the fast Hullma to cross the slow Hullma???
For that you would add this to the bottom of your script:
Code:
plot HMAcross= HMA1 crosses above HMA2 ;
In Scan Hacker: HMAcross is true
 
Hello and thank you in advance for your time. the code appears to buy sell signal based on period and not purly the color change of HMA. On a larger timr frames it becomes an issue. Would you please advise and make it so the buy and sell orders are placed solely based on HMA color change. Thanks again

hello sorry for the ignorant question. How could we make it take the trade and not just give alerts? thank you
 
Last edited by a moderator:
@Alex, try adding this to the bottom of your code.
Code:
AddOrder(OrderType.BUY_AUTO, HMA > HMA[1]);
AddOrder(OrderType.SELL_AUTO, HMA < HMA[1]);

Alert(HMA > HMA[1] and HMA[1] < HMA[2], "BUY SIGNAL", Alert.BAR, Sound.Ring);
Alert(HMA < HMA[1] and HMA[1] > HMA[2], "SELL SIGNAL", Alert.BAR, Sound.Ring);
Make sure you add the completed code to the Strategies section. It should place simulation trades in either direction on a change in the HMA color and give alerts. If you want to backtest only buy orders, change "BUY_AUTO" and "SELL_AUTO" to "BUY_TO_OPEN" and SELL_TO_CLOSE". Hope this helps! :)

Hello and thank you in advance for your time. the code appears to buy sell signal based on period and not purly the color change of HMA. On a larger timr frames it becomes an issue. Would you please advise and make it so the buy and sell orders are placed solely based on HMA color change. Thanks again

hello sorry for the ignorant question. How could we make it take the trade and not just give alerts? thank you
This is designed for signals when HMA changes direction.
Yes, it is best to tailor the "periods" lengths to each timeframe and instrument that you trade.

No, there is no automated trading on the ToS platform. ToS requires manual order entry
There is conditional order entries but still each order needs to be created manually.
 
Last edited:
Yes, it does Ben. Unfortunately, it's not allowing me to attach a snapshot. Will share over discord.

WRaCBKg.jpg


Thank you @MerryDay, I will check again as per your instructions. How do you get red/green column in the search results?
hey does anyone have the code for this, but one that has the arrow indicators I think the Ttm scalper alert
 
hey does anyone have the code for this, but one that has the arrow indicators I think the Ttm scalper alert
I see that you have asked the same question a few times.
Not sure what your question is?
What does a Hull ma strategy have to do with the TTM scalper alert?
Your post doesn't have enough information for anyone to be able to assist you.
To make sure your post gets the attention and assistance it deserves, here are a few directions to keep in mind:
  • Include a screenshot of your chart and a shared chart link. This will help others understand the context of your question and allow them to replicate the conditions that you are describing.
  • Annotate your image, highlighting the confluence that you're trying to achieve, along with a more detailed written explanation. This will help others understand your question even better and give them more specific information to work with.
If you're unsure of how to share chart links or upload screenshots to the forum, don't worry, we've included some easy-to-follow directions for you.
How to create a shared chart link:
https://usethinkscript.com/threads/how-to-share-a-chart-in-thinkorswim.14221/
How to upload screenshots to the forum:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-58714


We look forward to seeing your next post and helping you out!
 
edited 1am...
@VenB Here are the plots on a chart.

TaDa.... I got the scan to work.
And I must say, that you may have something here.
It is showing some interesting potential results.

Code:
# ########################################################
# TOS HullMovingAVg
plot HMA1 = HullMovingAvg(price = HL2, length = 10); #12
plot HMA2 = HullMovingAvg(price = HL2, length = 20); #89

def HMA1up = HMA1>HMA1[1];
def HMA2up = HMA2>HMA2[1];
plot HMAup = HMA1up and HMA2up ;

def HMA1dn = HMA1<HMA1[1];
def HMA2dn = HMA2<HMA2[1];
plot HMAdn = HMA1dn and HMA2dn ;


HMA1.SetLineWeight(2);
HMA1.DefineColor("Positive", Color.violet);
HMA1.DefineColor("Negative", Color.light_orange);
HMA1.AssignValueColor(if HMA1 > HMA1[1] then HMA1.color("Positive") else HMA1.color("Negative"));
                                                                
HMA2.SetLineWeight(2);
HMA2.DefineColor("Positive", Color.green);
HMA2.DefineColor("Negative", Color.red);
HMA2.AssignValueColor(if HMA2 > HMA2[1] then HMA2.color("Positive") else HMA2.color("Negative"));

View attachment 1965
I'm new to think script and in copying the code above, I receive multiple errors around "exactly one plot is expected". Can you give me some direction here?
 
@Alex, try adding this to the bottom of your code.
Code:
AddOrder(OrderType.BUY_AUTO, HMA > HMA[1]);
AddOrder(OrderType.SELL_AUTO, HMA < HMA[1]);

Alert(HMA > HMA[1] and HMA[1] < HMA[2], "BUY SIGNAL", Alert.BAR, Sound.Ring);
Alert(HMA < HMA[1] and HMA[1] > HMA[2], "SELL SIGNAL", Alert.BAR, Sound.Ring);
Make sure you add the completed code to the Strategies section. It should place simulation trades in either direction on a change in the HMA color and give alerts. If you want to backtest only buy orders, change "BUY_AUTO" and "SELL_AUTO" to "BUY_TO_OPEN" and SELL_TO_CLOSE". Hope this helps! :)
how to adjust trade size parameter? It's missing from properties window
1700538736082.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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