-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytrader_lite.py
647 lines (516 loc) · 24.8 KB
/
pytrader_lite.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import datetime as dt
import math
import os
from time import time
import matplotlib.dates as mdates
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from IPython.display import display
from matplotlib import cm
from matplotlib.ticker import FuncFormatter
from pandas.tseries.offsets import BDay, BMonthEnd
from sklearn.externals import joblib
from sklearn.metrics import f1_score, precision_recall_fscore_support
from sklearn.preprocessing import StandardScaler
import nolds # for hurst exponent
import pandas_datareader.data as web
import requests_cache
class trader(object):
def __init__(self, instrument, country="US", long_only=True,
num_periods=60, target_num_periods=20,
model_save_path="./models/"):
self.country = country
self.price_data = None
self.sentiment_data = None
self.fundamental_data = None
self.econ_data = None
self.model_save_path = model_save_path
self.long_only = long_only
self.all_returns = None
self.price_changes = None
self.num_periods = num_periods
self.target_num_periods = target_num_periods
self.instrument = instrument
#training and testing vectors, predictions
self.features = None
self.labels = None
self.X_train = None
self.y_train = None
self.X_test = None
self.y_test = None
self.predictions = None
#scores
self.train_score = None
self.test_score = None
self.bm_train_score = None
self.bm_test_score = None
#returns due to predictions
self.strategy_returns = None
self.bm_returns = None
#starting price level for q-learner
self.prices = None
self.train_start_price = 0.0
self.test_start_price = 0.0
#check if model_save dir exists, otherwise create it
#self._ensure_dir(self.model_save_path)
#scaler object set once train_test_split is run and can then be applied to test data
self.scaler = None
self.scaler_file_path = "./scaler.pkl"
def _ensure_dir(self, file_path):
if not os.path.exists(file_path):
os.makedirs(file_path)
def create_multi_period_features(self, periods):
px_col = 'Adj Close'
hi_col = 'High'
lo_col = 'Low'
if self.price_data is None:
raise Exception("self.price_data is None, please run get_price_data first: {}".format(instrument))
else:
self.price_data['1d_ret'] = self.price_data[px_col].pct_change()
#returns to create labels with, won't nead for learning
self.all_returns = self.price_data['1d_ret'].copy()
#price changes for q-learner rewards
self.price_changes = self.price_data[px_col] - self.price_data[px_col].shift(1)
#self.price_data['p_ret'] = self.price_data[px_col].pct_change(periods=self.num_periods)
self.price_data['TARGET_RET'] = self.price_data[px_col].pct_change(periods=self.target_num_periods).shift(-self.target_num_periods)
for period in periods:
prefix = str(period) + "_day_"
self.price_data[prefix + "ret"] = self.price_data[px_col].pct_change(periods=period)
self.price_data[prefix + "vol"] = self.price_data['1d_ret'].ewm(halflife=period, min_periods=period).std() * math.sqrt(period)
self.price_data[prefix + "sharpe"] = self.price_data[prefix + "ret"] / self.price_data[prefix + "vol"]
self.price_data[prefix + 'dist_to_mov_avg'] = self.price_data[px_col] / self.price_data[px_col].ewm(halflife=period, min_periods=period).mean()
self.price_data[prefix + 'skew'] = pd.Series.rolling(self.price_data['1d_ret'], window=period).skew() * (1/math.sqrt(period))
self.price_data[prefix + 'kurt'] = pd.Series.rolling(self.price_data['1d_ret'], window=period).kurt() * (1/period)
self.price_data[prefix + 'mom'] = self.price_data[prefix + "ret"].copy()
#self.price_data[prefix + 'up_down_ratio'] = pd.Series.rolling(self.price_data[self.price_data['1d_ret'] >= 0]['1d_ret'], window=period).mean() / pd.Series.rolling(self.price_data[self.price_data['1d_ret'] < 0]['1d_ret'], window=period).mean()
self.price_data.drop(str(prefix + "ret"), axis=1, inplace=True)
#add the hurst exponent - period 5,10,20 outputs nonsenical data
if period not in [5,10,20]:
hurst = lambda x: nolds.hurst_rs(x, fit="poly")
#print("Caluclating hurst...")
try:
self.price_data[prefix + 'hurst'] = pd.Series.rolling(self.price_data[px_col],
window=period).apply(hurst)
except:
self.price_data[prefix + 'hurst'] = 0.5
self.price_data['macd'] = self._calc_MACD()
self.price_data['rsi'] = self._calc_RSI()
self.prices = self.price_data[px_col].copy()
self.price_data.drop(['Open', 'High', 'Low',
'Close', px_col, 'Volume',
'1d_ret', '10_day_kurt', '5_day_skew',
'5_day_kurt', '10_day_skew'],
axis=1, inplace=True)
#print(self.price_data.shape)
self.price_data.dropna(inplace=True)
def _get_df_from_csv(self, filename):
"""Convenience function to load data from csv to dataframe and set index.
Parameters
----------
filename: the csv file to read
returns: pandas DataFrame with Date as Index
"""
df = pd.read_csv(filename)
df.set_index('Date', drop=True, inplace=True)
df.index = pd.to_datetime(df.index)
return df
def _reindex_to_business_days(self, data):
'''
Convenience function to reindex a pandas dataframe to a full 5 business day week
e.g. Mon - Fri
Parameters:
data: pandas DataFrame
returns: reindexed DataFrame
'''
start_date = data.index[0]
end_date = data.index[-1]
date_range = pd.bdate_range(start_date, end_date)
data = data.reindex(index=date_range, method='ffill', copy=True)
data.dropna(inplace=True)
return data
def create_labels(self):
go_short = -1
if self.long_only is True:
go_short = 0
self.price_data['BUY_SELL'] = self.price_data['TARGET_RET']
self.price_data['BUY_SELL'] = np.where(self.price_data['BUY_SELL'] < 0, go_short, 1)
self.labels = self.price_data['BUY_SELL']
self.price_data.drop('BUY_SELL', axis=1, inplace=True)
try:
self.price_data.drop('p_ret', axis=1, inplace=True)
except:
pass
self.price_data.drop('TARGET_RET', axis=1, inplace=True)
#print(self.price_data.head())
#print(self.price_data.tail())
self.features = self.price_data.copy()
self.price_changes = self.price_changes.loc[self.features.index[0]:self.features.index[-1]].shift(-1)
self.price_data = None
#print(self.features.shape)
def create_multi_period_features_and_labels(self, periods):
self.create_multi_period_features(periods)
self.create_labels()
def scale_data(self, X=None):
if self.scaler is None:
self.scaler = self.load_scaler()
else:
if X is None:
train_ix = self.X_train.index.copy()
train_cols = self.X_train.columns.copy()
test_ix = self.X_test.index.copy()
test_cols = self.X_test.columns.copy()
train_scaled = self.scaler.transform(self.X_train)
test_scaled = self.scaler.transform(self.X_test)
self.X_train = pd.DataFrame(data=train_scaled, index=train_ix, columns=train_cols)
self.X_test = pd.DataFrame(data=test_scaled, index=test_ix, columns=test_cols)
else:
return self.scaler.transform(X)
def train_classifier(self, clf, X, y):
''' Fits a classifier to the training data. '''
# Start the clock, train the classifier, then stop the clock
start = time()
clf.fit(X, y.values)
end = time()
# Print the results
diff = end-start
if diff < 60:
print ("Trained model in {:.2f} seconds".format(diff))
elif diff < 3600:
print("Trained model in {:.2f} minutes".format(diff/60))
else:
print("Trained model in {:.2f} hours".format(diff/3600))
def predict_labels(self, clf, features, target, testing=False):
''' Makes predictions using a fit classifier based on F1 score. '''
# Start the clock, make predictions, then stop the clock
start = time()
y_pred = clf.predict(features)
end = time()
# Print and return results
diff = end-start
#print ("Made predictions in {:.4f} seconds.".format(diff))
if testing is True:
self.predictions = y_pred
self.bm_test_score = f1_score(target.values,
np.ones(len(target.values)),
pos_label=1)
self.bm_train_score = f1_score(target.values,
np.ones(len(target.values)),
pos_label=1)
return f1_score(target.values, y_pred, pos_label=1)
def test_predict_new(self, clf, X_test, y_test):
self.test_score = self.predict_labels(clf, X_test, y_test, testing=True)
#print ("F1 score for {} for test set: {:.4f}.".format(clf.__class__.__name__, self.test_score))
return self.test_score
def create_strategy_from_predictions(self):
test_start = pd.to_datetime(self.X_test.index[0])
test_end = pd.to_datetime(self.X_test.index[-1])
self.strategy_returns = self.all_returns.loc[test_start:test_end].shift(-1) * self.predictions
self.strategy_returns.dropna(axis=0, inplace=True)
self.bm_returns = self.all_returns.loc[test_start:test_end].copy()
def load_scaler(self):
try:
self.scaler = joblib.load(self.scaler_file_path)
#print("Scaler loaded successfully to self.scaler")
except Exception as ex:
print(ex)
def save_scores(self, clf, security, perf_stats):
dir = self.model_save_path + str(security) + "/"
#self._ensure_dir(dir)
filepath = dir + str(security) + "_" + str(self.num_periods) + "_day_" + clf.__class__.__name__ + "_scores.csv"
data = { "train_f1": self.train_score,
"test_f1": self.test_score,
"train_bm_f1": self.bm_train_score,
"test_bm_f1" : self.bm_test_score,
"strategy_return_ann" : perf_stats.ann_perf,
"bm_return_ann" : perf_stats.bm_ann_perf,
"strategy_sharpe" : perf_stats.sharpe,
"bm_sharpe" : perf_stats.bm_sharpe,
"strategy_max_drawdown" : perf_stats.max_drawdown,
"bm_max_drawdown" : perf_stats.bm_max_drawdown
}
df = pd.DataFrame(data=data, index=(security,))
df = df[["train_f1",
"train_bm_f1",
"test_f1",
"test_bm_f1",
"strategy_return_ann",
"bm_return_ann",
"strategy_sharpe",
"bm_sharpe",
"strategy_max_drawdown",
"bm_max_drawdown"]]
#df.to_csv(filepath)
#print("Saved scores to {}".format(filepath))
return df
def _calc_MACD(self):
slow_ema = self.price_data['Adj Close'].ewm(span=26).mean()
fast_ema = self.price_data['Adj Close'].ewm(span=12).mean()
#self.price_data['MACD'] = fast_ema - slow_ema
return (fast_ema - slow_ema)
def _calc_RSI(self, n=14):
deltas = self.price_data['Adj Close'].diff()
seed = deltas[:n+1]
up = seed[seed >= 0].sum() / n
down = -seed[seed < 0].sum() / n
rs = up/down
rsi = np.zeros_like(self.price_data['Adj Close'])
rsi[:n] = 100.0 - 100.0 / (1.0 + rs)
for i in range(n, len(self.price_data['Adj Close'])):
delta = deltas[i-1]
if delta > 0:
upval = delta
downval = 0.0
else:
upval = 0.0
downval = -delta
up = (up*(n-1)+upval)/n
down = (down*(n-1)+downval)/n
rs = up/down
rsi[i] = 100.0 - 100.0/(1.0 + rs)
return rsi
def _calc_Williams_R(self, num_periods=60):
px_col = 'Adj Close'
hi_col = 'High'
lo_col = 'Low'
willr = ((self.price_data[hi_col].shift(-num_periods) - self.price_data[px_col]) / (self.price_data[hi_col].shift(-num_periods) - self.price_data[lo_col].shift(-num_periods))) * -100
return willr
def _calc_Momentum(self, num_periods):
mom = self.price_data['Adj Close'].pct_change(periods=num_periods)
return mom
def _calc_ATR(self, num_periods):
px_col = 'Adj Close'
hi_col = 'High'
lo_col = 'Low'
tr1 = self.price_data[hi_col] - self.price_data[lo_col]
tr2 = self.price_data[hi_col] - self.price_data[px_col].shift(1)
tr3 = self.price_data[lo_col] - self.price_data[px_col].shift(1)
df = pd.DataFrame(data=[tr1, tr2, tr3]).T
atr = df.max(axis=1)
atr = atr.ewm(halflife=num_periods).mean()
return atr
class stats(object):
def __init__(self, trader, year_day_count=252, risk_free=0):
self.trader = trader
self._daily_returns = self.trader.all_returns
self._positions = self.trader.predictions
self.year_day_count = year_day_count
self.risk_free = risk_free
self.num_days = len(self.daily_returns)
self.strategy_returns = trader.strategy_returns
self.bm_daily_returns = trader.bm_returns
self.cum_performance = np.nan
self.cum_volatility = np.nan
self.sharpe = np.nan
self.ann_perf = np.nan
self.ann_vol = np.nan
self.max_drawdown = np.nan
self.bm_cum_performance = np.nan
self.bm_cum_volatility = np.nan
self.bm_sharpe = np.nan
self.bm_ann_perf = np.nan
self.bm_ann_vol = np.nan
self.bm_max_drawdown = np.nan
self.rel_cum_performance = np.nan
self.rel_cum_volatility = np.nan
self.rel_sharpe = np.nan
self.rel_ann_perf = np.nan
self.rel_ann_vol = np.nan
self.rel_max_drawdown = np.nan
self.rolling_cum_perf = np.nan
self.rolling_bm_cum_perf = np.nan
self.rolling_max_drawdown = np.nan
self.rolling_bm_max_drawdown = np.nan
self._calculated = False
@property
def daily_returns(self):
return self._daily_returns
@property
def predicted_positions(self):
return self._positions
@daily_returns.setter
def daily_returns(self, value):
self._daily_returns = value
@predicted_positions.setter
def predicted_positions(self, value):
self._positions = value
def calculate(self):
try:
#print(self.strategy_returns)
self.cum_performance = np.prod(self.strategy_returns + 1) - 1
self.cum_volatility = np.std(self.strategy_returns) * math.sqrt(len(self.strategy_returns))
self.sharpe = self.cum_performance / self.cum_volatility
self.bm_cum_performance = np.prod(self.bm_daily_returns + 1) - 1
self.bm_cum_volatility = np.std(self.bm_daily_returns) * math.sqrt(len(self.bm_daily_returns))
self.bm_sharpe = self.bm_cum_performance / self.bm_cum_volatility
if len(self.strategy_returns) > self.year_day_count:
#annualise the data, otherwise leave it
self.ann_perf = math.pow((1 + self.cum_performance), 1 / (self.num_days / self.year_day_count)) - 1
self.ann_vol = np.std(self.strategy_returns) * math.sqrt(self.year_day_count)
self.sharpe = self.ann_perf / self.ann_vol
self.bm_ann_perf = math.pow((1 + self.bm_cum_performance), 1 / (self.num_days / self.year_day_count)) - 1
self.bm_ann_vol = np.std(self.bm_daily_returns) * math.sqrt(self.year_day_count)
self.bm_sharpe = self.bm_ann_perf / self.bm_ann_vol
else:
self.ann_perf = self.cum_performance
self.ann_vol = self.cum_volatility
self.bm_ann_perf = self.bm_cum_performance
self.bm_ann_vol = self.bm_cum_volatility
self.rel_cum_performance = self.cum_performance - self.bm_cum_performance
self.rel_cum_volatility = self.cum_volatility - self.bm_cum_volatility
self.rel_sharpe = self.sharpe - self.bm_sharpe
self.rel_ann_perf = self.ann_perf - self.bm_ann_perf
self.rel_ann_vol = self.ann_vol - self.bm_ann_vol
self.rel_max_drawdown = self.max_drawdown - self.bm_max_drawdown
self.rolling_cum_perf = np.cumprod(self.strategy_returns + 1) - 1
self.rolling_max_drawdown = self.rolling_cum_perf + 1
self.rolling_max_drawdown = self.rolling_max_drawdown.div(self.rolling_max_drawdown.cummax()).sub(1)
self.rolling_bm_cum_perf = np.cumprod(self.bm_daily_returns + 1) - 1
self.rolling_bm_max_drawdown = self.rolling_bm_cum_perf + 1
self.rolling_bm_max_drawdown = self.rolling_bm_max_drawdown.div(self.rolling_bm_max_drawdown.cummax()).sub(1)
self.max_drawdown = self.rolling_max_drawdown.min()
self.bm_max_drawdown = self.rolling_bm_max_drawdown.min()
self._calculated = True
except Exception as ex:
print("Error calculating stats for {}".format(self.trader.instrument))
def plot_tearsheet(self, instrument=None):
if not self._calculated:
print("Unable to plot tearsheet, calculate first")
if instrument is None:
instrument = self.trader.instrument
ncols = 2
nrows = 1
ctr = 0
fig, axs = plt.subplots(nrows=nrows, ncols=ncols, sharex=False, figsize=(12,4))
fig.suptitle("{} strategy and drawdowns vs. benchmark".format(instrument), fontsize=12, y=1.05)
for ax in axs.flat:
if ctr == 0:
self._plot_equity(self.rolling_cum_perf, self.rolling_bm_cum_perf, ax=ax)
if ctr == 1:
self._plot_drawdown(self.rolling_max_drawdown, self.rolling_bm_max_drawdown, ax=ax)
ctr += 1
#fig.subplots_adjust(top=1.2)
plt.tight_layout()
plt.show()
def _plot_equity(self, ts, bm_ts, ax=None):
def two_dec_format(x, pos):
return "{:.0%}".format(x)
equity = ts
if ax is None:
ax = plt.gca()
y_axis_formatter = FuncFormatter(two_dec_format)
ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))
ax.xaxis.set_tick_params(reset=True)
ax.yaxis.grid(linestyle=':')
ax.xaxis.set_major_locator(mdates.YearLocator(1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
ax.xaxis.grid(linestyle=':')
if bm_ts is not None:
benchmark = bm_ts
benchmark.plot(lw=2, color='grey', label="Benchmark", alpha=0.60, ax=ax)
equity.plot(lw=2, color='xkcd:azure', alpha=0.6, x_compat=False, label='Strategy', ax=ax)
ax.set_ylabel('Cumulative returns')
ax.legend(loc='best')
ax.set_xlabel('')
plt.setp(ax.get_xticklabels(), visible=True, rotation=0, ha='center')
return ax
def _plot_drawdown(self, ts, bm_ts, ax=None):
"""
Plots the underwater curve
"""
def two_dec_format(x, pos):
return "{:.0%}".format(x)
equity = ts
if ax is None:
ax = plt.gca()
y_axis_formatter = FuncFormatter(two_dec_format)
ax.yaxis.set_major_formatter(FuncFormatter(y_axis_formatter))
ax.xaxis.set_tick_params(reset=True)
ax.yaxis.grid(linestyle=':')
ax.xaxis.set_major_locator(mdates.YearLocator(1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
ax.xaxis.grid(linestyle=':')
if bm_ts is not None:
benchmark = bm_ts
benchmark.plot(kind='area', lw=2, color='grey', label="Benchmark", alpha=0.60, ax=ax)
equity.plot(kind='area', lw=2, color='xkcd:azure', alpha=0.6, x_compat=False, label='Strategy', ax=ax)
ax.set_ylabel('Drawdown')
ax.legend(loc='best')
ax.set_xlabel('')
plt.setp(ax.get_xticklabels(), visible=True, rotation=0, ha='center')
return ax
class model_runner(object):
def __init__(self, stock_tickers, model_filepath, start_date=dt.datetime(2017,3,31), end_date=dt.datetime.today()):
"""
Convenience class to run pytrader predictions
Parameters
----------
stock_tickers: single ticker or list of tickers for use with Yahoo! Finance data
start_date: data start date, default 31 March 2017
end_date: data end date, default today.
"""
self.trader = trader("MODEL_RUNNER")
self.tickers = stock_tickers
self.clf = self.load_model(model_filepath)
self.trader.load_scaler()
self.start_date = start_date
self.end_date = end_date
self.data_start_date = self.start_date - (120 * BDay()) # start 6 months prior
self._session = requests_cache.CachedSession(cache_name='cache',
backend=None, expire_after=30)
self.score_df = None
def run(self):
scores = []
for ticker in self.tickers:
try:
self.trader.price_data = web.DataReader(ticker,
'yahoo',
start=self.data_start_date,
end=self.end_date,
session=self._session)
self.trader.price_data = self.trader._reindex_to_business_days(self.trader.price_data)
#self.trader.price_data = self._get_local_data(instrument)
if self.trader.price_data is not None:
self.trader.create_multi_period_features_and_labels([5,10,20,60,120])
self.trader.X_test = self.trader.features
self.trader.y_test = self.trader.labels
self.trader.X_train = self.trader.features
self.trader.y_train = self.trader.labels
self.trader.scale_data()
self.trader.test_predict_new(self.clf, self.trader.X_test, self.trader.y_test)
self.trader.create_strategy_from_predictions()
perf_stats = stats(self.trader)
perf_stats.calculate()
score = self.trader.save_scores(self.clf, ticker, perf_stats)
perf_stats.plot_tearsheet(ticker)
scores.append(score)
except Exception as ex:
print("[EXCEPTION]: {}, {}".format(ticker, ex))
score_df = pd.concat(scores)
self.score_df = self._get_score_diffs(score_df)
def show_scores(self):
display(self.score_df.style.format("{:.4f}"))
def _get_local_data(self, instrument):
path = './validation/{}_20171018.csv'.format(instrument)
df = self.trader._get_df_from_csv(path)
return self.trader._reindex_to_business_days(df)
def _get_data(self, ticker, start_date, end_date):
try:
df = web.DataReader(ticker, 'yahoo', start=start_date, end=end_date)
#df.set_index('Date', inplace=True, drop=True)
return self.trader._reindex_to_business_days(df)
except Exception as ex:
print("pandas_datareader failed to get data from Yahoo! Finance for ticker {}: {}".format(ticker, ex))
return None
def load_model(self, filepath):
return joblib.load(filepath)
def _get_score_diffs(self, df):
diffs = pd.DataFrame()
diffs['f1_diff'] = df.test_f1 - df.test_bm_f1
diffs['return_diff'] = df.strategy_return_ann - df.bm_return_ann
diffs['sharpe_diff'] = df.strategy_sharpe - df.bm_sharpe
diffs['drawdown_diff'] = df.strategy_max_drawdown - df.bm_max_drawdown
#display(diffs)
return diffs