thinkScript learning path
thinkScript Tutorial
Start coding in thinkorswim with a practical thinkScript tutorial. Open the editor, declare variables, plot data, use functions, and build your first study with copyable examples and links to the official Learning Center.
# Your first thinkScript study
input length = 20;
def avg = Average(close, length);
def isAbove = close > avg;
plot SMA = avg;
SMA.SetDefaultColor(Color.CYAN);
AddLabel(yes,
if isAbove then "Above" else "Below",
if isAbove then Color.GREEN
else Color.RED);- Editor
- Built into thinkorswim Charts
- Language
- thinkScript domain-specific language
- Reference
- thinkorswim Learning Center
Direct answer
How to start writing thinkScript
thinkScript is the built-in programming language for thinkorswim. You write studies and strategies in the thinkScript Editor, which parses the source and renders plots, labels, and simulated orders on the chart. The Learning Center provides a tutorial series that starts with variables and builds up to strategies.
The fastest way to learn is to open the Editor, type a few lines, and watch the chart update. Each tutorial chapter in the Learning Center includes a quiz so you can check your understanding.
Tutorial steps
Four steps to your first study
- 01
Open the thinkScript Editor
Go to the Charts tab, click Studies in the upper right, select Edit Studies, then click Create. The Editor opens with a default plot Data = close line.
- 02
Declare variables with def
Use def to store calculated values. ThinkScript evaluates each def once per bar, and you can reference earlier values with square-bracket offsets like close[1].
- 03
Plot results with plot
Use plot to draw values on the chart. A plot can be a line, histogram, points, or arrows depending on the painting strategy you assign.
- 04
Add labels and format output
Use AddLabel to display text on the chart. Combine it with AsText or AsPercent to format numbers, and use Color constants to set label color.
Code examples
Copyable examples by chapter
Each example corresponds to a tutorial chapter in the Learning Center. Paste them into the Editor and add them to a chart to see the output.
Chapter 1: Defining Variables
# Chapter 1: Defining Variables
def myVariable = close;
def avgPrice = Average(close, 20);
def isAbove = close > avgPrice;
plot SMA = avgPrice;
SMA.SetDefaultColor(Color.CYAN);def declares a variable. plot draws its value on the chart.
Chapter 2: Mathematical Functions
# Chapter 2: Mathematical Functions
input length = 14;
def highest = Highest(high, length);
def lowest = Low(low, length);
def range = highest - lowest;
plot RangePlot = range;
RangePlot.SetDefaultColor(Color.ORANGE);Built-in functions like Highest and Low operate on a bar range.
Chapter 9: Formatting Output
# Chapter 9: Formatting Output
def avg = Average(close, 20);
def trend = if close > avg then "Up" else "Down";
AddLabel(yes, "Trend: " + trend,
if close > avg then Color.GREEN else Color.RED);AddLabel displays text. Use Color constants to set the label color.
Learning path
Official tutorial chapters
The Learning Center tutorial series covers thinkScript from variables to strategies. Each chapter includes examples and a quiz.
- Chapter 1: Defining Variablesdef, plot, and the basic syntax for declaring variables.
- Chapter 2: Mathematical FunctionsArithmetic, built-in math functions, and aggregation.
- Chapter 7: Creating StrategiesAddOrder, OrderType constants, and backtesting.
- Chapter 8: Formatting Output Part ISetPaintingStrategy, painting styles, and visual output.
- Chapter 9: Formatting Output Part IIAddLabel, Color constants, and text formatting.
- thinkScript Tutorials OverviewStarting point for the full tutorial series.
Frequently asked questions
Learn thinkScript by generating real code
Describe the study or strategy you want in plain English. The Pineify thinkorswim AI Coding Agent generates a complete thinkScript file and explains the code. Read the source to learn the syntax, then paste it into the Editor and test it.
This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results.