-
Notifications
You must be signed in to change notification settings - Fork 2
/
equal_weight.py
27 lines (19 loc) · 868 Bytes
/
equal_weight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import stock_data
import numpy as np
import pandas as pd
from alive_progress import alive_bar
from alive_progress import config_handler
config_handler.set_global(force_tty=True)
def ew_weights(stock_tickers, date):
stock_returns = stock_data.get_daily_returns(stock_tickers, date, date)
n_stocks = stock_returns.shape[1]
x = np.ones(n_stocks) / n_stocks
return x
def portfolio_weights_ew(tickers, start_date, end_date, portfolio_rebalance_period):
business_days_end_months = pd.date_range(start_date, end_date, freq=portfolio_rebalance_period)
portfolio_weights = pd.DataFrame(index=business_days_end_months, columns=tickers)
with alive_bar(len(business_days_end_months)) as bar:
for t in business_days_end_months:
portfolio_weights.loc[t] = ew_weights(tickers, t)
bar()
return portfolio_weights