-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overhaul Portfolio class #2021
Overhaul Portfolio class #2021
Conversation
Hey @montezdesousa, let me know when I should review :) |
Could you also look into the calculations of A portfolio with the benchmark (buying at the same moments as the portfolio) did not yield you a 300% return. This is visible when you use
What those commands do wrong is the assumption you only bought in 2010 and no other dates. If you bought everything at 2010, you probably had a 300% return by now. This is, however, not the case as we buy periodically. This is correctly reflected for the portfolio. |
There were some merge conflicts since I merged #2029. I believe I did this right 🙏 |
Description
The purpose of this PR is to refactor the Portfolio class that lives in portfolio_model.py and serves as base for the outputs of portfolio menu. The goal is to give it a more pythonic structure and if possible pull some logic from portfolio_view.py and portfolio_controller.py.
Following guidelines provided by @JerBouma the new Portfolio class should implement the following tasks:
Summary of changes:
Rename Portfolio class to PortfolioModel
1.1. now we have a single method to load prices instead of get_yahoo_close_prices() for stocks/etf and get_crypto_yfinance() for crypto
1.2. tickers are kept in a dictionary by asset class instead of lists
2.1. substitutes the 2 methods that previously pulled prices from yfinance
Return(t) = Price(t)*Quantity(t) / [Price(t-1)*Quantity(t-1) + Investment changes]
Return(t) = End Value(t) / Initial Value(t)
-> calculate_value() [Only supports buy orders]
Determine reserves
Determine allocations: this task was handled by calculate_allocations(), no changes here
Determine key metrics and ratios: moved some metrics from helpers and view to model
Other files
portfolio_view.py
make use of tickers list from Portfolio class to get all_holdings
display_cumulative()_returns uses portfolio_model for cumulative_returns instead of computing them
display_rolling_volatility(), display_rolling_sharpe(), display_rolling_sortino() use portfolio_model for rolling metrics
portfolio_controller.py
solve minor bug, portfolio help menu was being prompted twice
small fixes to adapt to new PortfolioModel class attributes (e.g. you can't create an empty portfolio in this new version)
call_show calls display_orderbook from portfolio_view.py: logic push to portfolio_view.py to handle rich table
portfolio_helpers.py
move get_sharpe_ratio(), get_sortino_ratio(), get_maximum_drawdown_ratio() to portfolio.model.py
How has this been tested?
Checklist:
Others
pre-commit install
.pytest tests/...
.