Hull Moving Average for ThinkorSwim

Solution
took a pass at porting it:

HMA
cAz6Qrf.png


EHMA
96bRpzV.png


THMA
yNCgIsD.png


Ruby:
#Basic Hull Ma Pack tinkered by InSilico
#Original Port from https://www.tradingview.com/script/hg92pFwS-Hull-Suite
#
#2019.10.30 1.0 @diazlaz - Original Port
#

#INPUTS

input modeSwitch = {default "Hma", "Thma", "Ehma"}; #Hull Variation
input length = 55; #Length(180-200 for floating S/R , 55 for swing entry)
input switchColor = yes; #Color Hull according to trend?
input candleCol = yes; #Color candles based on Hull's Trend?
input visualSwitch = yes; #Show as a Band?
input thicknesSwitch = 2; #Line Thickness
input showLabels = yes;
input src = close...

joshuadstout

New member
2019 Donor
Not sure if this is what you are looking for but this might help.

Code:
#MACD based on Hull Moving Average W/peak/ebb arrows

#TOS title = MACD_via_Hull_MA_fav



declare lower;



input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input AverageType = {SMA, EMA, default HULL};



plot Value;

plot Avg;

switch (AverageType) {

  case SMA:

    Value = Average(close, fastLength) - Average(close, slowLength);

    Avg = Average(Value, MACDLength);

  case EMA:

    Value = ExpAverage(close, fastLength) - ExpAverage(close, slowLength);

    Avg = ExpAverage(Value, MACDLength);

  case HULL:

    Value =  MovingAverage(AverageType.HULL, close, fastLength) -  MovingAverage(AverageType.HULL, close, slowLength);

    Avg = Average(Value, MACDLength);

}



plot Diff = Value - Avg;

plot ZeroLine = 0;

Value.SetDefaultColor(GetColor(1));

Avg.SetDefaultColor(GetColor(8));

Diff.SetDefaultColor(GetColor(5));

Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

Diff.SetLineWeight(3);

Diff.DefineColor("Positive and Up", Color.GREEN);

Diff.DefineColor("Positive and Down", Color.DARK_GREEN);

Diff.DefineColor("Negative and Down", Color.RED);

Diff.DefineColor("Negative and Up", Color.DARK_RED);

Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));

ZeroLine.SetDefaultColor(GetColor(0));



#plot Min arrows*

def MinArrow = if (Value < Value[1] and value[1] < Value[2] and value[2] < Value[3] and value[-1] > Value and value < 0)

  then 0

  else if (Value > Value[1])

  then double.nan

  else double.nan;



plot UpArrow = if(MinArrow == 0, value, double.nan);

UpArrow.AssignValueColor(Color.Green);

UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

UpArrow.SetLineWeight(1);

UpArrow.HideBubble();



#plot Max arrows*

def MaxArrow = if (Value > Value[1] and value[1] > Value[2] and value[2] > Value[3] and value[-1] < Value and value > 0)

  then 0

  else if (Value > Value[1])

  then double.nan

  else double.nan;



plot DwnArrow = if(MaxArrow == 0, value, double.nan);

DwnArrow.AssignValueColor(Color.cyan);

DwnArrow.SetPaintingStrategy(PaintingStrategy.ARROW_Down);

DwnArrow.SetLineWeight(1);

DwnArrow.HideBubble();



#plot Max signal(Avg) arrows*

def SignalArrow = if (Avg > Avg[1] and Avg[1] > Avg[2] and Avg[2] > Avg[3] and Avg[-1] < Avg and Avg > 0)

  then 0

  else if (Avg > Avg[1])

  then double.nan

  else double.nan;



plot downSignalArrow = if(SignalArrow == 0, avg, double.nan);

downSignalArrow.AssignValueColor(Color.yellow);

downSignalArrow.SetPaintingStrategy(PaintingStrategy.ARROW_Down);

downSignalArrow.SetLineWeight(1);

downSignalArrow.HideBubble();



AddCloud(ZeroLine, Value, color.RED, color.GREEN);

# end

This is the source I found it from.

https://github.com/jshingler/TOS-an...llection.md#MACD_BASED_ON_HULL_MOVING_AVERAGE
 
Last edited by a moderator:
  • Like
Reactions: MBF

ykd2018x

Member
2019 Donor
VIP
This script from TradingView provides a few variations of the Hull Moving Average indicator. Please convert them to ThinkorSwim.

https://www.tradingview.com/script/hg92pFwS-Hull-Suite/

