Reference

Scripting Reference

Global objects and functions

NameDescription
spxSPX market data object.
vixVIX market data object.
vix9dVIX9D market data object.
vix1dVIX1D market data object.
esES (Futures) market data object.
botsTrademade bots configuration.
botThe bot that is currently in scope. This value is only set for bot scripts and entry filter scripts, otherwise it is undefined.
externalsTrademade externals.
argvAn array of arguments passed to the current command script. This value is only set for command scripts, otherwise it is undefined.
kvKey-value store object.
accountValuesAccount values for the default Interactive Brokers account.
info(msg, markdown = false)Sends an info message via Telegram. The msg parameter must be a string. If markdown is true, the message will be sent with markdown formatting. The default is false.
warn(msg, markdown = false)Sends a warning message via Telegram. The msg parameter must be a string. If markdown is true, the message will be sent with markdown formatting. The default is false.
error(msg, markdown = false)Sends an error message via Telegram. The msg parameter must be a string. If markdown is true, the message will be sent with markdown formatting. The default is false.
log(msg)Emits a message to the Trademade log file. The msg parameter must be a string.
telegramCommand(cmd)Executes the cmd Telegram command. The preceding slash (/) is not required. For example, telegramCommand("status") would run the /status command with the output being returned to the user via Telegram.

Types

MarketData

Properties

NameTypeDescription
lastNumberLast price
openNumberCurrent session open price
closeNumberThe closing price from the previous day
highNumberHigh price for the day
lowNumberLow price for the day
isTradingHoursBooleanA boolean that indicates if the instrument is currently trading

Bots

An ECMAScript Array object with a few additional methods and properties.

Properties

NameTypeDescription
currentBotThe current bot in context for the script.
lengthNumberThe total number of bots.

Methods

NameReturn typeDescription
get(name)BotGet the bot with the specified name.
find(regexp)BotFinds the first bot that matches the given regular expression or string.
findAll(regxp)IteratorReturns an iterator with all bots that match the given regular expression.

Bot

Properties

NameTypeDescription
nameStringThe bot name
openBooleanA boolean value that indicates if the bot has any open positions
typeStringThe position type of the bot
isCreditBooleanA boolean value that indicates if the bot is a credit position
isDebitBooleanA boolean value that indicates if the bot is a debit position
openCountNumberThe number of open trades
isTradingHoursBooleanA boolean value that indicates if the market is currently open and trading
dailyClosedProfitNumberThe daily P/L for the bot's closed positions
dailyEntryCountNumberThe number of entries the bot has made for the current day
dailyExitCountNumberThe number of closed trades for the current day
tagsArray of StringThe array of tags that are applied to this bot.
tradeIdNumberThe unique trade ID for the currently open position. An error will be raised if there is no open position.
deltaNumberThe delta value for the currently open position. An error will be raised if there is no open position.
currentPriceNumberThe current price of the currently open position. An error will be raised if there is no open position.
entryPriceNumberThe entry price of the currently open position. An error will be raised if there is no open position.
expiryStringThe expiry date-time of the currently open position. This value can be parsed by Date.parse(). An error will be raised if there is no open position.
dteNumberThe number of days to expiration for the currently open position. An error will be raised if there is no open position.
positionNumberThe quantity for the currently open position. This value will be negative for credit and positive for debit positions. An error will be raised if there is no open position.

Methods

NameReturn typeDescription
close()NoneImmediately closes the position.
isStopEnabled(type)booleanReturns a boolean that indicates if type stop monitor is enabled. The type parameter is a string and must be one of: delta, percent, itm, value, or auto-close.
enableStop(type)NoneEnables the stop monitor for the specified stop type. The type parameter is a string and must be one of: delta, percent, itm, value, or auto-close.
disableStop(type)NoneDisables the stop monitor for the specified stop type. The type parameter is a string and must be one of: delta, percent, itm, value, or auto-close.
delta(legIndex)NumberThe delta value for the specified leg. The legIndex parameter is a number and must be between 0 and the total number of legs in the position. For spread positions 0 is the primary leg and 1 is the spread leg. For a strangle 0 is the put side and 1 is the call side. For an iron condor 0 is the short put, 1 is the long put, 2 is the short call, and 3 is the long call. An error will be raised if there is no open position.

Externals

External objects can be accessed through the [id] property of the externals object. For example: externals['fomc'] would return the external object defined with an id of fomc in the config. It is also possible to enumerate over the externals object to access each external using a for...in statement, for example:

for (const id in externals) {
  log(externals[id].data)
}

Properties

NameTypeDescription
idExternalThe external object identified by id. This is the id attribute defined on the external's configuration.

External

Properties

NameTypeDescription
dataObjectThe object returned by the API endpoint. This value will be null if the endpoint has not yet been fetched, or if there was an error accessing the endpoint.
updateTimeDateThe last time the data was updated. This value is null if the endpoint has not yet been fetched.
isErrorbooleanReturns a boolean that indicates if there was an error accessing the API endpoint.
errorTextStringThe text when an error is returned from the API endpoint.
errorCodeNumberThe response code when an error is returned from the API endpoint.
idStringThe identifier for the external. This is the id attribute defined on the external's configuration.

KVStore

The KVStore object is a key-value store that can be used to store data between script runs. It is able to store primitive types (string, number, boolean) only.

KVStore data is persisted between restarts but will be automatically cleared daily.

Methods

NameReturn typeDescription
get(key)ObjectGet an object from the key-value store. This will return null if no mapping exists for the given key. The key parameter must be a string.
set(key, value)NoneSets an object in the key-value store. The key parameter must be a string and value must be a primitive type (string, number or boolean).

Account Values

The Account Values object exposes the current account values that are available to TWS. Values are accessible as though they are keys in a hash map, for example:

log(accountValues['ExcessLiquidity'])

A null value will be returned if the value does not exist.

See the TWS API documentation for a full listing of available values.

Previous
config.yaml