Skip to main content

No project description provided

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quant_pml-0.1.6.tar.gz (76.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quant_pml-0.1.6-py3-none-any.whl (139.2 kB view details)

Uploaded Python 3

File details

Details for the file quant_pml-0.1.6.tar.gz.

File metadata

  • Download URL: quant_pml-0.1.6.tar.gz
  • Upload date:
  • Size: 76.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for quant_pml-0.1.6.tar.gz
Algorithm Hash digest
SHA256 c56b73cfa8cea96b316c5b30a160aab59a4d2be3131f8e0fc1aa72c06359deec
MD5 9d430674e70e8b9b1896f4061f64b5c1
BLAKE2b-256 19a941c3383e3f5a39b1c318310a0f6ac9f1a5c70c4a42807e04b21cd5f34e80

See more details on using hashes here.

File details

Details for the file quant_pml-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: quant_pml-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 139.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for quant_pml-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7f53a95565a45b0880104e08c05033b4696159f290a94db1e5d310c75d993300
MD5 fd7de965cb62882484eba882b0088d4d
BLAKE2b-256 28a84df4a4d79befbbcd3adbb0a01b4adcfd3c83e847d12f34d9f010dd69b427

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page