Trade Smarter with Automation

Custom Workers

This document will cover creating custom workers in Alta5. It assumes the reader has basic programming skills and has reviewed the Strategy Basics guide.

Configuration

get()

Workers should use the method this.get(name:string) to offer configuration options using the built-in naming conventions and trader parameter access.

Assuming you were to call:


var delay = this.get('delay');

get() would look up the value for delay using the following priority:

  1. Check the worker for a property named delay that has a value.
  2. Check that value to see if it is a parameter reference ($paramName). Resolves any parameter reference by calling trader.get('paramName').
  3. If the value is still undefined, it looks on the worker for property called delayDefault and uses the value as the default value.
  4. If the result of any of those is a function, the function will be called and the return value will be used.

Example:


Worker.define({
    delay: undefined,           // can be set directly, or with parameter value
    delayDefault: 1,            // default value

    onWork: function(trader){
        var delay = this.get('delay');
        ...
    }
});

getSecurity()

A shortcut function this.getSecurity(name:string) to get a Security from a configurable property. The same as calling Security.get(this.get('security')).

Ready State

Workers are responsible for managing their own ready state. An trader will not run while a Worker is not ready. A worker that doesn't need to block the ready state of its trader can set waitForReady: false on the Worker configuration.

The usual pattern for managing ready state is to set this.ready = false; when the worker begins doing any asynchronous work and calling this.markReady(); when that work is complete.

Example:


Worker.define({
    security: '$target',

    onWork: function(trader){
        this.ready = false;     // not ready when working

        var target = this.getSecurity('security');
        target.onReady(function(){
            // calculate and do some cool stuff
            // ...
            this.markReady();
        }, this);
    }
});

Worker Methods

Overview

Workers are implemented using predefined template methods that are error safe and automatically wired to any necessary events. onWork is the primary entry method for a worker. Setting any other template method to the string 'work' will cause onWork to be called when that method is called.

For example:


Worker.define({
    onInit: 'work',         // work on initialization
    onMarketOpen: 'work',   // work every time the market opens

    onWork: function(trader){
        this.ready = false;
        ...
    },

    onMarketClose: function(trader){
        // do some clean up / logging / etc
        ...
    }
});

Available Methods

The following methods are available to be implemented or trigger onWork in every Worker:

Method Name Description
onMarketBeforeOpen Called before the Market open at Market.beforeOpenTime (defaults to 9:15am).
onMarketOpen Called when the Market opens.
onMarketClose Called when the Market closes.
onMarketMinute Called every minute the market is open.
onMarketMinuteN Called every N minutes the market is open. N can be 5, 10, 15 or 30.
onMarketHour Called every hour, on the hour, that the market is open.
onPositionEnter Called every time the trader enters a position.
onPositionExit Called every time the trader exit a position.
onPositionExpire Called every time the trader has a position expire.
onPause Called every time the trader is paused.
onUnpause Called every time the trader is unpaused.

Sample Custom Worker


Worker.define({
    type: 'sampleworker',   // type used to create the worker, lowercase
    id: 'sampleWorker',   // default id, camel case

    onInit: 'work',
    onMarketOpen: 'work',

    // TODO
});