Looking For Input For HMA Strategy

METAL

Active member
Plus
I really think this is going to be a good strategy. I need to still add Labels so it will work with Macro Recorder. That isn't necessary though. I would like every ones feedback and criticism. Try it out. I will attempt to add labels. I also may add a couple of filters. Not sure if I will need them because with my testing so far, as long as you lengthen the Fast HMA, you can cut out most of the pullbacks and chop.

Latest Code:
HMA_V2 https://tos.mx/s900BxW

Code:
#Metal's-HMA-Strat-V1
#Credit @dap711 for providing the base code here https://usethinkscript.com/threads/moving-average-master-strategy-for-thinkorswim.13975/post-118000

input price = close;
input fastLength = 20;
input fastDisplace = 0;
input slowLength = 50;
input slowDisplace = 0;

plot Fast_HMA = MovingAverage(AverageType.HULL, price, fastLength)[-fastDisplace];
plot Slow_HMA = MovingAverage(AverageType.HULL, price, slowLength)[-slowDisplace];

Fast_HMA.DefineColor("Up", GetColor(1));
Fast_HMA.DefineColor("Down", GetColor(0));
Fast_HMA.AssignValueColor(if Fast_HMA > Fast_HMA[1] then Fast_HMA.Color("Up") else Fast_HMA.Color("Down"));

Slow_HMA.DefineColor("Up", GetColor(1));
Slow_HMA.DefineColor("Down", GetColor(0));
Slow_HMA.AssignValueColor(if Slow_HMA > Slow_HMA[1] then Slow_HMA.Color("Up") else Slow_HMA.Color("Down"));

plot BuySignal = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then Slow_HMA else Double.NaN;
BuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignal.AssignValueColor(Color.GREEN);
BuySignal.SetLineWeight(3);

plot SellSignal = if (Slow_HMA[1] >= Slow_HMA[2] and Slow_HMA < Slow_HMA[1]) then Slow_HMA else Double.NaN;
SellSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignal.AssignValueColor(Color.RED);
SellSignal.SetLineWeight(3);

plot BuyExit = if (Fast_HMA[1] >= Fast_HMA[2] and Fast_HMA < Fast_HMA[1]) then Fast_HMA else Double.NaN;
BuyExit.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BuyExit.AssignValueColor(Color.YELLOW);
BuyExit.SetLineWeight(3);

plot SellExit = if (Fast_HMA[1] <= Fast_HMA[2] and Fast_HMA > Fast_HMA[1]) then Fast_HMA else Double.NaN;
SellExit.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
SellExit.AssignValueColor(Color.YELLOW);
SellExit.SetLineWeight(3);

AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], 100, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, SellSignal, open[-1], 100, Color.RED, Color.RED);
AddOrder(OrderType.SELL_TO_OPEN, BuyExit, open[-1], 100, Color.YELLOW, Color.YELLOW);
AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], 100, Color.YELLOW, Color.YELLOW);
 
Last edited:
  • Love
Reactions: IPA
Thank you @MerryDay for making the correction. This appears not to repaint when testing. Does it still repaint?

It currently does not repaint as long as the fastDisplace and the slowDisplace remain at zero.
Okay. Did not know that affected the repainting. So I should remove the displace options then?

Can someone point me in the right direction for making the signals, Plot and order signals to trigger right at the up to down and down to up where the colors change. Right now it is one candle ahead for the plot and then another ahead for the order. Is this possible?
 

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

Thank you @MerryDay for making the correction. This appears not to repaint when testing. Does it still repaint?
It currently does not repaint as long as the fastDisplace and the slowDisplace remain at zero.

The displacement with future bars is used in studies to get confirmation to improve accuracy in signals.
However, as this causes repainting, displacement with future bars cannot be used in backtesting strategies.
So yes, remove displace options from scripts with ADDORDER() functions.

