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:
- Fixing the problem of not having a portfolio between t=0 and first rebalancing date
- Introducing a new risk model: ProfitTaking
- Introducing a new rebalancing: EndOfWeek
- Introducing new investment strategies: MinimumVariance, MaximumReturn, EqualWeight, EqualRisk, MaximumSharpe
- Removing the dependencies between Information and Strategy classes (enabling easier strategy creation for future)
- Creating an easy-to-use user interface to interact with the library
Installation
pip install pybacktestchain_ss
Usage
- Clone this repository (or copy the relevant files to your local environment).
- 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.
- Navigate to the directory containing this streamlit_app.py file.
- Run
streamlit run streamlit_app.py
or access directly via https://pybacktestchainss.streamlit.app/
Step-by-step guide for Streamlit app
- Choose start/end dates for the backtest
- Choose stocks from the SEC universe (typing the ticker or selecting from the dropdown)
- Select initial cash amount
- Specify a risk model (StopLoss or ProfitTaking) and its threshold
- Pick a portfolio optimization strategy (risk-averse, min variance, etc.)
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4827025de3b75840b042d2f0de91726f44352e5b25f48b058120d1b3567a528
|
|
| MD5 |
cc213e4a59554b15ac6770b623d6221d
|
|
| BLAKE2b-256 |
b7d7acc0c14560f449ad7ab7f1b1cab6594ba1d2a235b990ff32770e0f93fdb5
|
File details
Details for the file pybacktestchain_ss-0.10.9-py3-none-any.whl.
File metadata
- Download URL: pybacktestchain_ss-0.10.9-py3-none-any.whl
- Upload date:
- Size: 193.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.8 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e05da7703220919f9e1ccbc5996e4631149b995c450c0d1c4f354625df3c818e
|
|
| MD5 |
c21165f2a765d3486cdb20a85ce3ede2
|
|
| BLAKE2b-256 |
cd4dafb81d1abfb4e5d0661a7cc60c3f2b0acafa9733a86b26e1fb772401f0a9
|