Skip to main content

pybacktestchain_ss

This repository contains a Streamlit application for PyBacktestChain (backtesting repository developed by https://jfimbett.github.io), providing a user-friendly interface to run backtests on selected stocks with various portfolio strategies. The original code was improved and the UI was designed by Samuli Salonen for the M203 Python course.

This repository enhances the original repository by:

  1. Fixing the problem of not having a portfolio between t=0 and first rebalancing date
  2. Introducing a new risk model: ProfitTaking
  3. Introducing a new rebalancing: EndOfWeek
  4. Introducing new investment strategies: MinimumVariance, MaximumReturn, EqualWeight, EqualRisk, MaximumSharpe
  5. Removing the dependencies between Information and Strategy classes (enabling easier strategy creation for future)
  6. Creating an easy-to-use user interface to interact with the library

Installation

pip install pybacktestchain_ss

Usage

  1. Clone this repository (or copy the relevant files to your local environment).
  2. Install the necessary dependencies (e.g., streamlit, matplotlib, pandas, numpy, sec_cik_mapper, yfinance). Poetry lock file is included in the repository for quick installations.
  3. Navigate to the directory containing this streamlit_app.py file.
  4. Run
streamlit run streamlit_app.py

or access directly via https://pybacktestchainss.streamlit.app/

Step-by-step guide for Streamlit app

  1. Choose start/end dates for the backtest
  2. Choose stocks from the SEC universe (typing the ticker or selecting from the dropdown)
  3. Select initial cash amount
  4. Specify a risk model (StopLoss or ProfitTaking) and its threshold
  5. Pick a portfolio optimization strategy (risk-averse, min variance, etc.)
  6. Run the backtest, then view the final and initial portfolio allocations as pie charts, and a line chart of the portfolio value over time

Step-by-step guide for regular use

from pybacktestchain_ss.data_module import FirstTwoMoments
from pybacktestchain_ss.broker import Backtest, StopLoss, ProfitTaking
from pybacktestchain_ss.portfolio_strategies import RiskAverseStrategy, \
    MaximumReturnStrategy, MinimumVarianceStrategy, MaximumSharpeStrategy, EqualRiskStrategy, EqualWeightStrategy
from datetime import datetime, timedelta

# Set verbosity for logging
verbose = False  # Set to True to enable logging, or False to suppress it
backtest = Backtest(
    initial_date=datetime(2019, 1, 1),
    final_date=datetime(2020, 1, 1),
    universe = ['AAPL', 'MSFT', "WMT", "TSLA", "SNAP"],
    initial_cash = 1000000,
    information_class=FirstTwoMoments,
    risk_model=StopLoss,
    risk_threshold=0.1,
    name_blockchain="backtest",
    portfolio_strategy=EqualRiskStrategy,
    s=timedelta(days=360),
    verbose=verbose
)

df, initial_port, final_port = backtest.run_backtest()
df["Portfolio value"].plot();

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

pybacktestchain_ss was created by Samuli Salonen. It is licensed under the terms of the MIT license.

Credits

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

Metadata

Release files for pybacktestchain_ss 0.10.9

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_ss 0.10.9
File Size Uploaded
pybacktestchain_ss-0.10.9.tar.gz 150.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybacktestchain_ss 0.10.9
File Interpreter ABI Platform
pybacktestchain_ss-0.10.9-py3-none-any.whl Python 3 none any Details

Total release size: 343.4 kB

Release files / pybacktestchain_ss-0.10.9.tar.gz

Download URL pybacktestchain_ss-0.10.9.tar.gz
Size 150.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b4827025de3b75840b042d2f0de91726f44352e5b25f48b058120d1b3567a528
BLAKE2b-256 checksum
How to use checksums
b7d7acc0c14560f449ad7ab7f1b1cab6594ba1d2a235b990ff32770e0f93fdb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.8 Windows/11

Release files / pybacktestchain_ss-0.10.9-py3-none-any.whl

Download URL pybacktestchain_ss-0.10.9-py3-none-any.whl
Size 193.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e05da7703220919f9e1ccbc5996e4631149b995c450c0d1c4f354625df3c818e
BLAKE2b-256 checksum
How to use checksums
cd4dafb81d1abfb4e5d0661a7cc60c3f2b0acafa9733a86b26e1fb772401f0a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.8 Windows/11

Release history Release notifications | RSS feed

This release

0.10.9 This release

2 release files

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