Skip to content

Developers

Algorithm SDK

Every name your algorithm can use, one page each. The signatures on these pages are copied from the SDK’s type definitions, the same ones POST /api/algorithms/compile checks TypeScript against, and every example on them is typechecked against those definitions when the docs are built.

New to algorithms? Start with Algorithms, Algorithm concepts and the moving average crossover guide.

Your script runs once at the start, then Cloud9Trader calls the functions you define: onTick on every price update and onIntervalClose when each bar closes. Before each call it sets the values below for that moment, so BID in onTick is the bid of that tick. The same script runs the same way in a backtest and in a live session.

Code runs synchronously in a restricted environment: no timers, promises, network, files, require, eval or Function, and every variable must be declared. See What your code cannot do.

  • onTick: runs on every price update.
  • onIntervalClose: runs when each bar closes.
  • getBufferSize(): optional. Return how many bars of history to load before the run starts, if price() or prices() reach further back than your indicators do.
  • getRunUpCount(): optional. Return how many bars to run through before the start date. See History before the start.

Settings you declare in const externals = [...] become read-only numbers your code uses by name. See Externals.

  • TIME: the time of the tick, or the close of the bar.
  • BID, ASK, MID: the latest prices.
  • SPREAD: ask minus bid, in pips.

Math.average, Math.mean, Math.sum, Math.count, Math.highest, Math.lowest, Math.variance, Math.standardDeviation, Math.meanDeviation and Math.sumSquares work on lists of numbers.

  • log and console: write to the run log.
  • error: stop the run with a message.
  • plot: draw your own series on the backtest chart.
  • TICK_COUNT: ticks handled so far.

The type definitions are served at GET /api/algorithm-sdk. Externals and your own indicators are names only your script knows, so declare them yourself in TypeScript:

declare const stopLossPips: number;
declare function myMomentum(periods: number): number;