Skip to main content

Strategy

A backtest strategy: subclass it, implement on_update() and trade through self. on_update() runs once per pulse, after the clock has advanced and before the broker fills that pulse’s orders. The collaborators it reaches for (datafeed, resolver, broker, portfolio, pulse_index) are attached by the TradingEnvironment when the run starts, which is why they are not constructor arguments. Initialize the Strategy with its underlying id(s), a name and a description. Parameters

Strategy.id

property
Get the unique identifier of the strategy.

Strategy.underlying_id

property
Get the underlying ID(s) of the strategy.

Strategy.pulse_index

property
Get the pulse index of the strategy.

Strategy.last_pulse

property
Get the last pulse time of the strategy.

Strategy.open_trades

property
Get the list of open trades in the portfolio.

Strategy.has_open_trades

property
Check if there are any open trades in the portfolio.

Strategy.trades

property
Get the list of all trades in the portfolio.

Strategy.broker

property
Get the broker associated with the strategy.

Strategy.portfolio

property
Get the portfolio associated with the strategy.

Strategy.current_equity

property
Get the current equity of the portfolio.

Strategy.initial_equity

property
Get the strategy’s starting capital.

Strategy.pnl

property
Cumulative net PnL of the strategy. current_equity - initial_equity. Includes the impact of fees (because both terms are post-fee).

Strategy.pnl_pct

property
Cumulative PnL as a fraction of the account equity right now. pnl / abs(current_equity). The denominator is the current portfolio equity, so this is the live, sizing-aware return of the whole strategy. Same basis as TradesSummary.pnl_pct; differs from Trade.pnl_pct, which is fixed to each trade’s equity-at-open snapshot. abs keeps the sign coming from pnl. Returns 0.0 if current_equity is 0.

Strategy.pnl_pct_of_initial_equity

property
Cumulative PnL as a fraction of the strategy’s starting capital. pnl / abs(initial_equity). Stable denominator across the entire run, mirroring Trade.pnl_pct_of_initial_equity and TradesSummary.pnl_pct_of_initial_equity. Returns 0.0 if initial_equity is 0.

Strategy.datafeed

property
Get the datafeed associated with the strategy.

Strategy.resolver

property
Get the resolver associated with the strategy.

Strategy.stats

property
Get the statistics for the strategy.

Strategy.set_datafeed

Set the datafeed for the strategy. Parameters

Strategy.set_resolver

Set the resolver for the strategy. Parameters

Strategy.set_broker

Set the broker for the strategy. Parameters

Strategy.set_portfolio

Set the portfolio for the strategy. Parameters

Strategy.set_pulse_index

Set the pulse index for the strategy. Parameters

Strategy.on_update

This method should be implemented in the Strategy subclass to define the strategy’s logic.

Strategy.pulse

Pulse the strategy, executing trades.

Strategy.create_order

Place a market order. It is filled on the same pulse, at the current pulse’s market data. This is just a wrapper around the broker’s create_order method. You can also use::
Parameters

Strategy.buy

Buy an instrument. The order is filled on the same pulse, at the current pulse’s market data. Parameters

Strategy.sell

Sell an instrument. The order is filled on the same pulse, at the current pulse’s market data. Parameters

Strategy.close_all

Close all open trades in the portfolio. Raises

Strategy.calculate_stats

Get the statistics for the strategy. Parameters Returns

Strategy.get_trades_by_tags

Get all trades with specific tags. Parameters Returns

Strategy.close_trades_by_tags

Close all open trades with specific tags. Parameters

Strategy.calculate_trades_summary

Aggregate a group of trades into one TradesSummary, marked at the current pulse. Parameters Returns Raises

Strategy.get_trades_summary_by_tags

Get a summary of trades with specific tags. Parameters Returns

to_underlying_ids

Wrap one id or several into UnderlyingIDs, in the order given. A plain str gains the default declaration; an already declared id keeps its variable_contract_size. A single UnderlyingID is itself a str, which is why the isinstance branch treats it as one id rather than as a sequence of characters.
Last modified on November 4, 2026