Code:
//@version=4
//Basic Hull Ma Pack tinkered by InSilico
study("Hull Suite by InSilico", overlay=true)

//INPUT
src = input(close, title="Source")
modeSwitch = input("Hma", title="Hull Variation", options=["Hma", "Thma", "Ehma"])
length = input(55, title="Length(180-200 for floating S/R , 55 for swing entry)")
lengthMult = input(1.0, title="Length multiplier (Used to view higher timeframes with straight band)")

useHtf = input(false, title="Show Hull MA from X timeframe? (good for scalping)")
htf = input("240", title="Higher timeframe", type=input.resolution)

switchColor = input(true, "Color Hull according to trend?")
candleCol = input(false,title="Color candles based on Hull's Trend?")
visualSwitch  = input(true, title="Show as a Band?")
thicknesSwitch = input(1, title="Line Thickness")
transpSwitch = input(40, title="Band Transparency",step=5)

//FUNCTIONS
//HMA
HMA(_src, _length) =>  wma(2 * wma(_src, _length / 2) - wma(_src, _length), round(sqrt(_length)))
//EHMA   
EHMA(_src, _length) =>  ema(2 * ema(_src, _length / 2) - ema(_src, _length), round(sqrt(_length)))
//THMA   
THMA(_src, _length) =>  wma(wma(_src,_length / 3) * 3 - wma(_src, _length / 2) - wma(_src, _length), _length)
    
//SWITCH
Mode(modeSwitch, src, len) =>
      modeSwitch == "Hma"  ? HMA(src, len) :
      modeSwitch == "Ehma" ? EHMA(src, len) :
      modeSwitch == "Thma" ? THMA(src, len/2) : na

//OUT
_hull = Mode(modeSwitch, src, int(length * lengthMult))
HULL = useHtf ? security(syminfo.ticker, htf, _hull) : _hull
MHULL = HULL[0]
SHULL = HULL[2]

//COLOR
hullColor = switchColor ? (HULL > HULL[2] ? #00ff00 : #ff0000) : #ff9800

//PLOT
///< Frame
Fi1 = plot(MHULL, title="MHULL", color=hullColor, linewidth=thicknesSwitch, transp=50)
Fi2 = plot(visualSwitch ? SHULL : na, title="SHULL", color=hullColor, linewidth=thicknesSwitch, transp=50)
///< Ending Filler
fill(Fi1, Fi2, title="Band Filler", color=hullColor, transp=transpSwitch)
///BARCOLOR
barcolor(color = candleCol ? (switchColor ? hullColor : na) : na)
 
Last edited by a moderator:

diazlaz

Well-known member
2019 Donor
VIP
took a pass at porting it:

HMA
cAz6Qrf.png


EHMA
96bRpzV.png


THMA
yNCgIsD.png


Ruby:
#Basic Hull Ma Pack tinkered by InSilico
#Original Port from https://www.tradingview.com/script/hg92pFwS-Hull-Suite
#
#2019.10.30 1.0 @diazlaz - Original Port
#

#INPUTS

input modeSwitch = {default "Hma", "Thma", "Ehma"}; #Hull Variation
input length = 55; #Length(180-200 for floating S/R , 55 for swing entry)
input switchColor = yes; #Color Hull according to trend?
input candleCol = yes; #Color candles based on Hull's Trend?
input visualSwitch = yes; #Show as a Band?
input thicknesSwitch = 2; #Line Thickness
input showLabels = yes;
input src = close; #Source

addLabel (showLabels, modeSwitch, COLOR.ORANGE);
def na = Double.NaN;

# HMA

def hma;
switch (modeSwitch) {
case "Hma":
    hma = wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)));
case "Ehma":
    hma = expAverage(2 * expAverage(src, length / 2) - expAverage(src, length), round(sqrt(length)));
case "Thma":
    hma = wma(wma(src,(length/2) / 3) * 3 - wma(src, (length/2) / 2) - wma(src, (length/2)), (length/2));
}

# PLOTS

def HULL = hma;
def MHULL = HULL[0];
def SHULL = HULL[2];

plot Fi1 = MHULL;
Fi1.AssignValueColor(
if HULL > HULL[2] then COLOR.GREEN else COLOR.RED
);
Fi1.SetLineWeight(thicknesSwitch);

plot Fi2 = SHULL;
Fi2.AssignValueColor(
if HULL > HULL[2] then COLOR.GREEN else COLOR.RED
);
Fi2.SetLineWeight(thicknesSwitch);
Fi2.SetHiding(!visualSwitch);

