Guides
Getting started
This page walks you through writing a first indicator, checking it, adjusting its settings and keeping it in My indicators so you can add it to any chart.
1. Open the editor#
- Open the Indicators window of a chart.
- In the Custom category, add StrateScript (preview) to the chart.
- The StrateScript panel opens beside the chart. A new script is empty, so the panel first shows Create your first indicator, with a few example cards and an Empty script button. Click Empty script (or pick an example).
The panel has three modes, chosen at its top:
- Code: the code editor, with a Run button and the Console drawer below it. This is where you write scripts.
- Parameters: the Settings tab (your script's settings) and, once a strategy or research script has produced one, the Report tab.
- Assistant: the optional AI helper. You never need it to write, check or run a script.
A chart can hold several scripts. Use the script menu at the top of the panel (the script's name) to switch between scripts, start a new one (+ New indicator, which again opens on the empty view), remove one, open My indicators…, or load one of the built-in examples.
2. Write a first line#
Empty script fills the editor with a header, indicator "New indicator" { overlay: true }. A script can also be a single line: replace the editor's content with this:
plot(sma(close, 20))The panel checks the code as you type and runs it automatically about 250 ms after your last keystroke. Press ⌘↵ or click Run to run it at once. A 20-bar simple moving average of the close appears on the price chart.
With no header, a script is an indicator drawn over the prices, and it has no name.
3. Give it a header and a name#
The header says what kind of script this is, gives it a name, and sets its options. It must come before any other statement:
indicator "My average" { overlay: true }
plot(sma(close, 20), #e8c15a, title: "SMA 20", width: 2)indicator "My average"names the script. The name is used in the legend, in My indicators, and when another script imports it.overlay: truedraws on the price chart. Useoverlay: falseto draw in a separate pane below the prices.#e8c15ais a color written in hexadecimal.title:andwidth:are named arguments.
4. Add settings#
Fixed numbers are rarely what you want. Each input.* call creates a setting in the Settings tab (mode Parameters) and returns its current value:
indicator "My average" { overlay: true }
len = input.int("Length", 20, min: 1, max: 500)
col = input.color("Color", #e8c15a)
plot(sma(close, len), col, title: "SMA", width: 2)Change Length in the Settings tab and the chart updates. The setting's title is its identity: if you rename it in the code, the saved value is lost and the default comes back. See Settings.
5. Read the errors#
If the code has a mistake, a red box appears under the editor, for example:
Line 3 · "clsoe" is not defined
Did you mean "close"?(for a script whose third line is plot(sma(clsoe, len))). Each error has:
- the line and a message, on the first line of the box;
- often a suggested fix, on the second line;
- a code (
E930) and a column. Hover the box's title to see them (E930 · column 10). Codes are listed in Reference: errors.
For some errors the box offers a Fix button that applies the correction for you. While an error remains, the script does not run, and the chart keeps the last valid result.
The check runs your script on a short synthetic market: 300 one-minute bars, with trades, book snapshots and order-by-order events on the last 60 bars. So most mistakes that only show at run time are caught too, even inside on trade or on order handlers. A branch that never runs on that sample is not checked. If an if only becomes true on real data, an error inside it appears on the chart when that line first runs.
6. Print values while you work#
print(...) writes a line in the Console (the drawer under the editor, in mode Code) for each bar. It is the quickest way to see what a value really is:
indicator "Debug" { overlay: false }
range = high - low
print("range", range, "delta", delta, "time", str(bar.time, "HH:mm"))
plot(range, title: "Range")print only writes for the last 500 bars and keeps one line per call and per bar. It never changes what the script draws.
7. Keep it in My indicators#
Every named script of the panel is listed in the My indicators category of the Indicators window, ready to add to any chart. To keep the script on the current chart as an indicator of its own:
- Open the script menu at the top of the panel.
- Choose Add to My indicators.
It then runs on the chart even when the StrateScript panel is closed, and stays after a restart. Choosing the command again after editing the script updates it. Its settings belong to each chart where you add it, and editing the script updates every chart that uses it. A script without a name (no header, or indicator "") cannot be added to My indicators.
The panel also keeps earlier versions of each script. They are listed in the script menu, and one click brings a version back.
8. Where to go next#
- The language: variables,
na, historyx[n], functions, custom types. - Market data: what a script can read, bar by bar and trade by trade.
- Order flow: order-by-order events and the book.
- Examples: complete scripts to copy and modify.
A complete first indicator#
This indicator colors each bar by the sign of its delta and marks bars whose volume is far above the recent median.
indicator "Delta bars" { overlay: true }
mult = input.float("Volume multiple", 2.0, min: 1, step: 0.5)
upCol = input.color("Buyers", #26a69a)
dnCol = input.color("Sellers", #ef5350)
// color each candle by who was more aggressive during the bar
bar_color(delta > 0 ? upCol : dnCol)
// mark bars with unusual volume: more than `mult` times the median of the last 50 bars
heavy = volume > mult * stats.median(volume, 50)
marker(heavy and bar.confirmed, shape: "circle", location: "above", color: #e8c15a, tip: "volume " + str(volume, "0.0k"))bar.confirmed is false only on a bar that is still forming. Using it keeps the marker from appearing and disappearing while the live bar is being built.