Trading Strategy scripts are used for trading one symbol at a time such that each symbol gets its own strategy instance. Common use-cases include momentum strategies, crossover strategies and overbought / oversold strategies, all of which need to evaluate only a single symbol at a time in order to make trading decisions. 


Setup


A Trading Strategy script can be set up from the Desktop Strategy Wizard when a new strategy is added to a Desktop.


Functions


Name

Description

OnInitialize()

This function is used for accepting the script parameters and for initializing the script prior to all other function calls. Once the script is assigned to a Desktop strategy, its parameter values can be specified by the user and can be selected for optimization.

OnBarUpdate(int, int)

This function is called after each new bar of each symbol assigned to the Desktop strategy. It should evaluate the specified symbol and its new bar in order to determine whether to generate new orders for it. Never create indicators, signals or patterns from OnBarUpdate, for performance reasons those should be created from OnInitialize.

OnOrderFillUpdate(int, int, int)

This function is called for each new order fill.

OnOrderUpdate(int, int, C_Status)

This function is called when an order is executed or cancelled.

OnPositionUpdate(int, int, C_PositionStatus)

This function is called when a position is opened or closed.

OnSessionUpdate(int, C_SessionStatus)

This function is called when a session is opened or closed.

OnNewsUpdate(int, long, string, string, C_MessageType)

This function is called when a news update is received and only if the [NO_NEWS_UPDATES] comment is removed.

OnRSSUpdate(int, long, string, string, C_MessageType)

This function is called when an RSS update is received and only if the [NO_RSS_UPDATES] comment is removed.

OnAlertUpdate(int, long, string, C_MessageType)

This function is called when an alert update is received and only if the [NO_ALERT_UPDATES] comment is removed.

OnJournalUpdate(int, long, string, string, C_MessageType)

This function is called when a journal update is received and only if the [NO_JOURNAL_UPDATES] comment is removed.

OnDataConnectionUpdate(int, long, string, C_MessageType)

This function is called when a data connection update is received and only if the [NO_DATA_CONNECTION_UPDATES] comment is removed.

OnBrokerConnectionUpdate(long, string, C_MessageType)

This function is called when a broker connection update is received and only if the [NO_BROKER_CONNECTION_UPDATES] comment is removed.

OnShutdown()

This function is called when the script is shutdown.


Function Details


OnInitialize()

This function is used for accepting the script parameters and for initializing the script prior to all other function calls. Once the script is assigned to a Desktop strategy, its parameter values can be specified by the user and can be selected for optimization.


OnBarUpdate(int symbolIndex, int dataSeries)

This function is called after each new bar of each symbol assigned to the Desktop strategy. It should evaluate the specified symbol and its new bar in order to determine whether to generate new orders for it. Never create indicators, signals or patterns from OnBarUpdate, for performance reasons those should be created from OnInitialize.


Parameters


Type

Identifier

Description

int

symbolIndex

The index of the symbol in the strategy symbol table

int

dataSeries

The number indicating the data series from which the symbol was updated. According to the Desktop strategy data series settings: 0 for the main data series, 1 for the second data series, etc. Use the DataSeriesSwitch function to switch between them.


OnOrderFillUpdate(int symbolIndex, int orderIndex, int orderFillIndex)

This function is called for each new order fill.


Parameters


Type

Identifier

Description

int

symbolIndex

The underlying symbol index of the order

int

orderIndex

The order index

int

orderFillIndex

The order fill index


OnOrderUpdate(int symbolIndex, int orderIndex, C_Status status)

This function is called when an order is executed or cancelled.


Parameters


Type

Identifier

Description

int

symbolIndex

The underlying symbol index of the order

int

orderIndex

The order index

C_Status

status

The updated status of the order (View Options)


OnPositionUpdate(int symbolIndex, int positionIndex, C_PositionStatus status)

This function is called when a position is opened or closed.


Parameters


Type

Identifier

Description

int

symbolIndex

The underlying symbol index of the position

int

positionIndex

The position index

C_PositionStatus

status

The updated status of the position (View Options)


OnSessionUpdate(int symbolIndex, C_SessionStatus status)

This function is called when a session is opened or closed.


Parameters


Type

Identifier

Description

int

symbolIndex

The symbol index whose session is updated

C_SessionStatus

status

The session status (View Options)


OnNewsUpdate(int symbolIndex, long dateTime, string title, string message, C_MessageType type)

This function is called when a news update is received and only if the [NO_NEWS_UPDATES] comment is removed.


Parameters


Type

Identifier

Description

int

symbolIndex

The symbol index for the update

long

dateTime

The date/time in which the update was received by the platform

string

title

The update title

string

message

The update message

C_MessageType

type

The update message type (View Options)


OnRSSUpdate(int symbolIndex, long dateTime, string title, string message, C_MessageType type)

This function is called when an RSS update is received and only if the [NO_RSS_UPDATES] comment is removed.


Parameters


Type

Identifier

Description

int

symbolIndex

The symbol index for the update

long

dateTime

The date/time in which the update was received by the platform

string

title

The update title

string

message

The update message

C_MessageType

type

The update message type (View Options)


OnAlertUpdate(int symbolIndex, long dateTime, string message, C_MessageType type)

This function is called when an alert update is received and only if the [NO_ALERT_UPDATES] comment is removed.


Parameters


Type

Identifier

Description

int

symbolIndex

The symbol index for the update

long

dateTime

The date/time in which the update was received by the platform

string

message

The update message

C_MessageType

type

The update message type (View Options)


OnJournalUpdate(int symbolIndex, long dateTime, string title, string message, C_MessageType type)

This function is called when a journal update is received and only if the [NO_JOURNAL_UPDATES] comment is removed.


Parameters


Type

Identifier

Description

int

symbolIndex

The symbol index for the update

long

dateTime

The date/time in which the update was received by the platform

string

title

The update title

string

message

The update message

C_MessageType

type

The update message type (View Options)


OnDataConnectionUpdate(int symbolIndex, long dateTime, string message, C_MessageType type)

This function is called when a data connection update is received and only if the [NO_DATA_CONNECTION_UPDATES] comment is removed.


Parameters


Type

Identifier

Description

int

symbolIndex

The symbol index for the update

long

dateTime

The date/time in which the update was received by the platform

string

message

The update message

C_MessageType

type

The update message type (View Options)


OnBrokerConnectionUpdate(long dateTime, string message, C_MessageType type)

This function is called when a broker connection update is received and only if the [NO_BROKER_CONNECTION_UPDATES] comment is removed.


Parameters


Type

Identifier

Description

long

dateTime

The date/time in which the update was received by the platform

string

message

The update message

C_MessageType

type

The update message type (View Options)


OnShutdown()

This function is called when the script is shutdown.