Skip to content

Script SDK Reference

Complete API reference for @chartlabs/script-sdk.

Script Base Class

Every indicator extends the Script class and implements calculate().

typescript
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)

PropertyTypeDescription
this.opennumberOpen price of the current bar
this.highnumberHigh price
this.lownumberLow price
this.closenumberClose price
this.volumenumberVolume

Historical Access

MethodReturnsDescription
this.get(series, barsAgo)numberGet OHLCV value N bars ago. Series: 'open', 'high', 'low', 'close', 'volume'
this.getPlot(plotName, barsAgo)numberGet a previously plotted value N bars ago. Essential for crossover detection

Bar Metadata

PropertyTypeDescription
this.barnumberCurrent bar index (0 = first bar on chart)
this.isFirstBarbooleanTrue when bar === 0
this.isLastBarbooleanTrue on the most recent bar
this.barCountnumberTotal number of bars on the chart

State Storage

MethodDescription
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

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

TypeSyntaxExample
Numberinput.number(label, default, {min?, max?, step?})input.number('Period', 14, { min: 1, max: 200 })
Sourceinput.source(label, default?)input.source('Source', 'close')
Booleaninput.boolean(label, default?)input.boolean('Show MA', true)
Colorinput.color(label, default?)input.color('Line Color', '#3b82f6')
Stringinput.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.

FunctionDescription
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

ColorHexColorHex
color.red#ef4444color.green#22c55e
color.blue#3b82f6color.yellow#eab308
color.orange#f97316color.purple#a855f7
color.cyan#06b6d4color.magenta#ec4899
color.lime#84cc16color.pink#f472b6
color.teal#14b8a6color.gray#6b7280
color.silver#9ca3afcolor.gold#d97706
color.navy#1e3a5fcolor.maroon#7f1d1d
color.aqua#67e8f9color.olive#65a30d
color.white#ffffffcolor.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.

typescript
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: true on any plot to draw on the price chart — moving averages, bands, channels

ChartLabs Documentation