Trading and Orders

Trade Statistics


The TradeBuilder tracks the trades of your algorithm and calculates some statistics. You can adjust how it defines a trade and how it matches offsetting order fills.

Set Trade Builder

To set the TradeBuilder, in the Initializeinitialize method, call the SetTradeBuilderset_trade_builder method.

SetTradeBuilder(new TradeBuilder(groupingMethod, matchingMethod));
self.set_trade_builder(TradeBuilder(grouping_method, matching_method))

The following table describes the arguments the TradeBuilder constructor accepts:

ArgumentData TypeDescriptionDefault Value
groupingMethodgrouping_methodFillGroupingMethodThe method used to group order fills into trades
matchingMethodmatching_methodFillMatchingMethodThe method used to match offsetting order fills

The FillGroupingMethod enumeration has the following members:

The FillMatchingMethod enumeration has the following members:

Default Behavior

The default TradeBuilder uses FillGroupingMethod.FillToFillFillGroupingMethod.FILL_TO_FILL and FillMatchingMethod.FIFO.

Check Open Positions

To check if you have a position open for a security as defined by the FillGroupingMethod, call the HasOpenPositionhas_open_position method.

var hasOpenPosition = TradeBuilder.HasOpenPosition(_symbol);
has_open_position = self.trade_builder.has_open_position(self._symbol)

Get Closed Trades

To get your closed trades, use the ClosedTradesclosed_trades property.

var trades = TradeBuilder.ClosedTrades;
trades = self.trade_builder.closed_trades

This property returns a list of Trade objects, which have the following attributes:

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: