Modify the Hull Moving Average indicator?

S

spm009

Member
So I have this cool HMA with arrows script that @ebtrader worked on, but I wanted to make a small change thats a bit beyond my current skills. The current script paints the candles and plots a arrows based on price crossing HMA. I want to change it so that candles are painted and arrows placed when the HMA itself changes (ignoring price crossing, just focusing on HMA).

Thanks in advance to anyone who can help! :)

Here is the current script:

Code:
plot Data = close;#Hull Moving Average
#Colors and arrows and labels by EBTrader 02.22.15

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

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

HMA.DefineColor("Up", color.green);
HMA.DefineColor("Down", color.red);
HMA.AssignValueColor(if price > HMA then HMA.color("Up") else HMA.color("Down"));
AssignPriceColor(if price > HMA then HMA.color("Up") else HMA.color("Down"));

#===================start arrows and labels

input crossingType = {default any, above, below};
def signal = CompoundValue(1, if crosses(price, HMA,crossingDirection.ABOVE) then 1 else if crosses(price, HMA, crossingDirection.BELOW)  then -1 else signal[1], 0);
input showarrows = yes;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.green);
U1.SetLineWeight(3);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.red);
D1.SetLineWeight(3);

input usealerts = no;
Alert(usealerts and U1, "UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "DOWN", Alert.BAR, Sound.Chimes);

def uptrend = price > HMA;
def downtrend = price < HMA;

addlabel(uptrend, "Uptrend", color.green);
addlabel(downtrend, "Downtrend", color.red);




#================end arrows and labels
 
Last edited:
S

spm009

Member
If anyone can give any hints, I would love to try to work on this myself. The reason I don't want crosses of the Hull MA is because it creates too many false signals in choppy markets. A change when the Hull MA changes would be a nice clear signal, without the noise.
 
horserider

horserider

Well-known member
VIP
Hmmm look at the code and determine when plot changes color and when arrows are triggered to plot. What is making both plots change?
How will your idea change the noise?
 
markos

markos

Well-known member
VIP
I'm with @horserider , are you fully aware of how a Hullma is to be used? Please review alanhull.com and read what he says about his indicator if you haven't already.
 
S

spm009

Member
I'm with @horserider , are you fully aware of how a Hullma is to be used? Please review alanhull.com and read what he says about his indicator if you haven't already.
Yes I'm fully aware. I simply want to remove the hull ma from the chart and have arrows mark the candles when hull ma changes. Thats all.
 
S

spm009

Member
Hmmm look at the code and determine when plot changes color and when arrows are triggered to plot. What is making both plots change?
How will your idea change the noise?
Right now the indicator is set up to show arrows at candle/price crosses of the hull ma. Instead of that, I'm just looking to simply have an arrow indicate which direction Hull MA changed on the candle the change occurs at.

For my setup and the type of swing trading I do with option, this would be a pretty reliable help and visual cue.
 
S

spm009

Member
I know (or think) I have to modify this part below (probably the bold parts), but I'm pretty new to coding and my skills aren't there yet. So I don't know how I would replace price crossing Hull ma as a signal with just Hull MA switching as the signal.

input crossingType = {default any, above, below};
def signal = CompoundValue(1, if crosses(price, HMA,crossingDirection.ABOVE) then 1 else if crosses(price, HMA, crossingDirection.BELOW) then -1 else signal[1], 0);
input showarrows = yes;
 
horserider

horserider

Well-known member
VIP
Looks like that is what it does. Both change on price above or below hull ma.

HMA.AssignValueColor(if price > HMA then HMA.color("Up") else HMA.color("Down"));

def signal = CompoundValue(1, if crosses(price, HMA,crossingDirection.ABOVE) then 1 else if crosses(price, HMA, crossingDirection.BELOW) then -1 else signal[1], 0);

To show only arrrows def HMA, delete errors. delete input crossing type. Delete alerts and labels if do not want them. And delete that plot data = close; which should be deleted when starting a new study.
 
S

spm009

Member
Looks like that is what it does. Both change on price above or below hull ma.

HMA.AssignValueColor(if price > HMA then HMA.color("Up") else HMA.color("Down"));

def signal = CompoundValue(1, if crosses(price, HMA,crossingDirection.ABOVE) then 1 else if crosses(price, HMA, crossingDirection.BELOW) then -1 else signal[1], 0);

To show only arrrows def HMA, delete errors. delete input crossing type. Delete alerts and labels if do not want them. And delete that plot data = close; which should be deleted when starting a new study.
@horserider Thanks for the hints, but I'm going to post an image just to make sure we are all on the same page & to show exactly what I'm trying to do. I feel like I might be doing a poor job explaining it lol.



I circled the the spots where the Hull MA changes in yellow (where I want arrows). The other non-circled spots show extra arrows which only create noise and false signals. Thats all I'm really trying to do.

Currently the indicator puts arrows when price crosses HMA, but thats not what I want. I want arrows indicating when the HMA changes from purple to blue and vice versa. I feel this will eliminate these false signals or reduce them significantly.
 
S

spm009

Member
I think I found something close to what I’m looking for in alert form:

#Alert
Alert(HMA > HMA[1] and HMA[1] < HMA[2], "Up", Alert.BAR, Sound.Bell);
Alert(HMA < HMA[1] and HMA[1] > HMA[2], "Down", Alert.BAR, Sound.Bell);

source: https://futures.io/thinkorswim/35007-alerts-based-hull-moving-avg-direction.html

also came across this:
def hmaAscending = HMA > HMA[1] and HMA[1] > HMA[2];
def hmaDescending = HMA < HMA[1] and HMA[1] < HMA[2];
plot trigger = (hmaAscending and !hmaAscending[1]) or (hmaDescending and !hmaDescending[1]);

source: https://www.hahn-tech.com/ans/is-th...alerts-on-the-change-in-color-alert-via-text/
 
Last edited:
S

spm009

Member
So I'm very close... I think I'm almost there. It does basically what I want, just need to tweak it so that the correct options show up in settings customizations so users can make changes. I think the candles aren't being painted though, I believe they should be painted depending on the trend.

Here's the code:

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2017
#
input price = close;
input length = 20;
input displace = 0;
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def hmaAscending = HMA > HMA[1] and HMA[1] > HMA[2];
def hmaDescending = HMA < HMA[1] and HMA[1] < HMA[2];

#===================start arrows and labels

input crossingType = {default any, above, below};
def signal = CompoundValue(1, if hmaascending then 1 else if hmadescending then -1 else signal[1], 0);
input showarrows = yes;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.green);
U1.SetLineWeight(3);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.red);
D1.SetLineWeight(3);

input usealerts = no;
Alert(usealerts and U1, "UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "DOWN", Alert.BAR, Sound.Chimes);

def uptrend = price > HMA;
def downtrend = price < HMA;

addlabel(uptrend, "Uptrend", color.green);
addlabel(downtrend, "Downtrend", color.red);
Here's an image. Seems to be working nicely. I just wonder if there's a way to get the arrows to possibly show up on the Hull MA (if added to the chart) instead of on each candle.

 
S

spm009

Member
Ok got a little further. Only thing missing now is the option turn on/off the Hull MA & also the painting strategy in the options (input). I'm lost there for now.

Still would love the toggle option to have arrows on the Hull MA if visible or on the candles in settings. No idea how to do that.

I guess we can call it the "Hull MA Trend Indicator"


Code:
# Hull MA Trend Indicator - Arrows Alerts & Paint
# TD Ameritrade IP Company, Inc. (c) 2008-2017
# colors and arrows and labels by EBTrader 02.22.15
# modified by tiny bull 2.7.20
# version 1.2

input price = close;
input length = 20;
input displace = 0;
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def hmaAscending = HMA > HMA[1] and HMA[1] > HMA[2];
def hmaDescending = HMA < HMA[1] and HMA[1] < HMA[2];

#==============start painted candles...need to finish input (I'm lost there)

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

input paint_candles = yes;
HMA1.DefineColor("Up", color.green);
HMA1.DefineColor("Down", color.red);
HMA1.AssignValueColor(if hmaascending then HMA1.color("Up") else HMA1.color("Down"));
AssignPriceColor(if hmaascending then HMA1.color("Up") else HMA1.color("Down"));

#===================start arrows and labels

input crossingType = {default any, above, below};
def signal = CompoundValue(1, if hmaascending then 1 else if hmadescending then -1 else signal[1], 0);
input showarrows = yes;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.green);
U1.SetLineWeight(3);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.red);
D1.SetLineWeight(3);

