forked from MubingZhou/python_amibroker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculate_custom_metrics.py
1228 lines (1080 loc) · 60.3 KB
/
calculate_custom_metrics.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
import os
import math
import time
from tqdm import tqdm
import datetime
from bs4 import BeautifulSoup
import multiprocessing
import re
import random
import statsmodels.tsa.stattools as ts
from matplotlib import pyplot as plt
from shutil import copyfile
pd.set_option('display.max_columns', 500)
class CalculateCustomMetrics:
def __init__(self, strategy_root_path, HSI_price_path,
monte_carlo=False, run_monte_carlo=False):
# strategy_root_path should contain a list of strategies. E.g.
"""
strategy_root_path
strategy_result_1
trades.html
stats.html
...
strategy_result_2
trades.html
stats.html
...
strategy_result_3
trades.html
stats.html
...
"""
self.strategy_root_path = strategy_root_path
self.daily_pnl_path = os.path.join(self.strategy_root_path, 'daily_pnl.csv')
self.trades_path = os.path.join(self.strategy_root_path, 'trades.csv')
self.HSI_price_path = HSI_price_path
self.monte_carlo = monte_carlo # if do the Monte Carlo
self.run_monte_carlo = run_monte_carlo # suppose to substitute the above one
self.get_step1_summary = True
def generate_my_metrics(self):
"""
This serves as a comprehensive function that will put all our self-defined metrics into one sheet.
Must ensure that "daily_pnl.csv" and "trades.csv" are in place
:return:
"""
# check if 'daily_pnl.csv' exists
if not os.path.exists(os.path.join(self.strategy_root_path, 'daily_pnl.csv')):
CalculateCustomMetrics.generate_trade_csv_from_html(self.strategy_root_path)
CalculateCustomMetrics.generate_daily_pnl(os.path.join(self.strategy_root_path, 'trades.csv'),
self.HSI_price_path,
os.path.join(self.strategy_root_path, 'daily_pnl.csv')
)
daily_pnl = pd.read_csv(self.daily_pnl_path, parse_dates=['Date'])
trades = pd.read_csv(self.trades_path, parse_dates=['Date', 'Ex.Date'],
date_parser=lambda x: pd.datetime.strptime(x, '%m/%d/%Y %I:%M:%S %p'))
equity_curve = daily_pnl['PnL'].cumsum()
daily_pnl.loc[:, 'month'] = daily_pnl['Date'].apply(lambda x: x.month)
daily_pnl.loc[:, 'year'] = daily_pnl['Date'].apply(lambda x: x.year)
monthly_pnl = daily_pnl.groupby(by=['year', 'month']).sum()
k_ratio = CalculateCustomMetrics.cal_k_ratio(equity_curve)
slope = CalculateCustomMetrics.cal_slope(equity_curve)
gpr = CalculateCustomMetrics.cal_GPR(daily_pnl, method=1)
avg_hold_min = CalculateCustomMetrics.cal_avg_hold_min(trade_data=trades)
hhi_daily_10, hhi_trade_10, hhi_daily_5, hhi_trade_5 = CalculateCustomMetrics.cal_pnl_hhi(daily_pnl, trades)
_, max_dd_days = CalculateCustomMetrics.cal_mdd_n_mdd_period(equity_curve)
expectancy = CalculateCustomMetrics.cal_expectancy(trades['Profit'])
my_metrics = pd.DataFrame({'Name': [], 'Value': []})
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['K Ratio (Zephyr)'], 'Value': [k_ratio]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['Slope'], 'Value': [slope]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['GPR'], 'Value': [gpr]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['Average Hold Minutes'], 'Value': [avg_hold_min]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['HHI Trade 10'], 'Value': [hhi_trade_10]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['HHI Trade 5'], 'Value': [hhi_trade_5]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['HHI Daily 10'], 'Value': [hhi_daily_10]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['HHI Daily 5'], 'Value': [hhi_daily_5]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['Max. DD Days'], 'Value': [max_dd_days]}))
my_metrics = my_metrics.append(pd.DataFrame({'Name': ['Expectancy'], 'Value': [expectancy]}))
my_metrics.to_csv(os.path.join(self.strategy_root_path, 'my_metrics.csv'), index=None)
# CalculateCustomMetrics.cal_monte_carlo_one_strategy(
# self.strategy_root_path, mc_times=20, use_daily_pnl=True, use_trade_pnl=True)
acf_path = os.path.join(self.strategy_root_path, 'acf_pacf')
[trades_acf, trades_pacf, trades_pass_adf] = \
CalculateCustomMetrics.cal_acf_pacf(trades['Profit'], output_path=acf_path, output_file_prefix='trade')
[daily_pnl_acf, daily_pnl_pacf, daily_pnl_pass_adf] = \
CalculateCustomMetrics.cal_acf_pacf(daily_pnl['PnL'], output_path=acf_path, output_file_prefix='daily')
[monthly_pnl_acf, monthly_pnl_pacf, monthly_pnl_pass_adf] = \
CalculateCustomMetrics.cal_acf_pacf(monthly_pnl['PnL'], output_path=acf_path, output_file_prefix='monthly')
# first make a copy
html_raw_path = os.path.join(self.strategy_root_path, 'stats_raw.html')
if not os.path.exists(html_raw_path):
copyfile(os.path.join(self.strategy_root_path, 'stats.html'),
os.path.join(self.strategy_root_path, 'stats_raw.html'))
# output the result
# self-defined metrics
def get_inside_table_line(title, name, content):
return "<tr><TH TITLE='" + str(title) + "'>" + str(name) + "</TH><TD>" + str(content) + "</TD></tr>\n"
inside_table_line = '<TR><TD COLSPAN=4><hr size=1></TD></TR>\n'
inside_table_line += '<TR><TH><b>My Metrics</b></TH></TR>\n'
inside_table_line += get_inside_table_line('K Ratio (Zephyr)', 'K Ratio (Zephyr)', "{:.2f}".format(k_ratio))
inside_table_line += get_inside_table_line('Slope', 'Slope', "{:.2f}".format(slope))
inside_table_line += get_inside_table_line('Gain-to-Pain Ratio (>1.5 good)', 'GPR', "{:.2f}".format(gpr))
inside_table_line += get_inside_table_line(
'Average Hold Minutes', 'Average Hold Minutes', "{:.1f}".format(avg_hold_min))
inside_table_line += get_inside_table_line(
'Herfindahl-Hirschman Index by first 10 trades', 'HHI Trade 10', "{:.2f}".format(hhi_trade_10))
inside_table_line += get_inside_table_line(
'Herfindahl-Hirschman Index by first 5 trades', 'HHI Trade 5', "{:.2f}".format(hhi_trade_5))
inside_table_line += get_inside_table_line(
'Herfindahl-Hirschman Index by first 10 days\' pnl', 'HHI Daily 10', "{:.2f}".format(hhi_daily_10))
inside_table_line += get_inside_table_line(
'Herfindahl-Hirschman Index by first 5 days\' pnl', 'HHI Daily 5', "{:.2f}".format(hhi_daily_5))
inside_table_line += get_inside_table_line('Max Drawdown Days', 'Max. DD Days', "{:.2f}".format(max_dd_days))
inside_table_line += get_inside_table_line('Expectancy (>0.1 good)', 'Expectancy', "{:.2f}".format(expectancy))
# monte carlo results
mc_daily_path = os.path.join(self.strategy_root_path, 'MonteCarlo2', 'daily.xls')
mc_daily_data = pd.read_excel(mc_daily_path, sheet_name='All year')
mc_trade_path = os.path.join(self.strategy_root_path, 'MonteCarlo2', 'trades.xls')
mc_trade_data = pd.read_excel(mc_trade_path, sheet_name='All year')
mc_table = '</br><hr size=1>My Monte Carlo Results (Daily)</br><TABLE id="table_content">\n'
use_trade_data = False
for i in range(len(mc_daily_data.index) + 1):
mc_table += '<tr>'
for j in range(len(mc_daily_data.columns)):
if i == 0: # table head
if j == 0:
mc_table += '<td></td>'
mc_table += '<th>' + str(mc_daily_data.columns[j]) + '</th>'
else: # table body
if j == 0:
mc_table += '<th>' + mc_daily_data.index[i-1] + '(Daily)'
if mc_daily_data.index[i-1] in mc_trade_data.index:
mc_table += '</br>' + mc_daily_data.index[i-1] + '(Trade)'
use_trade_data = True
mc_table += '</th>'
mc_table += '<td>%.3f' % mc_daily_data.iloc[i-1, j]
if use_trade_data:
mc_table += '</br>%.3f' % mc_trade_data.loc[mc_daily_data.index[i-1], mc_daily_data.columns[j]]
mc_table += '</td>'
mc_table += '</tr>\n'
use_trade_data = False
mc_table += '</table>\n'
# acf & pacf
new_table_acf = '</br><hr size=2>My ACF/PACF Results</br><TABLE id="table_content">\n'
new_table_acf += '<tr><th></th><th>ADF</th>'
for i in range(len(trades_acf)):
new_table_acf += '<th>Lag' + str(i) + '</th>'
new_table_acf += '</tr>\n'
temp_dict = {
'ACF (Trades)': trades_acf, 'PACF (Trades)': trades_pacf,
'ACF (Daily)': daily_pnl_acf, 'PACF (Daily)': daily_pnl_pacf,
'ACF (Monthly)': monthly_pnl_acf, 'PACF (Monthly)': monthly_pnl_pacf,
}
pass_adf_dict = {
'ACF (Trades)': trades_pass_adf, 'PACF (Trades)': trades_pass_adf,
'ACF (Daily)': daily_pnl_pass_adf, 'PACF (Daily)': daily_pnl_pass_adf,
'ACF (Monthly)': monthly_pnl_pass_adf, 'PACF (Monthly)': monthly_pnl_pass_adf,
}
for name in ['ACF (Trades)', 'PACF (Trades)', 'ACF (Daily)', 'PACF (Daily)', 'ACF (Monthly)', 'PACF (Monthly)']:
data = temp_dict[name]
pass_adf = pass_adf_dict[name]
new_table_acf += '<tr><th>' + name + '</th><td>' + ('Y' if pass_adf else 'N') + '</td>'
for c in data:
new_table_acf += '<td>' + '{:.3f}'.format(c) + '</td>'
new_table_acf += '\n'
new_table_acf += '</table>\n'
# css style
css = """
#table_content {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#table_content td{
border: 1px solid #ddd;
padding: 2px;
}
#table_content tr{
font-size: 16px;
}
#table_content tr:nth-child(even){background-color: #f2f2f2;}
#table_content th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
border: 1px solid #ddd;
color: white;
}
"""
# insert the new content into original html
html_content = ''
with open(html_raw_path, 'r') as f:
html_content = f.read()
html_content = html_content.lower()
table_end = html_content.rfind('</table>')
new_html = html_content[0:table_end] + inside_table_line + '</table>' + \
mc_table + new_table_acf + \
html_content[table_end + len('</table>'):]
style_end = new_html.rfind('</style>')
new_html = new_html[0:style_end] + css + new_html[style_end:]
with open(os.path.join(self.strategy_root_path, 'stats.html'), 'w') as f:
f.write(new_html)
def generate_summary(self):
strategy_list = []
strategy_df = pd.DataFrame()
for s in os.listdir(self.strategy_root_path):
s_path = os.path.join(self.strategy_root_path, s)
# print(s_path)
if os.path.isdir(s_path):
strategy_list.append(s)
stats_str = ''
with open(os.path.join(s_path, 'stats.html'), 'r') as f:
stats_str = f.read().replace('\n', '')
temp = pd.read_html(stats_str)
if temp is not None and len(temp) > 1:
stats_table = temp[1]
stats_table = stats_table.drop(0, axis=0)
# print(stats_table.iloc[20, :])
stats_table.iloc[20, 0] = stats_table.iloc[20, 0] + ' Win'
stats_table.iloc[19, 0] = stats_table.iloc[19, 0] + ' Win'
stats_table.iloc[29, 0] = stats_table.iloc[29, 0] + ' Loss'
stats_table.iloc[28, 0] = stats_table.iloc[28, 0] + ' Loss'
stats_table = stats_table.set_index(0)
stats_table = stats_table.dropna()
# print(stats_table)
stats_table_new = pd.DataFrame({
s: list(stats_table.iloc[:, 0])
}, index=list(stats_table.index))
# print(stats_table.loc['Net Profit', 2])
stats_table_new.loc['Net Profit (Long)'] = [stats_table.loc['Net Profit', 2]]
# print(stats_table_new)
stats_table_new.loc['Net Profit (Short)'] = [stats_table.loc['Net Profit', 3]]
# pattern = re.compile(r"\((\d+)\)")
# print(stats_table)
winners_str = stats_table.loc['Winners', 1]
winners_str = winners_str.replace(' ', '').replace('%', '')
stats_table_new.loc['Winners No'] = [int(winners_str.split('(')[0])]
stats_table_new.loc['Winners %'] = [float(winners_str.split('(')[1].replace(')', ''))]
losers_str = stats_table.loc['Losers', 1]
losers_str = losers_str.replace(' ', '').replace('%', '')
stats_table_new.loc['Losers No'] = [int(losers_str.split('(')[0])]
stats_table_new.loc['Losers %'] = [float(losers_str.split('(')[1].replace(')', ''))]
winners_long_str = stats_table.loc['Winners', 2]
winners_long_str = winners_long_str.replace(' ', '').replace('%', '')
stats_table_new.loc['Winners No (Long)'] = [int(winners_long_str.split('(')[0])]
stats_table_new.loc['Winners % (Long)'] = [float(winners_long_str.split('(')[1].replace(')', ''))]
losers_long_str = stats_table.loc['Losers', 2]
losers_long_str = losers_long_str.replace(' ', '').replace('%', '')
stats_table_new.loc['Losers No (Long)'] = [int(losers_long_str.split('(')[0])]
stats_table_new.loc['Losers % (Long)'] = [float(losers_long_str.split('(')[1].replace(')', ''))]
winners_short_str = stats_table.loc['Winners', 3]
winners_short_str = winners_short_str.replace(' ', '').replace('%', '')
stats_table_new.loc['Winners No (Short)'] = [int(winners_short_str.split('(')[0])]
stats_table_new.loc['Winners % (Short)'] = [float(winners_short_str.split('(')[1].replace(')', ''))]
losers_short_str = stats_table.loc['Losers', 3]
losers_short_str = losers_short_str.replace(' ', '').replace('%', '')
stats_table_new.loc['Losers No (Short)'] = [int(losers_short_str.split('(')[0])]
stats_table_new.loc['Losers % (Short)'] = [float(losers_short_str.split('(')[1].replace(')', ''))]
if self.get_step1_summary:
strategy_df = strategy_df.append(stats_table_new.transpose())
continue
avg_win_dollar = float(stats_table.loc['Avg. Profit', 1])
# print(stats_table_new)
avg_loss_dollar = float(stats_table.loc['Avg. Loss', 1])
stats_table_new.loc['Expectancy'] = [
(avg_win_dollar * stats_table_new.loc['Winners %', s]/100 +
avg_loss_dollar * stats_table_new.loc['Losers %', s]/100) /
(-avg_loss_dollar)
]
daily_pnl = pd.read_csv(os.path.join(s_path, 'daily_pnl.csv'), parse_dates=['Date'])
total_pnl = daily_pnl['PnL'].sum()
# print(CalculateCustomMetrics.cal_k_ratio(daily_pnl['PnL'].cumsum()))
# win rate
weekly_winrate, monthly_winrate, yearly_winrate, weekly_pnl, monthly_pnl, yearly_pnl = \
CalculateCustomMetrics.cal_profit_calendar_matrix(daily_pnl, s_path)
daily_winrate = len(daily_pnl[daily_pnl['PnL'] > 0]) / (
len(daily_pnl[daily_pnl['PnL'] > 0]) + len(daily_pnl[daily_pnl['PnL'] < 0])
)
stats_table_new.loc['Daily WinRate'] = [daily_winrate]
stats_table_new.loc['Weekly WinRate'] = [weekly_winrate]
stats_table_new.loc['Monthly WinRate'] = [monthly_winrate]
stats_table_new.loc['Yearly WinRate'] = [yearly_winrate]
# Herfindahl-Hirschman Index (HHI)
hhi_daily_10 = 0
hhi_daily_5 = 0
if len(daily_pnl) > 10:
daily_pnl_sorted = daily_pnl['PnL'].sort_values(ascending=False) / total_pnl * 100
daily_pnl_sorted_sq = daily_pnl_sorted ** 2
hhi_daily_10 = daily_pnl_sorted_sq[0:10].sum()
hhi_daily_5 = daily_pnl_sorted_sq[0:5].sum()
stats_table_new.loc['HHI Daily 5'] = [hhi_daily_5]
stats_table_new.loc['HHI Daily 10'] = [hhi_daily_10]
trade_data = pd.read_csv(os.path.join(s_path, 'trades.csv'))
trade_pnl = trade_data['Profit']
hhi_trade_5 = hhi_trade_10 = 0
if len(trade_pnl) > 10:
trade_pnl_sorted_sq = (trade_pnl.sort_values(ascending=False) / total_pnl * 100) ** 2
hhi_trade_5 = trade_pnl_sorted_sq[0:5].sum()
hhi_trade_10 = trade_pnl_sorted_sq[0:10].sum()
stats_table_new.loc['HHI Trade 5'] = [hhi_trade_5]
stats_table_new.loc['HHI Trade 10'] = [hhi_trade_10]
# recovering period & slope
equity_curve = daily_pnl['PnL'].cumsum()
stats_table_new.loc['K Ratio'] = [CalculateCustomMetrics.cal_k_ratio(equity_curve)]
stats_table_new.loc['Max DD Period'] = [
CalculateCustomMetrics.cal_mdd_n_mdd_period(equity_curve)[1]]
stats_table_new.loc['Slope'] = [CalculateCustomMetrics.cal_slope(equity_curve)]
# monte carlo
if self.monte_carlo:
mc_data = pd.read_excel(
os.path.join(s_path, 'MonteCarlo', 'MonteCarloResultResultAllYear.xlsx'),
sheet_name='Summary')
index_list = ['CAR / MDD', 'MDD', 'Max Pool', 'Min Pool', 'End Pool']
col_list = ['90%', '70%', '50%', '30%', '10%']
for i in index_list:
for j in col_list:
stats_table_new.loc[i + '_' + j] = [mc_data.loc[i, j]]
if self.run_monte_carlo:
# print(monthly_pnl)
# print(isinstance(monthly_pnl, pd.Series))
res = CalculateCustomMetrics.cal_monte_carlo(monthly_pnl, freq='monthly')
stats_table_new.loc['MC_Monthly_MDD_90%'] = res[0][0]
stats_table_new.loc['MC_Monthly_MDD_Period_90%'] = res[0][1]
stats_table_new.loc['MC_Monthly_CAR_MDD_90%'] = res[0][4]
stats_table_new.loc['MC_Monthly_Max_Equity_90%'] = res[0][2]
stats_table_new.loc['MC_Monthly_Min_Equity_90%'] = res[0][3]
# print(stats_table.loc['Net Profit', 2])
# time.sleep(1000000)
strategy_df = strategy_df.append(stats_table_new.transpose())
strategy_df = strategy_df.rename({'Total Profit': 'Profit (Winners)',
'Total Loss': 'Profit (Losers)',
'All trades': 'Trade No.', 'CAR/MaxDD': 'CAR / MDD'}, axis=1)
# print(strategy_df)
# to_show_list = ['CAR/MaxDD', 'Net Profit', 'All trades', 'Avg. Bars Held', 'Winners', 'Avg. Profit',
# 'Losers', 'Avg. Loss', 'Max. system drawdown', 'Max. trade drawdown']
to_show_list = ['Net Profit', 'Net Profit (Long)', 'Net Profit (Short)',
'CAR / MDD', 'Max. system drawdown', 'Max DD Period', 'Trade No.', 'Avg. Profit/Loss',
'Daily WinRate', 'Weekly WinRate', 'Monthly WinRate', 'Yearly WinRate',
'Winners %', 'Losers %', 'Winners % (Long)', 'Losers % (Long)',
'Winners % (Short)', 'Losers % (Short)',
'Profit Factor', 'Avg. Bars Held',
'Expectancy', 'K Ratio', 'HHI Daily 5', 'HHI Daily 10', 'HHI Trade 5', 'HHI Trade 10'
]
if self.monte_carlo:
to_show_list.extend([
'CAR / MDD_90%', 'MDD_90%', 'Max Pool_90%', 'Min Pool_90%', 'End Pool_90%',
'CAR / MDD_70%', 'MDD_70%', 'Max Pool_70%', 'Min Pool_70%', 'End Pool_70%',
'CAR / MDD_50%', 'MDD_50%', 'Max Pool_50%', 'Min Pool_50%', 'End Pool_50%',
'CAR / MDD_30%', 'MDD_30%', 'Max Pool_30%', 'Min Pool_30%', 'End Pool_30%',
'CAR / MDD_10%', 'MDD_10%', 'Max Pool_10%', 'Min Pool_10%', 'End Pool_10%'
])
if self.run_monte_carlo:
to_show_list.extend([
'MC_Monthly_MDD_90%', 'MC_Monthly_MDD_Period_90%',
'MC_Monthly_CAR_MDD_90%', 'MC_Monthly_Max_Equity_90%', 'MC_Monthly_Min_Equity_90%'
])
if self.get_step1_summary:
to_show_list = ['Net Profit', 'Net Profit (Long)', 'Net Profit (Short)', 'Trade No.',
'Avg. Profit/Loss', 'Winners %', 'Profit Factor', 'CAR / MDD',
'Max. system drawdown', 'Avg. Bars Held', 'Sharpe Ratio of trades'
]
# print(list(strategy_df.columns))
# rest_list = [x for x in list(strategy_df.columns) if x not in to_show_list]
# to_show_list.extend(rest_list)
strategy_df = strategy_df[to_show_list]
strategy_df.index.name = 'Name'
strategy_df.to_csv(os.path.join(self.strategy_root_path, 'Summary.csv'))
def generate_trade_csv_strategy_root(self):
for s in tqdm(os.listdir(self.strategy_root_path)):
s_path = os.path.join(self.strategy_root_path, s)
print(s_path)
if os.path.isdir(s_path):
CalculateCustomMetrics.generate_trade_csv_from_html(s_path)
@staticmethod
def generate_trade_csv_from_html(Path):
pathOpen = open(os.path.join(Path, "trades.html"), 'r')
s = pathOpen.read()
pathOpen.close()
testtest = BeautifulSoup(s, "lxml")
x = 0
data_list = []
for tr in testtest.find_all('tr'):
if x >= 2:
tds = tr.find_all('td')
abc = tr.find_all('br')
data_list.append({
'Symbol': tds[0].contents[0],
'Trade': tds[1].contents[0],
'Date': tds[2].contents[0],
'Price': ''.join(abc[0].next_siblings),
'Ex.Date': tds[3].contents[0],
'Ex. Price': ''.join(abc[1].next_siblings),
'% chg': tds[4].contents[0],
'Profit': tds[5].contents[0],
'% Profit': ''.join(abc[2].next_siblings),
'Shares': tds[6].contents[0],
'Position value': tds[7].contents[0],
'Cum.profit': tds[8].contents[0],
'# bars': tds[9].contents[0],
'Profit/bar': tds[10].contents[0],
'MAE': tds[11].contents[0],
'MFE': ''.join(abc[3].next_siblings),
'Scale In/Out': tds[12].contents[0]
})
# print(tds[2].contents[0])
x += 1
if len(data_list) == 0:
efg = pd.DataFrame({
'Symbol': [], 'Trade': [], 'Date': [], 'Price': [], 'Ex.Date': [],
'Ex. Price': [], '% chg': [], 'Profit': [], '% Profit': [], 'Shares': [],
'Position value': [], 'Cum.profit': [], '# bars': [],
'Profit/bar': [], 'MAE': [], 'MFE': [], 'Scale In/Out': []
})
else:
efg = pd.DataFrame(data_list)
def recog_date_format(date_str, date_format):
if '00:00:00' in date_str:
date_format = date_format.replace(' %p', '').replace('%I', '%H')
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return 1
except Exception:
return 0
def convert_date_format(date_str, date_format):
if '00:00:00' in date_str:
date_format = date_format.replace(' %p', '').replace('%I', '%H')
# print(date_str)
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date.strftime('%m/%d/%Y %I:%M:%S %p')
potential_date_format = ['%m/%d/%Y %I:%M:%S %p', '%m/%d/%Y %H:%M', '%m/%d/%Y %H:%M:%S',
'%d/%m/%Y %H:%M:%S', '%d/%m/%Y %I:%M:%S %p', '%d/%m/%Y %H:%M']
for date_format in potential_date_format:
res = efg['Date'].apply(recog_date_format, args=(date_format,))
if res.sum() == len(efg):
efg.loc[:, 'Date'] = efg['Date'].apply(convert_date_format, args=(date_format,))
efg.loc[:, 'Ex.Date'] = efg['Ex.Date'].apply(convert_date_format, args=(date_format,))
continue
efg = efg[['Symbol', 'Trade', 'Date', 'Price', 'Ex.Date', 'Ex. Price', '% chg', 'Profit', '% Profit'
, 'Shares', 'Position value', 'Cum.profit', '# bars', 'Profit/bar', 'MAE', 'MFE', 'Scale In/Out']]
efg.to_csv(Path + "\\trades.csv", index=None)
def generate_daily_pnl_strategy_root(self):
for s in tqdm(os.listdir(self.strategy_root_path)):
s_path = os.path.join(self.strategy_root_path, s)
# print(s_path)
if os.path.isdir(s_path):
trade_file_path = os.path.join(s_path, 'trades.csv')
price_data_path = self.HSI_price_path
output_path = os.path.join(s_path, 'daily_pnl.csv')
CalculateCustomMetrics.generate_daily_pnl(trade_file_path, price_data_path, output_path)
@staticmethod
def generate_daily_pnl(trade_file_path, price_data_path, output_path):
"""
This function will first read a file containing all trades (this file comes from the html produced by AmiBroker)
Then it will compute the day-end pnl each day (daily close data should be provided via price_data_path)
It will finally produce a list of daily pnl.
"""
trade_data = pd.read_csv(trade_file_path)
# print(trade_data.columns)
# deal with date & time
# print(trade_data)
def get_datetime(date_str):
date_format = '%m/%d/%Y %I:%M:%S %p'
parsed_date = pd.Timestamp(2000, 1, 1)
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
date_format = '%m/%d/%Y %H:%M'
parsed_date = pd.Timestamp(2000, 1, 1)
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
date_format = '%d/%m/%Y %I:%M:%S %p'
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
date_format = '%m/%d/%Y %H:%M:%S'
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
date_format = '%d/%m/%Y %H:%M:%S'
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
date_format = '%d/%m/%Y %I:%M:%S %p'
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
date_format = '%Y-%m-%d %H:%M:%S'
try:
parsed_date = pd.Timestamp(
datetime.datetime.strptime(date_str, date_format)
)
return parsed_date
except Exception:
pass
print("%s can't be converted!" % date_str)
def judge_direction(row):
if 'LONG' in row['Trade'].upper():
return 1
if 'SHORT' in row['Trade'].upper():
return -1
trade_data['Date'] = trade_data.apply(lambda x: get_datetime(x['Date']), axis=1)
trade_data = trade_data.rename({'Ex. Date': 'Ex.Date'}, axis=1)
# print(trade_data)
trade_data['Ex.Date'] = trade_data.apply(lambda x: get_datetime(x['Ex.Date']), axis=1)
trade_data['Trade'] = trade_data.apply(judge_direction, axis=1)
trade_data = trade_data.sort_values(by=['Date'], ascending=True)
trade_data = trade_data[['Trade', 'Date', 'Price', 'Ex.Date', 'Ex. Price', 'Profit', 'Shares']]
# print(trade_data)
# read index day-end data
index_data = pd.read_csv(price_data_path,
parse_dates=['Dates'],
date_parser=lambda x: pd.datetime.strptime(x, '%d/%m/%Y'))
index_data = index_data.sort_values(by=['Dates'], ascending=True)
index_data = index_data.rename({'Dates': 'Date'}, axis=1)
# index_data will have two columns: Date, PX_LAST
# extract relevant data
start_date = pd.Timestamp(trade_data['Date'][0].date()) # Timestamp('2015-01-06 00:00:00')
end_date = pd.Timestamp(trade_data['Ex.Date'].iloc[-1].date())
pnl_data = pd.DataFrame({'Date': [], 'PnL': []})
IMAGINARY_TRADES = pd.DataFrame({'Symbol':[], 'Trade':[], 'Date':[], 'Price': [],
'Ex.Date': [], 'Ex.Price':[], 'Profit':[]})
for d in index_data['Date']:
if d < start_date or d > end_date:
continue
yest_close = index_data.iloc[list(index_data[index_data['Date'] == d].index)[0] - 1][1]
yest = index_data.iloc[list(index_data[index_data['Date'] == d].index)[0] - 1][0]
today_close = list(index_data[index_data['Date'] == d]['PX_LAST'])[0]
# print('Today date = %s, yest=%s, yest_close=%.1f, today_close=%.1f' % (d, yest, yest_close, today_close))
# e.g. d = 2018-09-16
day_end = pd.Timestamp(
datetime.datetime.strptime(
d.date().strftime('%Y-%m-%d') + " 16:30:01", '%Y-%m-%d %H:%M:%S'
)) # e.g. 2018-09-16 23:59:59
day_start = pd.Timestamp(
datetime.datetime.strptime(
yest.date().strftime('%Y-%m-%d') + " 16:30:01", '%Y-%m-%d %H:%M:%S'
)) # e.g. 2018-09-16 00:00:00
# print('day_start=%s, day_end=%s' % (day_start, day_end))
cond1 = trade_data['Date'] <= day_end
cond2 = trade_data['Ex.Date'] >= day_start
today_trade_data = trade_data[cond1 & cond2]
# print(today_trade_data)
# time.sleep(10000)
# print('today_trade_data (before) = \n%s\n' % today_trade_data)
# find those with open date earlier than day_start and modify their open price to be yesterday's close
def update_profit(row):
nonlocal IMAGINARY_TRADES
if row['Ex.Date'] > day_end and row['Date'] < day_start: # a trade crosses multiple days
pnl = row['Trade'] * (today_close - yest_close)
return pnl * row['Shares']
elif row['Ex.Date'] > day_end:
pnl = row['Trade'] * (today_close - row['Price']) - \
0.5 * (row['Trade'] * (row['Ex. Price'] - row['Price']) - row['Profit'])
day_end_str = day_end.strftime('%Y-%m-%d %I:%M:%S %p')
rec = pd.DataFrame({'Symbol':[row['Symbol']], 'Trade':[row['Trade']], 'Date':[row['Date']], 'Price': [row['Price']],
'Ex.Date': [day_end_str], 'Ex.Price':[today_close], 'Profit':[pnl]})
IMAGINARY_TRADES = IMAGINARY_TRADES.append(rec)
return pnl * row['Shares']
elif row['Date'] < day_start:
pnl = row['Trade'] * (row['Ex. Price'] - yest_close) - \
0.5 * (row['Trade'] * (row['Ex. Price'] - row['Price']) - row['Profit'])
day_start_str = day_end.strftime('%Y-%m-%d %I:%M:%S %p')
rec = pd.DataFrame({'Symbol': [row['Symbol']], 'Trade': [row['Trade']], 'Date': [day_start_str],
'Price': [yest_close],
'Ex.Date': [row['Ex.Date']], 'Ex.Price': [today_close['Ex.Price']], 'Profit': [pnl]})
IMAGINARY_TRADES = IMAGINARY_TRADES.append(rec)
return pnl * row['Shares']
else:
return row['Profit'] * row['Shares']
# today_trade_data.loc[:, 'Profit'] = today_trade_data.apply(update_profit_from_open, axis=1)
# today_trade_data.loc[:, 'Profit'] = today_trade_data.apply(update_profit_from_close, axis=1)
today_trade_data.loc[:, 'Profit'] = today_trade_data.apply(update_profit, axis=1)
# today_trade_data['Ex. Price'] = today_trade_data.apply(assign_close_price, axis=1)
# print('today_trade_data (after) = \n%s\n' % today_trade_data)
today_pnl = today_trade_data['Profit'].sum()
pnl_data = pnl_data.append(pd.DataFrame({'Date': [d], 'PnL': [today_pnl]}))
# print('today pnl = %.1f' % today_pnl)
# print('----------------------------------------------------------------------')
# time.sleep(2)
pnl_data.to_csv(output_path, index=None)
# print(pnl_data)
# print(output_path)
@staticmethod
def equity_curve_type_check(equity_curve, msg_head=''):
if not isinstance(equity_curve, (list, pd.Series, np.ndarray)):
if isinstance(equity_curve, pd.DataFrame):
if len(equity_curve.columns) != 1:
raise TypeError(msg_head + " equity_curve should have only one column.")
else:
equity_curve = np.array(equity_curve.iloc[:, 0])
else:
raise TypeError(msg_head +
" equity_curve must be a list or pandas.Series or pandas.DataFrame with one column.")
return True
@staticmethod
def cal_profit_calendar_matrix(daily_pnl_data, current_path=None, ignore_zero_winrate=True, to_csv=True):
# pnl_data should be a pandas.DataFrame from 'daily_pnl.csv'
# pnl_data = pd.read_csv(pnl_path, parse_dates=['Date'],
# date_parser=lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
daily_pnl_data.loc[:, 'year'] = daily_pnl_data['Date'].apply(lambda x: x.year)
daily_pnl_data.loc[:, 'month'] = daily_pnl_data['Date'].apply(lambda x: x.month)
daily_pnl_data.loc[:, 'week'] = daily_pnl_data['Date'].apply(lambda x: x.week)
pnl_data_year = daily_pnl_data.groupby(by=['year'])['PnL'].sum()
pnl_data_year_month = daily_pnl_data.groupby(by=['year', 'month'])['PnL'].sum()
pnl_data_year_week = daily_pnl_data.groupby(by=['year', 'week'])['PnL'].sum()
if to_csv:
if current_path is None:
raise ValueError('[cal_profit_calendar_matrix] if to_csv=True, current_path must not be None')
pnl_data_year.to_csv(os.path.join(current_path, 'yearly_pnl.csv'))
pnl_data_year_month.to_csv(os.path.join(current_path, 'monthly_pnl.csv'))
pnl_data_year_week.to_csv(os.path.join(current_path, 'weekly_pnl.csv'))
if ignore_zero_winrate:
weekly_win_rate = pnl_data_year_week.apply(lambda x: 1 if x > 0 else 0).sum() / \
len(pnl_data_year_week[pnl_data_year_week != 0])
monthly_win_rate = pnl_data_year_month.apply(lambda x: 1 if x > 0 else 0).sum() / \
len(pnl_data_year_month[pnl_data_year_month != 0])
yearly_win_rate = pnl_data_year.apply(lambda x: 1 if x > 0 else 0).sum() / \
len(pnl_data_year[pnl_data_year != 0])
else:
weekly_win_rate = pnl_data_year_week.apply(lambda x: 1 if x > 0 else 0).sum() / len(pnl_data_year_week)
monthly_win_rate = pnl_data_year_month.apply(lambda x: 1 if x > 0 else 0).sum() / len(pnl_data_year_month)
yearly_win_rate = pnl_data_year.apply(lambda x: 1 if x > 0 else 0).sum() / len(pnl_data_year)
return weekly_win_rate, monthly_win_rate, yearly_win_rate, pnl_data_year_week, pnl_data_year_month, pnl_data_year
@staticmethod
def cal_slope(equity_curve):
msg_head = '[cal_slope]'
CalculateCustomMetrics.equity_curve_type_check(equity_curve, msg_head)
equity_curve = np.array(equity_curve)
y = equity_curve
x = np.array(list(range(len(y)))) + 1
x_bar = x.mean()
y_bar = y.mean()
v1 = ((x - x_bar) * (y - y_bar)).sum()
v2 = ((x - x_bar) ** 2).sum()
return v1 / v2
@staticmethod
def cal_k_ratio(equity_curve):
"""
This will calculate Zephyr K-Ratio (slope/s.e. of slope)
:param equity_curve: should be a list or pandas.Series or pandas.DataFrame with one one column
:return:
"""
msg_head = '[cal_k_ratio]'
CalculateCustomMetrics.equity_curve_type_check(equity_curve, msg_head)
equity_curve = np.array(equity_curve)
y = equity_curve
x = np.array(list(range(len(y)))) + 1
x_bar = x.mean()
y_bar = y.mean()
v1 = (((x - x_bar) * (y - y_bar)).sum()) ** 2
v2 = ((x - x_bar)**2).sum()
v3 = ((y - y_bar)**2).sum()
steyx_v1 = math.sqrt(
1/(len(y) - 2) * (v3 - v1 / v2)
)
steyx_v2 = math.sqrt(v2)
steyx = steyx_v1 / steyx_v2
slope = math.sqrt(v1) / v2
return slope / steyx
@staticmethod
def cal_GPR(daily_pnl, method=1):
"""
Calculate Gai-toPain Ratio ( = total sum of monthly % return / abs(total sum of monthly % loss))
method = 1 -> use points pnl instead of % pnl
:param daily_pnl:
:return:
"""
msg_head = '[cal_GPR]'
if not isinstance(daily_pnl, pd.DataFrame):
raise TypeError('%s daily_pnl must be a DataFrame.' % msg_head)
if 'Date' not in daily_pnl.columns or 'PnL' not in daily_pnl.columns:
raise ValueError('%s \"Date\" and \"PnL\" must be in the columns' % msg_head)
if method == 1:
daily_pnl.loc[:, 'month'] = daily_pnl['Date'].apply(lambda x: x.month)
daily_pnl.loc[:, 'year'] = daily_pnl['Date'].apply(lambda x: x.year)
monthly_pnl = daily_pnl.groupby(by=['year', 'month']).sum()
# print(monthly_pnl)
total_pnl = monthly_pnl['PnL'].sum()
total_loss = monthly_pnl[monthly_pnl['PnL'] < 0]['PnL'].sum() * -1
if total_loss == 0:
return 99999
else:
return total_pnl / total_loss
@staticmethod
def cal_mdd_n_mdd_period(equity_curve):
msg_head = '[cal_mdd_n_mdd_period]'
CalculateCustomMetrics.equity_curve_type_check(equity_curve, msg_head)
mdd = 0
mdd_period = 0
dd_period = 0
peak = equity_curve[0]
for x in equity_curve:
if x > peak:
peak = x
dd_period = 0
else:
dd_period += 1
if peak == 0:
peak = 1
dd = float(peak - x) / peak
if dd > mdd:
mdd = dd
if dd_period > mdd_period:
mdd_period = dd_period
return mdd, mdd_period
@staticmethod
def cal_monte_carlo(pnl, initial_equity=50000, multiplier=10, times=10000, freq='daily',
use_tqdm=False):
"""
Apply Monte Carlo method to a series of daily PnL and calculate the 90%, 70%, 50%, 30%, and 10% values for different metrics.
"""
msg_head = '[cal_monte_carlo]'
CalculateCustomMetrics.equity_curve_type_check(pnl, msg_head)
one_year_period_dict = {'daily': 356, 'quarterly': 4, 'monthly': 12, 'weekly': 52, 'trades':1000}
one_year_period = one_year_period_dict[freq.lower()]
pnl = np.array(pnl)
equity_curve_raw = pnl
MDD = []
MDD_PERIOD = []
MAX_EQUITY = []
MIN_EQUITY = []
CAR_MDD = []
END_EQUITY = []
CAR = []
INIT_EQUITY = []
MONTHLY_WINRATE = []
WEEKLY_WINRATE = []
QUARTERLY_WINRATE = []
ra = tqdm(range(times)) if use_tqdm else range(times)
for i in ra:
# print(i)
random.shuffle(pnl)
cum_pnl = pnl.cumsum()
cum_pnl = initial_equity + multiplier * cum_pnl
# print(type(cum_pnl))
mdd, mdd_period = CalculateCustomMetrics.cal_mdd_n_mdd_period(cum_pnl)
car = math.log(cum_pnl[-1] / initial_equity) / (len(pnl) / one_year_period)
MDD.append(abs(mdd))
MDD_PERIOD.append(mdd_period)
MAX_EQUITY.append(max(cum_pnl))
MIN_EQUITY.append(min(cum_pnl))
CAR_MDD.append(car / abs(mdd) if mdd > 0 else 9999)
END_EQUITY.append(cum_pnl[-1])
CAR.append(car)
INIT_EQUITY.append(initial_equity)
temp_pnl = pd.DataFrame({'pnl': pnl, 'ind': list(range(len(pnl)))})
temp_pnl.loc[:, 'week'] = temp_pnl['ind'].apply(lambda x: (x - x % 5) / 5)
temp_pnl.loc[:, 'month'] = temp_pnl['ind'].apply(lambda x: (x - x % 20) / 20)
temp_pnl.loc[:, 'quarter'] = temp_pnl['ind'].apply(lambda x: (x - x % 60) / 60)
weekly_pnl = temp_pnl.groupby(by=['week']).sum()
weekly_winrate = len(weekly_pnl[weekly_pnl['pnl'] > 0]) / len(weekly_pnl)
monthly_pnl = temp_pnl.groupby(by=['month']).sum()
monthly_winrate = len(monthly_pnl[monthly_pnl['pnl'] > 0]) / len(monthly_pnl)
quarterly_pnl = temp_pnl.groupby(by=['quarter']).sum()
quarterly_winrate = len(quarterly_pnl[quarterly_pnl['pnl'] > 0]) / len(quarterly_pnl)
WEEKLY_WINRATE.append(weekly_winrate)
MONTHLY_WINRATE.append(monthly_winrate)
QUARTERLY_WINRATE.append(quarterly_winrate)
# WEEKLY_WINRATE.append(0)
# MONTHLY_WINRATE.append(0)
pnl = equity_curve_raw
MDD = sorted(MDD) # ascending
MDD_PERIOD = sorted(MDD_PERIOD) # ascending
MAX_EQUITY = sorted(MAX_EQUITY, reverse=True) # descending
MIN_EQUITY = sorted(MIN_EQUITY, reverse=True) # descending
CAR_MDD = sorted(CAR_MDD, reverse=True) # descending
WEEKLY_WINRATE = sorted(WEEKLY_WINRATE, reverse=True) # descending
MONTHLY_WINRATE = sorted(MONTHLY_WINRATE, reverse=True) # descending
QUARTERLY_WINRATE = sorted(QUARTERLY_WINRATE, reverse=True) # descending
n_90 = int(math.floor(len(MDD)*0.9))
n_70 = int(math.floor(len(MDD)*0.7))
n_50 = int(math.floor(len(MDD)*0.5))
n_30 = int(math.floor(len(MDD)*0.3))
n_10 = int(math.floor(len(MDD)*0.1))
# print(MDD[0:10])
# print(MDD_PERIOD[0:10])
# print(MAX_EQUITY[0:10])
# print(MIN_EQUITY[0:10])
# print(CAR_MDD[0:10])
return [
[MDD[n_90], MDD_PERIOD[n_90], MAX_EQUITY[n_90], MIN_EQUITY[n_90], CAR_MDD[n_90],
CAR[n_90], END_EQUITY[n_90], INIT_EQUITY[n_90],
WEEKLY_WINRATE[n_90], MONTHLY_WINRATE[n_90], QUARTERLY_WINRATE[n_90]],
[MDD[n_70], MDD_PERIOD[n_70], MAX_EQUITY[n_70], MIN_EQUITY[n_70], CAR_MDD[n_70],
CAR[n_70], END_EQUITY[n_70], INIT_EQUITY[n_70],
WEEKLY_WINRATE[n_70], MONTHLY_WINRATE[n_70], QUARTERLY_WINRATE[n_70]],
[MDD[n_50], MDD_PERIOD[n_50], MAX_EQUITY[n_50], MIN_EQUITY[n_50], CAR_MDD[n_50],
CAR[n_50], END_EQUITY[n_50], INIT_EQUITY[n_50],
WEEKLY_WINRATE[n_50], MONTHLY_WINRATE[n_50], QUARTERLY_WINRATE[n_50]],
[MDD[n_30], MDD_PERIOD[n_30], MAX_EQUITY[n_30], MIN_EQUITY[n_30], CAR_MDD[n_30],
CAR[n_30], END_EQUITY[n_30], INIT_EQUITY[n_30],
WEEKLY_WINRATE[n_30], MONTHLY_WINRATE[n_30], QUARTERLY_WINRATE[n_30]],
[MDD[n_10], MDD_PERIOD[n_10], MAX_EQUITY[n_10], MIN_EQUITY[n_10], CAR_MDD[n_10],
CAR[n_10], END_EQUITY[n_10], INIT_EQUITY[n_10],
WEEKLY_WINRATE[n_10], MONTHLY_WINRATE[n_10], QUARTERLY_WINRATE[n_10]]
]
@staticmethod
def cal_monte_carlo_one_strategy(strategy_path, mc_times=10000, use_daily_pnl=True, use_trade_pnl=False):
# ignore_zero_winrate - if to ignore zero when calculate win rate
# must ensure that 'daily_pnl.csv' is in place
mc_root = os.path.join(strategy_path, 'MonteCarlo2')
if not os.path.exists(mc_root):
os.mkdir(mc_root)
year_list = None
daily_pnl_data = None
pnl_data_year_daily = None
if use_daily_pnl:
daily_pnl_data = pd.read_csv(os.path.join(strategy_path, 'daily_pnl.csv'), parse_dates=['Date'])
daily_pnl_data.loc[:, 'year'] = daily_pnl_data['Date'].apply(lambda x: x.year)
daily_pnl_data.loc[:, 'month'] = daily_pnl_data['Date'].apply(lambda x: x.month)
daily_pnl_data.loc[:, 'week'] = daily_pnl_data['Date'].apply(lambda x: x.week)
year_list = daily_pnl_data['Date'].apply(lambda x: x.year).unique()
year_list = np.insert(year_list, 0, 0)
pnl_data_year_daily = daily_pnl_data.groupby(by=['year', 'Date'])['PnL'].sum()
pnl_data_year_month = daily_pnl_data.groupby(by=['year', 'month'])['PnL'].sum()
pnl_data_year_week = daily_pnl_data.groupby(by=['year', 'week'])['PnL'].sum()
trade_pnl_data = None
trade_pnl_data_year_daily = None
if use_trade_pnl:
trade_pnl_data = pd.read_csv(os.path.join(strategy_path, 'trades.csv'), parse_dates=['Date'],
date_parser=lambda x: pd.datetime.strptime(x, '%m/%d/%Y %I:%M:%S %p'))
trade_pnl_data.loc[:, 'year'] = trade_pnl_data['Date'].apply(lambda x: x.year)
trade_pnl_data.loc[:, 'month'] = trade_pnl_data['Date'].apply(lambda x: x.month)
trade_pnl_data.loc[:, 'week'] = trade_pnl_data['Date'].apply(lambda x: x.week)
trade_pnl_data_year_daily = trade_pnl_data.groupby(by=['year', 'Date'])['Profit'].sum()
year_list = trade_pnl_data['Date'].apply(lambda x: x.year).unique()
year_list = np.insert(year_list, 0, 0)
pnl_data = {
'daily': pnl_data_year_daily,
'trades': trade_pnl_data_year_daily
# 'weekly': pnl_data_year_week, 'monthly': pnl_data_year_month
}
# print(trade_pnl_data_year_daily)
# method 1: e.g. weekly -> random
initial_equity = 50000
for freq, data in pnl_data.items():
if data is None or len(data) == 0:
continue
excel_writer = pd.ExcelWriter(os.path.join(mc_root, freq + '.xls'))
for year in year_list:
print('%s: year = %d' % (freq, year))
this_year_data = data.loc[(year, )] if year > 0 else data
mc_res_this_year = CalculateCustomMetrics.cal_monte_carlo(
this_year_data, freq=freq, use_tqdm=True, initial_equity=initial_equity, times=mc_times
)
mc_res_this_year = pd.DataFrame(
mc_res_this_year,
columns=['MDD', 'MDD_Period', 'Max_Equity', 'Min_Equity', 'CAR/MDD', 'CAR', 'End_Equity',
'Init_Equity', 'Weekly_Win_Rate', 'Monthly_Win_Rate', 'Quarterly_Win_Rate'],
index=['90%', '70%', '50%', '30%', '10%'])
winrate_ignore_zero = len(this_year_data[this_year_data > 0]) / (
len(this_year_data[this_year_data != 0])
)
winrate_consider_zero = len(this_year_data[this_year_data > 0]) / (len(this_year_data))
mc_res_this_year.loc[:, 'Win_Rate_Ignore0'] = winrate_ignore_zero
mc_res_this_year.loc[:, 'Win_Rate_Consider0'] = winrate_consider_zero
# mc_res_this_year.loc[:, 'Initial_equity'] = initial_equity
required_columns = ['MDD', 'MDD_Period', 'Init_Equity', 'Max_Equity', 'Min_Equity', 'End_Equity']