forked from rkohli3/TSMOM
/
tsmom.py
1255 lines (1076 loc) · 45 KB
/
tsmom.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
import numpy as np
from pandas_datareader import data as web
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import datetime as dt
import cufflinks as cf
cf.go_offline()
from jupyterthemes import jtplot
#jtplot.style()
import arch
import statsmodels.tsa.api as smt
import matplotlib.pyplot as plt
import statsmodels.api as sm
import seaborn as sns
import pyfolio as pf
import pytz
from yahoo import YahooDailyReader
#sns.set_style('white')
import requests
import empyrical
import plotly.figure_factory as ff
import matplotlib
import YahooFinance as yf
import string
def get_adj_close(tickers,start, end, source = 'yahoo'):
"""F: to get adjusted close columns for a list of tickers using Paython's web data dreader
params
-------
tickers: list of tickers
start: `str` or `datetime` object
end: `str` or `datetime` object
source: (optional) str
takes input as yahoo, google
returns:
---------
pandas panel of Adj Close price if input is yahoo. Google has some errors with Adjustd Close
"""
panel = web.DataReader(tickers, source.lower(), start, end)
if source == 'yahoo':
table = panel['Adj Close']
elif source == 'google':
table = panel['Close']
return table.sort_index(ascending = True)
def get_yahoo_data(tickers, start = None, end = None, col = 'Adjclose'):
"""F: to get daily price data from yahoo.
params:
tickers: list of strings or string value. Is case sensitive
start: datetime isinstance, default is `None`, caclulates start date as Jan 1, 2010
end: datetime isinstance, default is `None`, gives today's datetime
col: string object or list of strings from 'Adjclose'(default), 'High', 'Low', 'Open',
'Volume', 'Dividend'
returns:
DataFrame of the `col` or multi index DataFrame of columns for `col` parameter
"""
if end is None:
end = dt.datetime.today()
if start is None:
start = dt.datetime(2010,1,1)
panel = {}
if (isinstance(tickers, list)) and (len(tickers) > 1):
high = pd.DataFrame([])
low = pd.DataFrame([])
open = pd.DataFrame([])
close = pd.DataFrame([])
volume = pd.DataFrame([])
adj_cl = pd.DataFrame([])
divs = pd.DataFrame([])
for i in tickers:
try:
data = yf.YahooDailyReader(i, start, end).read()
high[i] = data['high']
low[i] = data['low']
open[i] = data['open']
close[i] = data['close']
volume[i] = data['volume']
adj_cl[i] = data['adjclose']
divs[i] = data['dividend']
except KeyError:
print(str(i) + " is not available")
panel['High'] = high
panel['Low'] = low
panel['Open'] = open
panel['Close'] = close
panel['Volume'] = volume
panel['Adjclose'] = adj_cl
panel['Dividend'] = divs
final = pd.concat(panel, axis = 1)
if col:
return final[col]
else:
return final
elif (isinstance(tickers, list)) and (len(tickers) == 1):
tick = tickers[0]
final = yf.YahooDailyReader(tick, start, end).read()
final.columns = [string.capwords(i) for i in final.columns]
if col:
return final[col]
else:
return final
elif type(tickers) == str:
final = yf.YahooDailyReader(tickers, start, end).read()
final.columns = [string.capwords(i) for i in final.columns]
if col:
return final[col]
else:
return final
# def drawdown(df_returns, ret_type = 'log'):
# if ret_type == 'log':
# cum_returns = np.exp(df_returns.cumsum())
# elif ret_type == 'arth':
# cum_returns = (1 + df_returns).cumprod()
# draw = 1 - cum_returns.div(cum_returns.cummax())
# max_drawdown = np.max(draw)
# # print ("The maximum drawdown is:")
# # print (max_drawdown.apply(lambda x: "{0:,.2%}".format(x)) )
# return ("The maximum drawdown is: {0:,.2}%").format(max_drawdown)
def drawdown(df, data = 'returns', ret_type = 'arth', ret_ = 'text'):
if data == 'returns':
if ret_type == 'arth':
eq_line = (1 + df).cumprod()
elif ret_type == 'log':
eq_line = np.exp(df.cumsum())
if data == 'prices':
eq_line = df
draw = 1 - eq_line.div(eq_line.cummax())
max_drawdown = np.max(draw)
# if isinstance(max_drawdown, pd.core.series.Series):
# if ret_ != 'text':
# return max_drawdown.apply(lambda x: '{:,.2%}'.format(x))
if ret_ != 'text':
return max_drawdown
elif ret_ =='text':
return ("The maximum drawdown is: {0:,.2%}").format(max_drawdown)
def rolling_drawdown(df, data = 'returns', ret_type = 'arth'):
"""F: that calculates periodic drawdown.:
params:
df: takes in dataframe or pandas series
data: (optional) str, prices or returns,
ret_type: (optional) return type, log or arth"""
if data == 'returns':
if ret_type == 'arth':
eq_line = (1 + df).cumprod()
elif ret_type == 'log':
eq_line = np.exp(df.cumsum())
if data == 'prices':
eq_line = df
draw = eq_line.div(eq_line.cummax()) - 1
# max_drawdown = np.max(draw)
return draw
def cum_pfmnce(dataframe, data = 'prices'):
"""Function that caluclates the cumulative performance of panel of prices. This is similar
to cumproduct of returns ie geometric returns
Args:
dataframe: `DataFrame`
Returns:
`DataFrame` or `Panel` with cumulative performance
"""
if data == 'prices':
return dataframe.apply(lambda x: x/x[~x.isnull()][0])
elif data == 'returns':
line = dataframe.apply(lambda x: (1+x).cumprod())
return line
def get_eq_line(series, data = 'returns', ret_type = 'arth', dtime = 'monthly'):
"""Returns cumulative performance of the price/return series (hypothetical growth of $1)
params:
series: timeseries data with index as datetime
data: (optional) returns or prices str
ret_type: (optional) 'log' or 'arth'
dtime: (optional) str, 'monthly', 'daily', 'weekly'
returns:
series (cumulative performance)
"""
if (isinstance(series, pd.core.series.Series)) and (isinstance(series.index, pd.DatetimeIndex)):
pass
else:
raise NotImplementedError('Data Type not supported, should be time series')
series.dropna(inplace = True)
if data == 'returns':
rets = series
if ret_type == 'arth':
cum_rets = (1+rets).cumprod()
elif ret_type == 'log':
cum_rets = np.exp(rets.cumsum())
if dtime == 'daily':
cum_rets_prd = cum_rets
cum_rets_prd.iloc[0] = 1
elif dtime == 'monthly':
cum_rets_prd = cum_rets.resample('BM').last().ffill()
cum_rets_prd.iloc[0] = 1
elif dtime == 'weekly':
cum_rets_prd = cum_rets.resample('W-Fri').last().ffill()
cum_rets_prd.iloc[0] = 1
elif data == 'prices':
cum_rets = series/series[~series.isnull()][0]
if dtime == 'daily':
cum_rets_prd = cum_rets
elif dtime == 'monthly':
cum_rets_prd = cum_rets.resample('BM').last().ffill()
elif dtime == 'weekly':
cum_rets_prd = cum_rets.resample('W-Fri').last().ffill()
return cum_rets_prd
def get_exante_vol(series, alpha = 0.05, com = 60, dtime = 'monthly', dtype = 'returns'):
"""F: that provides annualized ex ante volatility based on the method of Exponentially Weighted Average\n
This method is also know as the Risk Metrics, where the instantaneous volatility is based on past volatility\n
with some decay
params:
-------
series: pandas series
com: center of mass (optional) (int)
dtime: str, (optional), 'monthly', 'daily', 'weekly'
returns:
ex-ante volatility with time index"""
if (isinstance(series, pd.core.series.Series)) and (isinstance(series.index, pd.DatetimeIndex)):
pass
else:
raise NotImplementedError('Data Type not supported, should only be timeseries')
if dtype == 'prices':
series = get_rets(series, kind = 'arth', freq = 'd')
vol = series.ewm(alpha = alpha, com = com).std()
ann_vol = vol * np.sqrt(261)
if dtime == 'daily':
ann_vol_prd = ann_vol
elif dtime == 'monthly':
ann_vol_prd = ann_vol.resample('BM').last().ffill()
elif dtime == 'weekly':
ann_vol_prd = ann_vol.resample('W-Fri').last().ffill()
return ann_vol_prd
def cnvert_daily_to(index, cnvrt_to = 'm'):
"""F: to convert a daily time series to monthly, weekly, quarterly, annually. Note this is not same as
resameple, as resample, take last, first, or middle values, even if they are not in the series.
This function takes the dates witnessed empirically
params:
--------
index: datetime index
cnvrt_to: 'str' (optional), currenty supported, 'daily', 'monthyl', 'quarterly', 'annually'
returns:
---------
index with the freq as mentioned"""
cnvrt_to = cnvrt_to.lower()
t_day_index = pd.DatetimeIndex(sorted(index))
t_years = t_day_index.groupby(t_day_index.year)
f_date = t_day_index[0]
ann_dt = [f_date]
qrter_dt = [f_date]
mnthly_dt = [f_date]
weekly_dt = [f_date]
for yr in t_years.keys():
yr_end = pd.DatetimeIndex(t_years[yr]).groupby(pd.DatetimeIndex(t_years[yr]).month)
qrter_end = pd.DatetimeIndex(t_years[yr]).groupby(pd.DatetimeIndex(t_years[yr]).quarter)
week_end = pd.DatetimeIndex(t_years[yr]).groupby(pd.DatetimeIndex(t_years[yr]).week)
ann_dt.append(max(yr_end[max(yr_end)]))
for q in qrter_end.keys():
qrter_dt.append(max(qrter_end[q]))
for m in yr_end.keys():
mnthly_dt.append(max(yr_end[m]))
for w, val in week_end.items():
weekly_dt.append(max(val))
if (cnvrt_to == 'monthly')| (cnvrt_to == 'm'):
return mnthly_dt
elif (cnvrt_to == 'quarterly')|(cnvrt_to == 'q'):
return qrter_dt
elif (cnvrt_to == 'annually')|(cnvrt_to == 'a'):
return ann_dt
elif (cnvrt_to == 'weekly')|(cnvrt_to == 'w'):
return weekly_dt
elif (cnvrt_to == 'daily')|(cnvrt_to == 'd'):
return index
def get_ytd(table, year = 2017):
"""Function to calculate year to date performance:
params:
--------
table: pd.series or dataframe:
year: (optional) int
"""
this_year = dt.date.today().year
grouped = table.index.groupby(table.index.year)
#frst_day = min(grouped[this_year])
index = grouped[this_year]
pct = (table.loc[index].iloc[-1]/table.loc[grouped[year]].iloc[0]) - 1
# return (pct.apply(lambda x: "{0:,.3f}".format(x*100)))
return pct#.apply(lambda x: "{0:,.3f}".format(x))
def get_rets(data, kind = 'arth', freq = 'm', shift = 1):
"""Function to get returns from a Timeseries (NDFrame or Series)
params:
data: `Dataframe` or `Series` daily EOD prices
kind: (str) 'log'(default) or arth
freq: (str) 'd' (default), 'w', 'm'
returns:
dataframe or Series"""
if (isinstance(data, pd.core.series.Series)) or (isinstance(data, pd.core.frame.DataFrame)):
if freq == 'm':
data_prd = data.resample('BM').last().ffill()
elif freq == 'd':
data_prd = data
elif freq == 'w':
data_prd = data.resample('W-Fri').last().ffill()
if kind == 'log':
returns = (np.log(data_prd/data_prd.shift(shift)))
elif kind == 'arth':
returns = data_prd.pct_change(periods = shift)
elif (isinstance(data, np.ndarray)):
raise KeyError('Data is not a time series. Pass data with index as datetime object')
return returns
def scaled_rets(data, freq = 'm'):
"""Function to scale returns on volatilty:
params:
--------
data: time series or dataframe
freq: (optional) str, 'm', 'd', 'w'
returns:
---------
timeseries returns scaled for ex ante volatility"""
rets = get_rets(data, kind='log', freq= freq)
rf = rf.reindex(rets.index, fill = 'pad')
cond_vol = rets.apply(lambda x: get_inst_vol(x, annualize= freq))
scal_rets = rets/cond_vol.shift(-1)
scal_rets.iloc[-1, :] = rets.mean()/rets.std()
return scal_rets
def get_excess_rets(data, freq = 'd', kind = 'arth', shift = 1, data_type = 'returns'):
"""Function to calculate excess returns from prices or returns:
params:
--------
data: timeseries(prices or returns)
freq : (optional) str, 'd', 'm', 'w'
kind : (optional) str return type 'arth' or 'log',
shift : (optional) `int` period shift1,
data_type : (optional) `str` 'returns' or 'prices'
returns:
excess returns ie R(t) - RF(t)"""
if data_type == 'returns':
rets = data
rets = get_rets(data, kind = kind, freq = freq)
start_date = rets.index[0]
if freq == 'm':
rets.index = rets.index.to_period(freq)
if isinstance(rets, pd.core.frame.DataFrame):
rets = rets.iloc[1:,:]
elif isinstance(rets, pd.core.series.Series):
rets = rets.iloc[1:]
if freq == 'd':
rf = (web.DataReader("F-F_Research_Data_Factors_daily",
"famafrench",
start= start_date)[0]['RF'])/100
elif freq == 'w':
rf =(web.DataReader("F-F_Research_Data_Factors_weekly",
"famafrench",
start= start_date)[0]['RF'])/100
elif freq == 'm':
rf = (web.DataReader("F-F_Research_Data_Factors",
"famafrench",
start= start_date)[0]['RF'])/100
rf = rf.reindex(rets.index, method = 'pad')
ex_rets = rets.sub(rf, axis = 0)
return ex_rets
def get_inst_vol(y,
annualize,
x = None,
mean = 'Constant',
vol = 'Garch',
dist = 'normal',
data = 'prices',
freq = 'd',
):
"""Fn: to calculate conditional volatility of an array using Garch:
params
--------------
y : {numpy array, series, None}
endogenous array of returns
x : {numpy array, series, None}
exogneous
mean : str, optional
Name of the mean model. Currently supported options are: 'Constant',
'Zero', 'ARX' and 'HARX'
vol : str, optional
model, currently supported, 'GARCH' (default), 'EGARCH', 'ARCH' and 'HARCH'
dist : str, optional
'normal' (default), 't', 'ged'
returns
----------
series of conditioanl volatility.
"""
if (data == 'prices') or (data =='price'):
y = get_rets(y, kind = 'arth', freq = freq)
if isinstance(y, pd.core.series.Series):
## remove nan.
y = y.dropna()
else:
raise TypeError('Data should be time series with index as DateTime')
# provide a model
model = arch.arch_model(y * 100, mean = 'constant', vol = 'Garch')
# fit the model
res = model.fit(update_freq= 5)
# get the parameters. Here [1] means number of lags. This is only Garch(1,1)
omega = res.params['omega']
alpha = res.params['alpha[1]']
beta = res.params['beta[1]']
inst_vol = res.conditional_volatility * np.sqrt(252)
if isinstance(inst_vol, pd.core.series.Series):
inst_vol.name = y.name
elif isinstance(inst_vol, np.ndarray):
inst_vol = inst_vol
# more interested in conditional vol
if annualize.lower() == 'd':
ann_cond_vol = res.conditional_volatility * np.sqrt(252)
elif annualize.lower() == 'm':
ann_cond_vol = res.conditional_volatility * np.sqrt(12)
elif annualize.lower() == 'w':
ann_cond_vol = res.conditional_volatility * np.sqrt(52)
return ann_cond_vol * 0.01
def get_lagged_params(y, param = 't', nlags = 24, name = None):
"""Function to calculate lagged parameters of a linear regression:
params:
--------
y: series or numpy array
param: (optiona) `str` parameter to show, either 't' or 'b'
nlags: (optional) `int`
name: None (optional) name of the series
returns:
----------
`pd.series` of lagged params with index as number of lags"""
if isinstance(y, pd.core.series.Series):
y = y
elif isinstance(y, np.ndarray):
y = pd.Series(y)
y.fillna(method = 'pad', inplace = True)
y.dropna(inplace = True)
if len(y) > nlags:
t_stats = {}
betas = {}
for lag in range(1, nlags + 1):
reg = sm.OLS(y.iloc[lag:], y.shift(lag).dropna()).fit()
if param == 't':
t_stats[lag] = reg.tvalues[0]
elif param == 'b':
t_stats[lag] = reg.params[0]
t_vals = pd.Series(t_stats)
t_vals.name = name
else:
raise KeyError('Not enough datapoints for lags')
return pd.Series(t_vals)
# def get_lagged_betas(y, nlags = 24)
def autocorr(x, t=1):
if isinstance(x, np.ndarray):
return np.corrcoef(x[t:], x[:-t])
elif isinstance(x, pd.core.series.Series):
return np.corrcoef(x.iloc[t:], x.shift(t).dropna())
def get_tseries_autocor(series, nlags = 40):
"""F: to calculate autocorrelations of a time series
params:
series: numpy array or series
nlags: number of lags
returns:
autocorrelation"""
if isinstance(series, pd.core.frame.DataFrame):
raise TypeError('Must be 1-d araay')
elif isinstance(series, np.ndarray):
series = series[~np.isnan(series)]
elif isinstance(series, pd.core.series.Series):
series.dropna(inplace = True)
name = series.name
auto_cor = {}
for i in range(1, nlags + 1):
auto_cor[i] = autocorr(series, i)[0, 1]
auto = pd.Series(auto_cor, name= name)
return auto
# get_tseries_autocor(logrets['SPY']).plot.bar()
def tsmom(series, mnth_vol, mnth_cum, tolerance = 0, vol_flag = False, scale = 0.4, lookback = 12):
"""Function to calculate Time Series Momentum returns on a time series.
params:
series: used for name purpose only, provide a series with the name of the ticker
tolerance: (optional) -1 < x < 1, for signal processing, x < 0 is loss thus short the asst and vice-versa
vol_flag: (optional) Boolean default is False,
scale: (optional) volatility scaling parameter
lookback: (optional) int, lookback months
returns:
new_longs, new_shorts and leverage"""
ast = series.name
df = pd.concat([mnth_vol[ast], mnth_cum[ast], mnth_cum[ast].pct_change(lookback)],
axis = 1,
keys = ([ast + '_vol', ast + '_cum', ast + '_lookback']))
cum_col = df[ast + '_cum']
vol_col = df[ast + '_vol']
lback = df[ast + '_lookback']
# n_longs = []
# n_shorts = []
pnl_long = {pd.Timestamp(lback.index[lookback]): 0}
pnl_short = {pd.Timestamp(lback.index[lookback]): 0}
lev_dict = {pd.Timestamp(lback.index[lookback]): 1}
for k, v in enumerate(lback):
if k <= lookback:
continue
if vol_flag == True:
leverage = (scale/vol_col[k-1])
if lback.iloc[k-1] > tolerance:
pnl_long[lback.index[k]] = ((cum_col.iloc[k]/float(cum_col.iloc[k-1])) - 1) * leverage
lev_dict[lback.index[k]] = leverage
elif lback.iloc[k-1] < tolerance:
pnl_short[lback.index[k]] = ((cum_col.iloc[k-1]/float(cum_col.iloc[k])) - 1) * leverage
lev_dict[lback.index[k]] = leverage
elif vol_flag == False:
leverage = 1
if lback.iloc[k-1] > tolerance:
pnl_long[lback.index[k]] = ((cum_col.iloc[k]/float(cum_col.iloc[k-1])) - 1)
lev_dict[lback.index[k]] = leverage
elif lback.iloc[k-1] < tolerance:
pnl_short[lback.index[k]] = ((cum_col.iloc[k-1]/float(cum_col.iloc[k])) - 1)
lev_dict[lback.index[k]] = leverage
new_lev = pd.Series(lev_dict)
new_longs = pd.Series(pnl_long)
new_shorts = pd.Series(pnl_short)
new_longs.name = ast
new_shorts.name = ast
new_lev.name = ast + 'Leverage'
return new_longs, new_shorts, new_lev
def get_long_short(mnth_cum, lookback = 12):
lback_ret = mnth_cum.pct_change(lookback)
lback_ret = lback_ret.dropna(how = 'all')
nlongs = lback_ret[lback_ret > 0].count(axis = 1)
nshorts = lback_ret[lback_ret < 0].count(axis = 1)
nlongs.name = 'Long Positions'
nshorts.name = 'Short Positions'
nshorts.index.name = None
nshorts.index.name = None
return pd.concat([nlongs, nshorts], axis = 1)
def get_stats(returns, dtime = 'monthly'):
"""Function to calulcte annualized mean, annualized volatility and annualized sharpe ratio
params:
returns: series or dataframe of retunrs
dtime: (optional) 'monthly' or 'daily'
returns:
tuple of stats(mean, std and sharpe)"""
if (isinstance(returns, pd.core.series.Series)) | (isinstance(returns, pd.core.frame.DataFrame)):
mean = returns.mean()
std = returns.std()
else:
try:
mean = np.mean(returns)
std = np.std(returns)
except:
raise TypeError
if dtime == 'monthly':
mean = mean * 12
std = std * np.sqrt(12)
elif dtime == 'daily':
mean = mean * 252
std = std * np.sqrt(252)
sr = mean/std
return (mean, std, sr)
def get_ts(df):
df_ts = {}
for i in df:
df_ts[i] = ((get_lagged_params(df.loc[:, i], nlags = 48)))
df_ts_df = (pd.DataFrame(df_ts))
return df_ts_df
def get_tsmom(mnth_vol, mnth_cum, flag = False, scale = 0.20, lookback = 12):
total = mnth_cum.apply(lambda x: tsmom(x, mnth_vol, mnth_cum, scale = scale, vol_flag= flag, lookback= lookback))
pnl_long = pd.concat([i[0] for i in total], axis = 1)
pnl_short = pd.concat([i[1] for i in total], axis = 1)
lev = pd.concat([i[2] for i in total], axis = 1)
port_long = pnl_long.mean(axis = 1)
port_short = pnl_short.mean(axis = 1)
if flag == True:
port_long.name = 'LongPnl VolScale'
port_short.name = 'ShortPnl VolScale'
port_long.name = 'LongPnl'
port_short.name = 'ShortPnl'
n_longs = pnl_long.count(axis = 1)
n_shorts = pnl_short.count(axis = 1)
# strat_df = port_pnl.to_frame
lev_mean = lev.mean(axis =1)
lev_mean = lev_mean.rolling(lookback).mean()
lev_mean.name = 'Leverage'
return port_long, port_short, lev_mean
def get_tsmom_port(mnth_vol, mnth_cum, flag = False, scale = 0.2, lookback = 12):
port_long, port_short, leverage = get_tsmom(mnth_vol,
mnth_cum,
flag = flag,
scale = scale,
lookback = lookback)
tsmom = port_long.add(port_short, fill_value = 0)
if flag == True:
tsmom.name = 'TSMOM VolScale'
elif flag == False:
tsmom.name = 'TSMOM'
return pd.concat([tsmom, leverage], axis = 1)
# empyrical.alpha(port_pnl, bnchmark, period = 'monthly')
def get_perf_att(series, bnchmark, rf = 0.03/12, freq = 'monthly'):
"""F: that provides performance statistic of the returns
params
-------
series: daily or monthly returns
returns:
dataframe of Strategy name and statistics"""
port_mean, port_std, port_sr = (get_stats(series, dtime = freq))
perf = pd.Series({'Annualized_Mean' : '{:,.2f}'.format(round(port_mean, 3)),
'Annualized_Volatility': round(port_std, 3),
'Sharpe Ratio' : round(port_sr, 3),
'Calmar Ratio' : round(empyrical.calmar_ratio(series,
period = freq),
3),
'Alpha' : round(empyrical.alpha(series,
bnchmark,
risk_free = rf,
period = freq),
3),
'Beta': round(empyrical.beta(series,
bnchmark),
3),
'Max Drawdown': '{:,.2%}'.format(drawdown(series, ret_ = 'nottext')),
'Sortino Ratio': round(empyrical.sortino_ratio(series,
required_return= rf,
period = freq
),
3),
},
)
perf.name = series.name
return perf.to_frame()
##def matplotlib_to_plotly(cmap):
## """Converts a matplotlib colormap to plotly colormap or colorscale, which is customized
##
## params:
## cmap: str, valid cmap in matplotlib"""
##
## pl_entries = 255
## _cmap = matplotlib.cm.get_cmap(cmap)
## h = 1/(pl_entries-1)
## pl_colorscale = []
##
## for k in range(pl_entries):
## C = list(map(np.uint8, np.array(_cmap(k*h)[:3])*(pl_entries)))
## pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
##
## return pl_colorscale
def matplotlib_to_plotly(cmap, vmin = 0, vmax = 255):
norm = matplotlib.colors.Normalize(vmin = vmin, vmax = vmax)
"""Converts a matplotlib colormap to plotly colormap or colorscale, which is customized
params:
cmap: str, valid cmap in matplotlib"""
pl_entries = 255
_cmap = matplotlib.cm.get_cmap(cmap)
h = 1/(pl_entries-1)
pl_colorscale = []
for k in range(pl_entries):
C = list(map(np.uint8, np.array(_cmap(norm(k))[:3])*(pl_entries)))
pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
return pl_colorscale
def plt_cscale(cmap):
_cmap = matplotlib.cm.get_cmap(cmap)
norm = matplotlib.colors.Normalize(vmin = -100, vmax =100)
colorscale =[]
for i in range(255):
k = matplotlib.colors.colorConverter.to_rgb(_cmap(norm(i)))
colorscale.append(k)
return colorscale
def get_monthly_heatmap(returns,
cmap,
font_size = 10,
yr_from = None,
yr_to = None,
cnvrt = 'monthly',
width = 600,
plt_type = 'iplot',
filename = None,
colors = ['white', 'black'],
online = False,
show_scale = False,
height = 600,
vmin = 0,
vmax = 255):
"""F: to plot heatmap of monthly returns:
params:
returns: str, daily or monthly returns, ideally a series with datetime index
cmap: (optional)str, eg 'RdYlGn'
font_size: (optional) font_size of annotations
yr_from: (optional) Heatmap year from
yr_to: (optional) Heatmap year to
cnvrt = (optional) str, convert returns to
"""
cscale = matplotlib_to_plotly(cmap, vmin = vmin, vmax = vmax)
## cscale = plt_cscale(cmap)
if yr_to is None:
yr_to = returns.index[-1].year
if yr_from is None:
yr_from = returns.index[0].year
grid = empyrical.aggregate_returns(returns, convert_to = 'monthly').unstack().fillna(0).round(4) * 100
grid = grid.loc[yr_from:yr_to,:]
z = grid.as_matrix()
y = grid.index.values.tolist()
x = grid.columns.values.tolist()
z = grid.values.tolist()
z.reverse()
z_text = np.round(z, 3)
fighm = ff.create_annotated_heatmap(z,
x = x,
y= y[::-1],
annotation_text= z_text,
colorscale = cscale,
reversescale = True,
hoverinfo = "y+z",
showscale = show_scale,
font_colors= colors)
for i in range(len(fighm.layout.annotations)):
fighm.layout.annotations[i].font.size = font_size
fighm.layout.title = 'Heatmap for {0} from {1} - {2}'.format(returns.name,
y[0],
y[-1])
fighm['layout']['yaxis']['title'] = 'Years'
fighm['layout']['yaxis']['dtick'] = 3
fighm['layout']['yaxis']['tick0'] = 2
fighm['layout']['width'] = width
fighm['layout']['height'] = height
# fighm.layout.xaxis.title = 'Months'
if online == False:
if plt_type == 'iplot':
return iplot(fighm,
show_link= False,
image_width = width,
image_height= 900)
elif plt_type == 'plot':
return plot(fighm,
show_link= False,
image_width = width,
image_height= 900,
filename = filename)
elif online == True:
return py.iplot(fighm, show_link = False, filename = filename)
def get_monthly_hist(series,
height = 400,
width = 900,
plt_type = 'iplot',
filename = None):
"""F: to plot histogram of monthly returns
params:
series: monthyl or daily returns
height: (optional) int
width: (optional)
returns:
plotly iplotint"""
if (len(series) > 200) and (len(series) < 500):
nbins = int(len(series)/2)
elif len(series) < 200:
nbins = int(len(series))
else:
nbins = int(len(series)/4)
hist = series.iplot(kind = 'histogram',
colors = '#5f4a52',
vline = series.mean(),
bins = nbins,
asFigure= True,
layout_update = {'plot_bgcolor': 'white',
'paper_bgcolor': 'white',
'title': 'Monthly Returns Histogram for {}'.format(series.name),
'margin': dict(t = 40, pad = -40),
'width': width,
'height': height,
'xaxis' : dict(title = 'Returns',
showgrid = False,
showticklabels = True,
zeroline = True,
zerolinewidth = 3,
color = 'black',
range = [-0.06, 0.06],
hoverformat = '0.2%'
),
'yaxis' : dict(title = 'Frequency',
showgrid = False,
showticklabels = True,
zeroline = True,
zerolinewidth = 1,
color = 'black'
),
'shapes' : [dict(type = 'line',
x0 = series.mean(),
x1 = series.mean(),
y0 = 0,
y1 = 1,
yref = 'paper',
line = dict(dash = 'dash' + 'dot',
width = 4,
color = 'orange'),
)
],
'showlegend' : True,
'legend' : dict(x = 0.85,
y = 0.9,
bgcolor = 'white'),
})
hist.layout.xaxis.tickformat = '0.00%'
if online == False:
if plt_type == 'iplot':
return iplot(hist, show_link= False)
elif plt_type == 'plot':
return plot(hist, show_link = False, filename = filename)
elif online ==True:
py.iplot(hist, show_link = False)
def underwater(series,
spy_series = None,
s_name = None,
width = 900,
height = 400,
color = 'red',
range = None,
plt_type = 'iplot',
online = False,
filename = None):
if s_name is not None:
name = s_name
name = series.name
eqspy = (1+series).cumprod()
dd = (eqspy/eqspy.cummax() - 1) * 100
dd = dd.apply(lambda x: np.round(x, 2))
pyfig = dd.iplot(kind = 'area',
fill = 'True',
colors = color,
asFigure = True,
title = 'Underwater plot for {}'.format(name),
layout_update = {'plot_bgcolor': 'white',
'paper_bgcolor': 'white',
# 'hovermode': 'closest',
'margin': dict(t = 70,
b = 80,
l = 50,
r = 50,
pad = 0),
'width': width,
'height': height,
'xaxis' : dict(title = 'Dates',
showgrid = False,
showticklabels = True,
zeroline = True,
color = 'black',
hoverformat = '%A, %b %d %Y '
),
'yaxis' : dict(title = 'Drawdown in %',
showgrid = False,
showticklabels = True,
zeroline = True,
color = 'black',
range = range,
),
'legend' : dict(bgcolor = 'white',
x = 0.85,
y = 0.2,
font = dict(size = 9))
}