Stacked Moving Averages
Find the best trends with stacked moving averages, including the 8, 21, and 34 EMA's and the 50 and 200 SMA's.
Introduction
This tutorial covers how to build a Stacked Moving Averages Indicator in ThinkOrSwim.
Moving averages are among the most widely used indicators for traders, and stacking them can help you visually identify bullish and bearish trends. It also spares you the time of adding ~5 different studies to your chart every time, and instead, allows you to add just one.
In this guide, you’ll learn how to add exponential and simple moving averages, customize their appearance, and set up stacked conditions that plot arrows when trends shift.
Volatility Box Invite
We are TOS Indicators.com, home of the Volatility Box.
The Volatility Box is our secret tool, to help us consistently profit from the market place. We’re a small team, and we spend hours every day, after the market close, doing nothing but studying thousands of data points to keep improving and perfecting the Volatility Box price ranges.
We have two different Volatility Boxes - a Futures Volatility Box and a Stock Volatility Box.
Futures Volatility Box - Trade Major Markets With an Edge
Designed For: Futures, Micro-Futures and Index Market Traders
Supported Models: Hourly Volatility Box models
Supported Markets: 10 Major Futures Markets
The Futures Volatility Box comes with:
- 5 Volatility Models for each market
- Support for 10 Futures Markets (/ES, /NQ, /YM, /RTY, /CL, /GC, /SI, /ZB, /HG, /NG)
- Video Setup Guide
- Trade Plan
- Access to all members-only resources, including Squeeze Course
Learn More About the Futures Volatility Box
Trade futures and micro futures with a consistent volatility edge
Stock Volatility Box - Powerful Web Based Volatility Platform
Designed For: Stock and Options Traders
Supported Models: Hourly and Daily Volatility Box models
Supported Markets: 10,000+ Stocks and ETFs (new markets added frequently)
A Stock Volatility Box membership includes access to:
- Live Scanner - A powerful scanner we've built from scratch, to scan 10,000 symbols every 2 seconds for new volatility breaches
- Dashboard - A quick and easy way to view daily volatility model levels online
- Short Interest Scanner - Short interest, Squeeze, and EMA data to find short squeezes
- Squeeze Course - All of our proprietary squeeze tools, including robust backtesters
- All Members Only Indicators - We don't nickel and dime you. Everything really is included.
- And much more!
Learn More About the Stock Volatility Box
Trade stocks and options with a consistent volatility edge
Step-by-Step Code Tutorial
Step 1: Start a New Study
Go to your ThinkOrSwim studies panel and create a new study. Name it “TI Stacked Moving Averages” and clear any default code from the editor.
Step 2: Define the Exponential Moving Averages
We’ll plot three EMAs: the 8, 21, and 34 periods. Here’s how to add them to your script:
plot EMA8 = ExpAverage(close,8);
plot EMA21 = ExpAverage(close,21);
plot EMA34 = ExpAverage(close,34);
Each line creates an EMA based on the closing price. The ExpAverage
function in ThinkOrSwim calculates this for us.
Step 3: Add the Simple Moving Averages
Next, we’ll add two SMAs with 50 and 200 periods:
plot SMA50 = SimpleMovingAvg(close,50);
plot SMA200 = SimpleMovingAvg(close,200);
Here, we use SimpleMovingAvg
to create a simple moving average.
Step 4: Customize Colors and Styles
Now, let’s assign colors to these lines so they’re easily distinguishable, and make the SMAs dashed:
EMA8.setDefaultColor(Color.Cyan);
EMA21.setDefaultColor(Color.Magenta);
EMA34.setDefaultColor(Color.Yellow);
SMA50.setDefaultColor(Color.Yellow);
SMA200.setDefaultColor(Color.White);
SMA50.setStyle(Curve.Short_Dash);
SMA200.setStyle(Curve.Short_Dash);
The setDefaultColor
function applies color, while setStyle
allows us to make the SMAs dashed for easier differentiation.
Step 5: Create Stacked Conditions
Now, we’ll define two conditions: one for when the moving averages are bullishly stacked and one for bear stacking:
def bullstacked = EMA8 > EMA21 and EMA21 > EMA34;
def bearstacked = EMA8 < EMA21 and EMA21 < EMA34;
In the code above, bullstacked
is true when the shorter EMAs are above the longer ones, while bearstacked
is true when the shorter EMAs are below the longer ones.
Step 6: Plot Bullish and Bearish Stacked Arrows
To make it easy to see when the moving averages stack bullishly or bearishly, let’s add arrows. We’ll plot them only when the stack changes:
plot bullStackedPlot = bullStacked and !bullStacked[1];
plot bearStackedPlot = bearStacked and !bearStacked[1];
bullStackedPlot.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
bearStackedPlot.setPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
bullStackedPlot.setDefaultColor(Color.Yellow);
bearStackedPlot.setDefaultColor(Color.Yellow);
The !bullStacked[1]
and !bearStacked[1]
parts ensure that the arrows only plot on the first bar where the condition changes, preventing continuous arrows on each bar in a stacked phase. The setPaintingStrategy
function controls the arrow direction.
Step 7: Apply and Test the Indicator
Save your code and apply it to your chart. Observe the indicator in different market conditions to see the arrows appear when a bullish or bearish stack starts. This indicator gives you an at-a-glance look at when trends begin to stack, helping you identify market shifts quickly.
Conclusion
The Stacked Moving Averages Indicator combines EMAs and SMAs to give a clear view of trend direction and strength. With custom colors, dashed styles, and stack indicators, this tool provides a visual aid for tracking trend shifts in ThinkOrSwim. Try experimenting with different lengths or adding more moving averages to customize it further for your trading needs.
downloads
Download the Stacked Moving Averages Indicator for ThinkorSwim.
The download contains a STUDY.ts file, which you can directly import into your ThinkOrSwim platform.
Download Indicator
Download the Stacked Moving Averages Indicator for ThinkorSwim.
The download contains a STUDY.ts file, which you can directly import into your ThinkOrSwim platform.