Can someone point me in the right direction for making the signals, Plot and order signals to trigger right at the up to down and down to up where the colors change. Right now it isone candle ahead for the plot and then another ahead for the order. Is this possible?
No, you cannot write strategies with trigger signals "right at the up to down and down to up" as it is not possible to place a trade in real-time at the exact moment the signal is forming.

The final signal after close is what will appear in backtesting results, all others will be repainted.
Therefore, backtesting strategies use an entry point that is placed on the candle after the signal to properly reflect the actual non-repainted signals.

You will notice on all backtesting scripts that the trade signal occurs on the candle after.
Here is @dap711's Moving Average Strategy as an example.
https://usethinkscript.com/threads/moving-average-master-strategy-for-thinkorswim.13975/
 
Last edited:
mod note:
Moving Averages are a trend-following / lagging, indicator because it is based on past prices. The longer the period for the moving average, the greater the lag. That lag is what is causing the problem in this strategy. By the time the signal is forming, the price is falling.
 
Last edited:
It currently does not repaint as long as the fastDisplace and the slowDisplace remain at zero.

The displacement with future bars is used in studies to get confirmation to improve accuracy in signals.
However, as this causes repainting, displacement with future bars cannot be used in backtesting strategies.
So yes, remove displace options from scripts with ADDORDER() functions.


No, you cannot write strategies with trigger signals "right at the up to down and down to up" as it is not possible to place a trade in real-time at the exact moment the signal is forming.

The final signal after close is what will appear in backtesting results, all others will be repainted.
Therefore, backtesting strategies use an entry point that is placed on the candle after the signal to properly reflect the actual non-repainted signals.

You will notice on all backtesting scripts that the trade signal occurs on the candle after.
Here is @dap711's Moving Average Strategy as an example.
https://usethinkscript.com/threads/moving-average-master-strategy-for-thinkorswim.13975/
Got it. I would like for the Addorder to at least be the next candle and not 2 over. Is that possible?
 
The Hull Moving Average has poorer smoothness than a linear moving average albeit with similar delays in the capacity to identify turning points. As the mathematics used to construct the HMA are based on the calculation of square roots, using average lengths of 20 and 50, the lengths used in the code above, rather than squared numbers (e.g. 4,9,16,25,36,49,64...), will further crimp any anticipated utility of the Hull over another moving average variety.

 
Last edited:
The Hull Moving Average has poorer smoothness than a linear moving average albeit with similar delays in the capacity to identify turning points. As the mathematics used to construct the HMA are based on the calculation of square roots, using average lengths of 20 and 50, the lengths used in the code above, rather than squared numbers (e.g. 4,9,16,25,36,49,64...), will further crimp any anticipated utility of the Hull over another moving average variety.

Okay. That is good to know. Is it possible to have other moving averages with up and down colors? The hull is the only one I have seen like that.
 
You might consider this Jurik clone (2-pole Gaussian) written by bigboss, which has a color-assignment, and plot it with a longer, less reactive double weighted moving average (DEMA) which is included with TOS, or perhaps a far-longer "Jurik" (included in quotes because the actual Jurik formula is not known). The default setting for this "Jurik" is 7 but you will likely find it most effective at settings of 20 or above.
https://usethinkscript.com/threads/jurik-moving-average.9817/#post-88377
 
Last edited by a moderator:
METALS_HMA_V2
I need to labels to work correctly if someone can assist. They are showing as black and not white. Any suggestion will be appreciated.
Here is the updated code. I left the displace option in the code so I can use it during testing. This code is not recommended to be used as a strategy if utilizing the displace options.
This strategy did really well for me today. I cleared 900.00 profit today and should have made more but I FOMOd out. I did not stay with the strategy. Oh well, That is typical for me. I have a lot of testing and trading to do before it is complete.
Code:
#Metal's-HMA-Strat-V2
#Credit @dap711 for providing the base code here https://usethinkscript.com/threads/moving-average-master-strategy-for-thinkorswim.13975/post-118000

