Quantitative Portfolio Machine Learning Library
Viacheslav Buchkov
ETH Zürich
Keywords: quant; quantitative finance; portfolio management; machine learning; reinforcement learning.
Install
pip install quant-pml
How To Run A Backtest
from quant_pml.config.trading_config import TradingConfig
from quant_pml.config.russell_3000_experiment_config import Russell3000ExperimentConfig
from quant_pml.hedge.market_futures_hedge import MarketFuturesHedge
from quant_pml.runner import build_backtest
from quant_pml.strategies.factors.momentum import Momentum
# Specify the model hyperparameters
REBAL_FREQ = "D" # Rebalance frequency (Month End)
QUANTILE = 0.1 # Sorted portfolio quantile
MODE = "long_short" # Trading mode
# Specify the trading configurations
trading_config = TradingConfig(
broker_fee=0, # Broker commission in decimals
bid_ask_spread=0, # Bid-ask spread in decimals
total_exposure=1, # Budget constraint
max_exposure=None, # Maximum weight constraint
min_exposure=None, # Minimum weight constraint
trading_lag_days=1, # Trading Lag
)
# Create a strategy with logic for weights selection
strategy = Momentum(
mode=MODE,
quantile=QUANTILE,
)
# Initialize the hedger (can be set to `None` if no hedging is required)
hedger = MarketFuturesHedge(market_name="spx")
# Build the backtest pipeline
preprocessor, runner = build_backtest(
experiment_config=Russell3000ExperimentConfig(),
trading_config=trading_config,
rebal_freq=REBAL_FREQ,
)
# Run the backtest
res = runner(
feature_processor=preprocessor,
strategy=strategy,
hedger=hedger,
)
How To Create A New Strategy
import pandas as pd
from quant_pml.strategies.base_strategy import BaseStrategy
from quant_pml.strategies.optimization_data import TrainingData, PredictionData
# Create a blueprint for your strategy
class NewStrategy(BaseStrategy):
def __init__(self) -> None:
super().__init__()
# Store the data across time, if needed
def _fit(self, training_data: TrainingData) -> None:
# Specify here your fitting logic
pass
def _get_weights(self, prediction_data: PredictionData, weights_: pd.DataFrame) -> pd.DataFrame:
# Specify here your prediction logic
# Update the weights DataFrame by modifying the respective columns of stock_ids
return weights_
How To Create A New Covariance Estimator
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import pandas as pd
from quant_pml.strategies.optimization_data import PredictionData, TrainingData
from quant_pml.cov_estimators.base_cov_estimator import BaseCovEstimator
class NewCovEstimator(BaseCovEstimator):
def __init__(self) -> None:
super().__init__()
self._fitted_cov = None
def _fit(self, training_data: TrainingData) -> None:
# Specify here your fitting logic (e.g., store a historical covmat)
...
def _predict(self, prediction_data: PredictionData) -> pd.DataFrame:
# Specify here your prediction logic (e.g., get a stored estimate)
...
How To Create A New Data-Driven Covariance Estimator
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import pandas as pd
from quant_pml.cov_estimators.rl.base_rl_estimator import BaseRLCovEstimator
class NewRLCovEstimator(BaseRLCovEstimator):
def __init__(self, shrinkage_type: str, window_size: int | None = None) -> None:
super().__init__(shrinkage_type=shrinkage_type, window_size=window_size)
def _fit_shrinkage(
self, features: pd.DataFrame, shrinkage_target: pd.Series
) -> None:
# Specify here your model (might be classical ML / RL / etc.)
self.model = ...
self.model.fit(X=features, y=shrinkage_target)
def _predict_shrinkage(self, features: pd.DataFrame) -> float:
pred = self.model.predict(features).item()
return pred
Metadata
Release files for quant-pml 0.1.20
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| quant_pml-0.1.20.tar.gz | 81.4 kB | Without provenance | Details for quant_pml-0.1.20.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| quant_pml-0.1.20-py3-none-any.whl | Python 3 | none | any | Details for quant_pml-0.1.20-py3-none-any.whl |
Total release size: 233.6 kB
Release files / quant_pml-0.1.20.tar.gz
| Download URL | quant_pml-0.1.20.tar.gz |
|---|---|
| Size | 81.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ce87a0d14f1063869ba4189f52c734ef52f98cf1e875bfe171a2146aa9c272d
|
|
BLAKE2b-256 checksum How to use checksums |
e172fe0dc917624bae8f025eb332e909b9e7eaad1d9f1cae1b426741defa49e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.5
|
Release files / quant_pml-0.1.20-py3-none-any.whl
| Download URL | quant_pml-0.1.20-py3-none-any.whl |
|---|---|
| Size | 152.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0fa508e647b924387e93b2dc51bdc6484c97628e0af97e9f8789ea11b03ba395
|
|
BLAKE2b-256 checksum How to use checksums |
5046e4631f672132408edfd4cd4d298a37a0ab61a44b6168f381c19aa16f989b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.5
|
Security for quant-pml 0.1.20
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.