Make the get_equal_weights handle portfolio's with stock's filtered out, due to the stocks having to many NaNs.

Co-authored-by: James Maslek <jmaslek11@gmail.com>
This commit is contained in:
northern-64bit
2023-01-17 03:48:12 +01:00
committed by GitHub
parent aaa4467160
commit 162c72715f
2 changed files with 14 additions and 1 deletions

View File

@@ -225,7 +225,10 @@ def get_equal_weights(
method=method,
)
weights = {stock: value * round(1 / len(symbols), 5) for stock in symbols}
weights = {
stock: value * round(1 / len(stock_returns.columns), 5)
for stock in stock_returns.columns
}
return weights, stock_returns

View File

@@ -334,6 +334,16 @@ def process_returns(
# Select stocks with low number of nans
selected_stocks = np.isnan(stock_returns).sum(axis=0)
selected_stocks = np.where(selected_stocks <= maxnan * stock_returns.shape[0])[0]
filtered_out = [
s
for s in stock_returns.columns
if s not in stock_returns.iloc[:, selected_stocks]
]
if filtered_out:
print(
"The following stocks were filtered out, due to too many NaNs: "
+ ", ".join(filtered_out)
)
stock_returns = stock_returns.iloc[:, selected_stocks]
# Replace values above and below threshold