input price = close;
input fastLength = 20;
input fastDisplace = 0;
input slowLength = 50;
input slowDisplace = 0;

plot Fast_HMA = MovingAverage(AverageType.HULL, price, fastLength)[-fastDisplace];
plot Slow_HMA = MovingAverage(AverageType.HULL, price, slowLength)[-slowDisplace];

#=========FAST LINE===========
Fast_HMA.DefineColor("Up", GetColor(1));
Fast_HMA.DefineColor("Down", GetColor(0));
Fast_HMA.AssignValueColor(if Fast_HMA > Fast_HMA[1] then Fast_HMA.Color("Up") else Fast_HMA.Color("Down"));

#=========SLOW LINE===========
Slow_HMA.DefineColor("Up", GetColor(1));
Slow_HMA.DefineColor("Down", GetColor(0));
Slow_HMA.AssignValueColor(if Slow_HMA > Slow_HMA[1] then Slow_HMA.Color("Up") else Slow_HMA.Color("Down"));

#=========PLOTS===========
plot BuySignal = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then Slow_HMA else Double.NaN;
BuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignal.AssignValueColor(Color.CYAN);
BuySignal.SetLineWeight(3);

plot SellSignal = if (Slow_HMA[1] >= Slow_HMA[2] and Slow_HMA < Slow_HMA[1]) then Slow_HMA else Double.NaN;
SellSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignal.AssignValueColor(Color.RED);
SellSignal.SetLineWeight(3);

plot BuyExit = if (Fast_HMA[1] >= Fast_HMA[2] and Fast_HMA < Fast_HMA[1]) then Fast_HMA else Double.NaN;
BuyExit.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BuyExit.AssignValueColor(Color.YELLOW);
BuyExit.SetLineWeight(3);

plot SellExit = if (Fast_HMA[1] <= Fast_HMA[2] and Fast_HMA > Fast_HMA[1]) then Fast_HMA else Double.NaN;
SellExit.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
SellExit.AssignValueColor(Color.YELLOW);
SellExit.SetLineWeight(3);

#=========ORDERS===========
AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], 1, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, SellSignal, open[-1], 1, Color.RED, Color.RED);
AddOrder(OrderType.SELL_TO_OPEN, BuyExit, open[-1], 1, Color.YELLOW, Color.YELLOW);
AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], 1, Color.YELLOW, Color.YELLOW);

#=========LABELS===========
def OpenOrders = GetQuantity();

AddLabel(yes, "     BUY      ", if BuySignal[1] and OpenOrders < 1 then CreateColor(153, 255, 153) else Color.White);
AddLabel(yes, "     SELL     ", if SellSignal[1] and OpenOrders > -1 then CreateColor(255, 102, 102) else Color.White);
AddLabel(yes, "     CLOSE     ", if (SellExit[1] and OpenOrders > 0) or (BuyExit[1] and OpenOrders < 0) then Color.Yellow else Color.White);

#=========END CODE===========

https://tos.mx/s900BxW
 
Last edited by a moderator:
You might consider this Jurik clone (2-pole Gaussian) written by bigboss, which has a color-assignment, and plot it with a longer, less reactive double weighted moving average (DEMA) which is included with TOS, or perhaps a far-longer "Jurik" (included in quotes because the actual Jurik formula is not known). The default setting for this "Jurik" is 7 but you will likely find it most effective at settings of 20 or above.
https://usethinkscript.com/threads/jurik-moving-average.9817/#post-88377
I have tried to code this like the HMA I presented, but for some reason when I give different timelines, they stay the same on the chart. I must have something wrong in the coding. I will keep trying.
 
@samer800, @HODL-Lay-HE-hoo!, @Christopher84 Can one of you guys tell me why the labels I have are showing up as black instead of white?

you are using plot variables that are sometimes NA , to determine a color.

you could change the label, so the first color condition checks if signal is NA,

if isnan(buysignal) then color.white else...


i try to never reference plot variables. i would make new variables for the conditions.

