-
Notifications
You must be signed in to change notification settings - Fork 1
feat(backtest): add intrabar fills and borrow fees #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
01e569b
feat(backtest): add engine wrapper and tests
AKKI0511 1521229
feat(backtest): add brownian intrabar simulator
AKKI0511 80f913e
fix(backtest): retain intrabar carry direction
AKKI0511 69ce003
docs: expand backtesting impact and intrabar features
AKKI0511 abe0bfe
Update __init__.py
AKKI0511 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,72 +1,31 @@ | ||
| """QuantTradeAI | ||
| ================= | ||
|
|
||
| High-level interface for the QuantTradeAI toolkit. The package bundles | ||
| data acquisition, feature engineering, model training and backtesting | ||
| utilities for quantitative trading research. | ||
|
|
||
| Public API: | ||
| - ``DataSource`` and concrete implementations | ||
| - ``DataLoader`` and ``DataProcessor`` | ||
| - ``MomentumClassifier`` model | ||
| - ``PortfolioManager`` and risk helpers | ||
| - ``simulate_trades`` and ``compute_metrics`` for backtesting | ||
|
|
||
| Quick Start: | ||
| ```python | ||
| from quanttradeai import DataLoader, DataProcessor, MomentumClassifier | ||
|
|
||
| loader = DataLoader() | ||
| data = loader.fetch_data() | ||
| processor = DataProcessor() | ||
| processed = {s: processor.process_data(df) for s, df in data.items()} | ||
| model = MomentumClassifier() | ||
| ``` | ||
| """ | ||
|
|
||
| from .data.datasource import ( | ||
| DataSource, | ||
| YFinanceDataSource, | ||
| AlphaVantageDataSource, | ||
| WebSocketDataSource, | ||
| ) | ||
|
|
||
| # Lazily import optional dependencies to keep lightweight usage possible | ||
| from .data.loader import DataLoader | ||
| from .data.processor import DataProcessor | ||
|
|
||
| try: # pragma: no cover - optional heavy dependency | ||
| from .models.classifier import MomentumClassifier | ||
| except Exception: # pragma: no cover - tolerate missing ML libs | ||
| MomentumClassifier = None # type: ignore[assignment] | ||
| from .trading.portfolio import PortfolioManager | ||
| from .trading.risk import apply_stop_loss_take_profit, position_size | ||
| from .backtest import ( | ||
| simulate_trades, | ||
| compute_metrics, | ||
| MarketImpactModel, | ||
| LinearImpactModel, | ||
| SquareRootImpactModel, | ||
| AlmgrenChrissModel, | ||
| ImpactCalculator, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "DataSource", | ||
| "YFinanceDataSource", | ||
| "AlphaVantageDataSource", | ||
| "WebSocketDataSource", | ||
| "DataLoader", | ||
| "DataProcessor", | ||
| "MomentumClassifier", | ||
| "PortfolioManager", | ||
| "apply_stop_loss_take_profit", | ||
| "position_size", | ||
| "simulate_trades", | ||
| "compute_metrics", | ||
| "MarketImpactModel", | ||
| "LinearImpactModel", | ||
| "SquareRootImpactModel", | ||
| "AlmgrenChrissModel", | ||
| "ImpactCalculator", | ||
| ] | ||
| """Lightweight package initializer for QuantTradeAI. | ||
|
|
||
| Only core trading and backtesting utilities are imported to keep the | ||
| module usable without optional heavy dependencies.""" | ||
|
|
||
| from .trading.portfolio import PortfolioManager | ||
| from .trading.risk import apply_stop_loss_take_profit, position_size | ||
| from .backtest import ( | ||
| simulate_trades, | ||
| compute_metrics, | ||
| MarketImpactModel, | ||
| LinearImpactModel, | ||
| SquareRootImpactModel, | ||
| AlmgrenChrissModel, | ||
| ImpactCalculator, | ||
| BacktestEngine, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "PortfolioManager", | ||
| "apply_stop_loss_take_profit", | ||
| "position_size", | ||
| "simulate_trades", | ||
| "compute_metrics", | ||
| "MarketImpactModel", | ||
| "LinearImpactModel", | ||
| "SquareRootImpactModel", | ||
| "AlmgrenChrissModel", | ||
| "ImpactCalculator", | ||
| "BacktestEngine", | ||
| ] |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Backtest engine coordinating portfolio and risk management.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Dict, Optional | ||
|
|
||
| import pandas as pd | ||
|
|
||
| from quanttradeai.trading.portfolio import PortfolioManager | ||
| from quanttradeai.trading.risk_manager import RiskManager | ||
|
|
||
| from .backtester import simulate_trades | ||
|
|
||
|
|
||
| @dataclass | ||
| class BacktestEngine: | ||
| """Simple wrapper around :func:`simulate_trades`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| portfolio: PortfolioManager | None | ||
| Portfolio manager used for capital allocation when backtesting multiple | ||
| symbols. | ||
| risk_manager: RiskManager | None | ||
| Risk manager coordinating guards during backtests. | ||
| """ | ||
|
|
||
| portfolio: Optional[PortfolioManager] = None | ||
| risk_manager: Optional[RiskManager] = None | ||
|
|
||
| def run( | ||
| self, | ||
| data: pd.DataFrame | Dict[str, pd.DataFrame], | ||
| execution: dict | None = None, | ||
| **kwargs, | ||
| ) -> pd.DataFrame | Dict[str, pd.DataFrame]: | ||
| """Execute a backtest using the underlying :func:`simulate_trades`. | ||
|
|
||
| Any additional keyword arguments are forwarded to | ||
| :func:`simulate_trades`. | ||
| """ | ||
| if self.portfolio is not None and self.risk_manager is not None: | ||
| # ensure portfolio uses provided risk manager | ||
| self.portfolio.risk_manager = self.risk_manager | ||
| return simulate_trades( | ||
| data, | ||
| execution=execution, | ||
| portfolio=self.portfolio, | ||
| drawdown_guard=( | ||
| self.risk_manager.drawdown_guard if self.risk_manager else None | ||
| ), | ||
| **kwargs, | ||
| ) |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.