Back in the early days of thinkorswim®, traders clamored for more charting tools like technical studies and strategy testing. Originally, the development team wrote these individual tools in the platform’s rather complex programming language. That was great for performance, but clunky for simpler ideas like “subtract the 10-day moving average from the 30-day moving average.”
Today, our programmers can still do it. But why not give traders the ability to do it themselves, while creating their own custom chart data using a simple language? With this lightning bolt of an idea, thinkScript was born.
No, thinkScript is not an add-on, plug-in, or something to download. And best of all, you don’t need to be a computer geek to learn it. That means ordinary traders like you and me can learn enough about thinkScript to make our daily tasks a little easier. At the closing bell, this article is for regular people. Not programmers.
Let’s Get Crackin’
thinkScript is most frequently used on the Charts and the MarketWatch tabs. Think of accessing it the same way you’d add a technical study, because the thinkScript editor that lets you write the thinkScript code exists inside the Chart studies and Quotes page.
1. Click on the Studies button.
2. Select “Edit Studies” in the new window that opens up (Figure 1).
3. Click on the “New” button in the lower-left-hand corner. That opens up a thinkScript editor with a default thinkScript code—“plot Data = close;”— inside it. You can delete that code and start typing your own in that field.
Note the menu of thinkScript commands and functions on the right hand side of the editor window. That’s a thinkScript library with quick definitions of each of the functions.
1. On the MarketWatch tab click Quotes in the top menu.
2. From the Quotes page, click on the small dot in the upper-left-hand corner next to the word “Symbol.”
3. Select “Customize” from the drop-down menu.
4. Scroll down the list of “Available Items” and click on one of the numbered “Custom” columns.
5. Double click to open the same thinkScript editor window that’s on Charts (Figure 1).
6. When you’re done writing your thinkScript code, hit “Apply” to display it on a chart or see it as a column on the Quotes page.
As I mentioned, you can script just about anything you want that’s not in the platform (within reason, of course). To get started, let’s look at a few cool examples you might want to try.
1. Technical Indicator: Moving-Average Crossover
Above all, thinkScript was created to tackle technical analysis. This is the code for a moving-average crossover shown in Figure 1, where you can see 10-day and 30-day simple-moving averages on a chart. Follow the steps described above for Charts scripts, and enter the following:
Huh? Let’s back up and clarify terms. “def”—Defines something in thinkScript. It says “define this thing named ‘tenday’ as referencing the study ‘simplemovingavg,’ which uses 10 days of data.” “def” also defines “thirtyday” as a simple-moving average that uses 30 days of data.
“reference”—A command of sorts that pulls studies into your code already written in thinkScript. As you know, developers have already created hundreds of studies. Save yourself time and use “reference” whenever you can. Here, thinkScript is pulling in a study called “simplemovingavg.” You can find “simplemovingavg” in the studies list on thinkorswim Charts. Once you find a study, reference it in your code. In this moving-average crossover code, the “tenday” is telling the simplemovingavg study to use “length = 10.” That means use 10 days of prices in the moving-average calculation. The “length = 30” tells the “thirtyday” simple-moving average to use 30 days of price data.
“plot”—Once you’ve defined the things for your chart, display them with the “plot” command. In this moving-average crossover, we’re plotting two lines—a 10-day moving average, and a 30–day moving average. So, we’ll need to create two plots and call them different things. I just created “plot data1” and “plot data2,” and told them to display what we just defined. “plot data1 = tenday” means “the plot command will display this thing called ‘data1,’ which we defined above as ‘tenday.’” “plot data2 = thirtyday” does the same thing for the 30-day simple-moving average.
By the way, at the end of each line of thinkScript code you’ll notice a semicolon (“;”). That tells thinkScript that this command sentence is over. Also, a trained programmer could write a thinkScript code for colors and all sorts of other things on a moving-average crossover. Don’t worry about that for now. Learn just enough thinkScript to get you started. You’ll go bonkers trying to figure it all out at once.
2. Custom Volatility: IV Percentile
If you want options data that doesn’t currently exist as a platform feature, why not create it yourself? Another handy trick of thinkScript allows the Market-Watch tab to display a metric for a stock list on a Quotes page. You may already be familiar with the “Current IV Percentile” in the Trade page’s “Today’s Options Statistics” section. That number shows the current overall implied volatility of a stock’s options, relative to its past year’s high-to-low range. But what if you want to see the IV percentile for a different time frame, say, 3 months? (See Figure 3.)
Following the steps described for the Quotes scripts, enter this:
This thinkScript code defines four things—“ivol,” “lowvol,” “highvol,” and “currentvol,” and bases them on the value of “imp_volatility.” “imp_volatility” is a study that gives you the platform’s “Vol Index” number, which is a stock’s options’ overall implied volatility. The “if !IsNaN” returns zero if the Vol Index is unavailable for a symbol. The “lowest” and “highest” are commands that order thinkScript to find the lowest or highest “ivol” over the previous 60 days. The “plot” command displays the results of a formula using the things we’ve defined.
You can change “60” to whatever number for which you want to see the range. Keep in mind that each month has about 20 trading days, so 60 trading days is about three months. If you want to show a yearly number, use “262,” which is approximately a year’s trading days. To get this into a Watch List, follow these steps on the Market Watch tab:
1. Click on the Quotes sub-tab.
2. Click on the dot to the left of the word “Symbol,” in the upper-left-hand corner of the Quotes tab.
3. Select “Customize” from the drop-down menu.
4. When the “Customize Quotes” box opens, click on one of the “Custom” choices in the list of “Available Items.” That opens the “Custom Quote Formula” box, where you can click on the thinkScript editor tab and write in the code. Remember to name your thinkScript code so you can add it to your Quotes list.
thinkScript is also used on thinkorswim charts as a technical analysis back-testing tool. With this feature you can see the potential profit and loss for hypothetical trades generated on technical signals. Bear in mind, this does not include commission costs, which will affect your true P/L.
Refer to Figure 4. Let’s review strategy results that get long (buy a stock or option) when a 10-day moving average crosses above the 30-day moving average; and get short (sell a stock or option) when a 30-day moving average crosses above a 10-day moving average. To do this, we have to write two scripts and separate them.
To combine the two and add this backtesting script to a chart, do the following:
1. Right click on a chart, and select “Studies,” then “Edit Studies.”
2. This time, click the “Strategies” tab in the upper-left-hand corner.
3. Then click “New” in the lower-left-hand corner. When the thinkScript Editor tab opens, enter the code under thinkScript 1 above.
4. Give it an easy name like MovingAvgBuy.
5. Click “OK” button in the lower-right-hand corner to close the thinkScript editor.
6. Click “New” again.
7. Enter a name like MovingAvgSell.
8. Click “OK.”
9. Look for the thinkScripts you just created in the Strategies list. Double click to see them in the “Added Studies and Strategies” window.
10. Hit “Apply.”
Notice on the chart in Figure 4, you’ll see buy-and-sell signals. To see profit/loss, carefully right click one of the chart’s trade signals. Then, select “Show Report” from the drop-down menu. The thinkScript code does this through the “AddOrder” command. This code specifies “Buy_Auto” when the “sma10” is greater than “sma30,” and “Sell_Auto” when “sma10” is less than “sma30.” Together, they create the chart’s hypothetical buys and sells. thinkScript also has commands for opening and closing buy-and-sell orders so you can create specific testing scenarios.
The “tickColor,” “arrowColor,” and “GetColor” are commands thinkScript uses to add color to buy-and-sell signals. The numbers “5” and “6” refer respectively to red and green.
Bonus Script: Script Alerts
Being tied into the markets doesn’t mean being tied to your computer. If you’re out and about, and don’t have time to watch the SPUs on TD Ameritrade’s mobile trading apps, the alert functionality on the thinkorswim® platform lets you write custom technical indicators and have messages sent to your phone or mobile device when the indicator reaches a certain level or value.
1. On the Market Watch tab, click on the Alerts sub-tab.
2. Click the Study Alert button in the upper-right-hand corner.
3.When the Study Alerts box opens, click the thinkScript editor tab.
4. You’ll be presented with “SimpleMovingAvg( )” to get you started. Delete that if you don’t want to be alerted on a moving average. But as an example, this is the code you would write to be alerted if the 30-day moving average moves above the 10-day moving average. There are other controls in the Study Alerts box, like the “Aggregation” period at the top that lets you choose intraday, daily, weekly, or monthly data.
There’s also the “Trigger if” drop-down menu that alerts you if the value of your thinkScript study meets certain conditions. When you’re done, click on the Create Alert button in the lower right, and you’re almost done.
To make sure you get messages with triggered alerts:
1. Click on Application Settings in the platform’s upper-right corner.
2. Click the “Notifications” sub-tab.
3. In the “Notify about” list, choose “Alert is triggered.”
4. Check a notification method under “Alert settings” such as email or SMS. You’ll need a confirmed email address or phone number for SMS to set up alert notifications, which you’ll do at the top of the “Notifications” sub-tab.
There you have it. Use thinkScripts for alerts, and you’ll never have to miss a trading signal again!
Okay, we couldn’t help but get a little bit geeky on that last script, but we’ve only scratched the surface of what thinkScript can do. If you have an idea for your own proprietary study, or want to tweak an existing one, thinkScript is about the most convenient and efficient way to do it. And you just might have fun doing it.