# Cool Scripts: Create a Stock Momentum Tool with a Twist

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.