About thinkScript
thinkScript is an interesting scripting language primarily for chart studies within Think or Swim's desktop trading software. It is the most primitive scripting language out of the various languages I cover on this site, but if you use Think or Swim it can be very useful.
thinkscript can be used to create studies and in a limited way for setting off alerts. Studies are graphical elements displayed on top of a price chart, or below it as a "lower study". A simple example of a study is a moving average.
Concepts
Period: A period is a segment of time. In trading software common periods are: 1 day, 30 minutes, 10 minutes, 1 minute. On a chart each period is often represented by a candle or bar.
Plot: All studies end with a plot statement, for example:
plot Data = (high + low) / 2);
The plot statement plots a point on the Y axis of the chart at the current time for the value given. The above code would plot the numerical average between the high and low price for the current time. If the period is set to day, then high is the high of the day, and low is the low of the day.
Recursion: thinkScript was designed for plotting points on a graph through time. When you write a custom study the plot statement of the study is calculated for each period or bar on the chart from left to right. Your thinkScript study is essentially running inside a foreach loop. For every period of time, plot the y value as calculated in this study. It is possible to store values in each run though, and retrieve them later in time. This allows you to change your plot based on observations you made previously. In thinkScript this is referred to as recursion, though it doesn't feel like real recursion since there's no way to get stuck and your always moving in one direction through time.
thinkScript for Alerts: When creating complex alerts that trigger based on thinkScript you must keep in mind that you are now operating outside the foreach loop that studies operate in. In this situation your formula is run once to see if the result triggers an alert. You can still go back in time using the array functionality of price to create your own comparisons over time.