Skip to main content

Enhancements and Features Added to pybacktestchain

The pybacktestchain package has been significantly enhanced with new functionality to make it more user-friendly, interactive, and versatile. Below is a detailed explanation of the improvements:

1. Interactive Dashboard

A new Streamlit-powered dashboard has been developed to allow users to interact with the package more intuitively. This dashboard simplifies the configuration of backtests and makes the package accessible even to users without extensive coding experience.

Key Features of the Dashboard:

  • Dynamic Portfolio Selection: Users can now easily select the stocks they want to include in their portfolio from a predefined list (e.g., S&P 500 companies).
  • Flexible Time Periods: Users can specify the start and end dates for their backtests with simple date pickers.
  • Initial Cash Allocation: Users can define the starting amount of cash for their portfolio.
  • Optimization and Risk Model Selection: The dashboard provides dropdown menus and sliders for selecting optimization methods, risk models, and their parameters, ensuring a smooth and configurable experience.

2. Portfolio Optimization Models

Three new portfolio optimization methods have been added to the package, alongside the original FirstTwoMoments model. These methods provide users with a wider range of portfolio construction techniques:

a. FirstTwoMoments (Original)

  • Objective: Optimizes the portfolio based on the mean and variance of asset returns.
  • How it Works:
    • Uses a simplified mean-variance framework to maximize returns for a given level of risk.
    • Suitable for users who want a basic portfolio optimization approach.

b. MaxSharpeRatio (New)

  • Objective: Maximizes the portfolio's Sharpe Ratio by balancing returns against risk.
  • Risk-Free Rate: The user can now specify a dynamic risk-free rate (default: 1% annually), which is factored into the optimization.
  • How it Works:
    • The optimization focuses on finding a portfolio allocation that maximizes the Sharpe Ratio, a measure of risk-adjusted returns.

c. MinimumVariancePortfolio (New)

  • Objective: Constructs a portfolio with the lowest possible variance (i.e., risk).
  • Use Case: Ideal for risk-averse investors who prioritize stability over returns.
  • How it Works:
    • The covariance matrix of asset returns is used to identify the portfolio with the minimum variance.
    • Constraints ensure weights sum to 1 and remain non-negative (long-only).

d. EqualRiskContributionPortfolio (New)

  • Objective: Allocates capital so that each asset contributes equally to the portfolio's overall risk.
  • Use Case: Useful for achieving balanced risk exposure across all assets, avoiding concentration in highly volatile stocks.
  • How it Works:
    • Risk contribution is calculated for each asset based on its volatility and correlation with other assets.
    • Optimization ensures equal contributions while satisfying constraints like weight sums and long-only allocations.

3. Risk Models

In addition to the original risk model, StopLoss, a new risk model, TrailingStop, has been implemented. Both models allow users to dynamically manage risk during backtests, ensuring that portfolios can respond to adverse market movements.

a. StopLoss (Original)

  • Objective: Protects individual positions by automatically selling assets if their price drops below a specified percentage of the entry price.
  • How it Works:
    • Users can specify a threshold (e.g., 10%) representing the maximum acceptable loss.
    • If the asset's current price falls below the threshold (relative to the entry price), the position is liquidated.

b. TrailingStop (New)

  • Objective: Dynamically locks in profits by setting a stop-loss price based on the highest price reached since the position was entered.
  • How it Works:
    • Tracks the highest price for each asset since purchase.
    • The stop-loss price is set as highest_price * (1 - threshold).
    • If the current price drops below the stop-loss price, the position is sold.
  • Use Case: Ideal for trending markets, where users want to secure gains while allowing positions to grow with upward momentum.

c. VolatilityStop (New)

  • Objective: Dynamically manages portfolio risk by monitoring the portfolio's or individual assets' volatility and triggering a stop-loss when the volatility exceeds a specified threshold.
  • How it Works:
    • Tracks rolling volatility over a specified window (e.g., 20 days) for each asset in the portfolio.
    • Computes the rolling standard deviation of percentage returns for each asset based on historical price data.
    • Users specify a threshold (e.g., 0.02 for 2% daily volatility). If the calculated volatility for an asset exceeds the threshold, the position is liquidated.
    • Volatility is dynamically updated during the backtest using pre-loaded historical data (e.g., from a CSV file).
  • Use Case:
    • Ideal for risk-averse strategies in volatile markets.
    • Prevents excessive losses by reducing exposure to high-volatility assets.
    • Maintains portfolio stability by automatically exiting positions that exceed the acceptable risk level.

4. Why These Changes Matter

The enhancements address several limitations of the original package and open it up to a broader audience:

  1. Ease of Use: The interactive dashboard makes the package accessible to both technical and non-technical users.
  2. Portfolio Customization:
    • Users can now choose from multiple optimization methods tailored to their goals, whether it’s maximizing returns, minimizing risk, or balancing risk contributions.
  3. Dynamic Risk Management:
    • The addition of TrailingStop offers a modern and adaptive approach to risk control, complementing the static StopLoss model.
  4. Flexibility:
    • Parameters like the risk-free rate, threshold levels, and optimization methods can now be easily configured, allowing for a highly customizable backtesting process.

How to Use the New Features

a. Configuring the Dashboard

  1. Launch the dashboard once you set up the path to the package/src/project:
    streamlit run dashboard.py
    
  2. Use the sidebar to:
    • Select stocks for your portfolio.
    • Set backtesting parameters such as dates and initial cash.
    • Choose an optimization method and its parameters.
    • Select a risk model and configure its threshold.

b. Running a Backtest with the CLI

The new features are also accessible via the command-line interface:

python run_backtest.py '{"initial_date": "2019-01-01", "final_date": "2020-01-01", "universe": ["AAPL", "MSFT"], "optimization_method": "MinimumVariancePortfolio", "risk_model": "StopLoss", "threshold": 0.05}'

project

Python Project, M2 203

Installation

$ pip install pybacktestchain-extensions

Usage

  • TODO

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

project was created by Maxime Lorenzo. It is licensed under the terms of the MIT license.

Credits

project was created with cookiecutter and the py-pkgs-cookiecutter template.

Metadata

Release files for pybacktestchain-extensions 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybacktestchain-extensions 1.1.1
File Size Uploaded Provenance Actions
pybacktestchain_extensions-1.1.1.tar.gz 20.4 kB Without provenance Details for pybacktestchain_extensions-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pybacktestchain-extensions 1.1.1
File Interpreter ABI Platform Actions
pybacktestchain_extensions-1.1.1-py3-none-any.whl Python 3 none any Details for pybacktestchain_extensions-1.1.1-py3-none-any.whl

Total release size: 41.3 kB

Release files / pybacktestchain_extensions-1.1.1.tar.gz

Download URL pybacktestchain_extensions-1.1.1.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
776f372b44514e3ee9dd4705545202682b4758909665507022afa50074479796
BLAKE2b-256 checksum
How to use checksums
8155199261f614a6713dc89aad06cbd35830d0b76dddcf1af2ca3cdc33469b83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.0 CPython/3.11.11 Darwin/24.1.0

Release files / pybacktestchain_extensions-1.1.1-py3-none-any.whl

Download URL pybacktestchain_extensions-1.1.1-py3-none-any.whl
Size 20.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6258a52c6bd498a80640302b14baff4c47839f38f99e1316625ce4b844424bda
BLAKE2b-256 checksum
How to use checksums
bc4c660446e3bb0ace5ad9d81daf2506908119e7ed78b6633a6e489adec13209
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.0 CPython/3.11.11 Darwin/24.1.0

Security for pybacktestchain-extensions 1.1.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page