forked from noterminusgit/statarb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrb.py
156 lines (127 loc) · 6.24 KB
/
rrb.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
from regress import *
from loaddata import *
from util import *
from calc import *
def calc_rrb_daily(daily_df, horizon):
print "Caculating daily rrb..."
result_df = filter_expandable(daily_df)
print "Calculating rrb0..."
result_df['rrb0'] = result_df['barraResidRet']
print result_df['rrb0'].head()
result_df['rrb0_B'] = winsorize_by_date(result_df['rrb0'])
demean = lambda x: (x - x.mean())
dategroups = result_df[['rrb0_B', 'gdate']].groupby(['gdate'], sort=False).transform(demean)
result_df['rrb0_B_ma'] = dategroups['rrb0_B']
print "Calculated {} values".format(len(result_df))
print "Calulating lags..."
for lag in range(1,horizon):
shift_df = result_df.unstack().shift(lag).stack()
result_df['rrb'+str(lag)+'_B_ma'] = shift_df['rrb0_B_ma']
return result_df
def calc_rrb_intra(intra_df):
print "Calculating rrb intra..."
result_df = filter_expandable(intra_df)
print "Calulating rrbC..."
result_df['rrbC'] = result_df['barraResidRetI']
result_df['rrbC_B'] = winsorize_by_ts(result_df['rrbC'])
print result_df['rrbC'].tail()
print "Calulating rrbC_ma..."
demean = lambda x: (x - x.mean())
dategroups = result_df[['rrbC_B', 'giclose_ts']].groupby(['giclose_ts'], sort=False).transform(demean)
result_df['rrbC_B_ma'] = dategroups['rrbC_B']
return result_df
def rrb_fits(daily_df, intra_df, horizon, name, middate):
insample_intra_df = intra_df
insample_daily_df = daily_df
outsample_intra_df = intra_df
if middate is not None:
insample_intra_df = intra_df[ intra_df['date'] < middate ]
insample_daily_df = daily_df[ daily_df.index.get_level_values('date') < middate ]
outsample_intra_df = intra_df[ intra_df['date'] >= middate ]
outsample_intra_df['rrb'] = np.nan
outsample_intra_df[ 'rrbC_B_ma_coef' ] = np.nan
for lag in range(1, horizon+1):
outsample_intra_df[ 'rrb' + str(lag) + '_B_ma_coef' ] = np.nan
fits_df = pd.DataFrame(columns=['horizon', 'coef', 'indep', 'tstat', 'nobs', 'stderr'])
for lag in range(1,horizon+1):
print insample_daily_df.head()
fitresults_df = regress_alpha(insample_daily_df, 'rrb0_B_ma', lag, True, 'daily')
fits_df = fits_df.append(fitresults_df, ignore_index=True)
plot_fit(fits_df, "rrb_daily_"+name+"_" + df_dates(insample_daily_df))
fits_df.set_index(keys=['indep', 'horizon'], inplace=True)
coef0 = fits_df.ix['rrb0_B_ma'].ix[horizon].ix['coef']
outsample_intra_df[ 'rrbC_B_ma_coef' ] = coef0
print "Coef0: {}".format(coef0)
for lag in range(1,horizon):
coef = coef0 - fits_df.ix['rrb0_B_ma'].ix[lag].ix['coef']
print "Coef{}: {}".format(lag, coef)
outsample_intra_df[ 'rrb'+str(lag)+'_B_ma_coef' ] = coef
outsample_intra_df['rrb'] = outsample_intra_df['rrbC_B_ma'] * outsample_intra_df['rrbC_B_ma_coef']
# outsample_intra_df['rrb_b'] = 0
for lag in range(1,horizon):
outsample_intra_df[ 'rrb'] += outsample_intra_df['rrb'+str(lag)+'_B_ma'] * outsample_intra_df['rrb'+str(lag)+'_B_ma_coef']
return outsample_intra_df
def calc_rrb_forecast(daily_df, intra_df, horizon, middate):
daily_results_df = daily_df
intra_results_df = intra_df
sector_name = 'Energy'
# print "Running rrb for sector {}".format(sector_name)
# sector_df = daily_results_df[ daily_results_df['sector_name'] == sector_name ]
# sector_intra_results_df = intra_results_df[ intra_results_df['sector_name'] == sector_name ]
# result1_df = rrb_fits(sector_df, sector_intra_results_df, horizon, "in", middate)
print "Running rrb for sector {}".format(sector_name)
sector_df = daily_results_df[ daily_results_df['sector_name'] != sector_name ]
sector_intra_results_df = intra_results_df[ intra_results_df['sector_name'] != sector_name ]
result2_df = rrb_fits(sector_df, sector_intra_results_df, horizon, "ex", middate)
result_df = pd.concat([result2_df], verify_integrity=True)
return result_df
if __name__=="__main__":
parser = argparse.ArgumentParser(description='G')
parser.add_argument("--start",action="store",dest="start",default=None)
parser.add_argument("--end",action="store",dest="end",default=None)
parser.add_argument("--mid",action="store",dest="mid",default=None)
parser.add_argument("--horizon",action="store",dest="horizon",default=3)
parser.add_argument("--freq",action="store",dest="freq",default='15Min')
args = parser.parse_args()
start = args.start
end = args.end
lookback = 30
horizon = int(args.horizon)
pname = "./rrb" + start + "." + end
freq = args.freq
start = dateparser.parse(start)
end = dateparser.parse(end)
middate = dateparser.parse(args.mid)
loaded = False
try:
daily_df = pd.read_hdf(pname+"_daily.h5", 'table')
intra_df = pd.read_hdf(pname+"_intra.h5", 'table')
loaded = True
except:
print "Could not load cached data..."
if not loaded:
uni_df = get_uni(start, end, lookback)
barra_df = load_barra(uni_df, start, end)
barra_df = transform_barra(barra_df)
PRICE_COLS = ['close', 'overnight_log_ret']
price_df = load_prices(uni_df, start, end, PRICE_COLS)
daily_df = merge_barra_data(price_df, barra_df)
DBAR_COLS = ['close', 'dvolume', 'dopen']
daybar_df = load_daybars(price_df[ ['ticker'] ], start, end, DBAR_COLS, freq)
intra_df = merge_intra_data(daily_df, daybar_df)
daily_df, factorRets_df = calc_factors(daily_df, True)
daily_df = calc_rrb_daily(daily_df, horizon)
forwards_df = calc_forward_returns(daily_df, horizon)
daily_df = pd.concat( [daily_df, forwards_df], axis=1)
intra_df, factorRets_df = calc_intra_factors(intra_df, True)
intra_df = calc_rrb_intra(intra_df)
intra_df = merge_intra_data(daily_df, intra_df)
daily_df.to_hdf(pname+"_daily.h5", 'table', complib='zlib')
intra_df.to_hdf(pname+"_intra.h5", 'table', complib='zlib')
full_df = calc_rrb_forecast(daily_df, intra_df, horizon, middate)
print full_df.columns
dump_alpha(full_df, 'rrb')
# dump_alpha(full_df, 'rrbC_B_ma')
# dump_alpha(full_df, 'rrb0_B_ma')
# dump_all(full_df)