Trend-Strength Moving Average Spread to ATR For ThinkOrSwim

justAnotherTrader

Well-known member
VIP
VIP Enthusiast
Whats up my fellow Trend Followers!

As someone who trades trends I have to ask myself 3 questions everytime I look at a potential trade.

1) Is There a Trend?
2) How long has it been a Trend?
3) Do I think the Trend will continue?

The downside to being a trend trader is you never show up first, you just hope you dont show up last. That's what this indicator is for. It is super simple, and would be a useful indicator, label, watchlist column and screen.

I am just going to give you the logic, a pretty picture, and some code and if you want to spin it up into something else that awesome sauce =)

Here is the logic:
A trend as far as charts goes is most obvious when a stock is above the 200sma and the 50sma. But not every stock above the 200SMA and 50SMA is a good trend.

Note the following chart:

7mP4tdX.png


This is one of the most commonly tracked ETF's. You will notice from the chart its in a long term uptrend, but you also might notice its been chopping for a bit as noted by the multiple crosses of the 50SMA. Notice how wide the gap is between the 50SMA and the 200SMA. That large gap is a strong indication that the trend is Mature, or what I call a Stage 3 trend.

Now consider the following chart:
lEI4J45.png


This chart has everything a trend is supposed to have right? I mean the price is above the 50SMA and the 200SMA, so its a trend yes? No, its obviously not. That is because the stock has obviously been crossing the 50sma and 200sma for quite a bit of time. We dont want that, this is a chop chart. Verdict, no Trend.

Lastly consider the following chart:
cZV1PGZ.png


This chart is beautiful. The 50SMA crossed the 200SMA and stayed above while the stock mostly respected the 50SMA as a support trend line. Also notice the gap between the 50SMA and the 200SMA. Not to crazy like what we saw on the QQQ. This is what I would call a Stage 2 Trend.

So what is a trend and can we define it mathematically through the use of indicators? I have come up with a method I think is mostly sufficient. A trend exists when the 50SMA-200SMA is greater than or equal to 3*ATR. I model this with (50SMA-200SMA)/(3*ATR) >=1. Why 3*ATR? Because 3*ATR is enough time for a stock to breathe and go through a couple of volatility cycles.

In my world view, a trend has 3 stages:
1) Stage 1 is when the stock initially satisfies the definition of the trend. That would have been about July 7th on the XLI chart above. In this stage the dips become tradeable as long as our supports hold and it respects the moving averages. If it breaks our 50SMA multiple times however its chopping and untradeable. However a break of the 200SMA is required to throw out the company.

2) Stage 2 is when (50SMA-200SMA)/(3*ATR) >=2 (the first time it happens when the trend occurs). This is when a trend is confirmed and the most durable.

3) Stage 3 is when (50SMA-200SMA)/(3*ATR) >=3 (the first time it happens when the trend occurs). This trend is mature, but its also the one you want to be more willing to cut the trade short. Lower size, or tighten stops.

I hope you will find some utility out of this. I dont use a bunch of indicators anymore. I am not smart enough to be a contrarian investor, I show up after the party has already started. My hope is to just get there as close to the beginning without being too early and also to get some idea when the party might end.

Enjoy!

The Code:

Code:
# ============================================
# Normalized Moving Average Spread
# (MA1 - MA2) / (ATR * Multiplier)
# LOWER STUDY
# ============================================

declare lower;

input fastMALength = 50;
input slowMALength = 200;
input maType = AverageType.SIMPLE;

input atrLength = 14;
input atrMultiplier = 3.0;

# === Moving Averages ===
def fastMA = MovingAverage(maType, close, fastMALength);
def slowMA = MovingAverage(maType, close, slowMALength);

# === ATR ===
def atr = Average(TrueRange(high, close, low), atrLength);

# === Spread ===
def maSpread = fastMA - slowMA;

# === Normalized Spread ===
def normalizedSpread = maSpread / (atr * atrMultiplier);

# === Plot ===
plot NormSpread = normalizedSpread;
NormSpread.SetLineWeight(2);

# === Zero Line ===
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.GRAY);

# === Threshold Bands ===
plot UpperBand = 1;
plot LowerBand = -1;

UpperBand.SetStyle(Curve.SHORT_DASH);
LowerBand.SetStyle(Curve.SHORT_DASH);
UpperBand.SetDefaultColor(Color.DARK_GRAY);
LowerBand.SetDefaultColor(Color.DARK_GRAY);

# === Color Logic ===
NormSpread.AssignValueColor(
    if NormSpread > 1 then Color.GREEN
    else if NormSpread < -1 then Color.RED
    else Color.YELLOW
);
 

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