Skip to content

Build

Algorithms

Write the rule once; it runs the same way in a backtest as in a live session.

An algorithm is a short JavaScript file that Cloud9Trader runs each time the price updates. Prices, bars, indicators, your positions and your account balance are all variables you can read, and you trade by calling openPosition. There is no broker code, data feed or server to manage. Algorithm concepts explains how your code runs, step by step.

  • An editor in the browser. The Edit tab has the code beside the API documentation. Syntax errors are marked as you type and stop you saving. Warnings, such as an external you declared but never use, appear above the editor.
  • Every save is a version. Saving creates a new numbered version with an optional commit message. Versions are never changed or deleted, and every backtest, optimization and trading session records the version it ran, so you can always read the code behind a result. History lists every version.
  • Externals. Settings you do not want to hard-code are declared as externals, then typed in each time you start a backtest or session, and searched over by the optimizer.
  • Sharing and cloning. You can share an algorithm with the community. Others can read its code and backtest and optimize it, but not change it. See Sharing algorithms. Anyone can Clone a shared algorithm into their own list as “Copy of” its name.
  • A per-algorithm workspace with tabs for Description, Edit, Backtest, Optimize, Trade and Overview.
  1. On Algorithms, type a name and select New. The editor opens on an unsaved version 1.

  2. Write the handlers: onTick runs on every price update and onIntervalClose once per bar. Open positions with openPosition.

  3. Declare any externals at the top:

    const externals = [
    { name: "stopLossPips", defaultValue: 25 },
    { name: "takeProfitPips", defaultValue: 30 },
    ];

    Each one becomes a read-only number your code can use by name. See Externals.

  4. Select Save, or press Ctrl+S.

  5. To share, use the share button in the list and choose to share the current version only, or to share it and every future save. Turn off sharing stops sharing new versions.

So that a backtest and a live session behave the same, algorithms run synchronously in a restricted environment:

  • No timers, async/await, promises or generators.
  • No network or file access, no require, eval or Function.
  • No undeclared globals: declare every variable with let, const or var.
  • JavaScript up to ES2017: optional chaining (a?.b) and ?? are refused.
  • No reassigning the API’s own names, such as BID or openPosition.

These rules are checked fully when a backtest or session starts, not in the editor, so a mistake shows up in that run’s Log.

  • Names are 1 to 100 characters; commit messages up to 255.
  • There is no autosave and no side-by-side comparison of versions.
  • The editor’s autocomplete only knows words already in your code, not the trading API.
  • A backtest stops at the first error your code throws. A live session stops if it errors three times within 60 seconds.
  • A run stops if the algorithm asks for 50 positions within one minute, or after 500 positions. At most 200 positions can be open at once.

Writing, saving, sharing and cloning algorithms is free. Running them uses backtest compute units or a trading session from your plan; see Backtesting.