Register Account

Algorithmic Trading

Writing Your Algorithms

The Basics

Cloud9Trader algorithms are written in plain JavaScript directly into the edit screen. Just select or create a new algorithm to get started. You declare an onTick function that executes every time a price tick comes in, i.e. every time the price changes. You can also declare an onIntervalClose function that will, as you guessed, be called every time a bar closes. Any code outside these functions is run once at the beginning of your trading session or backtest.

If you are new to JavaScript the good news is that it is a popular language with wide proliferation and plenty of online resources available. The other good news is that you only need to know the very basics to quickly get up and running in developing algorithmic trading scripts. All you'll need then is to use the API Reference to find the functions and variables that are available for you to use in your scripts. We recommend these online resources to get you started:

Mozilla Developer Network


Bear in mind that much of the complexities in JavaScript development stem from building for web browsers, where it is most widely implemented, and from the need for well architected object oriented applications. You just won't need any of that stuff to write trading scripts.

So, let's get going with what you will need to know specific to writing trading algorithms.


There's not much to say about how to structure your algorithms, just that they need to look something like this:

// Any initialization code goes here

function onTick() {
    // This code runs on every every tick

function onIntervalClose() {
    // This code runs every time an interval closes

You don't need to declare an onTick or onIntervalClose function, but if you declare neither your script won't do very much!

Variable Scope

We're not going to give you a JavaScript lesson, but algorithms will often need a value to be remembered between ticks, so it's worth mentioning variable scope. In JavaScript any variables declared outside a function are available to that function and any others declared alongside it. Variables declared inside a function are private to that function. What this means for your algorithm is that if you declare a variable outside of onTick or onIntervalClose and set its value inside one of those functions, it value will be remembered on the next tick or interval close.

Take a look at this example:

// This variable is declared globally so its value will be remembered on subsequent ticks
var previousPrice;

function onTick() {
    // Private variables are declared here. Set a variable to the current mid price
    var currentPrice = (BID + ASK) / 2;
    var priceMovement;

    // Check that previousPrice has been set (i.e. this isn't the first tick)
    if (previousPrice) {
        // Do something useful with the value we set last tick
        priceMovement = currentPrice - previousPrice;

    // Set previousPrice to the current price so it's available on the next tick
    previousPrice = currentPrice;

As we are declaring previousPrice outside the onTick function, the previously set value will be available each time the function is called.


Externals are any settings that you don't want hard coded into your script, typically because you want the same script to be adaptable for any given market behaviour. The purpose of backtesting is of course to determine that your script works and is profitable, but also to determine the best combination of external values for the instrument you are trading.

Optimization is the process of backtesting repeatedly with different permutations of external values to find the most profitable combination. You'll want to periodically re-optimize to ensure your scripts remain their most profitable.

Externals are registered by declaring a global externals variable and setting it to an array of external names as strings. The example below registers externals for your stop loss and take profit margins, and for the maximum spread you are willing to trade:

// Register a list of externals
var externals = ["stopLossPips", "takeProfitPips", "maxSpread"];

function onTick() {

    // If conditions are met and spread is less than or equal to external value
    if (tradeConditionsSatisfied() && SPREAD <= maxSpread) {

        // Open a long position
        openPosition("BUY", {
            quantity: 1000,
            stopLoss: BID - (PIP_SIZE * stopLossPips),
            takeProfit: BID + (PIP_SIZE * takeProfitPips)

function tradeConditionsSatisfied() {
    // Determine whether ok or not to trade and return true or false

The value of the externals are set on the backtest, optimization and trade screens when running backtests or starting a live trading session.

If you would like to set a default value for your externals, you can declare each one as an object, as below. The new backtest and trading session screens will then have these ready filled in for you.

// Register a list of externals
var externals = [
    {name: "stopLossPips", defaultValue: 25},
    {name: "takeProfitPips", defaultValue: 30}

Of course to write a strategy that is consistently profitable takes time and commitment, but in terms of getting started, that really is all there is to it. The API Reference is your look up manual to keep nearby as you write your scripts. Have a glance over it to see what's available to you and most importantly, happy coding!

Was this page useful? If you find any errors or have any questions please get in touch at