Create utils/helper.py
Browse files- utils/helper.py +77 -0
utils/helper.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# helper.py
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
from typing import List, Tuple, Dict
|
5 |
+
|
6 |
+
|
7 |
+
def download_stock_data(
|
8 |
+
tickers: List[str], start_date: str, end_date: str, w: int
|
9 |
+
) -> pd.DataFrame:
|
10 |
+
"""
|
11 |
+
Download stock data for given tickers between start_date and end_date.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
tickers (List[str]): List of stock ticker symbols.
|
15 |
+
start_date (str): Start date for data retrieval in 'YYYY-MM-DD' format.
|
16 |
+
end_date (str): End date for data retrieval in 'YYYY-MM-DD' format.
|
17 |
+
w (int): Size of the interval that is used to download data
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
pd.DataFrame: DataFrame with adjusted close prices for the given tickers.
|
21 |
+
"""
|
22 |
+
data = yf.download(tickers, start=start_date, end=end_date, interval=w)
|
23 |
+
return data["Adj Close"]
|
24 |
+
|
25 |
+
|
26 |
+
def create_portfolio_and_calculate_returns(
|
27 |
+
stock_data: pd.DataFrame, top_n: int
|
28 |
+
) -> pd.DataFrame:
|
29 |
+
"""
|
30 |
+
Create a portfolio and calculate returns based on the given window size.
|
31 |
+
|
32 |
+
Args:
|
33 |
+
stock_data (pd.DataFrame): DataFrame containing stock data.
|
34 |
+
window_size (int): Size of the window to calculate returns.
|
35 |
+
|
36 |
+
Returns:
|
37 |
+
pd.DataFrame: DataFrame containing calculated returns and portfolio history.
|
38 |
+
"""
|
39 |
+
# Compute returns
|
40 |
+
returns_data = stock_data.pct_change()
|
41 |
+
returns_data.dropna(inplace=True)
|
42 |
+
|
43 |
+
portfolio_history = [] # To keep track of portfolio changes over time
|
44 |
+
portfolio_returns = [] # To store portfolio returns for each period
|
45 |
+
|
46 |
+
# Loop over the data in window_size-day windows
|
47 |
+
window_size = 1
|
48 |
+
for start in range(0, len(returns_data) - window_size, window_size):
|
49 |
+
end = start + window_size
|
50 |
+
current_window = returns_data[start:end]
|
51 |
+
top_stocks = (
|
52 |
+
current_window.mean()
|
53 |
+
.sort_values(ascending=False)
|
54 |
+
.head(top_n)
|
55 |
+
.index.tolist()
|
56 |
+
)
|
57 |
+
next_window = returns_data[end : end + window_size][top_stocks].mean(axis=1)
|
58 |
+
|
59 |
+
portfolio_returns.extend(next_window)
|
60 |
+
added_length = len(next_window)
|
61 |
+
portfolio_history.extend([top_stocks] * added_length)
|
62 |
+
|
63 |
+
new_returns_data = returns_data.copy()
|
64 |
+
new_returns_data = new_returns_data.iloc[0:-window_size, :]
|
65 |
+
new_returns_data["benchmark"] = new_returns_data.apply(
|
66 |
+
lambda x: x[0:5].mean(), axis=1
|
67 |
+
)
|
68 |
+
new_returns_data["portfolio_returns"] = portfolio_returns
|
69 |
+
new_returns_data["portfolio_history"] = portfolio_history
|
70 |
+
new_returns_data["rolling_benchmark"] = (
|
71 |
+
new_returns_data["benchmark"] + 1
|
72 |
+
).cumprod()
|
73 |
+
new_returns_data["rolling_portfolio_returns"] = (
|
74 |
+
new_returns_data["portfolio_returns"] + 1
|
75 |
+
).cumprod()
|
76 |
+
|
77 |
+
return new_returns_data
|