thinkorswim programming language
thinkScript
thinkScript is the built-in programming language for thinkorswim. You write studies and strategies in the thinkScript Editor, and the platform renders plots, labels, alerts, and simulated orders on the chart.
input length = 20;
def avg = Average(close, length);
def isAbove = close > avg;
plot SMA = avg;
plot Signal = if isAbove then close else Double.NaN;
SMA.SetDefaultColor(Color.CYAN);
Signal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Signal.SetDefaultColor(Color.GREEN);
AddLabel(yes, if isAbove then "Above SMA" else "Below SMA",
if isAbove then Color.GREEN else Color.RED);- Platform
- thinkorswim desktop and web
- Output types
- Studies, strategies, watchlist columns
- Editor
- Built-in thinkScript Editor
Direct answer
What thinkScript is and what it builds
thinkScript is a domain-specific language embedded in thinkorswim. You use it to create custom chart studies, backtesting strategies, watchlist columns, and conditional alerts. The language provides built-in access to price data (open, high, low, close, volume), technical analysis functions, and visual output controls.
A study calculates values and plots them on the chart. A strategy does the same and adds simulated orders with AddOrder so you can review a backtest report. Both are single-file scripts written in the thinkScript Editor.
Open the Editor
In thinkorswim, go to Charts, click Studies, select Edit Studies, then click Create to open the thinkScript Editor with a default plot.
Write or paste source
Replace the default line with your study or strategy. Use def for variables, input for parameters, plot for visual output, and AddOrder for simulated strategies.
Parse and add to chart
Let the Editor check the syntax. Resolve any messages, save the study, and add it to a chart to inspect the plots, labels, and strategy behavior.
thinkScript guides
Topic pages with copyable code
Each guide covers one thinkScript feature with a complete code example, platform steps, and official source links.
- Probability indicatorBuild a rolling historical up-rate study with 5, 10, and 20-bar labels.
- PaintingStrategy.HORIZONTALDraw horizontal segments for fixed price levels instead of connected lines.
- Tutorial and documentationStart with variables, plots, functions, and the official Learning Center path.
- ColorsUse Color constants, CreateColor, GetColor, and DefineColor in plots and labels.
- AddLabel and AsTextAdd chart labels and format numbers with AsText and NumberFormat constants.
- AddOrder and strategiesPlace simulated orders with AddOrder and OrderType constants for backtesting.
- Mark priceReference the mark price with PriceType.MARK and understand its chart limits.
Language building blocks
Core thinkScript concepts
| Concept | Keyword or function | What it does |
|---|---|---|
| Variable | def | Stores a calculated value for reuse in the script. |
| Input parameter | input | Exposes a configurable value in the study settings. |
| Visual output | plot | Draws a line, point, or histogram on the chart. |
| Historical offset | close[1] | References the value from a previous bar. |
| Chart label | AddLabel | Adds text to the chart with a condition and color. |
| Simulated order | AddOrder | Adds a backtest order when a condition is true. |
| Painting style | SetPaintingStrategy | Changes how a plot is drawn on the chart. |
| Color | Color constants | Sets plot, label, and background colors. |
Primary sources
Official thinkScript references
The thinkorswim Learning Center hosts the full language reference, tutorials, and function documentation.
- thinkScript TutorialsOfficial tutorial series covering variables, functions, plotting, and strategies.
- thinkScript ReferenceFunctions, constants, declarations, operators, and reserved words.
- thinkScript Q&AOfficial FAQ on how to start scripting and where to find help.
- Creating StrategiesTutorial chapter on AddOrder and backtesting strategies.
Frequently asked questions
Write thinkScript without memorizing the syntax
Describe the study or strategy you need in plain English. The Pineify thinkorswim AI Coding Agent generates a complete, editable thinkScript file. Review the source, then paste it into the thinkScript Editor and test it on a chart.
This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results.