Create a Momentum Indicator with Moving Averages

August 28, 2023 Beginner
Learn to use simple moving averages (SMAs) to track stock momentum by creating scripts with the thinkScript® Editor on the thinkorswim® trading platform.

Traders and investors often overlay a simple moving average1 (SMA) on price bars to try and figure out if prices are in a trend. Trends are related to momentum, and one way traders might gauge that momentum is to look at the slope of an SMA. A steeper slope might potentially indicate more momentum, while a shallower slope might potentially indicate weaker momentum. One approach traders can use to see the slope is to plot the SMA's slope as a lower indicator on a chart on the thinkorswim® trading platform. Although there are hundreds of indicators and tools available on thinkorswim, traders can also use the thinkScript® tool to tailor an indicator to meet their needs. 

How to create the script

To locate the thinkScript Editor on thinkorswim, select Charts > Studies > Edit studies... and the Edit Studies and Strategies window will open. Select Create... to open the New script window. A predefined script appears in line 1. Start by deleting the script in line 1 and enter the following code, including all spaces and characters:

  1. declare lower;
  2. input length = 20;
  3. input price = close;
  4. input averageType = AverageType. SIMPLE;
  5. def avg = MovingAverage(averageType, price, length);
  6. def height = avg - avg[1];
  7. plot "Angle, deg" = Atan(height/length) * 180 / Double.Pi;
  8. "Angle, deg".AssignValueColor (if "Angle, deg" >=0 then color.ORANGE else color.BLUE);

What the code means

Line 1. This tells the editor the script should be displayed on the lower subchart below a chart.

Line 2. The number of bars needed to build the average. A trader can select a length based on how far back (in days) they want to go to calculate the average. A 20-day SMA will likely be closer to the price bars than a 100-day SMA.

Line 3. This allows the trader to choose the data point to calculate the SMA. This could be the high, low, open, or close. In this example, the trader wants the average closing price.

Line 4. On this line, the trader inputs the type of moving average preferred. Common choices include simple, exponential, weighted, or any other moving average type.

Line 5. This function calculates the average. Depending on the length, price, and moving average type chosen, the MovingAverage function will do the math.

Line 6. Slope equals rise over run, so height and distance are required to create the slope. In this case, rise is the difference between one average point to the next.

Line 7. The Atan function is short for arctangent and gives the angle of height over the length that's used to calculate the moving average. The second half of this line converts the angle into degrees. Adding quotations around the variable "Angle, deg" creates a variable name with a space.

Line 8. The AssignValueColor function allows a trader to assign different colors to a positive slope and negative slope. The colors make it easier to see the difference between the two slopes.

After inputting the code, select Apply to add the indicator to the subchart (see chart below).

Simple moving average slope

Chart demonstrates the custom SMA indicator added to the subchart using thinkScript on thinkorswim. When looking at the slope of the SMA below the chart, the bubble indicates how a steeper slope suggests the potential for greater momentum.

Source: thinkorswim platform

For illustrative purposes only. Past performance does not guarantee future results.

There are different ways to apply a moving average when it's plotted on a subchart. One way is to look at the slope. When the slope of the moving average is relatively steeper, it could suggest the trend has more momentum. If the SMA slope starts moving lower, it could indicate a slowdown, which might indicate the trend could reverse. Understanding how a SMA slope indicates momentum can potentially help a trader make more informed decisions about entry and exit points for a trade.

To take it a step further, traders can consider adding this script as a Study filter using the thinkorswim Stock Hacker tool and adding a line to plot the slope they want to scan.

1A simple moving average (SMA) is a technical indicator that's calculated by adding the closing price of a stock or other security over a specific period of time and dividing the total by the appropriate number of trading days. For example, a 20-day SMA is the average closing price over the previous 20 days.