feat: add per-symbol percentage margin schedule#22
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds percentage-of-notional futures margin support alongside existing fixed-dollar margin, wiring the new schedule through config, broker setup, and account policy.
Changes:
- Added
ContractSpec.margin_pct,BacktestConfig.margin_pct_schedule, broker wiring, and policy margin handling. - Added tests for config round-trip, overlap validation, broker wiring, and percentage margin behavior.
- Applied import/formatting cleanup and updated the README clone URL.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
src/ml4t/backtest/accounting/policy.py |
Adds percentage margin schedule support and overlap rejection. |
src/ml4t/backtest/broker.py |
Wires explicit and contract-derived percentage margin schedules into account policy. |
src/ml4t/backtest/config.py |
Adds config field, serialization, deserialization, and overlap validation for percentage margin schedules. |
src/ml4t/backtest/types.py |
Adds ContractSpec.margin_pct and example documentation. |
src/ml4t/backtest/execution/rebalancer.py |
Formatting-only line wrap. |
src/ml4t/backtest/datafeed.py |
Import formatting cleanup. |
src/ml4t/backtest/execution/schedule.py |
Import formatting cleanup. |
src/ml4t/backtest/result.py |
Import formatting cleanup. |
tests/accounting/test_margin_account_policy.py |
Adds percentage margin policy tests. |
tests/execution/test_rebalancer_futures.py |
Adds broker/contract-spec percentage margin wiring tests. |
tests/test_config_wiring.py |
Adds config round-trip and overlap validation tests, plus formatting cleanup. |
tests/test_artifact_spec.py |
Import ordering cleanup. |
tests/test_broker.py |
Import ordering cleanup. |
tests/test_core.py |
Import ordering cleanup. |
tests/test_datafeed_memory.py |
Import ordering cleanup. |
tests/test_equity_curve.py |
Import ordering cleanup. |
tests/test_result.py |
Import ordering cleanup. |
tests/test_strategy_templates.py |
Import ordering cleanup. |
tests/execution/test_rebalancer.py |
Import ordering cleanup. |
tests/execution/test_schedule.py |
Import ordering cleanup. |
tests/contracts/test_execution_contracts.py |
Import ordering cleanup. |
README.md |
Updates repository clone URL. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+422
to
+426
| # Price-aware per-asset percentage margin (preferred for futures) | ||
| if asset in self.margin_pct_schedule: | ||
| initial, maintenance = self.margin_pct_schedule[asset] | ||
| margin_rate = initial if for_initial else maintenance | ||
| return abs(quantity * price) * margin_rate |
Comment on lines
265
to
+266
| self.fixed_margin_schedule = fixed_margin_schedule or {} | ||
| self.margin_pct_schedule = margin_pct_schedule or {} |
Comment on lines
+516
to
+523
| fixed_assets = set(self.fixed_margin_schedule or {}) | ||
| pct_assets = set(self.margin_pct_schedule or {}) | ||
| overlapping_margin_assets = sorted(fixed_assets & pct_assets) | ||
| if overlapping_margin_assets: | ||
| issues.append( | ||
| "fixed_margin_schedule and margin_pct_schedule cannot both define: " | ||
| f"{overlapping_margin_assets}" | ||
| ) |
|
|
||
| ```bash | ||
| git clone https://github.com/ml4t/ml4t-backtest.git | ||
| git clone https://github.com/ml4t/backtest.git |
Comment on lines
147
to
+148
| effective_margin_schedule = dict(fixed_margin_schedule or {}) | ||
| effective_margin_pct_schedule = dict(margin_pct_schedule or {}) |
Comment on lines
+701
to
+702
| margin = policy.get_margin_requirement("ES", 2, 5000.0, for_initial=True) | ||
| assert margin == 500.0 |
| "long_maintenance_margin": self.long_maintenance_margin, | ||
| "short_maintenance_margin": self.short_maintenance_margin, | ||
| "fixed_margin_schedule": self.fixed_margin_schedule, | ||
| "margin_pct_schedule": self.margin_pct_schedule, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ContractSpec.margin_pctthrough broker/accounting config intoUnifiedAccountPolicyValidation
uv run ruff check src/ tests/uv run ty checkuv run pytest tests/ -quv buildpre-commit run --all-files