Script SDK Reference
Complete API reference for @chartlabs/script-sdk.
Script Base Class
Every indicator extends the Script class and implements calculate().
import { Script, input, plot, color } from '@chartlabs/script-sdk';
export default class MyIndicator extends Script {
length = input.number('Period', 14, { min: 1, max: 200 });
calculate() {
plot('Value', this.sma('close', this.length), { color: color.blue });
}
}OHLCV Data (Current Bar)
| Property | Type | Description |
|---|---|---|
this.open | number | Open price of the current bar |
this.high | number | High price |
this.low | number | Low price |
this.close | number | Close price |
this.volume | number | Volume |
Historical Access
| Method | Returns | Description |
|---|---|---|
this.get(series, barsAgo) | number | Get OHLCV value N bars ago. Series: 'open', 'high', 'low', 'close', 'volume' |
this.getPlot(plotName, barsAgo) | number | Get a previously plotted value N bars ago. Essential for crossover detection |
Bar Metadata
| Property | Type | Description |
|---|---|---|
this.bar | number | Current bar index (0 = first bar on chart) |
this.isFirstBar | boolean | True when bar === 0 |
this.isLastBar | boolean | True on the most recent bar |
this.barCount | number | Total number of bars on the chart |
State Storage
| Method | Description |
|---|---|
this.setState(key, value) | Store a numeric value that persists across bars |
this.getState(key, defaultValue?) | Retrieve stored value (returns defaultValue or 0 if not set) |
TIP
Built-in functions (ema, rsi, macd, etc.) manage their own state internally. Only use setState/getState for your own custom running calculations.
Debug
| Method | Description |
|---|---|
this.log(...args) | Print to script console. Only outputs on the last bar to avoid flooding. |
Built-in Functions
See Built-in Functions for the complete list with examples.
Moving Averages
sma, ema, wma, rma, smma, dema, tema, hma, vwma, linreg
Compound Indicators (return objects)
macd, bbands, stoch, supertrend
Single-Value Indicators
rsi, atr, cci, adx, mfi, obv, stdev
Range & Volatility
highest, lowest, sum, trueRange, change, roc, percentRank
Utility
crossOver, crossUnder, barsSince, valuewhen, rising, falling
Math
abs, max, min, round
Input Declarations
Inputs appear in the indicator settings dialog when users add your indicator to a chart.
| Type | Syntax | Example |
|---|---|---|
| Number | input.number(label, default, {min?, max?, step?}) | input.number('Period', 14, { min: 1, max: 200 }) |
| Source | input.source(label, default?) | input.source('Source', 'close') |
| Boolean | input.boolean(label, default?) | input.boolean('Show MA', true) |
| Color | input.color(label, default?) | input.color('Line Color', '#3b82f6') |
| String | input.string(label, default?, {options?}) | input.string('Mode', 'fast', { options: ['fast', 'slow'] }) |
Source options: 'close', 'open', 'high', 'low', 'volume', 'hl2', 'hlc3', 'ohlc4'
Plot Functions
See Plot Types Reference for detailed examples of each plot type.
| Function | Description |
|---|---|
plot(name, value, opts?) | Line plot |
plotHistogram(name, value, opts?) | Histogram bar |
plotColor(name, value, colors[], colorIndex, opts?) | Line with dynamic per-bar color |
hline(name, level, opts?) | Static horizontal reference line |
plotShape(name, condition, opts?) | Shape marker (triangle, circle, cross, diamond) |
plotArrow(name, condition, opts?) | Directional arrow marker |
plotArea(plot1, plot2, opts?) | Fill area between two plots |
bgcolor(color, opts?) | Background color for current bar |
barcolor(color) | Bar color for current bar |
Plot Options
plot(): { color?: string, width?: number, style?: 'solid'|'dashed'|'dotted', overlay?: boolean }
plotHistogram(): { color?: string, baseValue?: number }
plotColor(): colors[] = array of color strings, colorIndex = which color this bar
plotShape(): { shape?: 'triangleup'|'triangledown'|'circle'|'cross'|'diamond', location?: 'abovebar'|'belowbar', color?: string, size?: 'tiny'|'small'|'normal'|'large', text?: string }
Color Constants
| Color | Hex | Color | Hex | |
|---|---|---|---|---|
color.red | #ef4444 | color.green | #22c55e | |
color.blue | #3b82f6 | color.yellow | #eab308 | |
color.orange | #f97316 | color.purple | #a855f7 | |
color.cyan | #06b6d4 | color.magenta | #ec4899 | |
color.lime | #84cc16 | color.pink | #f472b6 | |
color.teal | #14b8a6 | color.gray | #6b7280 | |
color.silver | #9ca3af | color.gold | #d97706 | |
color.navy | #1e3a5f | color.maroon | #7f1d1d | |
color.aqua | #67e8f9 | color.olive | #65a30d | |
color.white | #ffffff | color.black | #000000 |
You can also use any hex string: '#ff6600', 'rgba(34,197,94,0.15)'
Strategy Class
Strategies extend Strategy instead of Script and gain position management and order execution.
import { Script, Strategy, input, plot, plotShape, color } from '@chartlabs/script-sdk';
export default class MyStrategy extends Strategy {
calculate() {
// Access position state
this.position.isLong // boolean
this.position.isShort // boolean
this.position.isFlat // boolean
this.position.size // number
this.position.entryPrice // number
// Execute orders
this.strategy.entry('Buy', 'long', { qty: 1 });
this.strategy.exit('Sell', 'long');
this.strategy.close('Close All');
}
}See Strategy Guide for complete strategy documentation.
Overlay vs Sub-pane
- Sub-pane (default): Indicators with different value ranges than price — RSI (0-100), MACD, histograms
- Overlay: Set
overlay: trueon any plot to draw on the price chart — moving averages, bands, channels