def buy1 = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then 1 else 0;

then reference these new variables in the plots and labels.
 
you are using plot variables that are sometimes NA , to determine a color.

you could change the label, so the first color condition checks if signal is NA,

if isnan(buysignal) then color.white else...


i try to never reference plot variables. i would make new variables for the conditions.

def buy1 = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then 1 else 0;

then reference these new variables in the plots and labels.
Ahhh that’s a better answer. You are a man amongst men @samer800 .
 
you are using plot variables that are sometimes NA , to determine a color.

you could change the label, so the first color condition checks if signal is NA,

if isnan(buysignal) then color.white else...


i try to never reference plot variables. i would make new variables for the conditions.

def buy1 = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then 1 else 0;

then reference these new variables in the plots and labels.
Okay. Awesome. I will give it a try. I am using Chat GPT to help write the code as I have no clue how to do it. It has got me this far. I will see if I can direct it to use this methodology.
 
Do I need to get rid of or redo the plots in the code?

This is what Chat GPT has done so far.

#=========PLOTS===========
def buy1 = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then 1 else 0;
plot BuySignal = buy1 * Slow_HMA; BuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignal.AssignValueColor(Color.CYAN);
BuySignal.SetLineWeight(5);

Is this correct?
 
This is the latest code. I changed the buy signals. I still cannot get the labels to work. By the way, I profited 769.00 today. It was kind of an easy day to trade so I don't know how much weight to put on it yet. I had no red trades. Of course I am using other indicators for confirmation.


Code:
#Metal's-HMA-Strat-V3
#Credit @dap711 for providing the base code here https://usethinkscript.com/threads/moving-average-master-strategy-for-thinkorswim.13975/post-118000

input price = close;
input fastLength = 27;
input fastDisplace = 0;
input slowLength = 39;
input slowDisplace = 0;
input showLabels = yes;

#=========Define HMA===========
plot Fast_HMA = MovingAverage(AverageType.HULL, price, fastLength)[-fastDisplace];
plot Slow_HMA = MovingAverage(AverageType.HULL, price, slowLength)[-slowDisplace];

#=========FAST LINE===========
Fast_HMA.DefineColor("Up", GetColor(1));
Fast_HMA.DefineColor("Down", GetColor(0));
Fast_HMA.AssignValueColor(if Fast_HMA > Fast_HMA[1] then Fast_HMA.Color("Up") else Fast_HMA.Color("Down"));
Fast_HMA.SetLineWeight(3);

#=========SLOW LINE===========
Slow_HMA.DefineColor("Up", GetColor(1));
Slow_HMA.DefineColor("Down", GetColor(0));
Slow_HMA.AssignValueColor(if Slow_HMA > Slow_HMA[1] then Slow_HMA.Color("Up") else Slow_HMA.Color("Down"));
Slow_HMA.SetLineWeight(3);

#=========PLOTS===========
def Buy = if (Slow_HMA[1] <= Slow_HMA[2] and Slow_HMA > Slow_HMA[1]) then Slow_HMA else Double.NaN;
plot BuySignal = Buy;
BuySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuySignal.AssignValueColor(Color.CYAN);
BuySignal.SetLineWeight(3);

def Sell = if (Slow_HMA[1] >= Slow_HMA[2] and Slow_HMA < Slow_HMA[1]) then Slow_HMA else Double.NaN;
plot SellSignal = Sell;
SellSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellSignal.AssignValueColor(Color.Magenta);
SellSignal.SetLineWeight(3);

def BuyExit = if (Fast_HMA[1] >= Fast_HMA[2] and Fast_HMA < Fast_HMA[1]) then Fast_HMA else Double.NaN;
plot BuyExitSignal = BuyExit;
BuyExitSignal.SetPaintingStrategy(PaintingStrategy.POINTS);
BuyExitSignal.AssignValueColor(Color.CYAN);
BuyExitSignal.SetLineWeight(5);

