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.
How an algorithm runs
Section titled “How an algorithm runs”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.
Functions you define
Section titled “Functions you define”- 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, ifprice()orprices()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.
Current price
Section titled “Current price”- TIME: the time of the tick, or the close of the bar.
- BID, ASK, MID: the latest prices.
- SPREAD: ask minus bid, in pips.
Instrument and timeframe
Section titled “Instrument and timeframe”- INSTRUMENT: the instrument’s currencies, precision and pip size.
- INSTRUMENTS: other instruments, by identifier.
- PRECISION and PIP_SIZE.
- INTERVAL and INTERVAL_MS: your session’s timeframe.
- Timeframes:
M1toD1, with their own bars, prices and indicators.
Bars and past prices
Section titled “Bars and past prices”- PRICE: the current or closing bar, with bid and ask.
- OPEN, HIGH, LOW, CLOSE: its mid prices.
- price(): one past price. prices(): a list of them.
- Indicators: the 67 built-in indicators and your own.
- SOURCES: outside data series (not verified to work today).
Account
Section titled “Account”Positions
Section titled “Positions”- openPosition: open a position at the market.
- OPEN_POSITIONS and OPEN_POSITIONS_COUNT: what is open.
- Position: one position, with
close(). - closeAllPositions: close everything.
- closePosition: not working; use
position.close().
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.
Logging and charts
Section titled “Logging and charts”- 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.
Kept for old algorithms
Section titled “Kept for old algorithms”- _ (Underscore): use the built-in array methods instead.
- createFlag: not working.
TypeScript
Section titled “TypeScript”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;