MarketLab Docs
ScriptingData Types

Candles

Candle data shapes for Market Lab scripts.

Candles

Candle data is available under input.candles.

Manifest

export const script = {
  name: "candle-script",
  version: "1",
  sources: ["candles"],
  params: {}
}

The command decides the mode. script backtest passes candle arrays. script run passes one live candle at a time.

Backtest Command

mlab script backtest ./scripts/candle-script.js \
  --provider mmt \
  --exchange bybitf \
  --symbol BTC/USDT \
  --from 1780307559000 \
  --to 1780523645331 \
  --source candles:timeframe=60

Stream Command

mlab script run ./scripts/candle-script.js \
  --provider mmt \
  --exchange bybitf \
  --symbol BTC/USDT \
  --source candles:timeframe=60

Input

Check input.mode before reading candle data.

Window mode:

type CandleWindowInput = {
  mode: "window";
  candles: {
    candles: Candle[];
  };
}

Stream mode:

type CandleStreamInput = {
  mode: "stream";
  candles: {
    candle: Candle;
  };
}

Window mode gives an array at input.candles.candles. Stream mode gives one live candle at input.candles.candle. If a script supports both commands, branch on input.mode explicitly.

Candle

type Candle = {
  t: number;
  o: number;
  h: number;
  l: number;
  c: number;
  vb: number;
  vs: number;
  tb: number;
  ts: number;
}

Fields:

  • t: candle timestamp in Unix seconds
  • o: open price
  • h: high price
  • l: low price
  • c: close price
  • vb: buy volume in quote/USD terms
  • vs: sell volume in quote/USD terms
  • tb: buy trade count
  • ts: sell trade count

Example

export function onData(ctx, input) {
  const candles = input.candles.candles
  const latest = candles[candles.length - 1]

  return {
    metrics: {
      candles: candles.length,
      latest_close: latest.c,
      latest_delta: latest.vb - latest.vs
    }
  }
}

Stream Example

export function onData(ctx, input) {
  const candle = input.candles.candle

  return {
    metrics: {
      close: candle.c,
      delta: candle.vb - candle.vs
    }
  }
}

On this page