Safe Hull Moving Average Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
This study defines trend and momentum by taking the Hull Average Of The MACD.

Using the MACD Hull Moving Average (HMA) filters out noise and allows the identification of broader market trend.

Blue confirms bullish trend and momentum
Red confirms bearish
Orange == trend and momentum cannot be confirmed.

This approach can help to reduce the number of false signals and improve the overall accuracy of trading decisions.

UrsgHRq.png


thinkScript Code

Rich (BB code):
input price = close;
input length = 100;
input displace = 0;

input FastLength = 180;
input SlowLength = 208;
input MACDLength = 72;

input AverageType = {SMA, default EMA};

def macd = MACD(FastLength, SlowLength, MACDLength, AverageType).Diff;

def halflength = Ceil(length / 2);
def sqrtlength = Ceil(Sqrt(length));
def val = 2 * wma(price, halflength) - wma(price, length);

plot HMA = wma(val, sqrtlength)[-displace];
HMA.SetDefaultColor(GetColor(1));

HMA.SetLineWeight(2);
HMA.DefineColor("UP", Color.BLUE);
HMA.DefineColor("DOWN", Color.RED);
HMA.DefineColor("NEUTRAL", Color.ORANGE);

def isUP;
def isDOWN;
def isNEUTRAL;

if HMA > HMA[1] and macd >= 0 then {
    isUP = yes;
    isDOWN = no;
    isNEUTRAL = no;
} else {
    if HMA < HMA[1] and macd <= 0 then {
        isUP = no;
        isDOWN = yes;
        isNEUTRAL = no;
    } else {
        isUP = no;
        isDOWN = no;
        isNEUTRAL = yes;
    }
}

HMA.AssignValueColor(if isUP then HMA.color("UP") else if isDOWN then HMA.color("DOWN") else HMA.color("NEUTRAL"));

Shareable Script

https://tos.mx/nbwaBU
 
Last edited by a moderator:
question: is this updated and what time period do you recommend? I read different from 9,20,50,105. also what timeframe?
No dog in this fight but there are articles on the web re strengths of various moving average studies. Hull is not ranked that high and, in fact, the old standbys of MACD, and 50/100/200 crossovers produced better results. Just an FYI. and sure you'll test any study before putting into practice. All the best.
 
Add this to the bottom of your script:
Ruby:
#Add Arrows
def UPcondition = isUP ;
def DNcondition = isDOWN ;
plot upArrow = !UPcondition[1] and UPcondition;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetDefaultColor(color.cyan) ;
upArrow.SetLineWeight(1);

plot dnArrow = !DNcondition[1] and DNcondition;
dnArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
dnArrow.SetDefaultColor(color.magenta) ;
dnArrow.SetLineWeight(1);


Here is a tutorial on how to create your own arrows on any script:
https://usethinkscript.com/resources/thinkscript-boolean-arrows-in-thinkorswim.17/
 
Last edited:
This study defines trend and momentum by taking the Hull Average Of The MACD.

Using the MACD Hull Moving Average (HMA) filters out noise and allows the identification of broader market trend.

Blue confirms bullish trend and momentum
Red confirms bearish
Orange == trend and momentum cannot be confirmed.

This approach can help to reduce the number of false signals and improve the overall accuracy of trading decisions.

View attachment 4223

thinkScript Code

Rich (BB code):
input price = close;
input length = 100;
input displace = 0;

input FastLength = 180;
input SlowLength = 208;
input MACDLength = 72;

input AverageType = {SMA, default EMA};

def macd = MACD(FastLength, SlowLength, MACDLength, AverageType).Diff;

def halflength = Ceil(length / 2);
def sqrtlength = Ceil(Sqrt(length));
def val = 2 * wma(price, halflength) - wma(price, length);

plot HMA = wma(val, sqrtlength)[-displace];
HMA.SetDefaultColor(GetColor(1));

HMA.SetLineWeight(2);
HMA.DefineColor("UP", Color.BLUE);
HMA.DefineColor("DOWN", Color.RED);
HMA.DefineColor("NEUTRAL", Color.ORANGE);

def isUP;
def isDOWN;
def isNEUTRAL;

if HMA > HMA[1] and macd >= 0 then {
    isUP = yes;
    isDOWN = no;
    isNEUTRAL = no;
} else {
    if HMA < HMA[1] and macd <= 0 then {
        isUP = no;
        isDOWN = yes;
        isNEUTRAL = no;
    } else {
        isUP = no;
        isDOWN = no;
        isNEUTRAL = yes;
    }
}

HMA.AssignValueColor(if isUP then HMA.color("UP") else if isDOWN then HMA.color("DOWN") else HMA.color("NEUTRAL"));

Shareable Script

https://tos.mx/nbwaBU
Would someone be so kind as to add option for color candles, same as Moving Average? Thank you very much!
 
