Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Auto-generated from source docstrings.
- get_position
- get_positions
- get_cash
- equity
- get_account_value
- get_rejected_orders
- set_position_rules
Expand Down
3 changes: 2 additions & 1 deletion docs/user-guide/stateful-strategies.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ Stateful strategies depend on querying execution state. Key broker methods:
|--------|---------|---------|
| `get_position(asset)` | Position or None | All patterns |
| `get_positions()` | Dict of all positions | Multi-asset |
| `get_account_value()` | Total portfolio value | Sizing |
| `equity()` | Current marked account equity | Sizing |
| `get_account_value()` | Same value as `equity()` | Existing code |
| `get_cash()` | Available cash | Capital allocation |
| `get_asset_stats(asset)` | Trade statistics | Kelly sizing |
| `get_order(order_id)` | Order status | Grid trading |
Expand Down
6 changes: 5 additions & 1 deletion src/ml4t/backtest/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,13 @@ def get_cash(self) -> float:
"""
return self.cash

def equity(self) -> float:
"""Calculate current marked account equity."""
return self._portfolio_ledger.get_account_value()

def get_account_value(self) -> float:
"""Calculate total account value (cash + position values)."""
return self._portfolio_ledger.get_account_value()
return self.equity()
Comment on lines 743 to +745

def get_rejected_orders(self, asset: str | None = None) -> list[Order]:
"""Get all rejected orders, optionally filtered by asset.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ def test_get_account_value(self, broker):
value = broker.get_account_value()
assert value == 100000.0

def test_equity_uses_current_mark_prices(self, broker_with_position):
"""Test equity returns cash plus current marked position value."""
broker_with_position._update_time(
datetime(2024, 1, 2, 9, 30),
{"AAPL": 155.0},
{"AAPL": 154.0},
{"AAPL": 156.0},
{"AAPL": 153.0},
{"AAPL": 1000.0},
{},
)
Comment on lines +60 to +68

assert broker_with_position.equity() == 115500.0
assert broker_with_position.get_account_value() == broker_with_position.equity()

def test_get_position_none(self, broker):
"""Test get_position returns None for no position."""
assert broker.get_position("AAPL") is None
Expand Down
Loading