context.portfolio to inspect strategy performance and positions during a run. Use context.account for account-level leverage and margin fields. Use context.record(...) to write custom metrics into the final performance output.
Portfolio fields
context.portfolio is a Portfolio object updated as the simulation runs.
Common fields:
Example:
Position helpers
Prefer helper methods when you need amounts or values for one asset.await context.portfolio.get_asset_positions(asset, exchange_name=None, trading_account_id=None)
Return positions that match the underlying asset id. This can aggregate across exchange listings of the same underlying asset.
await context.portfolio.get_exchange_asset_positions(asset, exchange_name=None, trading_account_id=None)
Return positions that match the exact exchange asset SID.
Amount and value helpers
exchange_asset versions when the exact listing matters. Use the asset versions when you want to treat listings of the same underlying asset together.
Position fields
EachPosition has:
Account fields
context.account contains account and leverage metrics.
Common fields:
Example:
Recording custom metrics
context.record(*args, **kwargs)
Recorded values are added to the daily performance output and are available in analyze.
Analyze recorded values
perf is produced after the run. Its exact columns depend on the metrics set and on the names you recorded.