Create a custom strategy from Hull moving average


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

Here is the default thinkorswim HullMovingAvg 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"));


Active member
@Alex, try adding this to the bottom of your 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! :)
Last edited:


New member
Can someone help with this strategy:
input tradeSize = 1;
def signal = HullMovingAvg(price=close, "length" = 10, "displace" = 1) crosses above HullMovingAvg(price=close,"length" = 20);
addOrder(OrderType.buy_TO_OPEN, signal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
def exit = HullMovingAvg(price=close, "length" = 10, "displace" = 1) crosses below HullMovingAvg(price=close,"length" = 20);
addOrder(OrderType.sell_TO_CLOSE, exit, open[-1], tradeSize, Color.MAGENTA, Color.MAGENTA);

I want the strategy and conditional order to only execute when the bar closes and it's true. For both it's executing when it is true.


New member
Hello, I appreciate all your guy's time and effort. I was wondering if its possible to do this same thing but conditionally with multiple HMA's, for example when a slow HMA's trend up or down matches a mid HMA and a fast HMA it enters the trade and when the fast HMA changes trend it exits. Please see attached picture. Any help would be awesome. Thank you



New member
Hello there, can you please help if you have TOS scan code when two HMA like 10 & 20 changes color either up or down? Thank you in advance.


Well-known member
@VenB Deleted this post... Because the logic didn't work..
I posted a revised study, down in message #14
Last edited:


New member
Thank you @MerryDay. I'm still exploring on how to utilize. Thank you
@MerryDay @BenTen

Thank you for the help. The above doesn't show when two HMA turned either green or red at the same time. Could you please take another look?

Another query for the code posted above, both up and down plots are having the same code. Should the down plot be used less than symbol?

Also, please help on how to add a column on scan results where red or green shown in the above snapshot?

Thank you.


@VenB I haven't had the chance to look at this request, but are you sure that both moving averages can turn either green or red at the same time? I imagine it would not be possible because one is 2x of the other.

Maybe you can post a screenshot of the scenario. I'll look at this later tonight.


Well-known member
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.

# ########################################################
# 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.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.AssignValueColor(if HMA2 > HMA2[1] then HMA2.color("Positive") else HMA2.color("Negative"));

Last edited:


Well-known member
@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.



New member
Yes, it does Ben. Unfortunately, it's not allowing me to attach a snapshot. Will share over discord.

Thank you @MerryDay, I will check again as per your instructions. How do you get red/green column in the search results?


Well-known member
That is the Finite Volume Elements (FVE) Indicator. It is a rather complex indicator. Red is not necessarily bad and Green is not necessarily good.

Instead of the FVE Volume; I combined your Hull script with the following volume profile:
# Scan for Price in value area and volume spike
# Mobius
# Chat Room Request 04.05.2018
def yyyymmdd = GetYYYYMMDD();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def period = Floor(day_number / 7);
def cond = 0 < period - period[1];
profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no);
vol.Show("va color" = Color.YELLOW);
def b = vol.GetHighestValueArea();
def a = vol.GetLowestValueArea();
plot condition = between(close, a, b) and volume > Average(volume, 21);
I scan for the Hull10 and Hull20 to be up on a daily agg and for the above volume profile to be true on a 15min agg.
This provides a nice pool of stocks against which I then run my favorite strategies such as @BenTen's BTD indicator.
Last edited:

Similar threads