Docs / Introduction

Introduction

TDSL (Trading Domain Specific Language) is CoreXTrader's native scripting language for building chart indicators and automated trading strategies. It uses a clean C-style syntax, is statically type-checked, and compiles to Rust / WebAssembly for execution speed.

Every script runs inside the CoreXTrader execution engine — with direct access to live price feeds, bars of other symbols and timeframes, position data, account information, and a library of built-in technical indicators. The compiler checks names, types and argument counts before anything runs, so most mistakes are reported in the IDE with the line and a suggestion (see Compile Errors).


Module Kinds

The kind is decided by the entry point: a file with calculate() and no Tick() is an indicator, a file with Tick() is a strategy.

Indicator
Entry point calculate()
  • Runs over the whole history once, then on every update from Chart.resume_index
  • Returns a struct (or a tuple) of buffers that the display block draws
  • Cannot execute trade orders
  • Cannot use Native/Custom proxies or Load() (those are strategy-only)
  • Supports Init() and Exit()
Strategy
Entry point Tick()
  • Executes on every live price tick (and every tester tick)
  • Full access to Trade, Position, Order, Deal and History APIs
  • Can draw through proxies: Load(), Native.* and Custom.*
  • Supports Init(), Exit(), Timer() and Trade()

File Layout

Everything lives at file level, in any order. A typical file has these parts:

Function / PropertyDescription
about { }Optional metadata (author, version, about, link).
enum Name { }Your own enumerations — shown as dropdowns when used as parameter types.
struct Name { }Result shape: each double field is one output buffer.
display { }How the buffers are drawn: type, colour, pane, levels, scale, fills.
parameters { }User inputs shown in the settings dialog.
state { } / staticGlobals that keep their value between calls.
series double x[];History-backed buffers (indicator outputs, proxy fetch targets).
helper functionsdouble Name(int a) { … } — plain functions, callable from every block.
lifecycle blocksInit(), calculate() / Tick(), Timer(), Trade(), Exit().

The about Block

Recognised keys are author, version, about and link, all string literals; other keys are ignored.

Script metadataTDSL
about {
  author:  "Your Name";
  version: "1.0.0";
  about:   "A short description of what this script does.";
  link:    "https://corextrader.com";
}

Lifecycle Blocks

Init()Once per loadRuns again only when parameters change. Set up state, call Load() / Native.* / Custom.* and Timer.Set() here.
calculate()Every update (Indicators)Full bar history available. Fill buffers from Chart.resume_index on and return them.
Tick()Every tick (Strategies)Live prices and trade execution. Globals keep their values between ticks.
Trade()On each trade event (Strategies)Position opened/closed/modified, order placed/filled/cancelled/expired, rejection. Read the event with Event.*.
Timer()Every Timer.Set(seconds) (Strategies)Runs on the broker clock, also between ticks; stop with Timer.Kill().
Exit()On shutdown / unloadCleanup and final logging.

The parentheses are optional (Init { } works too). init, tick and exit are accepted in lower case, and Shutdown is an alias of Exit; calculate is lower case only, Timer and Trade are capitalised only (lower-case trade is a reserved name).

Note
calculate() is called with the full price history first. The built-in Chart.resume_index is 0 on that first run and points at the first changed bar afterwards — start one bar earlier (Chart.resume_index - 1) so the bar that was still forming is recalculated too.

Bars & Timeframes

All bars are built from M1 data with one bucket rule, the same on the chart, in live scripts and in the tester:

  • M1 … D1 — fixed-length buckets on the UTC M1 timestamps (a D1 bar is a UTC day).
  • W1 — starts Sunday 00:00 UTC (the Sunday-evening open belongs to the new week).
  • MN — the calendar month.

Timeframes are a type of their own (timeframe): write Timeframe.H1, the bare H1, or the bare Timeframe for the chart's timeframe. Strings such as "H1" are rejected where a timeframe is expected. Besides the chart's own bars, a script can read any symbol and timeframe with close(symbol, timeframe, shift) and its siblings, and map chart bars onto them with Bars.Shift / Bars.Count — see Timeframes & Other Symbols.


Your First Indicator

A simple moving average drawn on the price chart. The buffer is a global series, re-sized to the chart with create_array(len) on every call; the engine keeps its earlier values, so only the new bars are computed.

SimpleMA.tdslTDSL
about {
    author: "CoreXTrader";
    version: "1.0.0";
    about: "Docs intro: first indicator (SMA)";
}

parameters {
    int period = 20;    // MA Period
}

struct MAResult {
    double value;
}

display {
    separate_window: false;
    outputs: 1
    MAResult.value { type: "line"; color: "#dc2626ff"; name: "SMA"; width: 2; };
}

series double sma_buf[];

calculate() {
    double closes[] = close();
    int len = length(closes);
    sma_buf[] = create_array(len);

    int start = Chart.resume_index;
    if (start > 0) {
        start = start - 1;          // recalculate the previous bar too
    } else {
        start = period - 1;         // first run: skip bars without enough history
    }

    double pd = period;
    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];
        }
        sma_buf[i] = sum / pd;
    }

    return MAResult { value: sma_buf };
}

Your First Strategy

A strategy that only reports: once per new bar it prints the last close and yesterday's daily close of the same symbol. It shows the lifecycle blocks, a state global and a price accessor on another timeframe.

FirstStrategy.tdslTDSL
about {
    author: "CoreXTrader";
    version: "1.0.0";
    about: "Docs intro: first strategy (no trading)";
}

state {
    int bars_seen = 0;
}

Init() {
    bars_seen = 0;
    Print("[INIT] first strategy on " + Symbol());
}

Tick() {
    if (!Time.IsNewBar()) { return; }
    bars_seen = bars_seen + 1;
    double prev_day = close(Symbol(), Timeframe.D1, 1);
    Print("[BAR] #" + ToString(bars_seen) + " close=" + ToString(close(0)) + " prevD1=" + ToString(prev_day));
}

Exit() {
    Print("[EXIT] " + ToString(bars_seen) + " bars");
}