Package created for the Python course project
Project description
package_203_project
A package for doing great things!
Installation
$ pip install package_203_project
Usage
Three classes define different rebalancing strategies:
EndOfMonth: Rebalances the portfolio on the last business day of each month.EndOfWeek: Rebalances the portfolio every Friday (last business day of the week).EndOfDay: Rebalances the portfolio every day.
Two classes define different portfolio construction methods:
-
SharpeRatioMaximization:
This class optimizes portfolio weights to maximize the Sharpe ratio.- Objective: Maximize the Sharpe ratio by calculating optimal weights based on expected returns and the covariance matrix of asset prices.
-
EqualWeightPortfolio:
This class assigns equal weights to all assets in the portfolio, providing a simple and robust strategy.- Objective: Create a portfolio where each asset has equal weight 1/n, where n is the number of assets.
Backtest Class
This is one of the core class of the script, designed to conduct a backtest for a portfolio strategy, modified from the original pybacktestchain script.
Key Attributes
initial_date&final_date: The start and end dates for the backtest.universe: A predefined list of stocks to be included in the portfolio.rebalance_flag: Defines how frequently the portfolio will be rebalanced (daily, weekly, or monthly).risk_model: Implements stop-loss functionality to manage portfolio risk.broker: Handles execution of trades and portfolio transactions.name_blockchain: The blockchain ledger used for storing transaction logs.information: The backtest is run based on a choice on three different portfolio strategies: equal weight pf, max share ratio, or first two moments from pybacktestchain
run_backtest:
- Retrieves historical stock data for the given time period.
- Simulates portfolio rebalancing based on the defined frequency.
- Calculates key portfolio statistics:
- Returns, volatility, skewness, kurtosis.
- Value-at-Risk (VaR) either as the normally distributed returns OR an adjusted methods based on skew/kurtosis.
- Stores results in CSV files for portfolio values and transaction logs.
- Saves cumulative returns and transaction details in a blockchain for secure storage.
- Generates a graph for cumulative returns over time.
- Visualizes portfolio weight allocation over time.
- Uses
plotlyfor creating interactive, stacked-area graphs of weight allocation = better than matlib for weight allocation vizualisation through time - Saves the resulting graph to a file for analysis.
Generated Outputs
- CSV Files:
- Portfolio values: Saved in
backtests_portfolio_values/. - Transaction logs: Saved in
backtests/.
- Portfolio values: Saved in
- Graphs:
- Cumulative returns: Saved in
backtests_portfolio_graphs/. - Portfolio weight allocation: Saved in
plot_portfolio_weight_graphs/.
- Cumulative returns: Saved in
Three classes to handle ndle stock analysis, data management, and benchmark data:
-
StockAnalysis:
Provides key analysis functionalities:- Rank stocks by volume: Ranks stocks based on their average trading volume.
- Calculate betas: Computes the sensitivity (beta) of each stock to a benchmark and calculates the average beta using OLS regression
-
StockDataHandler:- Draws historical stock data.
- Computes daily log returns for each stock and organizes them into a pivot table.
-
BenchmarkHandler:- Draws benchmark historical data.
- Calculates daily log returns for the chosen benchmark like S&P 500, CAC 40 etc.
Interactive User Analysis
The script allows user interaction to analyze stocks:
- Users input stock tickers, date range, and benchmark choice.
- Outputs include:
- Ranked stocks by volume.
- Beta values for each stock and the average beta.
- Visualizations:
- Cumulative Returns: Comparison of stock and benchmark performance.
- Rolling Volatility: Examines stock and benchmark volatility over time.
- Sharpe Ratios: Compares risk-adjusted returns for stocks and the benchmark.
Generated graphs are saved in the stocks_function_graphs/ folder.
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
package_203_project was created by Maelys Malichecq. It is licensed under the terms of the MIT license.
Credits
package_203_project 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 package_203_project-0.3.0.tar.gz.
File metadata
- Download URL: package_203_project-0.3.0.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.12.4 Darwin/22.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99867f6bc4e6f87bffc0453e437435cf0d6b405800b1e7b48b007ba4f8fda992
|
|
| MD5 |
3dc6ee4df8c30196790592044b8a0b12
|
|
| BLAKE2b-256 |
514ac05366c98e04aa3823fdb2b104b7f9f4deaa9b06dcd0f8f7a89ba757e9c7
|
File details
Details for the file package_203_project-0.3.0-py3-none-any.whl.
File metadata
- Download URL: package_203_project-0.3.0-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.12.4 Darwin/22.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
deaca5824eda796cf92ec79371685b810164cc18e21cb7d4137db52140237e76
|
|
| MD5 |
f9dae0fcb5cabeb3167ab2f1b881de0a
|
|
| BLAKE2b-256 |
17949e56710ac977ccd4c72c18751ee3522e73cd101056af4e03e7a9b541d00d
|