Skip to content

Samples ​

Ready-to-use indicator scripts demonstrating dxScript features.

Accumulation Distribution (ADL) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/accumulationdistribution

indicator({title: "Accumulation Distribution (ADL)"});

function onTick() {
	const moneyFlowMultiplier = (high[0] !== low[0]) ? ((close - low) - (high - close)) / (high - low) : 1.0;
	const moneyFlowVolume = moneyFlowMultiplier * volume;

	spline(ta.cum(moneyFlowVolume), {title: "adl", color: color.BLUE});
	spline(0, {title: "zero", color: color.GRAY});
}

Aroon ​

javascript
// https://devexperts.com/dxcharts/kb/docs/aroon-indicator

indicator({title: "Aroon"});

const n = input("n", 25);
const overBought = input("overBought", 70);
const overSold = input("overSold", 30);

function onTick() {
	const aroonUp = 100 * (n - ta.highestBar(high, n)) / n;
	const aroonDown = 100 * (n - ta.lowestBar(low, n)) / n;

	spline(aroonUp, {title: "aroonUp", color: color.GREEN, overlay: false});
	spline(aroonDown, {title: "aroonDown", color: color.RED, overlay: false});
	spline(overBought, {title: "overBought", color: color.GRAY, overlay: false});
	spline(overSold, {title: "overSold", color: color.GRAY, overlay: false});
}

Average True Range (ATR) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/average-true-range-atr

indicator({title: "Average True Range (ATR)"});

const n = input("n", 14);

function onTick() {
	spline(ta.atr(n), {title: "atr", color: color.BLUE});
}

Awesome Oscillator (AO) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/awesome-oscillator-ao

indicator({title: "Awesome Oscillator (AO)"});

const fastLength = input("Fast Length", 5);
const slowLength = input("Slow Length", 34);

function onTick() {
	const mid = hl2;
	const ao = ta.sma(mid, fastLength) - ta.sma(mid, slowLength);

	spline(ao, {title: "awesome", color: color.BLUE});
	spline(0, {title: "zero", color: color.GRAY});
}

Bollinger Bands (BB) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/bollinger-bands

indicator({title: "Bollinger Bands (BB)"});

const n = input("n", 20);
const displace = input("displace", 0);
const numDevUp = input("numDevUp", 2);
const numDevDown = input("numDevDown", -2);
const typical = input.source("typical", close);

function onTick() {
	const midLineValue = ta.sma(typical, n);
	const sd = ta.stdev(typical, n);
	spline(midLineValue, {title: "midLine", color: color.BLUE});
	spline(midLineValue + numDevUp * sd, {title: "upBand", color: color.RED});
	spline(midLineValue + numDevDown * sd, {title: "lowBand", color: color.GREEN});
}

Chande Momentum Oscillator (CMO) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/chandle-momentum-oscillator

indicator({title: "Chande Momentum Oscillator (CMO)"});

const n = input("n", 20);

function onTick() {
	const diff = close - close[1];
	const inc = ts(Math.max(diff, 0));
	const dec = ts(Math.max(diff * -1, 0));
	const hasFullWindow = !isNaN(inc[n - 1]) && !isNaN(dec[n - 1]);
	const sumInc = ta.sum(inc, n);
	const sumDec = ta.sum(dec, n);
	const cmo = hasFullWindow ? (sumInc - sumDec) / (sumInc + sumDec) * 100 : NaN;
	spline(cmo, {title: "CMO", color: color.BLUE});
	spline(50, {title: "UpperLevel", color: color.RED});
	spline(-50, {title: "LowerLevel", color: color.GREEN});
	spline(0, {title: "Zero", color: color.GRAY});
}

Commodity Channel Index (CCI) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/commodity-channel-index

indicator({title: "Commodity Channel Index (CCI)"});

const n = input("n", 14);
const overBought = input("overBought", 100);
const overSold = input("overSold", -100);

