Trading and Orders

Trade Statistics

Introduction

The TradeBuilder tracks the trades of your algorithm and calculates some statistics. Some of these statistics are a function of the risk free interest rate and the number of trading days per year. You can adjust how the TradeBuilder 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.

// Check if the trade builder has recorded any active trades (unclosed positions)
// for a security.
var hasOpenPosition = TradeBuilder.HasOpenPosition(_symbol);
# Check if the trade builder has recorded any active trades (unclosed positions)
# for a security.
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: