-
Notifications
You must be signed in to change notification settings - Fork 9
/
DIVERSIFIED_LONG_SHORT_ALGORITHM.py
417 lines (383 loc) · 15.6 KB
/
DIVERSIFIED_LONG_SHORT_ALGORITHM.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#Diversified long/short trading algorithm. A long or short position is taken for each stock in our universe based on its classifier's output.
#Topic: Developing an algorithmic trading strategy using supervised machine learning classification techniques and technical indicators
#Student: Max Fitzpatrick
#Professor: Yves Jegourel
#Course: MSc Banking, Finance & Commodity Trading
#University: University of Bordeaux
################################################################
################################################################
import quantopian.optimize as opt
import numpy as np
import pandas as pd
import math as m
import datetime
from sklearn.ensemble import RandomForestClassifier
###########################
#####SUPPORT FUNCTIONS#####
###########################
#Defining the functions for generating our technical indicators
#Rate of Change
def ROC(df, n):
M = df['Adj. Close'].diff(n - 1)
N = df['Adj. Close'].shift(n - 1)
ROC = pd.Series(M / N, name = 'ROC_' + str(n))
df['ROC'] = ROC
return df
#Bollinger Bands
def BBANDS(df, n):
MA = pd.Series(pd.rolling_mean(df['Adj. Close'], n))
MSD = pd.Series(pd.rolling_std(df['Adj. Close'], n))
b1 = 4 * MSD / MA
B1 = pd.Series(b1, name = 'BollingerB_' + str(n))
df['B1'] = B1
b2 = (df['Adj. Close'] - MA + 2 * MSD) / (4 * MSD)
B2 = pd.Series(b2, name = 'Bollinger%b_' + str(n))
df['B2'] = B2
return df
#Stochastic oscillator %K
def STOK(df,n):
SOk = pd.Series((df['Adj. Close'] - pd.rolling_min(df['Adj. Low'],n)) / (pd.rolling_max(df['Adj. High'],n) - pd.rolling_min(df['Adj. Low'],n)), name = 'SO%k')
df['SOk'] = SOk
return df
#MACD, MACD Signal and MACD difference
def MACD(df, n_fast, n_slow):
EMAfast = pd.Series(pd.ewma(df['Adj. Close'], span = n_fast, min_periods = n_slow - 1))
EMAslow = pd.Series(pd.ewma(df['Adj. Close'], span = n_slow, min_periods = n_slow - 1))
MACD = pd.Series(EMAfast - EMAslow, name = 'MACD_' + str(n_fast) + '_' + str(n_slow))
MACDsign = pd.Series(pd.ewma(MACD, span = 9, min_periods = 8), name = 'MACDsign_' + str(n_fast) + '_' + str(n_slow))
MACDdiff = pd.Series(MACD - MACDsign, name = 'MACDdiff_' + str(n_fast) + '_' + str(n_slow))
df['MACD'] = MACD
df['MACDsign'] = MACDsign
df['MACDdiff'] = MACDdiff
return df
#Commodity Channel Index
def CCI(df, n):
PP = (df['Adj. High'] + df['Adj. Low'] + df['Adj. Close']) / 3
CCI = pd.Series((PP - pd.rolling_mean(PP, n)) / pd.rolling_std(PP, n), name = 'CCI_' + str(n))
df['CCI'] = CCI
return df
#Pulling clf training data from csv (Quantopian workaround)
def grab_data(df):
global my_data
my_data = df
return df
#Training classifier functions (Quantopian workaround to avoid timing out)
def train_classifiers(context):
training_Data = pd.DataFrame(np.random.randint(0,100,size=(len(my_data), 2)), columns=list('AB'))
training_Data['Delta'] = my_data.ix[:, context.training_counter]
training_Data['MA10'] = my_data.ix[:,context.training_counter+1]
training_Data['MA20'] = my_data.ix[:,context.training_counter+2]
training_Data['MA100'] = my_data.ix[:,context.training_counter+3]
training_Data['ROC'] = my_data.ix[:,context.training_counter+4]
training_Data['B1'] = my_data.ix[:,context.training_counter+5]
training_Data['B2'] = my_data.ix[:,context.training_counter+6]
training_Data['SOk'] = my_data.ix[:,context.training_counter+7]
training_Data['MACD'] = my_data.ix[:,context.training_counter+8]
training_Data['MACDsign'] = my_data.ix[:,context.training_counter+9]
training_Data['MACDdiff'] = my_data.ix[:,context.training_counter+10]
training_Data['RSI14'] = my_data.ix[:,context.training_counter+11]
training_Data['CCI'] = my_data.ix[:,context.training_counter+12]
training_Data['Class'] = my_data.ix[:,context.training_counter+13]
training_Data = training_Data.drop(['A', 'B'], axis=1)
training_Data = training_Data.head(3400)
context.training_counter += 14
X = training_Data.drop(['Class'], 1)
y = training_Data['Class']
X = np.array(X)
y = np.array(y)
clf = RandomForestClassifier(random_state=20, n_estimators=1000)
clf.fit(X, y)
clf_list[ticker_list[context.asset_counter]] = clf
print("Random forest classifier is trained and stored for :", ticker_list[context.asset_counter])
context.asset_counter += 1
return clf_list, context
########################
#####MAIN FUNCTIONS#####
########################
#Initialize sets our global variables
def initialize(context):
#Defining our universe
set_symbol_lookup_date('2015-01-01')
context.assets = symbols("aapl",
"abt",
"adm",
"adp",
"adsk",
"aet",
"aig",
"amgn",
"axp",
"bac",
"bax",
"bby",
"bdx",
"bll",
"ca",
"cag",
"ci",
"clx",
"csco",
"csx",
"dis",
"dov",
"ecl",
"emn",
"emr",
"fdx",
"gis",
"gpc",
"gps",
"gww",
"hon",
"hrb",
"intc",
"itw",
"jnj",
"jwn",
"key",
"lb",
"lly",
"lmt",
"lnc",
"low",
"luv",
"mat",
"mdt",
"mmc",
"mro",
"msft",
"mu",
"nem",
"nke",
"noc",
"ntap",
"nue",
"nwl",
"orcl",
"oxy",
"pcar",
"ph",
"pki",
"px",
"shw",
"slb",
"sna",
"sti",
"swk",
"syy",
"tgt",
"tjx",
"txt",
"unh",
"wfc",
"wmb",
"wmt",
"xom")
global ticker_list
ticker_list = ["aapl",
"abt",
"adm",
"adp",
"adsk",
"aet",
"aig",
"amgn",
"axp",
"bac",
"bax",
"bby",
"bdx",
"bll",
"ca",
"cag",
"ci",
"clx",
"csco",
"csx",
"dis",
"dov",
"ecl",
"emn",
"emr",
"fdx",
"gis",
"gpc",
"gps",
"gww",
"hon",
"hrb",
"intc",
"itw",
"jnj",
"jwn",
"key",
"lb",
"lly",
"lmt",
"lnc",
"low",
"luv",
"mat",
"mdt",
"mmc",
"mro",
"msft",
"mu",
"nem",
"nke",
"noc",
"ntap",
"nue",
"nwl",
"orcl",
"oxy",
"pcar",
"ph",
"pki",
"px",
"shw",
"slb",
"sna",
"sti",
"swk",
"syy",
"tgt",
"tjx",
"txt",
"unh",
"wfc",
"wmb",
"wmt",
"xom"]
#Declaring global variables
context.h = 30 # <- investment time horizon is 30 trading days
context.training_counter = 1
context.asset_counter = 0
global clf
global clf_list
clf_list = {}
context.training_check = 0
context.trained = False
#Pulling our training data from dropbox
fetch_csv("https://dl.dropboxusercontent.com/s/xih9k23niw8j06r/MEGALS75.csv", pre_func=grab_data, date_column='Date', symbol='aapl')
#Setting our portfolio to rebalance every 30 trading days
context.iDays = 30
context.NDays = 30 # <- Set to desired N days market is open
schedule_function(func=rebalance,
date_rule=date_rules.every_day(),
half_days=True,
time_rule=time_rules.market_open(hours=0,minutes=1))
#We use handle data, which is called once per minute, in order to train our classifiers. Doing each classifier one by one avoids timing out on Quantopian's platform
def handle_data(context, data):
if context.training_check == 0:
train_classifiers(context)
if context.asset_counter == len(ticker_list):
context.training_check = 1
context.trained = True
else:
return
else:
pass
#rebalance function that runs every 30 trading days
def rebalance(context,data):
if context.trained == False:
return
context.iDays += 1
if (context.iDays % context.NDays) != 1:
return
# Start rebalance
log.info("starting rebalance")
#Generating new trades for the next 30 day period
i = 0
targetlist = pd.DataFrame(np.random.randint(0,100,size=(len(ticker_list), 3)))
targetlist.columns = ['Ticker', 'LongProbability', 'Class']
#Pulling OHLC data for the past 100 days and then generating our technical indicators for each asset in our universe
for asset in context.assets:
try:
df = pd.DataFrame(data.history(asset,'open', 100, '1d'))
df.columns = ['Adj. Open']
df['Adj. High'] = data.history(asset,'high', 100, '1d')
df['Adj. Low'] = data.history(asset,'low', 100, '1d')
df['Adj. Close'] = data.history(asset,'close', 100, '1d')
context.latest_Indicators = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('AB'))
context.latest_Indicators['Adj. Open'] = df['Adj. Open'].values
context.latest_Indicators['Adj. High'] = df['Adj. High'].values
context.latest_Indicators['Adj. Low'] = df['Adj. Low'].values
context.latest_Indicators['Adj. Close'] = df['Adj. Close'].values
context.latest_Indicators = context.latest_Indicators.drop(['A', 'B'], axis=1)
context.latest_Indicators['Delta'] = context.latest_Indicators['Adj. Close'].pct_change(context.h)
context.latest_Indicators['MA10'] = context.latest_Indicators['Adj. Close'].rolling(window=10).mean()
context.latest_Indicators['MA20'] = context.latest_Indicators['Adj. Close'].rolling(window=20).mean()
context.latest_Indicators['MA100'] = context.latest_Indicators['Adj. Close'].rolling(window=100).mean()
ROC(context.latest_Indicators,context.h)
BBANDS(context.latest_Indicators,20)
STOK(context.latest_Indicators,14)
MACD(context.latest_Indicators,12,26)
delta = context.latest_Indicators['Delta']
up, down = delta.copy(), delta.copy()
up[up < 0] = 0
down[down > 0] = 0
roll_up1 = pd.stats.moments.ewma(up, 14)
roll_down1 = pd.stats.moments.ewma(down.abs(), 14)
RS1 = roll_up1 / roll_down1
RSI1 = 100.0 - (100.0 / (1.0 + RS1))
context.latest_Indicators['RSI14'] = RSI1
CCI(context.latest_Indicators,20)
context.latest_Indicators = context.latest_Indicators.drop(['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close'], 1)
context.latest_Indicators = context.latest_Indicators[-1:]
X_recent = np.array(context.latest_Indicators)
LongProbability = clf_list[ticker_list[i]].predict_proba(X_recent)
LongProbability = LongProbability[:,1]
targetlist.ix[i,'Ticker'] = ticker_list[i]
targetlist.ix[i,'LongProbability'] = LongProbability
if targetlist.ix[i,'LongProbability'] >= 0.5:
targetlist.ix[i,'Class'] = 1
else:
targetlist.ix[i,'Class'] = -1
i += 1
except:
print("ERROR FOR: ", asset)
targetlist.ix[i,'Ticker'] = ticker_list[i]
targetlist.ix[i,'LongProbability'] = 0.5
targetlist.ix[i,'Class'] = 1
i += 1
#Creating shortlists of our long and short positions based on classifier output
targetlist['LongProbability'] = targetlist['LongProbability'].astype('float')
targetlist.sort_values(by = ['LongProbability'], ascending = False, inplace = True)
print(targetlist)
long_counter = 0
short_counter = 0
for i in range(len(targetlist)):
if targetlist.ix[i,'LongProbability'] > 0.5:
long_counter +=1
if targetlist.ix[i,'LongProbability'] < 0.5:
short_counter +=1
longs = targetlist.head(long_counter)
shorts = targetlist.tail(short_counter)
print(longs)
print(shorts)
try:
weight = float(len(longs)+len(shorts))
weight = float(1/weight)
print("WEIGHT: ", weight)
long_weight = float(weight)
short_weight = float(-weight)
except:
weight = 0
print("WEIGHT: ", weight)
long_weight = 0
short_weight = 0
long_list = []
short_list = []
for i in range(len(ticker_list)):
if ticker_list[i] in longs['Ticker'].values:
long_list.append(context.assets[i])
for i in range(len(ticker_list)):
if ticker_list[i] in shorts['Ticker'].values:
short_list.append(context.assets[i])
weights = {long_list[i]: long_weight for i in range(len(long_list))}
weights.update({short_list[i]: short_weight for i in range(len(short_list))})
print(weights)
#Placing the relevant orders
order_optimal_portfolio(objective=opt.TargetWeights(weights), constraints=[opt.MaxGrossExposure(1.0)])
record(leverage = context.account.leverage)