function onTick() {
	const cci = ta.cci(hlc3, n)
	spline(cci, {title: "cci", color: color.BLUE});
	spline(overBought, {title: "OverBought", color: color.RED});
	spline(overSold, {title: "OverSold", color: color.GREEN});
	spline(0, {title: "Zero", color: color.GRAY});
}

Directional Movement Index(DMI) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/directional-movement-index

indicator({title: "Directional Movement Index(DMI)"});

const n = input("length", 20);

function onTick() {
	const [dplus, dminus, adx] = ta.dmi(n);
	spline(dplus, {title: "D+", color: color.GREEN});
	spline(dminus, {title: "D-", color: color.RED});
	spline(adx, {title: "ADX", color: color.BLUE});
}

Double Exponential Moving Average ​

javascript
// https://devexperts.com/dxcharts/kb/docs/double-exponential-moving-average

indicator({title: "Double Exponential Moving Average"});

const n = input("n", 9);

function onTick() {
	const c = close;
	const ema1 = ta.ema(c, n);
	const ema2 = ta.ema(ema1, n);
	spline(2 * ema1 - ema2, {title: "dema", color: color.BLUE});
}

Exponential Moving Average (EMA) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/exponential-moving-average-ema

indicator({title: "Exponential Moving Average (EMA)"});

const n = input("n", 9);
const price = input.source("price", close)

function onTick() {
	spline(ta.ema(price, n), {title: "ema", color: color.BLUE});
}

Linear Regression Curve ​

javascript
// https://devexperts.com/dxcharts/kb/docs/linear-regression-curve

indicator({title: "Linear Regression Curve"});

const price = input.source("price", close);
const length = input("length", 20);

function onTick() {
	const liRegression = ta.lreg(price, length);
	spline(liRegression, {title: "LinearRegression", color: color.GREEN});
}

Mass Index ​

javascript
// https://devexperts.com/dxcharts/kb/docs/mass-index

indicator({title: "Mass Index"});

const n = input("n", 9);
const sumLength = input("sumLength", 25);
const triggerLevel = input("triggerLevel", 26.5);
const setupLevel = input("setupLevel", 27);

function onTick() {
	const diff = ts(high - low);
	const emaDiff1 = ta.ema(diff, n);
	const emaEmaDiff = ta.ema(emaDiff1, n);
	const emaEmaCoef = ts(emaDiff1 / emaEmaDiff);

	spline(ta.sum(emaEmaCoef, sumLength), {title: "mi", color: color.BLUE});
	spline(triggerLevel, {title: "trigger", color: color.ORANGE});
	spline(setupLevel, {title: "setup", color: color.RED});
}

Momentum ​

javascript
// https://devexperts.com/dxcharts/kb/docs/momentum

indicator({title: "Momentum"});

const n = input("n", 12);

function onTick() {
	const mom = close - close[n];
	spline(mom, {title: "momentum", color: mom >= 0 ? color.GREEN : color.RED});
}

Money Flow (MFI) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/money-flow-index-mfi

indicator({title: "Money Flow (MFI)"});

const n = input("n", 14);
const overBought = input("overBought", 80);
const overSold = input("overSold", 20);

function onTick() {
	const tp = ts((high + low + close) / 3);
	const tpPrev = ts((high[1] + low[1] + close[1]) / 3);
	const positiveMoneyFlow = ts((tp > tpPrev) ? tp * volume : 0);
	const negativeMoneyFlow = ts((tp < tpPrev) ? tp * volume : 0);
	const sumPositiveMoneyFlow = ta.sum(positiveMoneyFlow, n);
	const sumNegativeMoneyFlow = ta.sum(negativeMoneyFlow, n);
	const mfidx = 100 - (100 / (1 + sumPositiveMoneyFlow / sumNegativeMoneyFlow));
	spline(mfidx, {title: "MFidx", color: color.BLUE});
	spline(overBought, {title: "OverBought", color: color.RED});
	spline(overSold, {title: "OverSold", color: color.GREEN});
}

Moving Average Convergence Divergence (MACD) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/moving-average-convergence-divergence-macd

indicator({title: "Moving Average Convergence Divergence (MACD)"});

