Trades And Position Management


An essential task of any trading strategy is to continuously monitor its positions. An open position is a number of instrument units (shares, contracts, options etc.) that have been bought, sold, or sold short. A closed out position is one that is flat. Since Trading System API allows its strategies to have any number of instrument members, it can thus also have an equal number of positions, one per instrument. An instrument's position can be determined at any time by calling one or more of the following members:

 

instrument::is_long()

Returns true if the position state is long.

instrument::is_short()

Returns true if the position state is short.

instrument::is_flat()

Returns true if the position state is flat.

instrument::position_size()

Returns the size of the position, regardless of whether its state is long or short. The integer value returned is the absolute value of the integer returned by the instrument::position() member.

instrument::position()

Returns the position as an integer. The sign preceding the integer value determines whether the position state is long or short (negative means short). A value of 0 signifies that the position is flat.

 

Defining a 'Trade'


A trade is defined as a series of transactions during which an instrument's position state (long, short, flat) only changes twice, once when the trade is opened and a second time when it is closed out. The first transaction is referred to as the trade entry leg which can then be followed by any number of add-on and partial-exit transactions that increase or decrease the position without affecting the position state. Ultimately there is one last transaction that closes out the trade.

Trade Related Event Handlers


Class strategy  features two trade related event handlers which can be used to define custom behaviour when a trade is opened or closed.

 

virtual void on_trade_open(instrument&);

virtual void on_trade_close(instrument&, const trade_info&);

 

 

'Open Trade' Metrics


The instrument object tracks various trade related metrics while a trade is on. These can be accessed with the following instrument members:

 

instrument::trade_leg_count()

The number of transactions that make up the current trade.

instrument::trade_bar_count()

The number of bars since the trade was opened.

instrument::trade_first_leg_entry_price()

The price of the transaction that opened the trade.

instrument::trade_net_profit()

The net profit/loss of the current trade.

instrument::trade_first_leg_entry_timestamp()

The timestamp of the bar on which the trade was first opened.

instrument::trade_max_position()

The maximum trade position, in units (contracts, shares, options), so far.

instrument::trade_most_favorable_price()

The most favorable price since trade entry.