def SellExit = if (Fast_HMA[1] <= Fast_HMA[2] and Fast_HMA > Fast_HMA[1]) then Fast_HMA else Double.NaN;
plot SellExitSignal = SellExit;
SellExitSignal.SetPaintingStrategy(PaintingStrategy.SQUARES);
SellExitSignal.AssignValueColor(Color.MAGENTA);
SellExitSignal.SetLineWeight(5);

#=========END CODE===========[CODE]
 
Last edited:
  • Love
Reactions: IPA
I am hitting a brick wall here. I can get the labels to color properly but, I cannot get them to stay white. (I may just use them this way if I have to)
1) Do I need this to be a strategy in order for the labels to work so that they only trigger once the candle closes or at open of the next candle?
2) I also cannot get the "Exit" labels to combine into one so that the close/exit label triggers with either buyexit or sellexit.
3) I need to eliminate false triggers but using the openorders function but cannot get it to work. I think I need it to be a strategy for this to work.

This is the one that kinda works but not when the exits are combined:
AddLabel(showLabels, " BUY ", if BuySignal then Color.CYAN else Color.white);
AddLabel(showLabels, " SELL ", if SellSignal then Color.MAGENTA else Color.white);
AddLabel(showLabels, " EXIT (BUY) ", if BuyExitSignal then Color.CYAN else Color.white);
AddLabel(showLabels, " EXIT (SELL) ", if SellExitSignal then Color.MAGENTA else Color.white);

Exits combined and with open order function:
def OpenOrders = GetQuantity();
AddLabel(yes, " BUY ", if BuySignal[1] and OpenOrders < 1 then Color.CYAN else Color.white);
AddLabel(yes, " SELL ", if SellSignal[1] and OpenOrders > -1 then Color.MAGENTA else Color.white);
AddLabel(yes, " Close ", if (SellExitSignal[1] and OpenOrders > 0) or (BuyExitSignal[1] and OpenOrders > 0) then Color.YELLOW else Color.white);

Chat GPT gave me the middle finger 😝
 
I am hitting a brick wall here. I can get the labels to color properly but, I cannot get them to stay white. (I may just use them this way if I have to)
1) Do I need this to be a strategy in order for the labels to work so that they only trigger once the candle closes or at open of the next candle?
2) I also cannot get the "Exit" labels to combine into one so that the close/exit label triggers with either buyexit or sellexit.
3) I need to eliminate false triggers but using the openorders function but cannot get it to work. I think I need it to be a strategy for this to work.

This is the one that kinda works but not when the exits are combined:
AddLabel(showLabels, " BUY ", if BuySignal then Color.CYAN else Color.white);
AddLabel(showLabels, " SELL ", if SellSignal then Color.MAGENTA else Color.white);
AddLabel(showLabels, " EXIT (BUY) ", if BuyExitSignal then Color.CYAN else Color.white);
AddLabel(showLabels, " EXIT (SELL) ", if SellExitSignal then Color.MAGENTA else Color.white);

Exits combined and with open order function:
def OpenOrders = GetQuantity();
AddLabel(yes, " BUY ", if BuySignal[1] and OpenOrders < 1 then Color.CYAN else Color.white);
AddLabel(yes, " SELL ", if SellSignal[1] and OpenOrders > -1 then Color.MAGENTA else Color.white);
AddLabel(yes, " Close ", if (SellExitSignal[1] and OpenOrders > 0) or (BuyExitSignal[1] and OpenOrders > 0) then Color.YELLOW else Color.white);

Chat GPT gave me the middle finger 😝
You must edit your ADDLabel statements. That is the code that determines label color and determines whether it "stays" or goes.
Make sure your conditional statements within your AddLabel are referencing the previous closed bar, not the current bar, not the next bar: the previous closed bar syntax ==[1].
read more:
https://usethinkscript.com/threads/indicators-that-we-are-using-in-our-algos.11585/#post-100422
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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