|
| 1 | +from systems.provided.futures_chapter15.basesystem import * |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +from matplotlib.pyplot import show, plot, scatter, gca |
| 5 | +from syscore.pdutils import align_to_joint, uniquets, divide_df_single_column |
| 6 | +from syscore.dateutils import generate_fitting_dates |
| 7 | +from syscore.algos import robust_vol_calc |
| 8 | + |
| 9 | +from systems.portfolio import Portfolios |
| 10 | +config=Config("systems.provided.futures_chapter15.futuresconfig.yaml") |
| 11 | + |
| 12 | +rulename=["ewmac64_256"] |
| 13 | +rule_name=rulename[0] |
| 14 | + |
| 15 | +del(config.instrument_weights) ## so we use all the markets we have, equal weighted |
| 16 | +config.notional_trading_capital=10000000 |
| 17 | +config.forecast_weights=dict([(rule,1.0) for rule in rulename]) |
| 18 | +config.use_instrument_weight_estimates=True |
| 19 | +config.notional_trading_capital=10000000 |
| 20 | + |
| 21 | +system = System([Account(), Portfolios(), PositionSizing(), FuturesRawData(), ForecastCombine(), |
| 22 | + ForecastScaleCap(), Rules()], csvFuturesData(), |
| 23 | + config) |
| 24 | +system.set_logging_level("on") |
| 25 | + |
| 26 | +a2=system.accounts.portfolio() |
| 27 | + |
| 28 | +## autocorrelation |
| 29 | +pd.concat([a2.weekly.as_df(), a2.weekly.as_df().shift(1)], axis=1).corr() |
| 30 | +pd.concat([a2.monthly.as_df(), a2.monthly.as_df().shift(1)], axis=1).corr() |
| 31 | +pd.concat([a2.annual.as_df(), a2.annual.as_df().shift(1)], axis=1).corr() |
| 32 | + |
| 33 | +## recent volatility (market by market...) |
| 34 | +instrument_code="EDOLLAR" |
| 35 | +return_period=int((250/7.5)) |
| 36 | +days=256 |
| 37 | + |
| 38 | +def get_scatter_data_for_code_vol(system, instrument_code, rule_name, return_period=5, days=64): |
| 39 | + |
| 40 | + denom_price = system.rawdata.daily_denominator_price(instrument_code) |
| 41 | + x=system.rawdata.daily_returns(instrument_code) |
| 42 | + vol=robust_vol_calc(x, days) |
| 43 | + perc_vol = 100.0 * divide_df_single_column(vol, denom_price.shift(1)) |
| 44 | + |
| 45 | + volavg = pd.rolling_median(perc_vol, 1250, min_periods=10) |
| 46 | + vol_qq = (perc_vol - volavg)/volavg |
| 47 | + |
| 48 | + ## work out return for the N days after the forecast |
| 49 | + |
| 50 | + norm_data=system.accounts.pandl_for_instrument_forecast(instrument_code, rule_name) |
| 51 | + |
| 52 | + (vol_qq, norm_data) = align_to_joint(vol_qq, norm_data, ffill=(True, False)) |
| 53 | + |
| 54 | + period_returns = pd.rolling_sum(norm_data, return_period, min_periods=1) |
| 55 | + |
| 56 | + ex_post_returns = period_returns.shift(-return_period) |
| 57 | + lagged_vol = vol_qq.shift(1) |
| 58 | + |
| 59 | + return (list(ex_post_returns.iloc[:,0].values),list(lagged_vol.iloc[:,0].values)) |
| 60 | + |
| 61 | + |
| 62 | +def clean_data(x, y, maxstd=6.0): |
| 63 | + |
| 64 | + xcap=np.nanstd(x)*maxstd |
| 65 | + ycap=np.nanstd(y)*maxstd |
| 66 | + |
| 67 | + def _cap(xitem, cap): |
| 68 | + if np.isnan(xitem): |
| 69 | + return xitem |
| 70 | + if xitem>cap: |
| 71 | + return cap |
| 72 | + if xitem<-cap: |
| 73 | + return -cap |
| 74 | + return xitem |
| 75 | + |
| 76 | + x=[_cap(xitem, xcap) for xitem in x] |
| 77 | + y=[_cap(yitem, ycap) for yitem in y] |
| 78 | + |
| 79 | + return (x,y) |
| 80 | + |
| 81 | + |
| 82 | +def bin_fit(x, y, buckets=3): |
| 83 | + |
| 84 | + assert buckets in [3,25] |
| 85 | + |
| 86 | + xstd=np.nanstd(x) |
| 87 | + |
| 88 | + if buckets==3: |
| 89 | + binlimits=[np.nanmin(x), -xstd/2.0,xstd/2.0 , np.nanmax(x)] |
| 90 | + elif buckets==25: |
| 91 | + |
| 92 | + steps=xstd/4.0 |
| 93 | + binlimits=np.arange(-xstd*3.0, xstd*3.0, steps) |
| 94 | + |
| 95 | + binlimits=[np.nanmin(x)]+list(binlimits)+[np.nanmax(x)] |
| 96 | + |
| 97 | + fit_y=[] |
| 98 | + err_y=[] |
| 99 | + x_values_to_plot=[] |
| 100 | + for binidx in range(len(binlimits))[1:]: |
| 101 | + lower_bin_x=binlimits[binidx-1] |
| 102 | + upper_bin_x=binlimits[binidx] |
| 103 | + |
| 104 | + x_values_to_plot.append(np.mean([lower_bin_x, upper_bin_x])) |
| 105 | + |
| 106 | + y_in_bin=[y[idx] for idx in range(len(y)) if x[idx]>=lower_bin_x and x[idx]<upper_bin_x] |
| 107 | + |
| 108 | + fit_y.append(np.nanmedian(y_in_bin)) |
| 109 | + err_y.append(np.nanstd(y_in_bin)) |
| 110 | + |
| 111 | + ## no zeros |
| 112 | + |
| 113 | + |
| 114 | + return (binlimits, x_values_to_plot, fit_y, err_y) |
| 115 | + |
| 116 | +instrument_list=system.get_instrument_list() |
| 117 | + |
| 118 | +all_scatter=dict(returns=[], vol=[]) |
| 119 | + |
| 120 | + |
| 121 | +for instrument_code in instrument_list: |
| 122 | + this_instrument_data=get_scatter_data_for_code_vol(system, instrument_code, rule_name, return_period, days) |
| 123 | + all_scatter['returns']=all_scatter['returns']+this_instrument_data[0] |
| 124 | + all_scatter['vol']=all_scatter['vol']+this_instrument_data[1] |
| 125 | + |
| 126 | +(returns, forecast)=clean_data(all_scatter['returns'], all_scatter['vol']) |
| 127 | + |
| 128 | +(binlimits, x_values_to_plot, fit_y, err_y)=bin_fit(forecast, returns) |
| 129 | + |
| 130 | + |
| 131 | +## this time we multiply the forecast by the fitted Value |
0 commit comments