Would someone be so kind as to add option for color candles, same as Moving Average? Thank you very much!

paste this to the bottom of your study:
Code:
Assignpricecolor(if isUP then HMA.color("UP") else if isDOWN then HMA.color("DOWN") else HMA.color("NEUTRAL"));
 
Last edited by a moderator:
I really like the 10/30 HULL. This script is MA position aware and spread of MA aware with labels.
Code:
#========================================================
# HULL THERMAL MA ENGINE
# 10 / 30 Hull Moving Average
# Fast HMA thermal awareness vs Slow HMA
# antwerks
#========================================================

declare upper;

input fastHMALength = 10;
input slowHMALength = 30;

input expansionLookback = 3;
input percentStretchWarn = 1.00;
input percentStretchHot = 2.00;

input showCloud = yes;
input showLabels = yes;
input showSignals = yes;
input colorPriceBars = no;

def na = Double.NaN;

#-----------------------------
# HULL MOVING AVERAGES
#-----------------------------
plot FastHMA = HullMovingAvg(close, fastHMALength);
plot SlowHMA = HullMovingAvg(close, slowHMALength);

FastHMA.SetLineWeight(3);
SlowHMA.SetLineWeight(3);

#-----------------------------
# DISTANCE / SPREAD
#-----------------------------
def spread = FastHMA - SlowHMA;
def spreadPct = if SlowHMA != 0 then (spread / SlowHMA) * 100 else 0;

def absSpreadPct = AbsValue(spreadPct);

def spreadExpanding =
    AbsValue(spread) > AbsValue(spread[expansionLookback]);

def spreadContracting =
    AbsValue(spread) < AbsValue(spread[expansionLookback]);

def fastLeading = FastHMA > SlowHMA;
def fastLagging = FastHMA < SlowHMA;

#-----------------------------
# SLOPE
#-----------------------------
def fastRising = FastHMA > FastHMA[1];
def fastFalling = FastHMA < FastHMA[1];

def slowRising = SlowHMA > SlowHMA[1];
def slowFalling = SlowHMA < SlowHMA[1];

#-----------------------------
# THERMAL STATES
#-----------------------------
def bullExpansion =
    fastLeading and fastRising and slowRising and spreadExpanding;

def bullCooling =
    fastLeading and spreadContracting;

def bearExpansion =
    fastLagging and fastFalling and slowFalling and spreadExpanding;

def bearCooling =
    fastLagging and spreadContracting;

def compression =
    absSpreadPct < 0.25;

def stretched =
    absSpreadPct >= percentStretchWarn;

def hotStretch =
    absSpreadPct >= percentStretchHot;

#--------------------------------------------------
# THREADING / BRAIDING ENGINE
#--------------------------------------------------

input atrLengthThread = 14;

def atrThreadValue = Average(TrueRange(high, close, low), atrLengthThread);

def hmaSpreadValue =
    AbsValue(FastHMA - SlowHMA);

def spreadATRRatio =
    if atrThreadValue != 0
    then hmaSpreadValue / atrThreadValue
    else 0;

def threadingState =
    spreadATRRatio < 0.10;

def braidedState =
    spreadATRRatio >= 0.10 and
    spreadATRRatio < 0.35;

def expandingState =
    spreadATRRatio >= 0.35 and
    spreadATRRatio < 0.75;

def trendingState =
    spreadATRRatio >= 0.75;

#-----------------------------
# COLORS
#-----------------------------
FastHMA.AssignValueColor(
    if bullExpansion then Color.GREEN
    else if bullCooling then Color.CYAN
    else if bearExpansion then Color.RED
    else if bearCooling then Color.ORANGE
    else Color.GRAY
);

SlowHMA.AssignValueColor(
    if slowRising then Color.YELLOW
    else if slowFalling then Color.MAGENTA
    else Color.GRAY
);

#==================================================
# THREADING CLOUD
#==================================================

AddCloud(
    if showCloud and threadingState then FastHMA else Double.NaN,
    if showCloud and threadingState then SlowHMA else Double.NaN,
    Color.CYAN,
    Color.CYAN
);

#==================================================
# BRAIDED CLOUD
#==================================================

AddCloud(
    if showCloud and braidedState then FastHMA else Double.NaN,
    if showCloud and braidedState then SlowHMA else Double.NaN,
    Color.YELLOW,
    Color.YELLOW
);

#==================================================
# BULL EXPANSION CLOUD
#==================================================

AddCloud(
    if showCloud and fastLeading and !threadingState and !braidedState then FastHMA else Double.NaN,
    if showCloud and fastLeading and !threadingState and !braidedState then SlowHMA else Double.NaN,
    Color.GREEN,
    Color.GREEN
);

#==================================================
# BEAR EXPANSION CLOUD
#==================================================

AddCloud(
    if showCloud and fastLagging and !threadingState and !braidedState then FastHMA else Double.NaN,
    if showCloud and fastLagging and !threadingState and !braidedState then SlowHMA else Double.NaN,
    Color.RED,
    Color.RED
);

