Appearance
Quick Start
dxScript is a JavaScript-based language for creating custom technical analysis indicators with built-in support for time series, historical lookback, and technical analysis functions.
Script Structure
Every indicator has two sections: the init section that runs once, and the onTick function that runs for each bar.
javascript
// Init section — runs once
const length = input.double("length", 20);
// onTick function — runs for each tick
function onTick() {
const sma = ta.sma(close, length);
spline(sma, {title: "SMA"});
}Init Section
All code outside onTick() executes once when the indicator is created, before processing any market data.
Use for:
- Declaring input parameters
- Defining constants
- Defining helper functions
Example
javascript
// Input parameters
const length = input.double("length", 20, {min: 1, max: 200});
const source = input.source("source", close);
// Constants
const OVERBOUGHT = 70;
const OVERSOLD = 30;
// Helper functions
function inRange(value, min, max) {
return value >= min && value <= max;
}Caveats
- Price data (
open,high,low,close, etc.) is not available.
onTick Function
The onTick() function is called for each bar (candle) in the data.
Use for:
- Accessing market data (
open,high,low,close,volume) - Creating time series with
ts() - Performing calculations
- Producing outputs with
spline()
Example
javascript
function onTick() {
const sma = ta.sma(close, length);
const signal = ts(close > sma ? 1 : -1);
spline(sma, {title: "SMA"});
spline(signal, {title: "Signal"});
}Key Concepts
Inputs
Declare configurable parameters in the init section:
javascript
const length = input.double("length", 20, {min: 1, max: 200});
const useEma = input.bool("useEma", true);
const source = input.source("source", close);Time Series
Store calculated values with history access:
javascript
function onTick() {
const sma = ta.sma(close, length); // ta.* returns series
const prev = sma[1]; // previous bar value
const old = sma[10]; // 10 bars ago
}Outputs
Plot values on the chart:
javascript
function onTick() {
spline(close, {title: "Price", color: color.BLUE});
}Complete Example
Moving Average Crossover indicator:
javascript
// INIT
const length = input.double("length", 20, {min: 1, max: 200});
// TICK
function onTick() {
const sma = ta.sma(close, length);
const trend = close > sma ? color.GREEN : color.RED;
spline(sma, {title: "SMA"});
spline(close, {title: "Price", color: trend});
}Next Steps
- Explore Time Series in depth
- Understand Real-time Behavior
- Learn about the Runtime Environment
- Browse the Language Reference