weights = data.isel(time=0, field=0) weights[:] = 0 def step(data): latest = data.isel(time=-1) is_liquid = latest.sel(field="is_liquid") sroc = latest.sel(field='sroc') k = latest.sel(field='k') d = latest.sel(field='d') need_open = xrf.logical_and(sroc > 0.05, xrf.logical_and(k < 31, d < 31)) need_close = xrf.logical_or(xrf.logical_or(sroc < -0.05, is_liquid == 0), xrf.logical_and(k > 92, d > 92)) global weights weights.loc[need_open] = 1 weights.loc[need_close] = 0 return (weights / weights.sum('asset')).fillna(0) output = test_strategy(data_ext, step=step, init_data_length=200) stat = qnstats.calc_stat(data, output, max_periods=252 * 3) print(stat.to_pandas()) qndata.write_output(output)
assets = load_assets() print(len(assets)) ids = [i['id'] for i in assets] print(ids) data = load_data(assets=ids, min_date='2015-01-01', dims=(ds.TIME, ds.ASSET, ds.FIELD)) # print(data.sel(asset='AMEX:AIRI').dropna('time').to_pandas().to_csv()) output = test_strategy(data, init=init, step=step, init_data_length=init_data_length) write_output(output) # output = output.loc[:'2019-01-05'] # print(len(find_missed_dates(output, data))) pd.set_option('display.max_colwidth', -1) stat1 = calc_stat(data, output) stat2 = calc_stat(data, output[::-1]) print( xr.concat( [stat1.sel(field='sharpe_ratio'), stat2.sel(field='sharpe_ratio')], pd.Index(['f', 'b'], name='d')).to_pandas()) stat1.to_pandas().to_csv("stat.csv")