Skip to main content

Enchancing the pybacktestchain package. Made by Samuli Salonen as the individual assignment for the M203 Python course.

Project description

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.

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

pybacktestchain_ss-0.10.9.tar.gz (150.4 kB view details)

Uploaded Source

Built Distribution

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

pybacktestchain_ss-0.10.9-py3-none-any.whl (193.0 kB view details)

Uploaded Python 3

File details

Details for the file pybacktestchain_ss-0.10.9.tar.gz.

File metadata

  • Download URL: pybacktestchain_ss-0.10.9.tar.gz
  • Upload date:
  • Size: 150.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.8 Windows/11

File hashes

Hashes for pybacktestchain_ss-0.10.9.tar.gz
Algorithm Hash digest
SHA256 b4827025de3b75840b042d2f0de91726f44352e5b25f48b058120d1b3567a528
MD5 cc213e4a59554b15ac6770b623d6221d
BLAKE2b-256 b7d7acc0c14560f449ad7ab7f1b1cab6594ba1d2a235b990ff32770e0f93fdb5

See more details on using hashes here.

File details

Details for the file pybacktestchain_ss-0.10.9-py3-none-any.whl.

File metadata

File hashes

Hashes for pybacktestchain_ss-0.10.9-py3-none-any.whl
Algorithm Hash digest
SHA256 e05da7703220919f9e1ccbc5996e4631149b995c450c0d1c4f354625df3c818e
MD5 c21165f2a765d3486cdb20a85ce3ede2
BLAKE2b-256 cd4dafb81d1abfb4e5d0661a7cc60c3f2b0acafa9733a86b26e1fb772401f0a9

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