-
Notifications
You must be signed in to change notification settings - Fork 86
/
bsim_weights.py
372 lines (306 loc) · 16.8 KB
/
bsim_weights.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
from util import *
from regress import *
from loaddata import *
from calc import *
import opt
import gc
from collections import defaultdict
import argparse
def pnl_sum(group):
cum_pnl = ((np.exp(group['cum_log_ret_i_now' ] - group['cum_log_ret_i_then']) - 1) * group['position_then']).fillna(0).sum()
return cum_pnl
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("--fcast",action="store",dest="fcast",default=None)
parser.add_argument("--horizon",action="store",dest="horizon",default=3)
parser.add_argument("--mult",action="store",dest="mult",default=1.0)
parser.add_argument("--vwap",action="store",dest="vwap",default=False)
parser.add_argument("--maxiter",action="store",dest="maxiter",default=1500)
parser.add_argument("--kappa",action="store",dest="kappa",default=2.0e-8)
parser.add_argument("--slipnu",action="store",dest="slip_nu",default=.18)
parser.add_argument("--slipbeta",action="store",dest="slip_beta",default=.6)
parser.add_argument("--fast",action="store",dest="fast",default=False)
parser.add_argument("--exclude",action="store",dest="exclude",default=None)
parser.add_argument("--earnings",action="store",dest="earnings",default=None)
parser.add_argument("--locates",action="store",dest="locates",default=True)
parser.add_argument("--maxnot",action="store",dest="maxnot",default=200e6)
parser.add_argument("--maxdollars",action="store",dest="maxdollars",default=1e6)
parser.add_argument("--maxforecast",action="store",dest="maxforecast",default=0.0050)
parser.add_argument("--nonegutil",action="store",dest="nonegutil",default=True)
args = parser.parse_args()
print args
mkdir_p("opt")
ALPHA_MULT = float(args.mult)
horizon = int(args.horizon)
start = args.start
end = args.end
factors = ALL_FACTORS
max_forecast = float(args.maxforecast)
max_adv = 0.02
max_dollars = float(args.maxdollars)
participation = 0.015
opt.min_iter = 50
opt.max_iter = int(args.maxiter)
opt.kappa = float(args.kappa) # 4.3e-7
opt.max_sumnot = float(args.maxnot)
opt.max_expnot = 0.04
opt.max_trdnot = 0.5
opt.slip_alpha = 1.0
opt.slip_delta = 0.25
opt.slip_beta = float(args.slip_beta) # 0.6
opt.slip_gamma = 0 # 0.3
opt.slip_nu = float(args.slip_nu) # 0.14
opt.execFee= 0.00015
opt.num_factors = len(factors)
cols = ['ticker', 'iclose', 'tradable_volume', 'close', 'bvwap_b', 'tradable_med_volume_21_y', 'mdvp_y', 'overnight_log_ret', 'date', 'log_ret', 'bvolume', 'capitalization', 'cum_log_ret', 'srisk_pct', 'dpvolume_med_21', 'volat_21_y', 'mkt_cap_y', 'cum_log_ret_y', 'open', 'close_y', 'indname1', 'barraResidRet', 'split', 'div']
cols.extend( BARRA_FACTORS )
#cols.extend( BARRA_INDS )
cols.extend( INDUSTRIES )
forecasts = list()
forecastargs = args.fcast.split(',')
for fcast in forecastargs:
fdir, name, mult, weight = fcast.split(":")
forecasts.append(name)
factor_df = load_factor_cache(dateparser.parse(start), dateparser.parse(end))
pnl_df = load_cache(dateparser.parse(start), dateparser.parse(end), cols)
#print pnl_df.xs(10027954, level=1)['indname1']
pnl_df = pnl_df.truncate(before=dateparser.parse(start), after=dateparser.parse(end))
pnl_df.index.names = ['iclose_ts', 'sid']
pnl_df['forecast'] = np.nan
pnl_df['forecast_abs'] = np.nan
fcast_rets = dict()
fcast_weights = dict()
for fcast in forecastargs:
print "Loading {}".format(fcast)
fdir, name, mult, weight = fcast.split(":")
mu_df = load_mus(fdir, name, start, end)
pnl_df = pd.merge(pnl_df, mu_df, how='left', left_index=True, right_index=True)
retdf = pd.read_csv("./" + fdir + "/rets.txt", names=['date', 'ret'], sep=" ")
retdf['date'] = pd.to_datetime(retdf['date'])
retdf.set_index('date', inplace=True)
retdf['rollingret'] = pd.rolling_sum(retdf['ret'], 5).shift(1)
fcast_rets[name] = retdf
#daily_df = pnl_df.unstack().between_time('15:30', '15:30').stack()
daily_df = pnl_df.unstack().between_time('15:45', '15:45').stack()
daily_df = daily_df.dropna(subset=['date'])
daily_df = daily_df.reset_index().set_index(['date', 'sid'])
if args.locates is not None:
locates_df = load_locates(daily_df[['ticker']], dateparser.parse(start), dateparser.parse(end))
daily_df = pd.merge(daily_df, locates_df, how='left', left_index=True, right_index=True, suffixes=['', '_dead'])
daily_df = remove_dup_cols(daily_df)
locates_df = None
if args.earnings is not None:
earnings_df = load_earnings_dates(daily_df[['ticker']], dateparser.parse(start), dateparser.parse(end))
daily_df = pd.merge(daily_df, earnings_df, how='left', left_index=True, right_index=True, suffixes=['', '_dead'])
daily_df = remove_dup_cols(daily_df)
earnings_df = load_past_earnings_dates(daily_df[['ticker']], dateparser.parse(start), dateparser.parse(end))
daily_df = pd.merge(daily_df, earnings_df, how='left', left_index=True, right_index=True, suffixes=['', '_dead'])
daily_df = remove_dup_cols(daily_df)
earnings_df = None
#daily_df = transform_barra(daily_df)
pnl_df = pd.merge(pnl_df.reset_index(), daily_df.reset_index(), how='left', left_on=['date', 'sid'], right_on=['date', 'sid'], suffixes=['', '_dead'])
pnl_df = remove_dup_cols(pnl_df)
pnl_df.set_index(['iclose_ts', 'sid'], inplace=True)
# resid_df, factor_df = calc_factors(daily_df)
daily_df['residVol'] = horizon * (calc_resid_vol(pnl_df) / 100.0) / np.sqrt(252.0)
factor_df = calc_factor_vol(factor_df)
pnl_df = pd.merge(pnl_df.reset_index(), daily_df.reset_index(), how='left', left_on=['date', 'sid'], right_on=['date', 'sid'], suffixes=['', '_dead'])
pnl_df = remove_dup_cols(pnl_df)
pnl_df.set_index(['iclose_ts', 'sid'], inplace=True)
pnl_df['residVol'] = horizon * (pnl_df['srisk_pct'] / 100.0) / np.sqrt(252.0)
pnl_df['bvolume_d'] = pnl_df['bvolume'].groupby(level='sid').diff()
pnl_df.loc[ pnl_df['bvolume_d'] < 0, 'bvolume_d'] = pnl_df['bvolume']
pnl_df = push_data(pnl_df, 'bvolume_d')
pnl_df = push_data(pnl_df, 'bvwap_b')
#MIX FORECASTS
pnl_df[ 'forecast' ] = 0
for fcast in forecastargs:
fdir, name, mult, weight = fcast.split(":")
pnl_df[ name + '_adj' ] = pnl_df[ name ] * float(mult)
fcast_weights[name] = float(weight)
pnl_df['max_trade_shares'] = pnl_df[ 'bvolume_d_n' ] * participation
pnl_df['position'] = 0
pnl_df['traded'] = 0
pnl_df['target'] = 0
pnl_df['dutil'] = 0
pnl_df['dsrisk'] = 0
pnl_df['dfrisk'] = 0
pnl_df['dmu'] = 0
pnl_df['eslip'] = 0
pnl_df['cum_pnl'] = 0
pnl_df['max_notional'] = (pnl_df['tradable_med_volume_21_y'] * pnl_df['close_y'] * max_adv).clip(0, max_dollars)
pnl_df['min_notional'] = (-1 * pnl_df['tradable_med_volume_21_y'] * pnl_df['close_y'] * max_adv).clip(-max_dollars, 0)
if args.locates is not None:
pnl_df['borrow_notional'] = pnl_df['borrow_qty'] * pnl_df['iclose']
pnl_df['min_notional'] = pnl_df[ ['borrow_notional', 'min_notional'] ].max(axis=1)
pnl_df.ix[ pnl_df['fee_rate'] > 10, 'min_notional' ] = 0
last_pos = pd.DataFrame(pnl_df.reset_index()['sid'].unique(), columns=['sid'])
last_pos['shares_last'] = 0
last_pos.set_index(['sid'], inplace=True)
last_pos = last_pos.sort()
lastday = None
it = 0
groups = pnl_df.groupby(level='iclose_ts')
pnl_df = None
daily_df = None
new_pnl_df = None
gc.collect()
for name, date_group in groups:
dayname = name.strftime("%Y%m%d")
if (int(dayname) < int(start)) or (int(dayname) > int(end)): continue
# if args.fast:
# minutes = int(name.strftime("%M"))
# if minutes != 30: continue
hour = int(name.strftime("%H"))
if hour >= 16: continue
print "Looking at {}".format(name)
monthname = name.strftime("%Y%m")
timename = name.strftime("%H%M%S")
weekdayname = name.weekday()
date_group = date_group[ (date_group['iclose'] > 0) & (date_group['bvolume_d'] > 0) & (date_group['mdvp_y'] > 0) ].sort()
if len(date_group) == 0:
print "No data for {}".format(name)
continue
date_group = pd.merge(date_group.reset_index(), last_pos.reset_index(), how='outer', left_on=['sid'], right_on=['sid'], suffixes=['', '_last'])
date_group['iclose_ts'] = name
date_group = date_group.dropna(subset=['sid'])
date_group.set_index(['iclose_ts', 'sid'], inplace=True)
if lastday is not None and lastday != dayname:
date_group['shares_last'] = date_group['shares_last'] * date_group['split']
date_group['position_last'] = (date_group['shares_last'] * date_group['iclose']).fillna(0)
date_group.ix[ date_group['iclose'].isnull() | date_group['mdvp_y'].isnull() | (date_group['mdvp_y'] == 0) | date_group['bvolume_d'].isnull() | (date_group['bvolume_d'] == 0) | date_group['residVol'].isnull(), 'max_notional' ] = 0
date_group.ix[ date_group['iclose'].isnull() | date_group['mdvp_y'].isnull() | (date_group['mdvp_y'] == 0) | date_group['bvolume_d'].isnull() | (date_group['bvolume_d'] == 0) | date_group['residVol'].isnull(), 'min_notional' ] = 0
# if args.exclude is not None:
# attr, val = args.exclude.split(":")
# val = float(val)
# date_group.ix[ date_group[attr] < val, 'forecast' ] = 0
# date_group.ix[ date_group[attr] < val, 'max_notional' ] = 0
# date_group.ix[ date_group[attr] < val, 'min_notional' ] = 0
date_group.ix[ (date_group['mkt_cap_y'] < 1.6e9) | (date_group['iclose'] > 500.0) | (date_group['indname1'] == "PHARMA") , 'forecast' ] = 0
date_group.ix[ (date_group['mkt_cap_y'] < 1.6e9) | (date_group['iclose'] > 500.0) | (date_group['indname1'] == "PHARMA"), 'max_notional' ] = 0
date_group.ix[ (date_group['mkt_cap_y'] < 1.6e9) | (date_group['iclose'] > 500.0) | (date_group['indname1'] == "PHARMA"), 'min_notional' ] = 0
if args.earnings is not None:
days = int(args.earnings)
date_group[ date_group['daysToEarn'] == 3 ]['residVol'] = date_group['residVol'] * 2
date_group[ date_group['daysToEarn'] == 2 ]['residVol'] = date_group['residVol'] * 3
date_group[ date_group['daysToEarn'] == 1 ]['residVol'] = date_group['residVol'] * 4
date_group[ ( (date_group['daysToEarn'] <= days) | (date_group['daysFromEarn'] < days)) & (date_group['position_last'] >= 0)]['max_notional'] = date_group['position_last']
date_group[ ( (date_group['daysToEarn'] <= days) | (date_group['daysFromEarn'] < days)) & (date_group['position_last'] >= 0)]['min_notional'] = 0
date_group[ ( (date_group['daysToEarn'] <= days) | (date_group['daysFromEarn'] < days)) & (date_group['position_last'] <= 0)]['max_notional'] = 0
date_group[ ( (date_group['daysToEarn'] <= days) | (date_group['daysFromEarn'] < days)) & (date_group['position_last'] <= 0)]['min_notional'] = date_group['position_last']
print "Weights:"
for fcast in fcast_weights.keys():
weight = fcast_weights[fcast]
if dayname != lastday:
retdf = fcast_rets[fcast]
try:
last_ret = retdf.ix[ pd.to_datetime(dayname), 'rollingret']
if last_ret > 0:
weight *= 1.2
weight = min(weight, .9)
else:
weight *= .80
weight = max(weight, .1)
except:
pass
if fcast == "htb": weight = .5
print "{}: {}".format(fcast, weight)
fcast_weights[fcast] = weight
date_group['forecast'] = date_group['forecast'] + date_group[fcast + "_adj"].fillna(0) * weight
date_group['forecast'] = (ALPHA_MULT * date_group['forecast']).clip(-max_forecast, max_forecast)
print date_group['forecast'].describe()
#OPTIMIZATION
opt.num_secs = len(date_group)
opt.init()
opt.sec_ind = date_group.reset_index().index.copy().values
opt.sec_ind_rev = date_group.reset_index()['sid'].copy().values
opt.g_positions = date_group['position_last'].copy().values
opt.g_lbound = date_group['min_notional'].fillna(0).values
opt.g_ubound = date_group['max_notional'].fillna(0).values
opt.g_mu = date_group['forecast'].copy().fillna(0).values
opt.g_rvar = date_group['residVol'].copy().fillna(0).values
opt.g_advp = date_group[ 'mdvp_y'].copy().fillna(0).values
opt.g_price = date_group['iclose'].copy().fillna(0).values
opt.g_advpt = (date_group['bvolume_d'] * date_group['iclose']).fillna(0).values
opt.g_vol = date_group['volat_21_y'].copy().fillna(0).values * horizon
opt.g_mktcap = date_group['mkt_cap_y'].copy().fillna(0).values
find = 0
for factor in factors:
opt.g_factors[ find, opt.sec_ind ] = date_group[factor].fillna(0).values
find += 1
find1 = 0
for factor1 in factors:
find2 = 0
for factor2 in factors:
try:
factor_cov = factor_df[(factor1, factor2)].fillna(0).ix[pd.to_datetime(dayname)]
# factor1_sig = np.sqrt(factor_df[(factor1, factor1)].fillna(0).ix[pd.to_datetime(dayname)])
# factor2_sig = np.sqrt(factor_df[(factor2, factor2)].fillna(0).ix[pd.to_datetime(dayname)])
# print "Factor Correlation {}, {}: {}".format(factor1, factor2, factor_cov/(factor1_sig*factor2_sig))
except:
# print "No cov found for {} {}".format(factor1, factor2)
factor_cov = 0
opt.g_fcov[ find1, find2 ] = factor_cov * horizon
opt.g_fcov[ find2, find1 ] = factor_cov * horizon
find2 += 1
find1 += 1
try:
(target, dutil, eslip, dmu, dsrisk, dfrisk, costs, dutil2) = opt.optimize()
except:
date_group.to_csv("problem.csv")
raise
optresults_df = pd.DataFrame(index=date_group.index, columns=['target', 'dutil', 'eslip', 'dmu', 'dsrisk', 'dfrisk', 'costs', 'dutil2', 'traded'])
optresults_df['target'] = target
optresults_df['dutil'] = dutil
optresults_df['eslip'] = eslip
optresults_df['dmu'] = dmu
optresults_df['dsrisk'] = dsrisk
optresults_df['dfrisk'] = dfrisk
optresults_df['costs'] = costs
optresults_df['dutil2'] = dutil2
# pnl_df.ix[ date_group.index, 'target'] = optresults_df['target']
# pnl_df.ix[ date_group.index, 'eslip'] = optresults_df['eslip']
# pnl_df.ix[ date_group.index, 'dutil'] = optresults_df['dutil']
# pnl_df.ix[ date_group.index, 'dsrisk'] = optresults_df['dsrisk']
# pnl_df.ix[ date_group.index, 'dfrisk'] = optresults_df['dfrisk']
# pnl_df.ix[ date_group.index, 'dmu'] = optresults_df['dmu']
date_group['target'] = optresults_df['target']
date_group['dutil'] = optresults_df['dutil']
# tmp = pd.merge(last_pos.reset_index(), date_group['forecast'].reset_index(), how='inner', left_on=['sid'], right_on=['sid'])
# date_group['last_position'] = tmp.set_index(['iclose_ts', 'sid'])['position']
if args.nonegutil:
date_group.ix[ date_group['dutil'] <= 0, 'target'] = date_group['position_last']
date_group['max_move'] = date_group['position_last'] + date_group['max_trade_shares'] * date_group['iclose']
date_group['min_move'] = date_group['position_last'] - date_group['max_trade_shares'] * date_group['iclose']
date_group['position'] = date_group['target']
date_group['position'] = date_group[ ['position', 'max_move'] ].min(axis=1)
date_group['position'] = date_group[ ['position', 'min_move'] ].max(axis=1)
# df = date_group[ date_group['target'] > date_group['max_move']]
# print df[['max_move', 'min_move', 'target', 'position', 'max_trade_shares', 'position_last', 'bvolume_d_n']].head()
# print date_group.xs(10000108, level=1)[['max_move', 'min_move', 'target', 'position', 'max_trade_shares', 'position_last', 'bvolume_d_n']]
date_group['traded'] = date_group['position'] - date_group['position_last']
date_group['shares'] = date_group['position'] / date_group['iclose']
# pnl_df.ix[ date_group.index, 'traded'] = date_group['traded']
postmp = pd.merge(last_pos.reset_index(), date_group['shares'].reset_index(), how='outer', left_on=['sid'], right_on=['sid']).set_index('sid')
last_pos['shares_last'] = postmp['shares'].fillna(0)
postmp = None
# pnl_df.ix[ date_group.index, 'position'] = date_group['position']
optresults_df['forecast'] = date_group['forecast']
optresults_df['traded'] = date_group['traded']
optresults_df['shares'] = date_group['shares']
optresults_df['position'] = date_group['position']
optresults_df['iclose'] = date_group['iclose']
optresults_df = optresults_df.reset_index()
optresults_df['sid'] = optresults_df['sid'].astype(int)
optresults_df.set_index(['iclose_ts', 'sid'], inplace=True)
optresults_df.to_csv("./opt/opt." + "-".join(forecasts) + "." + dayname + "_" + timename + ".csv")
lastday = dayname
it += 1
# groups.remove(name)
date_group = None
gc.collect()
email("bsim done: " + args.fcast, "")
#pnl_df.to_csv("debug." + "-".join(forecasts) + "." + str(start) + "." + str(end) + ".csv")
#pnl_df.xs(testid, level=1).to_csv("debug.csv")