pwb-toolbox 0.1.6__tar.gz → 0.1.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/PKG-INFO +18 -3
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/README.md +15 -2
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/__init__.py +51 -0
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/base_strategy.py +33 -0
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/execution_models/__init__.py +153 -0
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/insight.py +21 -0
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/portfolio_models/__init__.py +290 -0
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/risk_models/__init__.py +175 -0
- pwb_toolbox-0.1.7/pwb_toolbox/backtest/universe_models/__init__.py +183 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pwb_toolbox/datasets/__init__.py +1 -1
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pwb_toolbox.egg-info/PKG-INFO +18 -3
- pwb_toolbox-0.1.7/pwb_toolbox.egg-info/SOURCES.txt +23 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/setup.cfg +4 -2
- pwb_toolbox-0.1.7/tests/test_backtest.py +12 -0
- pwb_toolbox-0.1.7/tests/test_execution_models.py +114 -0
- pwb_toolbox-0.1.7/tests/test_portfolio_models.py +111 -0
- pwb_toolbox-0.1.7/tests/test_risk_models.py +77 -0
- pwb_toolbox-0.1.7/tests/test_universe_models.py +49 -0
- pwb_toolbox-0.1.6/pwb_toolbox.egg-info/SOURCES.txt +0 -11
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/LICENSE.txt +0 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pwb_toolbox/__init__.py +0 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pwb_toolbox.egg-info/dependency_links.txt +0 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pwb_toolbox.egg-info/requires.txt +0 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pwb_toolbox.egg-info/top_level.txt +0 -0
- {pwb_toolbox-0.1.6 → pwb_toolbox-0.1.7}/pyproject.toml +0 -0
@@ -1,15 +1,17 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: pwb-toolbox
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.7
|
4
4
|
Summary: A toolbox library for quant traders
|
5
5
|
Home-page: https://github.com/paperswithbacktest/pwb-toolbox
|
6
6
|
Author: Your Name
|
7
7
|
Author-email: hello@paperswithbacktest.com
|
8
8
|
License: MIT
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
10
12
|
Classifier: License :: OSI Approved :: MIT License
|
11
13
|
Classifier: Operating System :: OS Independent
|
12
|
-
Requires-Python: >=3.
|
14
|
+
Requires-Python: >=3.10
|
13
15
|
Description-Content-Type: text/markdown
|
14
16
|
License-File: LICENSE.txt
|
15
17
|
Requires-Dist: datasets
|
@@ -31,6 +33,7 @@ To install the pwb-toolbox package:
|
|
31
33
|
```bash
|
32
34
|
pip install pwb-toolbox
|
33
35
|
```
|
36
|
+
This package requires Python 3.10 or higher.
|
34
37
|
|
35
38
|
To login to Huggingface Hub with Access Token
|
36
39
|
|
@@ -116,6 +119,19 @@ df = pwb_ds.load_dataset(
|
|
116
119
|
)
|
117
120
|
```
|
118
121
|
|
122
|
+
## Backtest engine
|
123
|
+
|
124
|
+
The `pwb_toolbox.backtest` module offers simple building blocks for running
|
125
|
+
Backtrader simulations. Alpha models generate `Insight` objects which are turned
|
126
|
+
into portfolio weights and executed via Backtrader orders.
|
127
|
+
|
128
|
+
```python
|
129
|
+
from pwb_toolbox.backtest.examples import GoldenCrossAlpha, EqualWeightPortfolio
|
130
|
+
from pwb_toolbox.backtest import run_backtest
|
131
|
+
|
132
|
+
run_backtest(["SPY", "QQQ"], GoldenCrossAlpha(), EqualWeightPortfolio(), start="2015-01-01")
|
133
|
+
```
|
134
|
+
|
119
135
|
## Contributing
|
120
136
|
|
121
137
|
Contributions to the `pwb-toolbox` package are welcome! If you have any improvements, new datasets, or strategy ideas to share, please follow these guidelines:
|
@@ -150,5 +166,4 @@ The `pwb-toolbox` package is released under the MIT license. See the LICENSE fil
|
|
150
166
|
## Contact
|
151
167
|
|
152
168
|
For any questions, issues, or suggestions regarding the `pwb-toolbox` package, please contact the maintainers or create an issue on the repository. We appreciate your feedback and involvement in improving the package.
|
153
|
-
|
154
169
|
Happy trading!
|
@@ -13,6 +13,7 @@ To install the pwb-toolbox package:
|
|
13
13
|
```bash
|
14
14
|
pip install pwb-toolbox
|
15
15
|
```
|
16
|
+
This package requires Python 3.10 or higher.
|
16
17
|
|
17
18
|
To login to Huggingface Hub with Access Token
|
18
19
|
|
@@ -98,6 +99,19 @@ df = pwb_ds.load_dataset(
|
|
98
99
|
)
|
99
100
|
```
|
100
101
|
|
102
|
+
## Backtest engine
|
103
|
+
|
104
|
+
The `pwb_toolbox.backtest` module offers simple building blocks for running
|
105
|
+
Backtrader simulations. Alpha models generate `Insight` objects which are turned
|
106
|
+
into portfolio weights and executed via Backtrader orders.
|
107
|
+
|
108
|
+
```python
|
109
|
+
from pwb_toolbox.backtest.examples import GoldenCrossAlpha, EqualWeightPortfolio
|
110
|
+
from pwb_toolbox.backtest import run_backtest
|
111
|
+
|
112
|
+
run_backtest(["SPY", "QQQ"], GoldenCrossAlpha(), EqualWeightPortfolio(), start="2015-01-01")
|
113
|
+
```
|
114
|
+
|
101
115
|
## Contributing
|
102
116
|
|
103
117
|
Contributions to the `pwb-toolbox` package are welcome! If you have any improvements, new datasets, or strategy ideas to share, please follow these guidelines:
|
@@ -132,5 +146,4 @@ The `pwb-toolbox` package is released under the MIT license. See the LICENSE fil
|
|
132
146
|
## Contact
|
133
147
|
|
134
148
|
For any questions, issues, or suggestions regarding the `pwb-toolbox` package, please contact the maintainers or create an issue on the repository. We appreciate your feedback and involvement in improving the package.
|
135
|
-
|
136
|
-
Happy trading!
|
149
|
+
Happy trading!
|
@@ -0,0 +1,51 @@
|
|
1
|
+
from .base_strategy import BaseStrategy
|
2
|
+
|
3
|
+
from .insight import Direction, Insight
|
4
|
+
|
5
|
+
from .portfolio_models import (
|
6
|
+
PortfolioConstructionModel,
|
7
|
+
EqualWeightingPortfolioConstructionModel,
|
8
|
+
InsightWeightingPortfolioConstructionModel,
|
9
|
+
MeanVarianceOptimizationPortfolioConstructionModel,
|
10
|
+
BlackLittermanOptimizationPortfolioConstructionModel,
|
11
|
+
RiskParityPortfolioConstructionModel,
|
12
|
+
UnconstrainedMeanVariancePortfolioConstructionModel,
|
13
|
+
TargetPercentagePortfolioConstructionModel,
|
14
|
+
DollarCostAveragingPortfolioConstructionModel,
|
15
|
+
InsightRatioPortfolioConstructionModel,
|
16
|
+
)
|
17
|
+
|
18
|
+
__all__ = [
|
19
|
+
"Direction",
|
20
|
+
"Insight",
|
21
|
+
"PortfolioConstructionModel",
|
22
|
+
"EqualWeightingPortfolioConstructionModel",
|
23
|
+
"InsightWeightingPortfolioConstructionModel",
|
24
|
+
"MeanVarianceOptimizationPortfolioConstructionModel",
|
25
|
+
"BlackLittermanOptimizationPortfolioConstructionModel",
|
26
|
+
"RiskParityPortfolioConstructionModel",
|
27
|
+
"UnconstrainedMeanVariancePortfolioConstructionModel",
|
28
|
+
"TargetPercentagePortfolioConstructionModel",
|
29
|
+
"DollarCostAveragingPortfolioConstructionModel",
|
30
|
+
"InsightRatioPortfolioConstructionModel",
|
31
|
+
"RiskManagementModel",
|
32
|
+
"TrailingStopRiskManagementModel",
|
33
|
+
"MaximumDrawdownPercentPerSecurity",
|
34
|
+
"MaximumDrawdownPercentPortfolio",
|
35
|
+
"MaximumUnrealizedProfitPercentPerSecurity",
|
36
|
+
"MaximumTotalPortfolioExposure",
|
37
|
+
"SectorExposureRiskManagementModel",
|
38
|
+
"MaximumOrderQuantityPercentPerSecurity",
|
39
|
+
"CompositeRiskManagementModel",
|
40
|
+
]
|
41
|
+
from .risk_models import (
|
42
|
+
RiskManagementModel,
|
43
|
+
TrailingStopRiskManagementModel,
|
44
|
+
MaximumDrawdownPercentPerSecurity,
|
45
|
+
MaximumDrawdownPercentPortfolio,
|
46
|
+
MaximumUnrealizedProfitPercentPerSecurity,
|
47
|
+
MaximumTotalPortfolioExposure,
|
48
|
+
SectorExposureRiskManagementModel,
|
49
|
+
MaximumOrderQuantityPercentPerSecurity,
|
50
|
+
CompositeRiskManagementModel,
|
51
|
+
)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import backtrader as bt
|
2
|
+
from tqdm import tqdm
|
3
|
+
|
4
|
+
|
5
|
+
class BaseStrategy(bt.Strategy):
|
6
|
+
"""Base strategy providing progress logging utilities."""
|
7
|
+
|
8
|
+
params = (("total_days", 0),)
|
9
|
+
|
10
|
+
def __init__(self):
|
11
|
+
super().__init__()
|
12
|
+
self.pbar = tqdm(total=self.params.total_days)
|
13
|
+
self.log_data = []
|
14
|
+
|
15
|
+
def is_tradable(self, data):
|
16
|
+
"""Return True if the instrument's price is not constant."""
|
17
|
+
if len(data.close) < 3:
|
18
|
+
return False
|
19
|
+
return data.close[0] != data.close[-2]
|
20
|
+
|
21
|
+
def __next__(self):
|
22
|
+
"""Update progress bar and log current value."""
|
23
|
+
self.pbar.update(1)
|
24
|
+
self.log_data.append(
|
25
|
+
{
|
26
|
+
"date": self.datas[0].datetime.date(0).isoformat(),
|
27
|
+
"value": self.broker.getvalue(),
|
28
|
+
}
|
29
|
+
)
|
30
|
+
|
31
|
+
def get_latest_positions(self):
|
32
|
+
"""Get a dictionary of the latest positions."""
|
33
|
+
return {data._name: self.broker.getposition(data).size for data in self.datas}
|
@@ -0,0 +1,153 @@
|
|
1
|
+
"""Execution models for order placement using Backtrader."""
|
2
|
+
|
3
|
+
from __future__ import annotations
|
4
|
+
|
5
|
+
from typing import Dict
|
6
|
+
import backtrader as bt
|
7
|
+
|
8
|
+
|
9
|
+
class ExecutionModel:
|
10
|
+
"""Base execution model class."""
|
11
|
+
|
12
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
13
|
+
"""Place orders on the given strategy."""
|
14
|
+
raise NotImplementedError
|
15
|
+
|
16
|
+
|
17
|
+
class ImmediateExecutionModel(ExecutionModel):
|
18
|
+
"""Immediately send market orders using ``order_target_percent``."""
|
19
|
+
|
20
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
21
|
+
for data in strategy.datas:
|
22
|
+
target = weights.get(data._name, 0.0)
|
23
|
+
strategy.order_target_percent(data=data, target=target)
|
24
|
+
|
25
|
+
|
26
|
+
class StandardDeviationExecutionModel(ExecutionModel):
|
27
|
+
"""Only trade when recent volatility exceeds a threshold."""
|
28
|
+
|
29
|
+
def __init__(self, lookback: int = 20, threshold: float = 0.01):
|
30
|
+
self.lookback = lookback
|
31
|
+
self.threshold = threshold
|
32
|
+
|
33
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
34
|
+
prices = strategy.prices
|
35
|
+
for data in strategy.datas:
|
36
|
+
symbol = data._name
|
37
|
+
series = prices[symbol]["close"] if (symbol, "close") in prices.columns else prices[symbol]
|
38
|
+
if len(series) < self.lookback:
|
39
|
+
continue
|
40
|
+
vol = series.pct_change().rolling(self.lookback).std().iloc[-1]
|
41
|
+
if vol is not None and vol > self.threshold:
|
42
|
+
target = weights.get(symbol, 0.0)
|
43
|
+
strategy.order_target_percent(data=data, target=target)
|
44
|
+
|
45
|
+
|
46
|
+
class VolumeWeightedAveragePriceExecutionModel(ExecutionModel):
|
47
|
+
"""Split orders evenly over a number of steps to approximate VWAP."""
|
48
|
+
|
49
|
+
def __init__(self, steps: int = 3):
|
50
|
+
self.steps = steps
|
51
|
+
self._progress: Dict[str, int] = {}
|
52
|
+
|
53
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
54
|
+
for data in strategy.datas:
|
55
|
+
symbol = data._name
|
56
|
+
step = self._progress.get(symbol, 0)
|
57
|
+
if step >= self.steps:
|
58
|
+
continue
|
59
|
+
target = weights.get(symbol, 0.0) * (step + 1) / self.steps
|
60
|
+
strategy.order_target_percent(data=data, target=target)
|
61
|
+
self._progress[symbol] = step + 1
|
62
|
+
|
63
|
+
|
64
|
+
class VolumePercentageExecutionModel(ExecutionModel):
|
65
|
+
"""Execute only a percentage of the target each call."""
|
66
|
+
|
67
|
+
def __init__(self, percentage: float = 0.25):
|
68
|
+
self.percentage = percentage
|
69
|
+
self._filled: Dict[str, float] = {}
|
70
|
+
|
71
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
72
|
+
for data in strategy.datas:
|
73
|
+
symbol = data._name
|
74
|
+
current = self._filled.get(symbol, 0.0)
|
75
|
+
target = weights.get(symbol, 0.0)
|
76
|
+
remaining = target - current
|
77
|
+
if abs(remaining) < 1e-6:
|
78
|
+
continue
|
79
|
+
step_target = current + remaining * self.percentage
|
80
|
+
self._filled[symbol] = step_target
|
81
|
+
strategy.order_target_percent(data=data, target=step_target)
|
82
|
+
|
83
|
+
|
84
|
+
class TimeProfileExecutionModel(ExecutionModel):
|
85
|
+
"""Execute orders based on a predefined time profile (e.g. TWAP)."""
|
86
|
+
|
87
|
+
def __init__(self, profile: Dict[int, float] | None = None):
|
88
|
+
self.profile = profile or {0: 1.0}
|
89
|
+
self._called = 0
|
90
|
+
|
91
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
92
|
+
factor = self.profile.get(self._called, 0.0)
|
93
|
+
for data in strategy.datas:
|
94
|
+
target = weights.get(data._name, 0.0) * factor
|
95
|
+
strategy.order_target_percent(data=data, target=target)
|
96
|
+
self._called += 1
|
97
|
+
|
98
|
+
|
99
|
+
class TrailingLimitExecutionModel(ExecutionModel):
|
100
|
+
"""Use trailing limit orders for execution."""
|
101
|
+
|
102
|
+
def __init__(self, trail: float = 0.01):
|
103
|
+
self.trail = trail
|
104
|
+
|
105
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
106
|
+
prices = strategy.prices
|
107
|
+
for data in strategy.datas:
|
108
|
+
symbol = data._name
|
109
|
+
price = prices[symbol]["close"].iloc[-1] if (symbol, "close") in prices.columns else prices[symbol].iloc[-1]
|
110
|
+
target = weights.get(symbol, 0.0)
|
111
|
+
if target > 0:
|
112
|
+
strategy.buy(data=data, exectype=bt.Order.Limit, price=price * (1 - self.trail))
|
113
|
+
elif target < 0:
|
114
|
+
strategy.sell(data=data, exectype=bt.Order.Limit, price=price * (1 + self.trail))
|
115
|
+
|
116
|
+
|
117
|
+
class AdaptiveExecutionModel(ExecutionModel):
|
118
|
+
"""Switch between immediate and VWAP execution based on volatility."""
|
119
|
+
|
120
|
+
def __init__(self, threshold: float = 0.02, steps: int = 3):
|
121
|
+
self.threshold = threshold
|
122
|
+
self.vwap = VolumeWeightedAveragePriceExecutionModel(steps=steps)
|
123
|
+
self.immediate = ImmediateExecutionModel()
|
124
|
+
|
125
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
126
|
+
prices = strategy.prices
|
127
|
+
for data in strategy.datas:
|
128
|
+
symbol = data._name
|
129
|
+
series = prices[symbol]["close"] if (symbol, "close") in prices.columns else prices[symbol]
|
130
|
+
if len(series) < 2:
|
131
|
+
continue
|
132
|
+
vol = series.pct_change().iloc[-1]
|
133
|
+
if abs(vol) > self.threshold:
|
134
|
+
self.immediate.execute(strategy, {symbol: weights.get(symbol, 0.0)})
|
135
|
+
else:
|
136
|
+
self.vwap.execute(strategy, {symbol: weights.get(symbol, 0.0)})
|
137
|
+
|
138
|
+
|
139
|
+
class BufferedExecutionModel(ExecutionModel):
|
140
|
+
"""Only execute when target differs sufficiently from last order."""
|
141
|
+
|
142
|
+
def __init__(self, buffer: float = 0.05):
|
143
|
+
self.buffer = buffer
|
144
|
+
self._last: Dict[str, float] = {}
|
145
|
+
|
146
|
+
def execute(self, strategy: bt.Strategy, weights: Dict[str, float]):
|
147
|
+
for data in strategy.datas:
|
148
|
+
symbol = data._name
|
149
|
+
target = weights.get(symbol, 0.0)
|
150
|
+
last = self._last.get(symbol)
|
151
|
+
if last is None or abs(target - last) > self.buffer:
|
152
|
+
strategy.order_target_percent(data=data, target=target)
|
153
|
+
self._last[symbol] = target
|
@@ -0,0 +1,21 @@
|
|
1
|
+
from dataclasses import dataclass
|
2
|
+
from enum import Enum, auto
|
3
|
+
from datetime import datetime
|
4
|
+
|
5
|
+
|
6
|
+
class Direction(Enum):
|
7
|
+
"""Possible directions for an Insight."""
|
8
|
+
|
9
|
+
UP = auto()
|
10
|
+
DOWN = auto()
|
11
|
+
FLAT = auto()
|
12
|
+
|
13
|
+
|
14
|
+
@dataclass
|
15
|
+
class Insight:
|
16
|
+
"""Simple trading signal produced by an Alpha model."""
|
17
|
+
|
18
|
+
symbol: str
|
19
|
+
direction: Direction
|
20
|
+
timestamp: datetime
|
21
|
+
weight: float = 1.0
|
@@ -0,0 +1,290 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
from abc import ABC, abstractmethod
|
4
|
+
from typing import Dict, Iterable
|
5
|
+
import numpy as np
|
6
|
+
import pandas as pd
|
7
|
+
|
8
|
+
from .. import Insight, Direction
|
9
|
+
|
10
|
+
|
11
|
+
class PortfolioConstructionModel(ABC):
|
12
|
+
"""Abstract base class for portfolio construction models."""
|
13
|
+
|
14
|
+
@abstractmethod
|
15
|
+
def weights(
|
16
|
+
self,
|
17
|
+
insights: Iterable[Insight],
|
18
|
+
price_data: pd.DataFrame | None = None,
|
19
|
+
) -> Dict[str, float]:
|
20
|
+
"""Return target weights for each symbol."""
|
21
|
+
pass
|
22
|
+
|
23
|
+
|
24
|
+
class EqualWeightingPortfolioConstructionModel(PortfolioConstructionModel):
|
25
|
+
"""Allocate equal weight to all non-flat insights."""
|
26
|
+
|
27
|
+
def weights(
|
28
|
+
self,
|
29
|
+
insights: Iterable[Insight],
|
30
|
+
price_data: pd.DataFrame | None = None,
|
31
|
+
) -> Dict[str, float]:
|
32
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
33
|
+
if not active:
|
34
|
+
return {}
|
35
|
+
w = 1.0 / len(active)
|
36
|
+
return {i.symbol: (w if i.direction == Direction.UP else -w) for i in active}
|
37
|
+
|
38
|
+
|
39
|
+
class InsightWeightingPortfolioConstructionModel(PortfolioConstructionModel):
|
40
|
+
"""Weight positions according to insight weight attribute."""
|
41
|
+
|
42
|
+
def weights(
|
43
|
+
self,
|
44
|
+
insights: Iterable[Insight],
|
45
|
+
price_data: pd.DataFrame | None = None,
|
46
|
+
) -> Dict[str, float]:
|
47
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
48
|
+
if not active:
|
49
|
+
return {}
|
50
|
+
total = sum(abs(i.weight) for i in active)
|
51
|
+
if total == 0:
|
52
|
+
return {}
|
53
|
+
return {
|
54
|
+
i.symbol: (i.weight / total) * (1 if i.direction == Direction.UP else -1)
|
55
|
+
for i in active
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
class RiskParityPortfolioConstructionModel(PortfolioConstructionModel):
|
60
|
+
"""Simple risk parity based on inverse volatility."""
|
61
|
+
|
62
|
+
def __init__(self, lookback: int = 20):
|
63
|
+
self.lookback = lookback
|
64
|
+
|
65
|
+
def weights(
|
66
|
+
self,
|
67
|
+
insights: Iterable[Insight],
|
68
|
+
price_data: pd.DataFrame | None = None,
|
69
|
+
) -> Dict[str, float]:
|
70
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
71
|
+
if not active or price_data is None:
|
72
|
+
return {}
|
73
|
+
vols = {}
|
74
|
+
for ins in active:
|
75
|
+
prices = (
|
76
|
+
price_data[ins.symbol]["close"]
|
77
|
+
if (ins.symbol, "close") in price_data.columns
|
78
|
+
else price_data[ins.symbol]
|
79
|
+
)
|
80
|
+
if len(prices) < self.lookback:
|
81
|
+
return {}
|
82
|
+
vols[ins.symbol] = prices.pct_change().rolling(self.lookback).std().iloc[-1]
|
83
|
+
inv_vol = {s: 1.0 / v for s, v in vols.items() if v > 0}
|
84
|
+
total = sum(inv_vol.values())
|
85
|
+
if total == 0:
|
86
|
+
return {}
|
87
|
+
return {
|
88
|
+
s: (inv_vol[s] / total)
|
89
|
+
* (
|
90
|
+
1
|
91
|
+
if next(i for i in active if i.symbol == s).direction == Direction.UP
|
92
|
+
else -1
|
93
|
+
)
|
94
|
+
for s in inv_vol
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
class MeanVarianceOptimizationPortfolioConstructionModel(PortfolioConstructionModel):
|
99
|
+
"""Mean-variance optimization with weight normalization."""
|
100
|
+
|
101
|
+
def __init__(self, lookback: int = 60):
|
102
|
+
self.lookback = lookback
|
103
|
+
|
104
|
+
def weights(
|
105
|
+
self,
|
106
|
+
insights: Iterable[Insight],
|
107
|
+
price_data: pd.DataFrame | None = None,
|
108
|
+
) -> Dict[str, float]:
|
109
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
110
|
+
if not active or price_data is None:
|
111
|
+
return {}
|
112
|
+
symbols = [i.symbol for i in active]
|
113
|
+
df = price_data[symbols]
|
114
|
+
if isinstance(df.columns, pd.MultiIndex):
|
115
|
+
df = df.xs("close", axis=1, level=-1)
|
116
|
+
if len(df) < self.lookback:
|
117
|
+
return {}
|
118
|
+
rets = df.pct_change().dropna()
|
119
|
+
mu = rets.mean()
|
120
|
+
cov = rets.cov()
|
121
|
+
inv_cov = np.linalg.pinv(cov.values)
|
122
|
+
exp = np.array(
|
123
|
+
[
|
124
|
+
mu[s]
|
125
|
+
* (
|
126
|
+
1
|
127
|
+
if next(i for i in active if i.symbol == s).direction
|
128
|
+
== Direction.UP
|
129
|
+
else -1
|
130
|
+
)
|
131
|
+
for s in mu.index
|
132
|
+
]
|
133
|
+
)
|
134
|
+
raw = inv_cov.dot(exp)
|
135
|
+
total = np.sum(np.abs(raw))
|
136
|
+
if total == 0:
|
137
|
+
return {}
|
138
|
+
w = raw / total
|
139
|
+
return {s: float(w[i]) for i, s in enumerate(mu.index)}
|
140
|
+
|
141
|
+
|
142
|
+
class UnconstrainedMeanVariancePortfolioConstructionModel(PortfolioConstructionModel):
|
143
|
+
"""Mean-variance optimization without normalization of weights."""
|
144
|
+
|
145
|
+
def __init__(self, lookback: int = 60):
|
146
|
+
self.lookback = lookback
|
147
|
+
|
148
|
+
def weights(
|
149
|
+
self,
|
150
|
+
insights: Iterable[Insight],
|
151
|
+
price_data: pd.DataFrame | None = None,
|
152
|
+
) -> Dict[str, float]:
|
153
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
154
|
+
if not active or price_data is None:
|
155
|
+
return {}
|
156
|
+
symbols = [i.symbol for i in active]
|
157
|
+
df = price_data[symbols]
|
158
|
+
if isinstance(df.columns, pd.MultiIndex):
|
159
|
+
df = df.xs("close", axis=1, level=-1)
|
160
|
+
if len(df) < self.lookback:
|
161
|
+
return {}
|
162
|
+
rets = df.pct_change().dropna()
|
163
|
+
mu = rets.mean()
|
164
|
+
cov = rets.cov()
|
165
|
+
inv_cov = np.linalg.pinv(cov.values)
|
166
|
+
exp = np.array(
|
167
|
+
[
|
168
|
+
mu[s]
|
169
|
+
* (
|
170
|
+
1
|
171
|
+
if next(i for i in active if i.symbol == s).direction
|
172
|
+
== Direction.UP
|
173
|
+
else -1
|
174
|
+
)
|
175
|
+
for s in mu.index
|
176
|
+
]
|
177
|
+
)
|
178
|
+
raw = inv_cov.dot(exp)
|
179
|
+
return {s: float(raw[i]) for i, s in enumerate(mu.index)}
|
180
|
+
|
181
|
+
|
182
|
+
class BlackLittermanOptimizationPortfolioConstructionModel(
|
183
|
+
MeanVarianceOptimizationPortfolioConstructionModel
|
184
|
+
):
|
185
|
+
"""Simplified Black-Litterman model using a blend of market and view returns."""
|
186
|
+
|
187
|
+
def __init__(self, lookback: int = 60, view_weight: float = 0.5):
|
188
|
+
super().__init__(lookback)
|
189
|
+
self.view_weight = view_weight
|
190
|
+
|
191
|
+
def weights(
|
192
|
+
self,
|
193
|
+
insights: Iterable[Insight],
|
194
|
+
price_data: pd.DataFrame | None = None,
|
195
|
+
) -> Dict[str, float]:
|
196
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
197
|
+
if not active or price_data is None:
|
198
|
+
return {}
|
199
|
+
symbols = [i.symbol for i in active]
|
200
|
+
df = price_data[symbols]
|
201
|
+
if isinstance(df.columns, pd.MultiIndex):
|
202
|
+
df = df.xs("close", axis=1, level=-1)
|
203
|
+
if len(df) < self.lookback:
|
204
|
+
return {}
|
205
|
+
rets = df.pct_change().dropna()
|
206
|
+
market_mu = rets.mean()
|
207
|
+
view_mu = pd.Series(
|
208
|
+
{i.symbol: (1 if i.direction == Direction.UP else -1) for i in active}
|
209
|
+
)
|
210
|
+
mu = (1 - self.view_weight) * market_mu + self.view_weight * view_mu
|
211
|
+
cov = rets.cov()
|
212
|
+
inv_cov = np.linalg.pinv(cov.values)
|
213
|
+
exp = mu.loc[market_mu.index].values
|
214
|
+
raw = inv_cov.dot(exp)
|
215
|
+
total = np.sum(np.abs(raw))
|
216
|
+
if total == 0:
|
217
|
+
return {}
|
218
|
+
w = raw / total
|
219
|
+
return {s: float(w[i]) for i, s in enumerate(market_mu.index)}
|
220
|
+
|
221
|
+
|
222
|
+
class TargetPercentagePortfolioConstructionModel(PortfolioConstructionModel):
|
223
|
+
"""Return predefined target portfolio percentages."""
|
224
|
+
|
225
|
+
def __init__(self, targets: Dict[str, float]):
|
226
|
+
self.targets = targets
|
227
|
+
|
228
|
+
def weights(
|
229
|
+
self,
|
230
|
+
insights: Iterable[Insight],
|
231
|
+
price_data: pd.DataFrame | None = None,
|
232
|
+
) -> Dict[str, float]:
|
233
|
+
active_symbols = {
|
234
|
+
i.symbol: i for i in insights if i.direction != Direction.FLAT
|
235
|
+
}
|
236
|
+
return {
|
237
|
+
s: (
|
238
|
+
self.targets.get(s, 0.0)
|
239
|
+
* (1 if active_symbols[s].direction == Direction.UP else -1)
|
240
|
+
)
|
241
|
+
for s in active_symbols
|
242
|
+
if s in self.targets
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
class DollarCostAveragingPortfolioConstructionModel(PortfolioConstructionModel):
|
247
|
+
"""Allocate a fixed percentage to each new insight."""
|
248
|
+
|
249
|
+
def __init__(self, allocation: float = 0.1):
|
250
|
+
self.allocation = allocation
|
251
|
+
|
252
|
+
def weights(
|
253
|
+
self,
|
254
|
+
insights: Iterable[Insight],
|
255
|
+
price_data: pd.DataFrame | None = None,
|
256
|
+
) -> Dict[str, float]:
|
257
|
+
active = [i for i in insights if i.direction != Direction.FLAT]
|
258
|
+
if not active:
|
259
|
+
return {}
|
260
|
+
return {
|
261
|
+
i.symbol: self.allocation * (1 if i.direction == Direction.UP else -1)
|
262
|
+
for i in active
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
class InsightRatioPortfolioConstructionModel(PortfolioConstructionModel):
|
267
|
+
"""Scale long and short exposure by the ratio of insights."""
|
268
|
+
|
269
|
+
def weights(
|
270
|
+
self,
|
271
|
+
insights: Iterable[Insight],
|
272
|
+
price_data: pd.DataFrame | None = None,
|
273
|
+
) -> Dict[str, float]:
|
274
|
+
ups = [i for i in insights if i.direction == Direction.UP]
|
275
|
+
downs = [i for i in insights if i.direction == Direction.DOWN]
|
276
|
+
total = len(ups) + len(downs)
|
277
|
+
if total == 0:
|
278
|
+
return {}
|
279
|
+
up_share = len(ups) / total
|
280
|
+
down_share = len(downs) / total
|
281
|
+
weights = {}
|
282
|
+
if ups:
|
283
|
+
per = up_share / len(ups)
|
284
|
+
for i in ups:
|
285
|
+
weights[i.symbol] = per
|
286
|
+
if downs:
|
287
|
+
per = down_share / len(downs)
|
288
|
+
for i in downs:
|
289
|
+
weights[i.symbol] = -per
|
290
|
+
return weights
|