Skip to content

Commit eb980c1

Browse files
fmilthalernuviousgithub-actions[bot]
authored
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] <github-actions[bot]@users.noreply.github.com>
1 parent e9ec4c6 commit eb980c1

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Thank you to all the individuals who have contributed to this project!
1717
- @drcsturm: bug fix for single stock portfolio
1818
- @donin1129: bug fix for pandas index reference
1919
- @aft90: helped to implement the Sortino Ratio
20+
- 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).
2021

2122
## Special Thanks
2223

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<img src="https://img.shields.io/github/stars/fmilthaler/FinQuant.svg?style=social&label=Star" alt='pypi'>
88
</a>
99
<a href="https://pypi.org/project/FinQuant">
10-
<img src="https://img.shields.io/badge/pypi-v0.5.0-brightgreen.svg?style=popout" alt='pypi'>
10+
<img src="https://img.shields.io/badge/pypi-v0.6.0-brightgreen.svg?style=popout" alt='pypi'>
1111
</a>
1212
<a href="https://github.com/fmilthaler/FinQuant">
1313
<img src="https://github.com/fmilthaler/finquant/actions/workflows/pytest.yml/badge.svg?branch=master" alt='GitHub Actions'>

README.tex.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<img src="https://img.shields.io/github/stars/fmilthaler/FinQuant.svg?style=social&label=Star" alt='pypi'>
88
</a>
99
<a href="https://pypi.org/project/FinQuant">
10-
<img src="https://img.shields.io/badge/pypi-v0.5.0-brightgreen.svg?style=popout" alt='pypi'>
10+
<img src="https://img.shields.io/badge/pypi-v0.6.0-brightgreen.svg?style=popout" alt='pypi'>
1111
</a>
1212
<a href="https://github.com/fmilthaler/FinQuant">
1313
<img src="https://github.com/fmilthaler/finquant/actions/workflows/pytest.yml/badge.svg?branch=master" alt='GitHub Actions'>

finquant/portfolio.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,29 @@ def var_confidence_level(self, val):
182182
# now that this changed, update VaR
183183
self._update()
184184

185-
def add_stock(self, stock: Stock) -> None:
185+
def add_stock(self, stock: Stock, defer_update=False) -> None:
186186
"""Adds a stock of type ``Stock`` to the portfolio. Each time ``add_stock``
187187
is called, the following instance variables are updated:
188188
189189
- ``portfolio``: ``pandas.DataFrame``, adds a column with information from ``stock``
190190
- ``stocks``: ``dictionary``, adds an entry for ``stock``
191191
- ``data``: ``pandas.DataFrame``, adds a column of stock prices from ``stock``
192192
193-
Also, the following instance variables are (re-)computed:
193+
Also, if argument ``defer_update`` is ``True``,
194+
the following instance variables are (re-)computed:
194195
195196
- ``expected_return``: Expected Return of the portfolio
196197
- ``volatility``: Volatility of the portfolio
198+
- ``downside_risk``: Downside Risk
199+
- ``var``: Value at Risk of the portfolio
197200
- ``sharpe``: Sharpe Ratio of the portfolio
201+
- ``sortino``: Sortino Ratio of the portfolio
198202
- ``skew``: Skewness of the portfolio's stocks
199203
- ``kurtosis``: Kurtosis of the portfolio's stocks
200204
201205
:Input:
202206
:stock: an object of ``Stock``
207+
:defer_update: bool, if True _update() is not called after the stock is added.
203208
"""
204209
# adding stock to dictionary containing all stocks provided
205210
self.stocks.update({stock.name: stock})
@@ -212,8 +217,9 @@ def add_stock(self, stock: Stock) -> None:
212217
# also add stock data of stock to the dataframe
213218
self._add_stock_data(stock)
214219

215-
# update quantities of portfolio
216-
# self._update() # the update will be done at the end of building portfolio
220+
if not defer_update:
221+
# update quantities of portfolio
222+
self._update()
217223

218224
def _add_stock_data(self, stock: Stock) -> None:
219225
# insert given data into portfolio stocks dataframe:
@@ -1109,8 +1115,9 @@ def _build_portfolio_from_df(
11091115
name = pf_allocation.iloc[i].Name
11101116
# extract data column of said stock
11111117
stock_data = data.loc[:, [name]].copy(deep=True).squeeze()
1112-
# create Stock instance and add it to portfolio
1113-
pf.add_stock(Stock(pf_allocation.iloc[i], data=stock_data))
1118+
# create Stock instance and add it to portfolio,
1119+
# and defer updating portfolio attributes until all stocks are added
1120+
pf.add_stock(Stock(pf_allocation.iloc[i], data=stock_data), defer_update=True)
11141121
# update the portfolio
11151122
pf._update()
11161123
return pf

version

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=0.5.0
2-
release=0.5.0
1+
version=0.6.0
2+
release=0.6.0

0 commit comments

Comments
 (0)