Fix index error for portfolio historical trades. fixes #2450 (#2451)

Co-authored-by: Jeroen Bouma <jer.bouma@gmail.com>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
This commit is contained in:
Jalil F
2022-08-30 09:38:49 -04:00
committed by GitHub
parent 8940297289
commit 0ad8c9549b

View File

@@ -1165,14 +1165,19 @@ class PortfolioModel:
# Set current price of benchmark
self.benchmark_trades["Last price"] = self.benchmark_historical_prices[-1]
self.benchmark_trades[["Benchmark Quantity", "Trade price"]] = float(0)
# Iterate over orderbook to replicate trades on benchmark
for index, trade in self.__orderbook.iterrows():
# Select date to search (if not in historical prices, get closest value)
if trade["Date"] not in self.benchmark_historical_prices.index:
date = self.benchmark_historical_prices.index.searchsorted(
found_date = self.benchmark_historical_prices.index.searchsorted(
trade["Date"]
)
# Fix error if trade was today and there are no historical price data rows
if self.benchmark_historical_prices.index.isin([found_date]).any():
date = found_date
else:
# If no historical price data, use last row
date = self.benchmark_historical_prices.index[-1]
else:
date = trade["Date"]