Add multi asset backtest support with full single-asset backward compatibility#1351
Open
TheNoobiCat wants to merge 4 commits intokernc:masterfrom
Open
Add multi asset backtest support with full single-asset backward compatibility#1351TheNoobiCat wants to merge 4 commits intokernc:masterfrom
TheNoobiCat wants to merge 4 commits intokernc:masterfrom
Conversation
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.
Why
The change makes multi-asset portfolio simulation a first-class capability in
backtesting.pywhile preserving existing single-asset behavior and API.The goal is to let users run one backtest across multiple symbols with minimal friction (ideally only changing
datafrom aDataFrameto adict[symbol -> DataFrame]), and keep strategy ergonomics close to current single-asset usage.What I changed
1) Backtest input now supports multi-asset data
Backtest(...)now accepts:pd.DataFrame(legacy, unchanged)Mapping[str, pd.DataFrame](preferred multi-asset API)Sequence[pd.DataFrame](fallback; auto-symbols)2) Strategy trading API extended with optional
symbol=Strategy.buy(...)andStrategy.sell(...)now accept optionalsymbol.symbolis optional and legacy code still works.3) Data access expanded for per-symbol use
self.datasupports per-symbol access in multi-asset strategies:self.data['AAPL'].Close[-1]self.data.get('BTC-USD')self.data.Close[-1]4) Position / order / trade internals are symbol-aware
self.position['AAPL']provides per-symbol position view.self.position.sizeremains portfolio aggregate (legacy semantics preserved).5) Stats remain portfolio-level; trade table adds symbol attribution
Return,Sharpe,Max Drawdown, etc.).stats._tradesnow includesSymbolin multi-asset mode for attribution.6) Plotting supports multi-asset runs
bt.plot()signature remains compatible.7) Compatibility fix for
FractionalBacktestFractionalBacktest.run()now patches both internal_dataand_asset_datapaths so fractional scaling continues to work correctly after multi-asset integration.Backward compatibility
.run(),.plot(), and stats consumption patterns remain valid.Tests
Added focused coverage for:
Symbolpropagation in_tradesposition[...]access + aggregate portfolio position behaviorAlso verified key existing single-asset tests continue to pass.
Notes
cash,commission,margin, etc.) remain shared across assets