AddCloud (if visualSwitch then Fi1 else na, Fi2);

# COLORBARS
AssignPriceColor(
if !candleCol then
Color.CURRENT
else
if HULL > HULL[2] then COLOR.GREEN else COLOR.RED
);

#END OF HULL

Please provide any feedback, bugs, backtests, experiences, optimizations on how to best use it.
 
Solution

SmellyCat

New member
VIP
I've looked at it with Crude on a daily chart with the 55 setting. It confirms a trend VERY well, however, it is a bit too slow for crude for an entry, but that is only crude. And when I change it to, say a 34 period, it generated a few too many whip saws on crude. But Heck! to confirm a trend like it does is awesome! it would keep someone in a swing trade much longer and it appears to capture much more of a longer swing trade if one could follow the past signals.
 

tomsk

Well-known member
VIP
@shizah I have modified @diazlaz Hull Pack study to add alerts. It now rings an alert whenever the color transitions from green to red or vice versa. Here is the revised study, I have tested it both ways on the /ES. Have fun
Code:
#Basic Hull Ma Pack tinkered by InSilico
#Original Port from https://www.tradingview.com/script/hg92pFwS-Hull-Suite
#
# 2019.10.30 1.0 [USER=258]@diazlaz[/USER] - Original Port
# 2019.11.14 2.0 [USER=1369]@tomsk[/USER]   - Added alerts for color bar transitions

input modeSwitch = {default "Hma", "Thma", "Ehma"}; #Hull Variation
input length = 55; #Length(180-200 for floating S/R , 55 for swing entry)
input switchColor = yes; #Color Hull according to trend?
input candleCol = yes; #Color candles based on Hull's Trend?
input visualSwitch = yes; #Show as a Band?
input thicknesSwitch = 2; #Line Thickness
input showLabels = yes;
input src = close; #Source

addLabel (showLabels, modeSwitch, Color.ORANGE);

def hma;
switch (modeSwitch) {
case "Hma":
    hma = wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)));
case "Ehma":
    hma = expAverage(2 * expAverage(src, length / 2) - expAverage(src, length), round(sqrt(length)));
case "Thma":
    hma = wma(wma(src,(length/2) / 3) * 3 - wma(src, (length/2) / 2) - wma(src, (length/2)), (length/2));
}

def hull = hma;
def Mhull = hull[0];
def Shull = hull[2];
def transition = hull > hull[2];

plot Fi1 = Mhull;
Fi1.AssignValueColor(if transition then Color.GREEN else Color.RED);
Fi1.SetLineWeight(thicknesSwitch);

plot Fi2 = Shull;
Fi2.AssignValueColor(if transition then Color.GREEN else Color.RED);
Fi2.SetLineWeight(thicknesSwitch);
Fi2.SetHiding(!visualSwitch);

AddCloud (if visualSwitch then Fi1 else Double.NaN, Fi2);
AssignPriceColor(if !candleCol then Color.CURRENT else if transition then COLOR.GREEN else COLOR.RED);

Alert((!transition[1] and transition) or (transition[1] and !transition), "Hull Color Change", Alert.BAR, Sound.RING);
# END
 

tomsk

Well-known member
VIP
@tomsk How do you post this study as a scan?


@mailbagman2000 Here is the study converted into a scan. Note that there are two plot statements in the scan. I have configured this for the bullish case. If you'd like to use the bullish bearish scan, just comment out the bullish scan and uncomment the bearish scan. All this is listed at the end of the file. In case you're not sure, please read the following post.

https://usethinkscript.com/threads/how-to-use-the-tos-scanner.284/#post-10505
Here then is your COMPLETED scan code. I have tested this against a scan of the S&P 500 using daily aggregation a few minutes ago. A bullish scan has 4 results while a bearish scan has 5 results.

Code:
# Basic Hull Ma Pack Scan
# tomsk
# 12.27.2019

# Basic Hull Ma Pack tinkered by InSilico
# Original Port from https://www.tradingview.com/script/hg92pFwS-Hull-Suite

input modeSwitch = {default "Hma", "Thma", "Ehma"}; #Hull Variation
input length = 55; #Length(180-200 for floating S/R , 55 for swing entry)
input src = close; #Source

def hma;
switch (modeSwitch) {
case "Hma":
    hma = wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)));
