-
Notifications
You must be signed in to change notification settings - Fork 2
/
risk_parity.py
29 lines (22 loc) · 1.09 KB
/
risk_parity.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
27
28
29
import numpy as np
import pandas as pd
import riskparityportfolio as rp
from dateutil.relativedelta import relativedelta
import stock_data
from alive_progress import alive_bar
from alive_progress import config_handler
config_handler.set_global(force_tty=True)
def weights_risk_parity(tickers, start_date, end_date):
prices = stock_data.get_prices(tickers, start_date, end_date)
cov_matrix = stock_data.get_covariance_matrix(prices)
budget = np.full(len(tickers), 1 / len(tickers)) # parity of risk budget
w = rp.vanilla.design(cov_matrix, budget)
return w
def portfolio_weights_risk_parity(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] = weights_risk_parity(tickers, t + relativedelta(months=-12), t)
bar()
return portfolio_weights