XS
SM
MD
LG
XL

Cool Scripts: Create a Stock Momentum Tool with a Twist

Creating a new way of looking at a simple moving average to track stock momentum through thinkScript in thinkorswim.

Print
https://tickertapecdn.tdameritrade.com/assets/images/pages/md/thinkorswim scripting
3 min read

WHAT’S A TREND? To some it’s a resurgence of bell bottoms and big 80’s hair—both of which can be found on my colleagues here on a daily basis. To me, it’s something that happens in a similar way enough times so that one can, with a fairly low level of analysis, predict the general details of the next occurrence. Trends in the market work the same way. But like bell bottoms, what is a trend to me may not be a trend to you. With the following script, let’s look at price trends from a slightly different angle.

The idea is simple (pun intended)—Rather than a traditional simple moving average (SMA) superimposed over the price action in the chart, let’s plot the SMA’s slope as a lower indicator in a thinkorswim® chart to look at it in a different light.

FIGURE 1: SIMPLE MOVING AVERAGE ON ITS HEAD. With this script in thinkorswim®, you can view a simple moving average below the chart, in its own subgraph, giving you a different view of what the trend is, and perhaps a new type of confirming indicator. With the SMA below the chart (labeled "1" above), you may be able to see extremes in the price action more clearly, indicating possible turning points.

The Script

From the thinkScript Editor in thinkorswim enter the following code exactly as you see here, including all spaces and characters. To locate the thinkScript Editor, go to Charts > Studies > Edit studies > New > thinkScript Editor. [Or if you just want to skip to the steps ahead, download the following script.]

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 It All Means

Line 1. Start by telling the Editor that this script should be displayed on the lower subgraph below a chart.

Line 2. The number of bars needed to build your average. In general you can think of the length as the amount of time you want to your trade to be active for, the longer the time accounted for in your average, the longer the trade may need to work.

Line 3. This allows the user of the script to choose which data point she wants to calculate her moving average, based on the stock price bar (i.e. high, low, open, close). We use close by default since we want the average of the closing price.

Line 4. Your favorite type of average. For newbies, start with a simple moving average since it’s the most straightforward.

Line 5. This function does the average math for us. Depending on your length, price, and average type the MovingAverage function will calculate the average.

Line 6. To create a slope you need to know a height and distance. Recall middle school geometry: Rise over Run. In this case our rise is the difference between one average point to the next.

Line 7. Next class, Trigonometry 101. The Atan function is short for arctangent, giving us the angle of height over the length 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. Lastly we make it pretty. As in previous scripts we use the AssignValueColor function to show differences in a positive slope and a negative slope.

For Some Really Geeky Fun. Try adding this script as a Study Filter in the thinkorswim Stock Hacker and adding a line to plot the slope that you want to scan. If that sounds like Greek, call the Trading Desk at 800-669-3900.

Script It!

Skip to the punchline with thinkScript Study. Simply follow the on-screen instructions.

Call Us
800-454-9272
adChoicesAdChoices

Market volatility, volume, and system availability may delay account access and trade executions.

Past performance of a security or strategy does not guarantee future results or success.

Options are not suitable for all investors as the special risks inherent to options trading may expose investors to potentially rapid and substantial losses. Options trading subject to TD Ameritrade review and approval. Please read Characteristics and Risks of Standardized Options before investing in options.

Supporting documentation for any claims, comparisons, statistics, or other technical data will be supplied upon request.

The information is not intended to be investment advice or construed as a recommendation or endorsement of any particular investment or investment strategy, and is for illustrative purposes only. Be sure to understand all risks involved with each strategy, including commission costs, before attempting to place any trade. Clients must consider all relevant risk factors, including their own personal financial situations, before trading.

This is not an offer or solicitation in any jurisdiction where we are not authorized to do business or where such offer or solicitation would be contrary to the local laws and regulations of that jurisdiction, including, but not limited to persons residing in Australia, Canada, Hong Kong, Japan, Saudi Arabia, Singapore, UK, and the countries of the European Union.

TD Ameritrade, Inc., member FINRA/SIPC. TD Ameritrade is a trademark jointly owned by TD Ameritrade IP Company, Inc. and The Toronto-Dominion Bank. © 2018 TD Ameritrade.

Scroll to Top