case "Ehma":
    hma = expAverage(2 * expAverage(src, length / 2) - expAverage(src, length), round(sqrt(length)));
case "Thma":
    hma = wma(wma(src,(length/2) / 3) * 3 - wma(src, (length/2) / 2) - wma(src, (length/2)), (length/2));
}

def hull = hma;
def transition = hull > hull[2];

# Delete (#) the plot not needed

plot scanBull = !transition[1] and transition;
#plot scanBear = transition[1] and !transition;

# End Basic Hull Ma Pack Scan
 

HighBredCloud

Well-known member
VIP
I have been looking for a Hull Moving Average scanner and I have found this scan on here below: I was told that in order to use this scanner I first need to save it as a study then open it as custom then study and edit condition to (is true) (within 1 bar). Please see the pic below:

5JChyVR.png


When I called TOS I was told that because this is a moving average study I need to use (cross below) for a bear scan and or (cross above) for a bull scan under the edit conditions. Also I need to set the other conditions to as follows...Please see pic below:

NjEvcpc.png


So which is right? setting the edit condition to true or setting the conditions to cross below or above depending on bull or bear scan as per TOS instructions.

Not to further complicate things...but TOS also has their own Hull Average Change option under Study ---> Price Performance ---> Hull_Average_Change. Please see pic below:

cjYLquR.png


The weird part about all this is when I run the custom scan under the same parameters such as 15 length 1 hour scan...I get different results between the Hull Average Change TOS scanner vs the Hull Moving Average Scan that I found here...Please see pic below: Why is that IF all parameters are the same?

uuazxrV.png


Lastly...I heard and or experienced that when scanning for moving average cross overs not so much a Hull Moving Average scan...that the results can be delayed anywhere from 2-3 bars depending on the timeframe. Meaning that if you select a 1 hour time frame...such a delay can be 2-3 hours...Is this true? If so do I need to scan lets say on a 15 min timeframe to get the results desired for a 1 hour time frame? I thought that such results would be in real time or with a 3 min delay as per TOS scanner updates once the condition of Hull Moving Average were met on a 1 hour timeframe...

Perhaps I am doing something wrong here...anyone with experience can you please clarify this? I have read over the section that @markos wrote on the scanner...but none of these questions pertain to what I am experiencing here. Thanks in advance.

Code:
# Basic Hull Ma Pack tinkered by InSilico
# Original Port from https://www.tradingview.com/script/hg92pFwS-Hull-Suite

input modeSwitch = {default "Hma", "Thma", "Ehma"}; #Hull Variation
input length = 55; #Length(180-200 for floating S/R , 55 for swing entry)
input src = close; #Source

def hma;
switch (modeSwitch) {
case "Hma":
    hma = wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)));
case "Ehma":
    hma = expAverage(2 * expAverage(src, length / 2) - expAverage(src, length), round(sqrt(length)));
case "Thma":
    hma = wma(wma(src,(length/2) / 3) * 3 - wma(src, (length/2) / 2) - wma(src, (length/2)), (length/2));
}

def hull = hma;
def transition = hull > hull[2];

# Delete (#) the plot not needed

#plot scanBull = !transition[1] and transition;
plot scanBear = transition[1] and !transition;

# End Basic Hull Ma Pack Scan
 

BenTen

Administrative
Staff member
Staff
VIP
You should use the "True" statement when there is an arrow or an indication on the chart. For example, if there is a function in the code that plot an arrow every time the price crosses above or below the moving average, then in the Scanner, you would want to use the "True" option. If there isn't such a thing, you will have to use "crosses," "crosses above," or "crosses below" in the Scanner.

Looking at the source code, it already include the scan snippet for you.

As far as delay time, it's between 5-10 mins; it doesn't matter which timeframe you're using.
 

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

J007RMC

Well-known member
2019 Donor
Came across this Hull streategy and its working very well... https://tos.mx/XJnywQ4

lGxq2VA.png


Code:
#  One Note Download
addOrder(OrderType.BUY_AUTO, no);input price = close;
input length = 49;
input displace = 0;

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

HMA.DefineColor("Bullish", GetColor(1));
HMA.DefineColor("Bearish", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Bullish") else HMA.color("Bearish"));
AssignPriceColor(if HMA > HMA[1] then HMA.color("Bullish") else HMA.color("Bearish"));
addOrder(OrderType.BUY_AUTO, HMA crosses above HMA[1]);
addOrder(OrderType.Sell_AUTO, HMA crosses below HMA[1]);
 

Similar threads

Top