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:
| Field | Meaning |
|---|---|
starting_cash | Starting cash balance |
cash | Current cash |
portfolio_value | Cash plus position value |
pnl | Profit and loss |
returns | Current return value |
cash_flow | Cash changes |
positions_value | Total market value of positions |
positions_exposure | Total exposure of positions |
positions | Nested position storage by exchange/account/asset in current runtime |
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:
| Field | Meaning |
|---|---|
asset | Exchange asset |
amount | Current amount |
cost_basis | Average cost per share or contract |
last_sale_price | Last synced price |
last_sale_date | Last sale datetime, when available |
Account fields
context.account contains account and leverage metrics.
Common fields:
| Field | Meaning |
|---|---|
settled_cash | Settled cash |
buying_power | Buying power |
equity_with_loan | Equity with loan value |
total_positions_value | Total position value |
total_positions_exposure | Total position exposure |
available_funds | Available funds |
excess_liquidity | Excess liquidity |
leverage | Gross leverage |
net_leverage | Net leverage |
net_liquidation | Net liquidation value |
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.