Create a custom strategy from Hull moving average

A

Alex

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



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"));
 
P

Pensar

Active member
VIP
@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! :)
 
Last edited:
A

Alex

Member
VIP
@Pensar appreciate the help exactly what i was looking for. Thanks!
 
F

frick53

New member
VIP
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.
 

Similar threads

Top