From eb980c1ce4b144e4d5de9288829476b7e3b229e7 Mon Sep 17 00:00:00 2001 From: Frank Milthaler Date: Thu, 3 Aug 2023 15:42:15 +0200 Subject: [PATCH] Added defer_update flag to add_stock function (#125) Added defer_update flag to add_stock function so bulk adding of stocks can have update deferred until after all are added. This closes #57. --------- Co-authored-by: David Cheeseman <5287736+nuvious@users.noreply.github.com> Co-authored-by: github-actions[bot] --- CONTRIBUTORS.md | 1 + README.md | 2 +- README.tex.md | 2 +- finquant/portfolio.py | 19 +++++++++++++------ version | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a4b95722..39bb097a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -17,6 +17,7 @@ Thank you to all the individuals who have contributed to this project! - @drcsturm: bug fix for single stock portfolio - @donin1129: bug fix for pandas index reference - @aft90: helped to implement the Sortino Ratio +- David Cheeseman (@nuvious): added `defer_update` flag to `add_stock` function so bulk adding of stocks can have update deferred until after all are added (improved performance). ## Special Thanks diff --git a/README.md b/README.md index 85971e8e..95af6aa1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ pypi - pypi + pypi GitHub Actions diff --git a/README.tex.md b/README.tex.md index 357e4593..afb34bc8 100644 --- a/README.tex.md +++ b/README.tex.md @@ -7,7 +7,7 @@ pypi - pypi + pypi GitHub Actions diff --git a/finquant/portfolio.py b/finquant/portfolio.py index e24b74b8..67037bae 100644 --- a/finquant/portfolio.py +++ b/finquant/portfolio.py @@ -182,7 +182,7 @@ def var_confidence_level(self, val): # now that this changed, update VaR self._update() - def add_stock(self, stock: Stock) -> None: + def add_stock(self, stock: Stock, defer_update=False) -> None: """Adds a stock of type ``Stock`` to the portfolio. Each time ``add_stock`` is called, the following instance variables are updated: @@ -190,16 +190,21 @@ def add_stock(self, stock: Stock) -> None: - ``stocks``: ``dictionary``, adds an entry for ``stock`` - ``data``: ``pandas.DataFrame``, adds a column of stock prices from ``stock`` - Also, the following instance variables are (re-)computed: + Also, if argument ``defer_update`` is ``True``, + the following instance variables are (re-)computed: - ``expected_return``: Expected Return of the portfolio - ``volatility``: Volatility of the portfolio + - ``downside_risk``: Downside Risk + - ``var``: Value at Risk of the portfolio - ``sharpe``: Sharpe Ratio of the portfolio + - ``sortino``: Sortino Ratio of the portfolio - ``skew``: Skewness of the portfolio's stocks - ``kurtosis``: Kurtosis of the portfolio's stocks :Input: :stock: an object of ``Stock`` + :defer_update: bool, if True _update() is not called after the stock is added. """ # adding stock to dictionary containing all stocks provided self.stocks.update({stock.name: stock}) @@ -212,8 +217,9 @@ def add_stock(self, stock: Stock) -> None: # also add stock data of stock to the dataframe self._add_stock_data(stock) - # update quantities of portfolio - # self._update() # the update will be done at the end of building portfolio + if not defer_update: + # update quantities of portfolio + self._update() def _add_stock_data(self, stock: Stock) -> None: # insert given data into portfolio stocks dataframe: @@ -1109,8 +1115,9 @@ def _build_portfolio_from_df( name = pf_allocation.iloc[i].Name # extract data column of said stock stock_data = data.loc[:, [name]].copy(deep=True).squeeze() - # create Stock instance and add it to portfolio - pf.add_stock(Stock(pf_allocation.iloc[i], data=stock_data)) + # create Stock instance and add it to portfolio, + # and defer updating portfolio attributes until all stocks are added + pf.add_stock(Stock(pf_allocation.iloc[i], data=stock_data), defer_update=True) # update the portfolio pf._update() return pf diff --git a/version b/version index 9305189e..5e1e2d80 100644 --- a/version +++ b/version @@ -1,2 +1,2 @@ -version=0.5.0 -release=0.5.0 +version=0.6.0 +release=0.6.0