Indicators
Indicators process price history and render visual overlays on the chart. Write the math yourself in an indicator file, or — inside a strategy — wrap a native engine or another script through a proxy.
Four Modes Overview
Native.* / Custom.* proxies and Load() work only in strategies (files with a Tick() block). They are bound in Init(), filled in a Name.calculate() block and read in Tick() with Name.field(0). An indicator file computes its own buffers in calculate() — it can still read other timeframes and symbols with close(symbol, timeframe, shift) (see the higher-timeframe overlay). To draw on top of native / custom indicators, use an indicator-style strategy that never trades.Mode A — Custom Standard
Write all indicator math directly in TDSL. The proven layout:
- Declare every output (and every helper buffer you read back) once at file level:
series double t1_upper[];— unique names. - At the top of
calculate()size them to the chart:t1_upper[] = create_array(len);. Earlier values are kept. - Start the loop at
Chart.resume_index - 1on updates and at the lookback on the first run, so[i - n]never goes below 0. - Return a struct; the display block draws its fields.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Test 1: bands, arrows, fills";
}
struct BandSignal {
double upper;
double middle;
double lower;
double sell_signal;
double buy_signal;
}
display {
separate_window: false;
outputs: 5
BandSignal.upper { type: "line"; color: "#c42626ff"; name: "Upper Band"; width: 1; };
BandSignal.middle { type: "line"; color: "#b0aaaaff"; name: "Middle Band"; width: 0.5; };
BandSignal.lower { type: "line"; color: "#50c426ff"; name: "Lower Band"; width: 1; };
BandSignal.sell_signal { type: "arrow"; color: "#c42626ff"; name: "Sell Arrow"; width: 0.5; arrow_code: 234; };
BandSignal.buy_signal { type: "arrow"; color: "#50c426ff"; name: "Buy Arrow"; width: 0.5; arrow_code: 233; };
fill { buffer_a: 0; buffer_b: 1; color: "rgba(19, 75, 228, 0.25)"; };
fill { buffer_a: 1; buffer_b: 2; color: "rgba(0, 189, 31, 0.25)"; };
}
parameters {
int period = 20; // Bands Period
double multiplier = 2.0; // Deviation
double arrow_gap = 50; // Arrow distance (points)
}
series double t1_middle[];
series double t1_upper[];
series double t1_lower[];
series double t1_sell[];
series double t1_buy[];
calculate() {
double closes[] = close();
double highs[] = high();
double lows[] = low();
int len = length(closes);
t1_middle[] = create_array(len);
t1_upper[] = create_array(len);
t1_lower[] = create_array(len);
t1_sell[] = create_array(len);
t1_buy[] = create_array(len);
int start = Chart.resume_index;
if (start > 0) {
start = start - 1; // recalculate the previous bar too
} else {
start = period; // one extra bar so [i - 1] is valid
}
double pt = Symbol.Point();
for (int i = start; i < len; i = i + 1) {
double sum = 0.0;
for (int j = 0; j < period; j = j + 1) {
sum = sum + closes[i - j];
}
double mean = sum / period;
t1_middle[i] = mean;
double sq = 0.0;
for (int j = 0; j < period; j = j + 1) {
double d = closes[i - j] - mean;
sq = sq + (d * d);
}
double sd = Math.Sqrt(sq / period);
t1_upper[i] = mean + (multiplier * sd);
t1_lower[i] = mean - (multiplier * sd);
// 0.0 = no arrow on this bar
t1_buy[i] = 0.0;
t1_sell[i] = 0.0;
if (closes[i - 1] < t1_lower[i - 1] && closes[i] > t1_lower[i]) {
t1_buy[i] = lows[i] - (arrow_gap * pt);
}
if (closes[i - 1] > t1_upper[i - 1] && closes[i] < t1_upper[i]) {
t1_sell[i] = highs[i] + (arrow_gap * pt);
}
}
return BandSignal {
upper: t1_upper,
middle: t1_middle,
lower: t1_lower,
sell_signal: t1_sell,
buy_signal: t1_buy,
};
}Example: Multi-Buffer Oscillator (fixed scale + levels)
A Wilder RSI with a fast and a slow EMA of itself, in one window fixed to 0–100 with chart levels at 30 / 50 / 70. The smoothing state (rs_up, rs_dn) lives in series buffers too, so each bar continues from the previous one.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: RSI + signals, fixed scale, levels";
}
struct RSISignal {
double rsi;
double fast_sig;
double slow_sig;
}
display {
separate_window: true;
scale: fixed(0, 100);
levels: [30, 50, 70];
outputs: 3
RSISignal.rsi { type: "line"; color: "#3b82f6"; name: "RSI"; width: 2; };
RSISignal.fast_sig { type: "line"; color: "#f97316"; name: "Fast Signal"; width: 1; style: "dotted"; };
RSISignal.slow_sig { type: "line"; color: "#8b5cf6"; name: "Slow Signal"; width: 1; style: "dashed"; };
}
parameters {
int period = 14; // RSI Period
int fast_len = 9; // Fast Signal EMA
int slow_len = 21; // Slow Signal EMA
}
series double rs_rsi[];
series double rs_up[];
series double rs_dn[];
series double rs_fast[];
series double rs_slow[];
calculate() {
double closes[] = close();
int len = length(closes);
rs_rsi[] = create_array(len);
rs_up[] = create_array(len);
rs_dn[] = create_array(len);
rs_fast[] = create_array(len);
rs_slow[] = create_array(len);
double pd = period;
double pm = pd - 1.0;
double fl = fast_len;
double sl = slow_len;
double fa = 2.0 / (fl + 1.0);
double sa = 2.0 / (sl + 1.0);
int start = Chart.resume_index;
if (start > 0) { start = start - 1; } else { start = period; }
for (int i = start; i < len; i = i + 1) {
if (i == period) {
// seed: simple average of the first `period` gains / losses
double g = 0.0;
double l = 0.0;
for (int j = 1; j <= period; j = j + 1) {
double d0 = closes[j] - closes[j - 1];
if (d0 > 0.0) { g = g + d0; } else { l = l - d0; }
}
rs_up[i] = g / pd;
rs_dn[i] = l / pd;
} else {
// Wilder smoothing
double d = closes[i] - closes[i - 1];
double up = 0.0;
double dn = 0.0;
if (d > 0.0) { up = d; } else { dn = 0.0 - d; }
rs_up[i] = (rs_up[i - 1] * pm + up) / pd;
rs_dn[i] = (rs_dn[i - 1] * pm + dn) / pd;
}
rs_rsi[i] = rs_dn[i] == 0.0 ? 100.0 : 100.0 - (100.0 / (1.0 + rs_up[i] / rs_dn[i]));
// EMAs of the RSI, seeded with the first RSI value
if (i == period) {
rs_fast[i] = rs_rsi[i];
rs_slow[i] = rs_rsi[i];
} else {
rs_fast[i] = rs_fast[i - 1] + fa * (rs_rsi[i] - rs_fast[i - 1]);
rs_slow[i] = rs_slow[i - 1] + sa * (rs_rsi[i] - rs_slow[i - 1]);
}
}
return RSISignal { rsi: rs_rsi, fast_sig: rs_fast, slow_sig: rs_slow };
}Example: Arrow Signal Indicator (per-buffer window)
The RSI line moves to its own window with a buffer-level separate_window: true;; the arrows stay on the price chart. Signals compare the previous bar's RSI (ra_rsi[i - 1]) with the current one.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: RSI reversal arrows";
}
struct RSIArrows {
double rsi;
double buy_arrow;
double sell_arrow;
}
display {
outputs: 3
RSIArrows.rsi { type: "line"; color: "#3b82f6"; name: "RSI"; width: 2; separate_window: true; };
RSIArrows.buy_arrow { type: "arrow"; color: "#16a34a"; name: "Buy"; width: 1; arrow_code: 233; };
RSIArrows.sell_arrow { type: "arrow"; color: "#dc2626"; name: "Sell"; width: 1; arrow_code: 234; };
}
parameters {
int period = 14; // RSI Period
double oversold = 30.0; // Oversold Level
double overbought = 70.0; // Overbought Level
double arrow_gap = 50; // Arrow distance (points)
}
series double ra_rsi[];
series double ra_up[];
series double ra_dn[];
series double ra_buy[];
series double ra_sell[];
calculate() {
double closes[] = close();
double highs[] = high();
double lows[] = low();
int len = length(closes);
ra_rsi[] = create_array(len);
ra_up[] = create_array(len);
ra_dn[] = create_array(len);
ra_buy[] = create_array(len);
ra_sell[] = create_array(len);
double pd = period;
double pm = pd - 1.0;
double pt = Symbol.Point();
int start = Chart.resume_index;
if (start > 0) { start = start - 1; } else { start = period; }
for (int i = start; i < len; i = i + 1) {
if (i == period) {
double g = 0.0;
double l = 0.0;
for (int j = 1; j <= period; j = j + 1) {
double d0 = closes[j] - closes[j - 1];
if (d0 > 0.0) { g = g + d0; } else { l = l - d0; }
}
ra_up[i] = g / pd;
ra_dn[i] = l / pd;
} else {
double d = closes[i] - closes[i - 1];
double up = 0.0;
double dn = 0.0;
if (d > 0.0) { up = d; } else { dn = 0.0 - d; }
ra_up[i] = (ra_up[i - 1] * pm + up) / pd;
ra_dn[i] = (ra_dn[i - 1] * pm + dn) / pd;
}
ra_rsi[i] = ra_dn[i] == 0.0 ? 100.0 : 100.0 - (100.0 / (1.0 + ra_up[i] / ra_dn[i]));
ra_buy[i] = 0.0;
ra_sell[i] = 0.0;
if (i > period) {
if (ra_rsi[i - 1] < oversold && ra_rsi[i] >= oversold) {
ra_buy[i] = lows[i] - (arrow_gap * pt);
}
if (ra_rsi[i - 1] > overbought && ra_rsi[i] <= overbought) {
ra_sell[i] = highs[i] + (arrow_gap * pt);
}
}
}
return RSIArrows { rsi: ra_rsi, buy_arrow: ra_buy, sell_arrow: ra_sell };
}Example: Adaptive Moving Average
The efficiency ratio (net move ÷ sum of bar-to-bar moves) decides how fast the average follows price: close to the fast EMA in clean trends, close to the slow EMA in noise. The ratio itself is shown as a histogram in its own window.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: adaptive moving average";
}
struct KamaResult {
double kama;
double er;
}
display {
outputs: 2
KamaResult.kama { type: "line"; color: "#f97316"; name: "Adaptive MA"; width: 2; };
KamaResult.er { type: "histogram"; color: "#94a3b8"; name: "Efficiency Ratio"; width: 2; separate_window: true; };
}
parameters {
int er_period = 10; // Efficiency Period
int fast_len = 2; // Fast EMA Length
int slow_len = 30; // Slow EMA Length
}
series double ka_kama[];
series double ka_er[];
calculate() {
double closes[] = close();
int len = length(closes);
ka_kama[] = create_array(len);
ka_er[] = create_array(len);
double fd = fast_len;
double sd = slow_len;
double fast_a = 2.0 / (fd + 1.0);
double slow_a = 2.0 / (sd + 1.0);
int start = Chart.resume_index;
if (start > 0) { start = start - 1; } else { start = er_period; } // reads closes[i - er_period]
for (int i = start; i < len; i = i + 1) {
// efficiency ratio: net move / sum of bar-to-bar moves
double change = Math.Abs(closes[i] - closes[i - er_period]);
double noise = 0.0;
for (int j = 0; j < er_period; j = j + 1) {
noise = noise + Math.Abs(closes[i - j] - closes[i - j - 1]);
}
double er = noise > 0.0 ? change / noise : 0.0;
ka_er[i] = er;
// smoothing constant between the slow and the fast EMA, squared
double sc = er * (fast_a - slow_a) + slow_a;
sc = sc * sc;
// first bar seeds from the previous close
double prev = i == er_period ? closes[i - 1] : ka_kama[i - 1];
ka_kama[i] = prev + sc * (closes[i] - prev);
}
return KamaResult { kama: ka_kama, er: ka_er };
}Example: Higher-Timeframe Overlay
Each chart bar is mapped to the H4 bar that contains it with Bars.Shift; the lines show the previous H4 bar's high, low and close, so they step once per H4 bar. The other series loads in the background — Bars.Count is 0 until it is there, and the htf_ready flag makes the first pass with data redo the whole history.
close / high / low(symbol, timeframe, shift) and Bars.* are registered for indicator files too, but are live-verified so far only in strategies. In the tester, another symbol has no bars yet (values are empty); a higher timeframe of the chart symbol is built from the tester's own bars.about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: previous H4 bar levels";
}
struct HtfLevels {
double prev_high;
double prev_low;
double prev_close;
}
display {
outputs: 3
HtfLevels.prev_high { type: "line"; color: "#16a34a"; name: "Prev H4 High"; width: 1; style: "dashed"; };
HtfLevels.prev_low { type: "line"; color: "#dc2626"; name: "Prev H4 Low"; width: 1; style: "dashed"; };
HtfLevels.prev_close { type: "line"; color: "#9ca3af"; name: "Prev H4 Close"; width: 1; };
}
series double hl_hi[];
series double hl_lo[];
series double hl_cl[];
static int htf_ready = 0; // 1 once the H4 history was there for a full pass
calculate() {
datetime times[] = time();
int len = length(times);
hl_hi[] = create_array(len);
hl_lo[] = create_array(len);
hl_cl[] = create_array(len);
string sym = Symbol();
int start = Chart.resume_index;
if (start > 0) { start = start - 1; } else { start = 0; }
if (htf_ready == 0) { start = 0; } // H4 bars still loading last time: redo all bars
if (Bars.Count(sym, Timeframe.H4) > 1) {
htf_ready = 1;
for (int i = start; i < len; i = i + 1) {
// shift of the H4 bar that contains this chart bar; -1 = before the H4 history
int s = Bars.Shift(sym, Timeframe.H4, times[i]);
if (s >= 0) {
hl_hi[i] = high(sym, Timeframe.H4, s + 1);
hl_lo[i] = low(sym, Timeframe.H4, s + 1);
hl_cl[i] = close(sym, Timeframe.H4, s + 1);
}
}
}
return HtfLevels { prev_high: hl_hi, prev_low: hl_lo, prev_close: hl_cl };
}Mode B — Native Fetch (strategies)
Wrap a native engine. Call Native.Source, Native.Init, Native.Parameter and Native.Bind in Init() (in that order, once per proxy), then declare the targets and fetch the buffers in the proxy's .calculate() block. Native.Init takes a symbol and a typed timeframe — Timeframe for the chart's, or e.g. Timeframe.H1; another symbol works too (it is loaded in the background).
series double declared in the block and sized with create_array(len) before the fetch. Series buffers are allocated by name across the whole script, so two proxies must not reuse a name.about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: native Bollinger proxy";
}
parameters {
int period = 20; // Bands Period
double std_dev = 2.0; // Deviation
}
struct BollingerSignal { double upper; double middle; double lower; }
display {
outputs: 3
NativeBands.upper { type: "line"; color: "#c42626ff"; name: "Upper"; width: 1; };
NativeBands.middle { type: "line"; color: "#b0aaaaff"; name: "Middle"; width: 0.5; };
NativeBands.lower { type: "line"; color: "#50c426ff"; name: "Lower"; width: 1; };
}
Init() {
Native.Source(i.Bollinger);
Native.Init(Symbol(), Timeframe);
Native.Parameter(period, std_dev, Price.Close);
Native.Bind(NativeBands);
}
NativeBands.calculate() {
int len = candle_count();
series double nb_mid[]; series double nb_up[]; series double nb_lo[];
nb_mid[] = create_array(len); nb_up[] = create_array(len); nb_lo[] = create_array(len);
// Buffer indices: 0 = Middle, 1 = Upper, 2 = Lower
Native.Fetch(0, nb_mid);
Native.Fetch(1, nb_up);
Native.Fetch(2, nb_lo);
return BollingerSignal { upper: nb_up, middle: nb_mid, lower: nb_lo };
}
Tick() {
if (!Time.IsNewBar()) { return; }
Print("[NATIVE] upper=" + ToString(NativeBands.upper(0)) + " lower=" + ToString(NativeBands.lower(0))
+ " prev_upper=" + ToString(NativeBands.upper(1)));
}Native.Init(other, Timeframe.H1)) reads its own bars; Name.field(0) is that series' newest bar. Its buffer is drawn one value per chart bar for now (no stepped drawing of other-timeframe buffers yet).Mode C — Load / Custom (strategies)
Load(Name) creates a proxy whose math you write yourself in Name.calculate(), inside the strategy. Its fields are drawn by the display block and read in Tick() like any proxy. To wrap another compiled script from your library instead, use the Custom.* calls (Custom.Source / Custom.Bind / Custom.Fetch) described on the Strategies page.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: Load() proxy";
}
parameters {
int period = 20; // Bands Period
double mult = 2.0; // Deviation
}
struct BandSignal { double upper; double middle; double lower; }
display {
outputs: 3
Bands.upper { type: "line"; color: "#c42626ff"; name: "Upper"; width: 1; };
Bands.middle { type: "line"; color: "#b0aaaaff"; name: "Middle"; width: 0.5; };
Bands.lower { type: "line"; color: "#50c426ff"; name: "Lower"; width: 1; };
fill { buffer_a: 0; buffer_b: 2; color: "rgba(19, 75, 228, 0.15)"; };
}
Init() {
Load(Bands);
}
Bands.calculate() {
double closes[] = close();
int len = length(closes);
series double lp_mid[] = create_array(len);
series double lp_up[] = create_array(len);
series double lp_lo[] = create_array(len);
int start = Chart.resume_index;
if (start == 0) { start = period - 1; }
for (int i = start; i < len; i = i + 1) {
double sum = 0.0;
for (int j = 0; j < period; j = j + 1) { sum = sum + closes[i - j]; }
double mean = sum / period;
lp_mid[i] = mean;
double sq = 0.0;
for (int j = 0; j < period; j = j + 1) {
double d = closes[i - j] - mean;
sq = sq + (d * d);
}
double sd = Math.Sqrt(sq / period);
lp_up[i] = mean + (mult * sd);
lp_lo[i] = mean - (mult * sd);
}
return BandSignal { upper: lp_up, middle: lp_mid, lower: lp_lo };
}
Tick() {
if (!Time.IsNewBar()) { return; }
if (close(2) < Bands.lower(2) && close(1) > Bands.lower(1)) {
Print("[LOAD] back above lower " + ToString(Bands.lower(1)));
}
if (close(2) > Bands.upper(2) && close(1) < Bands.upper(1)) {
Print("[LOAD] back below upper " + ToString(Bands.upper(1)));
}
}Mode D — Hybrid Extension (strategies)
Fetch data from a native engine, then compute secondary values over it in the same proxy — here the change of a native EMA over a few bars, drawn as a histogram in its own window.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: native EMA + slope";
}
parameters {
int period = 50; // EMA Period
int lookback = 5; // Slope Bars
}
struct SlopeLines { double value; double slope; }
display {
outputs: 2
BaseEMA.value { type: "line"; color: "#3b82f6"; name: "EMA"; width: 1; };
BaseEMA.slope { type: "histogram"; color: "#9ca3af"; name: "EMA slope"; width: 2; separate_window: true; };
}
Init() {
Native.Source(i.MA);
Native.Init(Symbol(), Timeframe);
Native.Parameter(period, MA.EMA, Price.Close); // period, type, source
Native.Bind(BaseEMA);
}
BaseEMA.calculate() {
int len = candle_count();
series double hy_ema[];
series double hy_slope[];
hy_ema[] = create_array(len);
hy_slope[] = create_array(len);
Native.Fetch(0, hy_ema);
int start = Chart.resume_index;
if (start > 0) { start = start - 1; } else { start = period + lookback; }
for (int i = start; i < len; i = i + 1) {
hy_slope[i] = hy_ema[i] - hy_ema[i - lookback];
}
return SlopeLines { value: hy_ema, slope: hy_slope };
}
Tick() {
if (!Time.IsNewBar()) { return; }
if (BaseEMA.slope(2) < 0.0 && BaseEMA.slope(1) > 0.0) { Print("[HYBRID] EMA turned up"); }
if (BaseEMA.slope(2) > 0.0 && BaseEMA.slope(1) < 0.0) { Print("[HYBRID] EMA turned down"); }
}Indicator-Style Strategy (wrap native / custom indicators)
An indicator file can't bind native or custom indicators. To build an indicator on top of other indicators, write a strategy that never trades: bind the engines in Init(), compute in the proxy .calculate() blocks, and leave Tick() empty. Its display buffers draw on the chart like any indicator.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Docs indicators: Bollinger of a native RSI";
}
parameters {
int rsi_period = 14; // RSI Period
int band_period = 20; // Band Period
double multiplier = 2.0; // Deviation
}
struct RSIBands { double rsi; double upper; double lower; }
display {
separate_window: true; // whole strategy output in its own window
outputs: 3
RsiBands.rsi { type: "line"; color: "#3b82f6"; name: "RSI"; width: 1; };
RsiBands.upper { type: "line"; color: "#dc2626"; name: "Upper"; width: 1; };
RsiBands.lower { type: "line"; color: "#16a34a"; name: "Lower"; width: 1; };
}
Init() {
Native.Source(i.RSI);
Native.Init(Symbol(), Timeframe);
Native.Parameter(rsi_period, Price.Close);
Native.Bind(RsiBands);
}
RsiBands.calculate() {
int len = candle_count();
series double rb_rsi[];
series double rb_up[];
series double rb_lo[];
rb_rsi[] = create_array(len);
rb_up[] = create_array(len);
rb_lo[] = create_array(len);
Native.Fetch(0, rb_rsi);
double bp = band_period;
int start = Chart.resume_index;
if (start > 0) { start = start - 1; } else { start = rsi_period + band_period; }
for (int i = start; i < len; i = i + 1) {
double sum = 0.0;
for (int j = 0; j < band_period; j = j + 1) { sum = sum + rb_rsi[i - j]; }
double mean = sum / bp;
double sq = 0.0;
for (int j = 0; j < band_period; j = j + 1) {
double d = rb_rsi[i - j] - mean;
sq = sq + (d * d);
}
double sd = Math.Sqrt(sq / bp);
rb_up[i] = mean + (multiplier * sd);
rb_lo[i] = mean - (multiplier * sd);
}
return RSIBands { rsi: rb_rsi, upper: rb_up, lower: rb_lo };
}
Tick() {
// no trading: this strategy only draws
}separate_window: true; moves the whole output into one extra window with one scale. To keep some buffers on the price chart and move only others, put separate_window: true; inside those buffer blocks instead — each flagged buffer gets its own window. See Display & Visuals.Native Buffer Index Map
i.Bollinger0 Middle · 1 Upper · 2 Loweri.MACD0 MACD Line · 1 Signal · 2 Histogrami.RSI0 Value (0–100)i.Stochastic0 %K · 1 %Di.ATR0 Valuei.Ichimoku0 Tenkan · 1 Kijun · 2 Span A · 3 Span B · 4 Chikoui.SAR0 Valuei.Supertrend0 Value · 1 Direction (+1 up / −1 down)i.Pivot0 Pivot · 1 R1 · 2 S1 · 3 R2 · 4 S2 · 5 R3 · 6 S3i.MA0 Valuei.MACross0 Fast MA · 1 Slow MAi.Momentum / i.DeMarker / i.RVI / i.WilliamsR0 Valuei.BullsPower0 Value (period)i.BearsPower0 Value (period)i.ROC0 Value (period, source)i.StdDev / i.OBV / i.MFI / i.CMF / i.ForceIndex0 Valuei.ZigZag0 Value