const fastLen = input("fastLen", 12);
const slowLen = input("slowLen", 26);
const macdLen = input("macdLen", 9);
const avgFn = input.enum("averageType", "EXPONENTIAL", {
	"EXPONENTIAL": ta.ema,
	"SMA": ta.sma,
	"WMA": ta.wma,
	"WILDERS": ta.wima,
});

function onTick() {
	const series = close;
	const MACD = ts(ta.ema(series, fastLen) - ta.ema(series, slowLen));
	const avg = avgFn(MACD, macdLen);
	const diff = MACD - avg;
	spline(MACD, {title: "MACD", color: color.BLUE});
	spline(avg, {title: "avg", color: color.ORANGE});
	spline(diff, {title: "diff", color: diff >= 0 ? color.GREEN : color.RED});
}

On Balance Volume (OBV) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/on-balance-volume

indicator({title: "On Balance Volume (OBV)"});

function onTick() {
	const diff = close - close[1];
	const v = (diff > 0) ? volume : ((diff < 0) ? -volume : 0);
	spline(ta.cum(v), {title: "obv", color: color.BLUE});
}

Pivot High ​

javascript
// https://devexperts.com/dxcharts/kb/docs/pivot-points

indicator({title: "Pivot High"});

const left = input("left", 3);
const right = input("right", 2);

function onTick() {
	let pivotH = ta.pivotHigh(close, left, right);
	spline(pivotH, {title: "pivotHigh", color: color.RED, offset: -right});
}

Pivot Low ​

javascript
// https://devexperts.com/dxcharts/kb/docs/pivot-points

indicator({title: "Pivot Low"});

const left = input("left", 3);
const right = input("right", 2);

function onTick() {
	let pivotL = ta.pivotLow(close, left, right);
	spline(pivotL, {title: "pivotLow", color: color.GREEN, offset: -right});
}

Price Oscillator ​

javascript
// https://devexperts.com/dxcharts/kb/docs/price-oscillator

indicator({title: "Price Oscillator"});

const fastLen = input("fastLen", 9);
const slowLen = input("slowLen", 18);

function onTick() {
	const emaFast = ta.ema(close, fastLen);
	const emaSlow = ta.ema(close, slowLen);
	const ppo = emaFast - emaSlow;

	spline(ppo, {title: "PriceOscillator", color: ppo >= 0 ? color.GREEN : color.RED});
	spline(0, {title: "Zero", color: color.GRAY});
}

Price Volume Trend (PVT) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/price-and-volume-trend

indicator({title: "Price Volume Trend (PVT)"});

function onTick() {
	const prevClose = close[1];
	const currentClose = close;
	const v = (currentClose - prevClose) / prevClose * volume;
	spline(ta.cum(v), {title: "pvt", color: color.BLUE});
}

Rate of Change (ROC) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/rate-of-change

indicator({title: "Rate of Change (ROC)"});

const n = input("n", 12);

function onTick() {
	const prevClose = close[n];
	const currentClose = close;
	const roc = ((currentClose - prevClose) / prevClose) * 100;
	spline(roc, {title: "roc", color: roc >= 0 ? color.GREEN : color.RED});
	spline(0, {title: "zero", color: color.GRAY});
}

Relative Strength Index (RSI) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/relative-strength-index-rsi

indicator({title: "Relative Strength Index (RSI)"});

const n = input("n", 14);
const overBought = input("overBought", 70);
const overSold = input("overSold", 30);

function onTick() {
	const curr = close;
	const prev = close[1];
	const net = ts(curr - prev);
	const total = ts(Math.abs(prev - curr));
	const averageNet = ta.wima(net, n);
	const averageTotal = ta.wima(total, n);
	spline(50 * (1 + averageNet / averageTotal), {title: "RSI", color: color.BLUE});
	spline(overBought, {title: "overBought", color: color.RED});
	spline(overSold, {title: "overSold", color: color.GREEN});
}

Relative Vigor Index (RVI) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/relative-vigor-index

