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.

FirstStudythinkScript
# 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);
input
def + plot
AddLabel
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.

Start with a small study in the desktop Editor and compare each output with the input data. Use the official tutorial examples and available exercises to check your understanding.

Tutorial steps

Four steps to your first study

  1. 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.

  2. 02

    Declare variables with def

    Use def to store calculated values. Studies normally recalculate their latest values after each tick. Historical offsets like close[1] refer to earlier bars; do not assume the current bar is final.

  3. 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.

  4. 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

These illustrative examples follow the topics of the linked chapters. They have not been executed in thinkorswim here. Check them in the Editor and inspect the chart output.

Chapter 1: Defining Variables

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

Mathematical Functions
# Chapter 2: Mathematical Functions
input length = 14;

def highestPrice = Highest(high, length);
def lowestPrice = Lowest(low, length);
def range = highestPrice - lowestPrice;

plot RangePlot = range;
RangePlot.SetDefaultColor(Color.ORANGE);

Highest and Lowest find extrema over the specified window; low is the price series, not the rolling-minimum function.

Chapter 9: Formatting Output

Formatting Output
# Chapter 9: Formatting Output
def avg = Average(close, 20);
def trendUp = close > avg;

AddLabel(yes, if trendUp then "Trend: Up" else "Trend: Down",
    if close > avg then Color.GREEN else Color.RED);

AddLabel displays text. Use Color constants to set the label color.

Frequently asked questions

How do I start learning thinkScript?

Open the thinkorswim Charts tab, click Studies, select Edit Studies, then click Create to open the Editor. Start with the official tutorial series in the Learning Center, which covers variables, functions, plotting, and strategies chapter by chapter.

Where is the official thinkScript documentation?

The thinkorswim Learning Center hosts the full thinkScript reference at toslc.thinkorswim.com. It includes tutorials, a function reference, constants, declarations, operators, and reserved words. Each function page has syntax, parameters, and examples.

What is the difference between def and input in thinkScript?

def declares a calculated variable that the script computes. input declares a configurable parameter that appears in the study settings, letting you change values without editing the source.

Can I see examples of thinkScript code?

Yes. The Learning Center tutorials include working examples in every chapter. Where a study exposes source in the Editor, inspect it alongside the reference; source visibility is not a promise that every underlying function implementation is available.

What is the thinkScript manual?

The thinkScript manual is the reference section of the Learning Center. It provides function, constant, declaration, and reserved-word references. The tutorials are the guided learning path, while the reference is the lookup tool for specific syntax.

Can Pineify help me learn thinkScript?

Yes. The Pineify thinkorswim AI Coding Agent can draft thinkScript and explain the source. Generated explanations and code still need verification; Pineify does not run them in thinkorswim. You can ask it to build a study, then read the generated source to learn the syntax, functions, and conventions.

Learn thinkScript by generating real code

Describe the study or strategy you want in plain English. The Pineify thinkorswim AI Coding Agent can draft a source file and explain it. Verify the explanation against the reference to learn the syntax, then paste it into the Editor and test it.

Open the thinkScript AI agent

This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results.