Skip to content

Commit

Permalink
minor bug fix (.iloc[i] instead of .loc[i]) and optimization on portf…
Browse files Browse the repository at this point in the history
…olio (#32)

Using .iloc[i] instead of .loc[i], and calling pf._update only once,
after portfolio was generated. This closes #31

Co-authored-by: Donin <[email protected]>
  • Loading branch information
fmilthaler and donin1129 authored Jul 16, 2023
1 parent 40bef5b commit 2e2eb78
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions finquant/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def add_stock(self, stock: Stock) -> None:
self._add_stock_data(stock)

# update quantities of portfolio
self._update()
# self._update() # the update will be done at the end of building portfolio

def _add_stock_data(self, stock: Stock) -> None:
# insert given data into portfolio stocks dataframe:
Expand Down Expand Up @@ -1023,11 +1023,13 @@ def _build_portfolio_from_df(
pf.market_index = Market(data=market_data)
for i in range(len(pf_allocation)):
# get name of stock
name = pf_allocation.loc[i].Name
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.loc[i], data=stock_data))
pf.add_stock(Stock(pf_allocation.iloc[i], data=stock_data))
# update the portfolio
pf._update()
return pf


Expand Down

0 comments on commit 2e2eb78

Please sign in to comment.