input usealerts = no;
Alert(usealerts and U1, "UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "DOWN", Alert.BAR, Sound.Chimes);

def uptrend = hmaascending;
def downtrend = hmadescending;

addlabel(uptrend, "Uptrend", color.green);
addlabel(downtrend, "Downtrend", color.red);
Here's an updated image of it with arrows and candle painting on. Have to test it real time and see how it works, but its just a different way of looking at Hull MA. Ideally I want to make it so that one can toggle the painting and hull MA itself on/off, just have to figure out how to code that part. :D

@horserider let me know what you think so far & if you have any hints I'm all ears/eyes 👀

Edit: whoops I realized the image has Hurst bands still on lol, this is the setup I'm working with though so its fine I guess.
Also looks like I might have to fix the label too, not sure yet.

 
Last edited:
horserider

horserider

Well-known member
VIP
input usehma = yes;
plot HMA1 = if usehma then hma else double.NaN;

add paint candles variable to your assignpricecolor
 
S

spm009

Member
@horserider thanks! :). I'm almost there!

So I made some small changes. You can toggle the HMA on and off. Can't figure out how to make it dual colored (up/down) like its supposed to be though, but its fine the way it is I guess since the arrows show direction change. That and have to fix the labels (uptrending/downtrending) to work, that broke somehow.

Only thing missing at this point is assigning the candle painting variable to a toggle on/off option in the settings.

Here is the current code:

Code:
# Hull MA Trend Indicator - Arrows Alerts & Paint
# TD Ameritrade IP Company, Inc. (c) 2008-2017
# colors and arrows and labels by EBTrader 02.22.15
# modified by tiny bull 2.7.20
# version 1.3

input price = close;
input length = 20;
input displace = 0;
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def hmaAscending = HMA > HMA[1] and HMA[1] > HMA[2];
def hmaDescending = HMA < HMA[1] and HMA[1] < HMA[2];

#==============start painted candles...need to finish input (I'm lost there)...
#told to add paint candles variable to your assignpricecolor
#need to make hma dual colored up/down, currently just one color

input usehma = yes;
plot HullMA = if usehma then hma else double.NaN;

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

input paint_candles = yes;
candlesHMA.DefineColor("Up", color.green);
candlesHMA.DefineColor("Down", color.red);
candlesHMA.AssignValueColor(if hmaascending then candlesHMA.color("Up") else candlesHMA.color("Down"));
AssignPriceColor(if hmaascending then candlesHMA.color("Up") else candlesHMA.color("Down"));

#===================start arrows and labels

input crossingType = {default any, above, below};
def signal = CompoundValue(1, if hmaascending then 1 else if hmadescending then -1 else signal[1], 0);
input showarrows = yes;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.green);
U1.SetLineWeight(3);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.red);
D1.SetLineWeight(3);

input usealerts = no;
Alert(usealerts and U1, "UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "DOWN", Alert.BAR, Sound.Chimes);

def uptrend = hmaascending;
def downtrend = hmadescending;

#label stopped working, need to fix

addlabel(uptrend, "Uptrend", color.green);
addlabel(downtrend, "Downtrend", color.red);
 
horserider

horserider

Well-known member
VIP
:LOL: No Just a lot of work for little difference. If you are learning from the process that is great. Simple is better. As an example you have now plotted the hma twice.
 
S

spm009

Member
I think there are Hull MA related scans. This is simply just a different way of visualizing Hull MA.

I'm still working on this indicator, but its almost done.
 
horserider

horserider

Well-known member
VIP
@jvbaty Sorry cannot say. This is spm009's study. Not used it at all so no idea how it behaves.
 
N

NathanJames

New member
Hello Expert,

I just want to ask the expert in coding if you know how to add EMA,SMA with this code below, thank you--Nathan

Code:
#Hull Moving Average
#Colors and arrows and labels by EBTrader 02.22.15

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

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

HMA.DefineColor("Up", color. GREEN);
HMA.DefineColor("Down", color.red);
HMA.AssignValueColor(if price > HMA then HMA.color("Up") else HMA.color("Down"));
AssignPriceColor(if price > HMA then HMA.color("Up") else HMA.color("Down"));

#===================start arrows and labels

input crossingType = {default any, above, below};
def signal = CompoundValue(1, if crosses(price, HMA,crossingDirection.ABOVE) then 1 else if crosses(price, HMA, crossingDirection.BELOW)  then -1 else signal[1], 0);
input showarrows = yes;
plot U1 = showarrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.green);
U1.SetLineWeight(3);
plot D1 = showarrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.red);
D1.SetLineWeight(3);

input usealerts = no;
Alert(usealerts and U1, "UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "DOWN", Alert.BAR, Sound.Chimes);

def uptrend = price > HMA;
def downtrend = price < HMA;

addlabel(uptrend, "Uptrend", color. GREEN);
addlabel(downtrend, "Downtrend", color.VIOLET);


#================end arrows and labels
Credit to EBTrader for this script
 
Last edited by a moderator:

Similar threads

Top