Skip to content

feat: add per-symbol percentage margin schedule#22

Merged
stefan-jansen merged 1 commit into
mainfrom
feat/per-symbol-margin-pct
May 16, 2026
Merged

feat: add per-symbol percentage margin schedule#22
stefan-jansen merged 1 commit into
mainfrom
feat/per-symbol-margin-pct

Conversation

@stefan-jansen

Copy link
Copy Markdown
Contributor

Summary

  • add per-symbol percentage-of-notional margin schedules alongside fixed-dollar futures margin
  • wire ContractSpec.margin_pct through broker/accounting config into UnifiedAccountPolicy
  • add config validation and round-trip coverage for overlapping margin models
  • include the pending README clone URL correction and repo-wide hook-driven formatting fixes

Validation

  • uv run ruff check src/ tests/
  • uv run ty check
  • uv run pytest tests/ -q
  • uv build
  • pre-commit run --all-files

Copilot AI review requested due to automatic review settings May 16, 2026 19:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}"
)
Comment thread README.md

```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,
@stefan-jansen stefan-jansen merged commit 6addac9 into main May 16, 2026
8 checks passed
@stefan-jansen stefan-jansen deleted the feat/per-symbol-margin-pct branch May 16, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants