On-screen text: Garrett Kelly, Trader Private Client Services, Senior Manager.
Narrator: Hello traders. In today's video I will give an introduction to our thinkScript® programming language on thinkorswim®. We'll discuss what thinkScript is and how it can be used, the resources available to our thinkscript users and even demonstrate how to alter and create an indicator with thinkscript.
thinkScript is a built-in programming language on the thinkorswim platform that gives you the ability to create your own analysis tools such as studies, watchlist columns, alerts, scan queries, and even conditional orders. Through thinkscript, you gain complete control of the analytical power of the thinkorswim platform. In this video, I'll demonstrate how to create our very own indicator using thinkScript in addition to using thinkScript to further customize some of the existing indicators within thinkorswim.
Now, before I show you how to actually build your own indicator, I'm going to first show you how to view the code for one of the pre-built indicators within thinkorswim so we can take a look at what thinkScript looks like. From a thinkorswim chart, if we open up the edit studies and strategies window, we'll see a list of all of those existing pre-built indicators. You'll notice to the left of each one there's a small scroll icon and if we just click on that, it'll open up our thinkScript editor.
Here I've opened it up for the ATR, or average true range study. You'll see the name at the top and then in the body of the editor the actual thinkScript used. One of the first things I hope you notice is that even without knowing all of the ins and outs of thinkScript, we can certainly infer how this thinkScript is working. We see a declare lower statement, which tells me the ATR study should plot below the price subgraph. We see inputs for length an average type that we'd like to use in our calculation. A plot statement for the ATR including that average and length that we talked about before. And then we see a set default color statement to customize that look and feel of the ATR as it shows.
This same method can be used to look at any of the pre-built indicators to take a look at how the thinkScript works and even copy and paste it into your own codes as we get into later.
Now, to create our very first indicator, I'd like to start with something simple, that already exists in the platform just so we can dip our toes on how this works, a simple moving average. So, to get started, in the lower left, I'll hit create, which will open up a fresh thinkScript editor window for us. And we'll see its input NewStudy0 as the name since we don't have any new studies in the platform just yet. I'll go ahead and change that to MyMovingAverage.
And it's put a line in here for us plot data equals close. We can backspace this out to have a clean slate, and anytime we create a fresh thinkScript, it'll always have that first line in there. So, just keep in mind, you might need to backspace that out.
Now when we're creating any type of new indicator, I usually try to first, think of what data I need for my indicator, what I'd like to do with that data, and then how I'd like to display the results.
So, first, let's begin defining a variable and bringing our data in. For our moving average, we need to bring in the last price or the close. So I'll define a variable, last price. Last price I'll define as close. And since we're saying the close of each bar, this is on a daily chart. So, this would mean a daily close, and you'll see I've ended that with a semicolon. In thinkScript, all of our statements have to end with a semicolon. And if we ever forget it, you'll notice that this entire line highlights it in red and we see an invalid statement error at the bottom. So, if you ever notice that just make sure we end with a semicolon. I'll clear it right up.
Now we need to create our average calculation. So, I'll create a new variable, MyAverage. We need to use the average function to do some math for us here. So, if I start typing, you'll see it'll prefill some of the possible solutions. And the very first one is average.
Now if I'm not quite sure exactly what parameters are available to use in average, if we just click on it, you'll see we have an inspector tool on the right. Wherever your cursor is, that inspector tool will let us know what types of parameters are available to us and what we can change.
In the case of average, his function has two parameters, the data that we're averaging and the length of that average. Now we've already brought in the last price, so I'll go ahead and put that last price in as the data that we're averaging. And I'll set the length to 30 since a 30-period simple moving average is fairly common. We just separate those parameters with a comma. And you can also edit that within that inspector tool as well. So, if I'd ever like that length to say 20 instead of 30, I can just type 20, press enter, and you see it updates right away in the thinkScript editor. For now, we'll stick with 30.
Now, I need to plot this information. Right now we've only defined it, but we haven't actually plotted any of it. So, let's write a plot statement.
On-screen text: Lines of code. Line 1: What data. Line 2: What to do with the data. Line 3: How to display the data.
Narrator: We'll call it MyLine. And my line will equal that, my average calculation that we just wrote together. We'll end once more of the semicolon. And if we hit okay, you'll see that my moving average is now shown in the current studies and strategies on my chart.
If I hit apply, you see right in the background we have our very own simple moving average that's now been added to our chart.
Now that was simple, but I think most of us know that the simple moving average is one of those pre-built indicators that comes with thinkorswim. So, let's create something that isn't necessarily on here just yet. And something that'll help me with some of my own day-to-day account management—make things a little bit easier.
What I'd like to do is create a new study. So, I'll create that create button. And this one I'd like to be an indicator of some percent change information I'd like to have right at my fingertips. Percent changes over different periods of time.
So, I can see details like the percent change today, over a year, and maybe even over a month, as well. So, let's start this from scratch. Just like we did with that simple moving average, we'll delete our plot data equals close line that populated for us, and we'll give it a fresh name. That we'll call PercentChangeLabels.
Now, to begin defining our first variable, in this one we'll have to do, just a little bit more math to create a percent change. So, I'll define a variable using def and then put PercentChange. And percent change will need to equal the close, divided by the previous close. And to define previous close, I'll put the number one in brackets, which means to look back one period.
So, we've got the current close and then the close from one bar ago. And since we're on a daily chart, that would mean current close and the close of yesterday's bar. If we just subtract one from that, put a semicolon, we now have a percent change calculation that we can begin to work with.
Now, as I mentioned, I would not like for this one to be a line like the simple moving average. What I'm hoping for is that I have a label, just some quick data, that I can reference. So, I'll use the addlabel function. I'm going to let our inspector help us out once more with this so we can recall exactly what parameters are available. So now that I've typed it with the open/close parenthesis, I will click on it. You'll see the inspector updates to let me know that there are three parameters available to me. I have an input whether it's visible, the text to show, and the color.
Now, we can definitely get complicated and add conditions for when this should show or not show, but for now, I'll just keep it easy. I'd like this to be shown at all times. So, I'll put yes. The text we've already created, we wrote percent change and that's where I'd like it to show.
We can keep the color red just to keep things easy for now.
Now, if I hit okay, so we can take a look at our work so far. We'll hit apply.
You'll see in the upper left I now have that label but there's a couple things that I don't love about it. It's a decimal instead of a percent in format and it doesn't really have any text that tells me what this information actually is. It's just the number. So, let's make some tweaks.
If I open up our addlabel command here one more time, we can use a couple functions to clean this up. The first one I'll use is aspercent. If I use as percent and put percent change variable within that so the percent change variable becomes the parameter for the as percent function. I now, if I apply, have changed, the formatting of my label to display this information as a percent.
Now, I'd like to add some text to inform me of what this is showing me. So, I'll put in quotes percent change, the colon, and I'm going to put a plus in between our text and then value that we have calculated to indicate that we'd like both of them. If I apply that, see I now have a percent change label that actually tells me what it is and we have it displayed as a percentage format.
This is great. This is a good start. But the next thing I'd like is for this to show me aggregation periods that aren't what I currently have charted. I mentioned this is currently on a daily chart but if I'd like this information to give me a year percent change or a month percent change, we have to do one more thing.
So, let's take what we've already created, I'm going to copy it so we don't have to restart this from scratch, and even in the same thinkScript editor, I'll paste that down below.
Now to make this next label, which will make a month percent change, first, we see this error because I've used the variable percent change twice. We can't use the exact same variable twice or define it twice, I should say. So, we'll give it a new name, and we'll call it MonthPercentChange. Now to make sure we, we're keeping things consistent, I'm also going to update the text, so it actually says MonthPercentChange. And then I'll make sure that the variable that we reference will be month percent change below, as well.
Now the only other problem that we still have is that this is still the same calculation. This is still just close divided by close with an offset of one. I need to specifically declare that this close is from the previous month. So, you'll notice, if I use the inspector once more, I can actually set the parameter for the close to be a particular period. So, I'll change that to be a month period. You'll see that updates in my thinkScript immediately. If I hit apply, I'll move this to the side so we can see, we now have a second label. We have a month percent change and we see it displayed as a percent, just like our first one—with the text key to help us out.
Let's do one more of these, so we get a year. So, we'll start out the same way. As in that, just the, the very first label we created. We'll adjust the variable. We have a YearPercentChange, update our text key, and I'll update the variable below as well. Now, just like we did in the previous one, I'll need to update that close period to be the close of a year. And we now have three different addlabel statements all in one thinkScript. One is showing me the close divided by the previous close as our percent change. The other one is showing me a month percent change, and the final one we just wrote is a year percent change.
If I apply that, and hit okay, we can see our final results here. We now have a total of three labels, one is that DayPercentChange since we're on a daily chart. We have a MonthPercentChange. And we have a YearPercentChange all at our fingertips.
And you'll notice that if I were to change symbols, it'll update real time and easily give me that information nice and quick, so I have to spend quite so much time looking for it in the future.
Animation: A new Edit Studies and Strategies window is opened. PERC is entered int the search bar.
Narrator: Now I'd also like to show how to customize some of the existing indicators that are already within thinkorswim. If I stick with this same theme, looking at percent change, we can find the default percent change study that exists in the platform. If I add this to my chart, you'll see that we get a line, and that line is showing us the percent change of the security over the defined number of periods, which is 14. And it's using closing prices, so in this case, because we're on a daily chart, this is showing me percent change information over 14 days.
Now one of the things that I'd like to point out is that if we look at the ways that we can adjust this study without thinkScript. We still have plenty of inputs to customize this and make this our own, but let's say that I wanted this study to, instead of showing me, 14 days percent change, I wanted it to just be today. Specifically, I wanted it to be today since market open. What's the percent change just since market open?
Well, I run into some difficulties because I can't change this price to just be open, it would be open to open or close to close. I can't say to look at the current close or current price last price compared to market open. So, let's take a look at the thinkScript and see how this is actually working.
I'm going to right click and use duplicate which will give me a fresh thinkScript editor, but it'll already have copied in the entire thinkScript for that pre-built study for me. So, we can start with the full thing.
Now, as we look through this there might look like there's a little bit more on here than we've looked at before, but the reality is we just have a few inputs that we're using for our calculations. We see the plot statement percent change, which is the one that we definitely want to focus on here. And then we see some various colors and customization below.
So, if we look at that plot percent change line, you'll see that really our problem is just this price variable. We're taking price divided by price with an offset of that length input. And so, anytime I change price from close to open or vice versa, I'm changing both of them. I can't have one be close and one be open. So, we can make that change right here, right now.
If I change that to plot percent change equals 100 times. And then in parentheses, price divided by open minus one. If I apply this right now, you'll see I have both the original percent change and my very own percent change below. Now, the original is still using that normal calculation looking at 14 days percent change. Whereas mine is simply looking at the current close compared to the same bar's open. So, since we're on a daily chart, today's open. So this is giving me the percent change sense open, exactly like what I was hoping it would give me.
And we can continue with these types of changes throughout many many indicators. And we can even bring this same calculation back to the label that we wrote before. If we would now like to have a price change since open label.
I'll copy and paste this one last time and we'll use exactly what we did in our percent change study. We're taking close divided by open. We'll call this the OpenPercentChange. So we have a new variable, give it a fresh text label, and change the OpenPercentChange in our variables below as well. If we apply that, and take a look, you'll see that now we have one final percent change label that says OpenPercentChange. And you'll see we got 0.56%.
And down below, we see the exact same value for our line. So now we have two different ways to give ourselves the exact same information, whichever we'd prefer.
Now the last thing I'd like to show you in this demonstration is one of the most valuable resources when you're learning thinkScript. Or even if you're an expert and you just need a reference, and that's our Learning Center.
From the Education tab on the top, if you head to Learning Center, the homepage you'll be met with has four tiles. The tile on the far right is thinkScript. This Learning Center page has everything from tutorials that are basic through advanced, to a full glossary of the functions, constructed, declarations. You name it, they're available to you in thinkScript along with examples of how to apply each one.
Now we have shown how to use the thinkScript editor, how to create a custom indicator with thinkScript, and how to adjust an existing indicator. The possibilities with thinkScript are endless. And I hope you feel encouraged to take advantage of this unique tool.
Learn more check out our other videos and resources such as the thinkorswim Learning Center.
On-screen text: [Schwab logo] Own your tomorrow®