std.append(var_t**0.5) p = prices['2800'][0:len(mean)].values df = pd.DataFrame({ '2800': p, 'mean': mean, 'std': std }, index=prices.index[0:len(mean)]) df['mal'] = MA(df['2800'], 120) df['mas'] = MA(df['2800'], 10) df['diff'] = (df['mas'] / df['mal'] - 1) / 10 df['slope'] = df['diff'].pct_change() df['mean_std'] = 2 * df['std'] - df['mean'] df['zero'] = 0. df = df.dropna() from pyetf.figure import plot_chart plot_chart(df[['2800', 'mal']], sub=df[['zero', 'mean_std']], sub_fill=df['std']) # df1 = df[df['slope'] > 0] df2 = df[df['slope'] <= 0] x1 = df1['diff'].values y1 = -df1['mean_std'].values b1, t1 = liner_regression(x1, y1) x2 = df2['diff'].values y2 = -df2['mean_std'].values b2, t2 = liner_regression(x2, y2)
# -*- coding: utf-8 -*- """ Created on Sat Jul 27 10:01:54 2019 @author: w """ import ffn from pyetf.data import eod from pyetf.data import tushare from pyetf.figure import plot_chart # retrieve data from eod and combine etf_tickers = [ 'IVV', 'GLD', 'VNQ', 'AGG', 'TLT', 'IEF', 'IEI', 'OIL', 'USO', 'DBC' ] etf_tickers = ['IVV', 'GLD', 'TLT', 'IEF', 'DBC'] prices = ffn.get(tickers=etf_tickers, market='US', provider=eod, start='2000-01-01') stats = prices.calc_stats() stats.display() pl = prices.copy() pl.dropna() pl = pl.rebase() plot_chart(pl)
mc_budget = [0.05, 0.55] # retrieve data from eod and combine start_date_str = '2013-01-01' prices = ffn.get(tickers=etf_tickers, market='US', provider=eod, start=start_date_str) # calc portfolio weights #w1 = prices.to_weights(func_weighting=rpw_standard, risk_weights=mc_budget).dropna() #w2 = prices.to_weights(func_weighting=rpw_garch, risk_weights=mc_budget).dropna() w3 = prices.to_weights(func_weighting=rpw_future, hist_length=-30, risk_weights=mc_budget).dropna() # calc portfolio performance #pf1 = prices.to_NAV(w1) #pf2 = prices.to_NAV(w2) pf3 = prices.to_NAV(w3) #pf1.calc_stats().display() #pf2.calc_stats().display() pf3.calc_stats().display() # plot portfolio pl = pf3.copy() w = w3.copy() pl.dropna() pl = pl.rebase() w.dropna() plot_chart(pl[['NAV', 'spy']], sub_fill=w)
pdvar_lstm[i] = var_lstm[i] pdvar_lstm.columns = prices.columns var_tmp = pdvar_lstm.loc[pdvar_lstm.index.year>=start_year].copy() var_lstm = var_tmp.loc[var_tmp.index<=end_date].copy() # half end year # GARCH from pyetf.algos import forecast_var_from_garch var_garch = [] for t in range(0, len(prices)-m+1): p = prices.iloc[t:t+m].copy() r = p.to_returns().dropna() v = [] for i in range(len(r.columns)): v_tmp, _ = forecast_var_from_garch(100.0*r[r.columns[i]]) v.append(v_tmp) var_garch.append(v) pdvar_garch = pd.DataFrame(data=var_garch, index=prices.index[len(prices)-len(var_garch):len(prices)], columns=prices.columns) var_tmp = pdvar_garch.loc[pdvar_garch.index.year>=start_year].copy() var_garch = var_tmp.loc[var_tmp.index<=end_date].copy() # half end year ''' # plot e = benchmark varl = pd.DataFrame(data=var[e], index=var.index) #varl['lstm']=var_lstm[e]/10000 #varl['garch']=var_garch[e]/10000 varl['risk'] = mv[e] #varl['mv']=varl[e]*(1+mv_t[e]*10000) #varl['mv'].loc[varl['mv']<=0]=0.00001 plot_chart(pl[e], sub_fill=varl[['risk']]) #plot_chart(pl[e], sub=varl)
w = prices.to_weights(func_weighting=rpw_ledoit_wolf, risk_weights=mc_budget, min_assets_number=minn, max_assets_number=maxn).dropna() elif method == 'mean_var': #historical w = prices.to_weights(func_weighting=rpw_mean_var, model="mean_var", risk_weights=mc_budget, min_assets_number=minn, max_assets_number=maxn).dropna() # calc portfolio performance pf = prices.to_NAV(w) # plot portfolio #pl = pf.copy() # all data pl_tmp = pf.loc[pf.index.year >= start_year].copy() pl = pl_tmp.loc[pl_tmp.index <= end_date].copy() # half end year #pl = pl_tmp.loc[pl_tmp.index.year<=end_year].copy() # whole end year pl.dropna() w_tmp = w.loc[w.index.year >= start_year].copy() wl = w_tmp.loc[w_tmp.index <= end_date].copy() # half end year #wl = w_tmp.loc[w_tmp.index.year<=end_year].copy() # whole end year wl.dropna() pl = pl.rebase() plot_chart(pl[['NAV', benchmark]], sub_fill=wl) # performance pl.calc_stats().display() #for y in range(start_year, end_year+1): # pl[str(y)].calc_stats().display()
# hist hts = ts.loc['2013'].values var1 = [] # rolling estimate var while (len(hts) < len(ts)): f_var1 = hts[-hln:].var() var1.append(f_var1) hts = np.append(hts, ts.iloc[len(hts)]) var1 = np.append(np.zeros([len(prices) - len(var1), 1]), var1) # garch method hts = ts.loc['2013'].values var2 = [] # rolling estimate var while (len(hts) < len(ts)): f_var2, r2 = forecast_var_from_garch(hts[-hln:]) var2.append(f_var2) hts = np.append(hts, ts.iloc[len(hts)]) var2 = np.append(np.zeros([len(prices) - len(var2), 1]), var2) #lstm var3 = forecast_var_from_lstm(prices[e]) prices['var1'] = var1 prices['var2'] = var2 prices['var3'] = var3 * 10 from pyetf.figure import plot_chart plot_chart(prices[['spy']], sub=prices[['var1', 'var2', 'var3']])