Guide

Scripting

Trademade has a scripting engine that can be used to define custom logic to:

  • Determine when a new trade should be entered
  • Manage an open position

The scripting engine implements ECMAScript 6 (read: Javascript).

Entry filter scripts

Trademade allows an entry filter script to be defined on a bot. This script will run before Trademade attempts to open a new position, and the return value from the script determines if a new position should be opened. If the script returns false, the position will not be opened. For example:

bots:
  - name: Test bot
    ...
    entryFilter: false

The Test bot bot would never open, because the entry filter script always returns false.

Entry filter scripts can be used to only allow bots to open new positions when certain criteria are met. For example, the following script would only open a position when SPX is within 1% of the previous day's close:

Math.abs((spx.last / spx.close) - 1) < 0.01

Bot scripts

Bot scripts will run on an open position during market hours. A script can be run at a defined interval or at a set time every day.

Global scripts

Global scripts will run at a defined time or interval. This could be used, for example, to have Trademade send a daily chart every day at market close:

scripts:
  - time: '16:01'
    script: |
      if (bots.some(bot => bot.dailyEntryCount > 0)) {
        telegramCommand('daychart')
      }

Init scripts

Init scripts are used to define global functions that can be used by all scripts.

A good practice is to use an init script to simply load a separate script file that defines all global variables and functions:

initScript: load('scripts.js');

Any variables or functions defined in an init script will be visible to entry and adjustment scripts.

Key Value Store

The Trademade scripting engine has a key-value store that is accessible via the kv global variable. The key-value allows state to be stored between script runs. This could be used, for example, to keep track of the current daily high of day or low of day to make trading decisions:

const p = kv.get('spx_hod')
if (p == null || p > spx.last) {
  kv.set('spx_hod', p)
}

The key-value store data is persisted between restarts but will be cleared daily.

Externals

The Externals API allows you to make trading decisions based on external data. Externals are API URL endpoints that return JSON that can be accessed by scripts. For example, imagine you don't want to make trades on FOMC meeting days, and you have an API service that can inform you when there is an FOMC meeting scheduled for the current day. First, you would set up an external in your config.yaml to access that endpoint:

externals:
  - id: fomc
    url: https://is-it-fomc-day.com/api
    interval: 30m

When you call the endpoint assume that it returns a boolean value, true when there is an FOMC meeting, otherwise false.

The following bot could be set up so that it would use the return value of the external to decide whether to enter the trade:

bots:
  - name: 0DTE Put
    type: Put
    ...
    entryFilter: externals.fomc.data == false

The entry filter would return false whenever the FOMC API endpoint returns false, which would allow the trade to be entered.

The return value of the API endpoint is stored in the data property of the external. See the Scripting reference for more information.

Previous
Exiting positions