#-----------------------------
# SIGNALS
#-----------------------------
def bullCross =
    FastHMA crosses above SlowHMA;

def bearCross =
    FastHMA crosses below SlowHMA;

plot BullCrossSignal =
    if showSignals and bullCross then low else na;
BullCrossSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullCrossSignal.SetDefaultColor(Color.GREEN);
BullCrossSignal.SetLineWeight(4);

plot BearCrossSignal =
    if showSignals and bearCross then high else na;
BearCrossSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearCrossSignal.SetDefaultColor(Color.RED);
BearCrossSignal.SetLineWeight(4);

plot CoolingDot =
    if showSignals and bullCooling and !bullCooling[1] then high else na;
CoolingDot.SetPaintingStrategy(PaintingStrategy.POINTS);
CoolingDot.SetDefaultColor(Color.CYAN);
CoolingDot.SetLineWeight(4);

plot BearCoolingDot =
    if showSignals and bearCooling and !bearCooling[1] then low else na;
BearCoolingDot.SetPaintingStrategy(PaintingStrategy.POINTS);
BearCoolingDot.SetDefaultColor(Color.ORANGE);
BearCoolingDot.SetLineWeight(4);

#-----------------------------
# PRICE COLOR
#-----------------------------
AssignPriceColor(
    if !colorPriceBars then Color.CURRENT
    else if bullExpansion then Color.GREEN
    else if bullCooling then Color.CYAN
    else if bearExpansion then Color.RED
    else if bearCooling then Color.ORANGE
    else Color.GRAY
);

#-----------------------------
# LABELS
#-----------------------------
AddLabel(
    showLabels,
    if fastLeading then "HMA STATE: FAST LEADING 30"
    else if fastLagging then "HMA STATE: FAST BELOW 30"
    else "HMA STATE: NEUTRAL",
    if fastLeading then Color.GREEN
    else if fastLagging then Color.RED
    else Color.GRAY
);

AddLabel(
    showLabels,
    "SPREAD: " + Round(spreadPct, 2) + "%",
    if spreadPct > 0 then Color.GREEN
    else if spreadPct < 0 then Color.RED
    else Color.GRAY
);

AddLabel(
    showLabels,
    if bullExpansion then "THERMAL: BULL EXPANDING"
    else if bullCooling then "THERMAL: BULL COOLING"
    else if bearExpansion then "THERMAL: BEAR EXPANDING"
    else if bearCooling then "THERMAL: BEAR COOLING"
    else "THERMAL: MIXED",
    if bullExpansion then Color.GREEN
    else if bullCooling then Color.CYAN
    else if bearExpansion then Color.RED
    else if bearCooling then Color.ORANGE
    else Color.GRAY
);

AddLabel(
    showLabels,
    if hotStretch then "STRETCH: HOT / TAKE CARE"
    else if stretched then "STRETCH: EXTENDED"
    else if compression then "STRETCH: COMPRESSED"
    else "STRETCH: NORMAL",
    if hotStretch then Color.MAGENTA
    else if stretched then Color.YELLOW
    else if compression then Color.CYAN
    else Color.GRAY
);

AddLabel(
    showLabels,
    if fastLeading and slowRising then "STRATEGY: LONG BIAS"
    else if fastLagging and slowFalling then "STRATEGY: SHORT / DEFENSIVE"
    else if bullCooling then "STRATEGY: TRAIL / WATCH PULLBACK"
    else if bearCooling then "STRATEGY: BEAR RELIEF / WATCH FAIL"
    else "STRATEGY: WAIT",
    if fastLeading and slowRising then Color.GREEN
    else if fastLagging and slowFalling then Color.RED
    else if bullCooling then Color.CYAN
    else if bearCooling then Color.ORANGE
    else Color.GRAY
);

AddLabel(
    showLabels,
    "STRUCTURE: " +
    (if threadingState then "THREADING"
     else if braidedState then "BRAIDED"
     else if expandingState then "EXPANDING"
     else "TRENDING"),
    if threadingState then Color.CYAN
    else if braidedState then Color.YELLOW
    else if expandingState then Color.GREEN
    else Color.MAGENTA
);

AddLabel(
    showLabels,
    "ATR SPREAD: " +
    Round(spreadATRRatio, 2),
    if spreadATRRatio < 0.15 then Color.CYAN
    else if spreadATRRatio < 0.35 then Color.YELLOW
    else if spreadATRRatio < 0.75 then Color.GREEN
    else Color.MAGENTA
);

AddLabel(
    showLabels,
    "LEAD: " + Round(spreadATRRatio,2) + " ATR",
    if threadingState then Color.CYAN
    else if braidedState then Color.YELLOW
    else if expandingState then Color.GREEN
    else Color.MAGENTA
);

 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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