indicator({title: "Relative Vigor Index (RVI)"});

function onTick() {
	const nominator = close - open;
	const denominator = high - low;
	const rvi = denominator !== 0 ? nominator / denominator: 0;
	spline(rvi, {title: "rvi", color: rvi >= 0 ? color.GREEN : color.RED});
}

Simple Moving Average ​

javascript
// https://devexperts.com/dxcharts/kb/docs/simple-moving-average-sma

indicator({title: "Simple Moving Average"});

const n = input("n", 9);
const price = input.source("price", open)

function onTick() {
	spline(ta.sma(price, n), {title: "sma", color: color.BLUE});
}

Smoothed Moving Average ​

javascript
// https://devexperts.com/dxcharts/kb/docs/smoothed-simple-moving-average

indicator({title: "Smoothed Moving Average"});

const n = input("n", 9);

function onTick() {
	spline(ta.wima(close, n), {title: "SMMA", color: color.BLUE});
}

Triple Exponential Average (TRIX) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/triple-exponential-average

indicator({title: "Triple Exponential Average (TRIX)"});

const n = input("n", 9);

function trix(x, n) {
	const ema1 = ta.ema(x, n);
	const ema2 = ta.ema(ema1, n);
	return ta.ema(ema2, n);
}

function onTick() {
	const logPrice = ts(Math.log(close));
	const logPrevPrice = ts(Math.log(close[1]));

	const triple = trix(logPrice, n);
	const triplePrev = trix(logPrevPrice, n);
	const trixValue = (triple - triplePrev) * 10000;

	spline(trixValue, {title: "TRIX", color: trixValue >= 0 ? color.GREEN : color.RED});
}

Triple Exponential Moving Average (TEMA) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/triple-exponential-moving-average

indicator({title: "Triple Exponential Moving Average (TEMA)"});

const n = input("n", 9);

function onTick() {
	const series = close;
	const ema1 = ta.ema(series, n);
	const ema2 = ta.ema(ema1, n);
	const ema3 = ta.ema(ema2, n);

	spline(3 * ema1 - 3 * ema2 + ema3, {title: "tema", color: color.BLUE});
}

True Strength Index (TSI) ​

javascript
// https://devexperts.com/dxcharts/kb/docs/true-strength-index

indicator({title: "True Strength Index (TSI)"});

const price = input.source("source", close)
const longLength = input("longLength", 25);
const shortLength = input("shortLength", 13);
const signalLength = input("signalLength", 8);

function onTick() {
	const tsi = ta.tsi(price, shortLength, longLength);
	spline(tsi, {title: "tsi", color: color.BLUE});
	spline(ta.ema(tsi, signalLength), {title: "signal", color: color.ORANGE});
	spline(0, {title: "zero", color: color.GRAY});
}

Weighted Moving Average ​

javascript
// https://devexperts.com/dxcharts/kb/docs/weighted-moving-average-wma

indicator({title: "Weighted Moving Average"});

const n = input("n", 9);
const price = input("price", open);

function onTick() {
	spline(ta.wma(price, n), {title: "wma", color: color.BLUE});
}

Williams Alligator ​

javascript
// https://devexperts.com/dxcharts/kb/docs/williams-alligator

indicator({title: "Williams Alligator"});

const jawLen = input("jawLen", 13);
const jawDisplace = input("jawDisplace", 8);
const teethLen = input("teethLen", 8);
const teethDisplace = input("teethDisplace", 5);
const lipsLen = input("lipsLen", 5);
const lipsDisplace = input("lipsDisplace", 3);

function onTick() {
	const median = ts((high + low) / 2);
	const lips = ta.wima(median, lipsLen);
	const teeth = ta.wima(median, teethLen);
	const jaw = ta.wima(median, jawLen);
	spline(lips, {title: "Lips", color: color.LIME, offset: lipsDisplace});
	spline(teeth, {title: "Teeth", color: color.RED, offset: teethDisplace});
	spline(jaw, {title: "Jaw", color: color.BLUE, offset: jawDisplace});
}