forked from skydownacai/Quant-Strategy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stratege.py
1288 lines (1262 loc) · 57.2 KB
/
stratege.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
default_setting = '''backtest
start: 2019-06-10 00:00:00
end: 2019-08-10 00:00:00
period: 1m
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD","balance":10000,"stocks":3}]'''
from fmz import *
import pandas as pd
from matplotlib.pylab import date2num ## 导入日期到数值一一对应的转换工具
import json
from datetime import datetime,timedelta
import matplotlib.pyplot as plt
import matplotlib
import random
from hmmlearn import hmm
from dateutil.parser import parse ## 导入转换到指定格式日期的工具
import numpy as np
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family']='sans-serif'
#解决负号'-'显示为方块的问题
matplotlib.rcParams['axes.unicode_minus'] = False
import mpl_finance as mpf
class PeriodTooLess(Exception):
def __init__(self):
pass
def TIME_STAMP(TIME,mode = 1):
'返回字符串时间戳'
if mode:
return int(time.mktime(time.strptime(TIME+' 08:00:00','%Y-%m-%d %H:%M:%S')))
else:
return int(time.mktime(time.strptime(TIME,'%Y-%m-%d %H:%M:%S')))
ALL_month = [
]
ALL_day = []
startday = datetime.strptime('2018-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')
endday = datetime.strptime('2019-08-10 00:00:00', '%Y-%m-%d %H:%M:%S')
while True:
ALL_day.append(str(startday))
if str(startday) == str(endday):
break
lastday = startday - timedelta(days=1)
if lastday.month != startday.month:
ALL_month.append(str(startday))
startday = startday + timedelta(days=1)
class settings:
def __init__(self,start = "2019-06-10 00:00:00",end="2019-07-10 00:00:00",eid = "Bitfinex",currency ="BTC_USD",balance = "10000",stocks = "1",period = "1d"):
self.setting_str = default_setting
self.settings = {
"start":start,
"end" :end,
"eid" :eid,
"currency":currency,
"balance":str(balance),
"stocks":str(stocks),
"period":period
}
self.setting_str = self.setting_str.replace("2019-06-10 00:00:00",self.settings["start"])
self.setting_str = self.setting_str.replace("2019-08-10 00:00:00",self.settings["end"])
self.setting_str = self.setting_str.replace("Bitfinex",self.settings["eid"])
self.setting_str = self.setting_str.replace("BTC_USD",self.settings["currency"])
self.setting_str = self.setting_str.replace("10000",self.settings["balance"])
self.setting_str = self.setting_str.replace("stocks:3","stocks:{}".format(float(self.settings["stocks"])))
self.setting_str = self.setting_str.replace("1m",self.settings["period"])
def export(self):
return self.setting_str
def get_day(timestamp):
'返回月份 与 日'
format_time = time.localtime(timestamp/1000)
return (format_time[0],format_time[1],format_time[2])
class strategy:
def __init__(self):
pass
def init(self,PRINT = False):
self.task = VCtx(self.setting)
self.init_account = exchange.GetAccount()
self.realizible_profit = 0
if PRINT:
print("Strategy Name:",self.name)
print("Setting:", self.setting)
#print(self.setting)
#print("初始账户信息:",self.init_account)
def main(self):
while True:
self.onTick()
Sleep(1000)
def exit_f(self):
pass
def format_outcome(self):
for key in self.outcome:
if key == "RuntimeLogs":
continue
if type(self.outcome[key]) != type([]) and type(self.outcome[key]) != type({}):
print(key, self.outcome[key])
elif type(self.outcome[key]) == type({}):
print(key)
for small_key in self.outcome[key]:
print(' ', small_key, self.outcome[key][small_key])
else:
print(key)
for item in self.outcome[key]:
for small_key in item:
print(' ', small_key, item[small_key])
#for Log in self.outcome['RuntimeLogs']:
#print(Log)
def run(self,PRINT = False):
'''运行回测然后得到收益'''
self.init(PRINT)
try:
self.main()
except EOFError:
#print("回测结束\n")
self.exit_f()
self.outcome = json.loads(self.task.Join())
for Log in self.outcome['RuntimeLogs']:
Log[1] = get_day(Log[1])
#print(Log)
Snapshort = self.outcome['Snapshort'][0]
balance_change = Snapshort['Balance'] - self.init_account['Balance']
stock_change = Snapshort['Stocks'] - self.init_account['Stocks']
self.realizible_profit = balance_change + stock_change * Snapshort['Symbols']['BTC_USD_Bitfinex']['Last'] - Snapshort['Commission']
self.commission = float(Snapshort['Commission'])
#print(Snapshort)
def Multiperiodbacktest(self,periods,filename = time.strftime("%b_%d_%Y_%H_%M_%S", time.localtime(time.time()))):
result = {
"period":[],
"profit":[],
"commission":[],
}
if len(periods) <= 1:
raise PeriodTooLess
labels = []
this_dir = self.dir +filename + "/"
try:
os.mkdir(this_dir)
except:
pass
write = pd.ExcelWriter(this_dir+ "result.xls")
for period in periods:
start = period[0]
end = period[1]
if start.endswith("00:00:00"):
start = start[:10]
if end.endswith("00:00:00"):
end = end[:10]
key = start
labels.append(start)
print('period:',start + " - " + end)
result['period'].append(start + " - " + end)
self.backtest(start,end,False,False)
result['profit'].append(self.realizible_profit)
result['commission'].append(self.commission)
pd.DataFrame(result).to_excel(write,sheet_name="具体收益")
average_profit = sum(result['profit'])/len(result['profit'])
samplenum = len(result['profit'])
inflowmonthnum = sum([1 if profit > 0 else 0 for profit in result['profit']])
metrics = {
"average_profit":[average_profit],
"极差":[max(result['profit']) - min(result['profit'])],
"正收益月数":[inflowmonthnum],
"正收益月数比率":[round(inflowmonthnum/samplenum,2)],
"平均手续费":[round(sum(result['commission'])/samplenum,2)],
"参数:":[" "]
}
for key in self.param:
metrics[key] = [self.param[key]]
pd.DataFrame(metrics).to_excel(write, sheet_name="指标")
write.save()
fig = plt.figure(figsize=(20,8),frameon =False)
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 指定下边的边作为 x 轴 指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0)) # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
plt.plot()
plt.plot(list(range(samplenum)),result['profit'],label = 'profit')
plt.plot(list(range(samplenum)),[average_profit]*samplenum,label = "average = {}".format(average_profit))
plt.legend(loc = 'upper right')
plt.xlabel("period")
plt.ylabel("profit")
plt.xticks(list(range(samplenum)),labels,rotation = 45)
plt.title("profit curve")
plt.savefig(this_dir + "profitcurve.png")
print("done!:",filename)
return result
def dailybacktest(self,gap):
period = []
for i in range(len(ALL_day) - gap):
period.append((ALL_day[i],ALL_day[i+gap]))
self.Multiperiodbacktest(period,"{}日内交易回测".format(gap))
def monthlybacktest(self,gap):
period = []
for i in range(len(ALL_month) - gap):
period.append((ALL_month[i],ALL_month[i+gap]))
suffix = ""
for key in self.param:
suffix += str(key) +"="+str(self.param[key]) + "&"
self.Multiperiodbacktest(period,str(gap) + "月内交易回测(param_" + suffix[:-1]+")")
def backtest(self,start,end,PRINTSETTING=True,PRINTLOG = False):
#self.param["start"] = start
#self.param['end'] = end
if not start.endswith("00:00:00"):
start+= " 00:00:00"
if not end.endswith("00:00:00"):
end += " 00:00:00"
self.setting = settings(start = start,end = end,period='1d').export()
self.run(PRINT=PRINTSETTING)
print('profit', self.realizible_profit)
if PRINTLOG:
for log in self.outcome['RuntimeLogs'][::-1]:
print(log)
@property
def param_suffix_str(self):
suffix = ""
for key in self.param:
suffix += str(key) +"="+str(self.param[key]) + "&"
suffix = suffix[:-1]
return suffix
@ property
def setting(self):
try:
return self._setting
except AttributeError:
self._setting = settings().export()
return self._setting
@ setting.setter
def setting(self,value):
self._setting = value
@property
def param(self):
try:
return self._param
except AttributeError:
self._param = {}
return self._param
@param.setter
def param(self,value):
self._param = value
def __getitem__(self, item):
return self._param[item]
def __setitem__(self, key, value):
self._param[key] = value
@property
def name(self):
if self._name not in locals().keys():
self._name = "Unnamed Strategy"
return self._name
@name.setter
def name(self,value):
self._name = value
self.dir = self._name + "/"
if os.path.exists(self.dir) == False:
os.mkdir(self.dir)
@staticmethod
def ticktime(tick):
return (time.strftime("%b %d %Y %H:%M:%S", time.localtime(int(str(tick['Time'])[:-3]))))
class R_breaker(strategy):
'''
p : 每次日内交易的btc个数
'''
def load_param(self,p = 0.1):
self
self.p = p
def main(self):
LastDay = get_day(exchange.GetRecords(PERIOD_D1)[-1].Time - 1000)
LastTimeStamp = exchange.GetRecords(PERIOD_D1)[-1].Time / 1000
yestoday = 0
BreakSsteup = False
BreakBsteup = False
STATE = "IDLE"
Fan = False
while True:
ticker = exchange.GetTicker()
NowDay = get_day(ticker.Time)
if NowDay != LastDay:
STATE = "IDLE"
yestoday = exchange.GetRecords(PERIOD_D1)[-2] # -1是今日
BreakSsteup = False
BreakBsteup = False
Log(NowDay)
Fan = False
NowPrice = ticker.Last
High = yestoday.High
Low = yestoday.Low
Close = yestoday.Close
Open = yestoday.Open
TodayHigh = ticker.High
TodayLow = ticker.Low
Ssteup = High + 0.35 * (Close - Low) # 观察卖出价
Bsteup = Low - 0.35 * (High - Close) # 观察买入价
Senter = 1.07 / 2 * (High + Low) - 0.07 * Low # 反转卖出价
Benter = 1.07 / 2 * (High + Low) - 0.07 * High # 反转买入价
Bbreak = Ssteup + 0.25 * (Ssteup - Bsteup) # 突破买入价
Sbreak = Bsteup - 0.25 * (Ssteup - Bsteup) # 突破卖出价
if TodayHigh > Ssteup:
BreakSsteup = True
if TodayLow < Bsteup:
BreakBsteup = True
'''
空仓的情况下:趋势跟踪
1.如果盘中价格超过突破买入价,则采取趋势策略,即在该点位开仓做多
2.如果盘中价格跌破突破卖出价,则采取趋势策略,即在该点位开仓做空
持仓的情况下:
1.当日内最高价超过观察卖出价后,盘中价格出现回落,且进一步跌破反转卖出价构成的支撑线时,采取反转策略,即在该点位(反手、开仓)做空;
2.当日内最低价低于观察买入价后,盘中价格出现反弹,且进一步超过反转买入价构成的阻力线时,采取反转策略,即在该点位(反手、开仓)做多;
'''
if STATE == "IDLE":
if NowPrice > Bbreak:
Log("价格超过突破买入价,趋势跟踪开仓做多")
exchange.Buy(ticker.Sell * 1.001, self.p)
STATE = "LONG"
elif NowPrice < Sbreak:
Log("价格跌破突破卖出价,趋势跟踪开仓做空")
exchange.Sell(ticker.Buy * 0.999, self.p)
STATE = "SHORT"
else:
if BreakSsteup and NowPrice < Senter and STATE == "LONG" and Fan == False:
Log("当日内最高价超过观察卖出价,盘中价格回落跌破反转卖出价,反手做空")
exchange.Sell(ticker.Buy * 0.999, 2 * self.p)
STATE = "SHORT"
Fan = True
if BreakBsteup and NowPrice > Benter and STATE == "SHORT"and Fan == False:
Log("当日内最低价低于观察买入价,盘中价格回落跌破反转卖出价,反手做多")
exchange.Buy(ticker.Sell * 1.001, 2 * self.p)
STATE = "LONG"
Fan = True
'''每日收盘前2分钟,进行平仓'''
if ticker.Time / 1000 - LastTimeStamp >= 86250:
LastTimeStamp = LastTimeStamp + 86400
if STATE == "LONG":
Log("每日收盘平仓")
exchange.Sell(ticker.Buy * 0.999, self.p) # 做多后做空平仓
if STATE == "SHORT":
Log("每日收盘平仓")
exchange.Buy(ticker.Sell * 1.001, self.p) # 做空后做多平仓
Log(exchange.GetAccount())
LastDay = NowDay
Sleep(500)
class Dual_Thrust(strategy):
def __init__(self):
self.name = 'DualThrust'
self.param = {
"N":5,
"k1":0.9,
"k2":0.5,
"p":1
}
def main(self):
Histroy_record = exchange.GetRecords()
dopen = Histroy_record[-1].Open # 每日的开盘价
LastDay = get_day(Histroy_record[-1].Time / 1000) # 上一次获取行情的日期
Nperiod = Histroy_record[-1 * self.param['N'] - 1:][:-1] # 前N日的K数据
Track = self.get_track(Nperiod,dopen) # 获取上下轨数据
LastState = 'IDLE'
while True:
ticker = exchange.GetTicker()
#print(time.strftime("%b %d %Y %H:%M:%S",time.localtime(int(str(ticker['Time'])[:-3]))))
NowPrice = ticker.Last # 当前市场最后成交价格
NowDay = get_day(ticker.Time) # 当前日期
if (NowDay != LastDay):
# 进入了新的一天,重新更新前N天数据与今日开盘价,与今日上下轨点数
Histroy_record = exchange.GetRecords(PERIOD_D1)
#for tick in Histroy_record:
# print(time.strftime("%b %d %Y %H:%M:%S",time.localtime(int(str(tick['Time'])[:-3]))))
Nperiod = Histroy_record[-1 * self.param['N'] - 1:][:-1]
dopen = Histroy_record[-1].Open
Track = self.get_track(Nperiod,dopen)
Log(NowDay, ':', Log(exchange.GetAccount()))
if (NowPrice > Track['uptrack'] and LastState != 'LONG'):
Log('当前市价格', NowPrice, '突破做多触发价:', Track['downtrack'])
# cancel_pending_orders(ORDER_TYPE_SELL) # 撤回所有卖单
if LastState == "IDLE":
exchange.Buy(ticker.Sell * 1.001, self.param['p'])
else:
exchange.Buy(ticker.Sell * 1.001, 2 * self.param['p'])
# days_operation = True
LastState = 'LONG'
if (NowPrice < Track['downtrack'] and LastState != 'SHORT'):
Log('当前市价格', NowPrice, '突破做空触发价:', Track['downtrack'])
# cancel_pending_orders(ORDER_TYPE_BUY) # 撤回所有买单
if LastState == "IDLE":
exchange.Sell(ticker.Buy * 0.999, self.param['p'])
else:
exchange.Sell(ticker.Buy * 0.999, 2 * self.param['p'])
LastState = 'SHORT'
LastDay = NowDay # 更新上次获取行情的日期
def get_track(self,Nperiod,dopen):
'获得上下轨点数'
HH = max([day.High for day in Nperiod])
LC = min([day.Close for day in Nperiod])
HC = max([day.Close for day in Nperiod])
LL = min([day.Low for day in Nperiod])
RANGE = max(HH - LC, HC - LL)
return {'uptrack': dopen + self.param['k1'] * RANGE, 'downtrack': dopen - self.param['k2'] * RANGE}
class 菲阿里四价(strategy):
def __init__(self):
self.name = "菲阿里四价"
self.param['p'] = 0.1
def main(self):
STATE = "IDLE"
LastDay = get_day(exchange.GetRecords(PERIOD_D1)[-1].Time)
LastTimeStamp = exchange.GetRecords(PERIOD_D1)[-1].Time / 1000
while True:
p = self.param['p']
ticker = exchange.GetTicker()
NowDay = get_day(ticker.Time)
#TODAY = exchange.GetRecords(PERIOD_D1)[-1]
yestoday = exchange.GetRecords(PERIOD_D1)[-2] # -1是今日
uptrack = yestoday.High
downtrack = yestoday.Low
NowPrice = ticker.Last
if NowDay != LastDay:
STATE = "IDLE"
Log(NowDay)
'''日内价格突破上下轨,进行做多或者做空'''
if STATE == "IDLE" and NowPrice > uptrack:
exchange.Buy(ticker.Sell * 1.001, p)
STATE = "LONG"
if STATE == "IDLE" and NowPrice < downtrack:
exchange.Sell(ticker.Buy * 0.999, p)
STATE = "SHORT"
'''每日收盘前2分钟,进行平仓'''
if ticker.Time / 1000 - LastTimeStamp >= 86250:
LastTimeStamp = LastTimeStamp + 86400
if STATE == "LONG":
exchange.Sell(ticker.Buy * 0.999, p) # 做多后做空平仓
if STATE == "SHORT":
exchange.Buy(ticker.Sell * 1.001, p) # 做空后做多平仓
LastDay = NowDay
Sleep(500)
class skypark(strategy):
def load_param(self,p = 0.1,k1=1.01,k2=0.99):
self.p = p
self.k1 = k1
self.k2 = k2
def main(self):
p = self.p
k1 = self.k1
k2 = self.k2
History = exchange.GetRecords(PERIOD_D1)
TODAY = History[-1]
Yestoday = History[-2]
LastTimeStamp = TODAY.Time / 1000
LastDay = get_day(TODAY.Time - 1000)
DayFirstCandle = 0 # 每日第一个k线
DayOpen = TODAY.Open
LastOpen = Yestoday.Open
STATE = "IDLE"
while True:
ticker = exchange.GetTicker()
NowDay = get_day(ticker.Time)
if LastDay != NowDay:
'进入新的更新每日第一条k线与昨日和今日的开盘价'
DayFirstCandle = ticker
History = exchange.GetRecords(PERIOD_D1)
TODAY = History[-1]
Yestoday = History[-2]
DayOpen = TODAY.Open
LastOpen = Yestoday.Open
if DayOpen > LastOpen * k1:
Log("今日高开", "今日开盘价:", DayOpen, "昨日开盘价", LastOpen)
if DayOpen < LastOpen * k2:
Log("今日低开", "今日开盘价:", DayOpen, "昨日开盘价", LastOpen)
STATE = "IDLE"
Nowprice = ticker.Last
if DayOpen > LastOpen * k1 and STATE == "IDLE" and Nowprice > DayFirstCandle.High and ticker.Time / 1000 - LastTimeStamp < 86250:
Log("价格", Nowprice, "突破上轨", DayFirstCandle.High, ",买入开仓")
exchange.Buy(ticker.Sell * 1.001, p)
STATE = "LONG"
if DayOpen < LastOpen * k2 and STATE == "IDLE" and Nowprice < DayFirstCandle.Low and ticker.Time / 1000 - LastTimeStamp < 86250:
Log("价格", Nowprice, "突破下轨", DayFirstCandle.High, ",卖入开仓")
exchange.Sell(ticker.Buy * 0.999, p)
STATE = "SHORT"
'''每日收盘前2分钟,进行平仓'''
if ticker.Time / 1000 - LastTimeStamp >= 86250:
LastTimeStamp = LastTimeStamp + 86400
if STATE == "LONG":
Log("做多后做空平仓")
exchange.Sell(ticker.Buy * 0.999, p) # 做多后做空平仓
if STATE == "SHORT":
Log("做空后做多平仓")
exchange.Buy(ticker.Sell * 1.001, p) # 做空后做多平仓
LastDay = NowDay # 更新获取上一次行情的日期
class Dual_Thrust_improved(strategy):
def __init__(self):
self.name = 'DualThrust_improved'
self.defaultparam = {
"N": 5,
"k1": 0.4,
"k2": 0.4,
"p": 0.1,
'stop': 1000,
"KDJ_N":9,
}
self.param = self.defaultparam.copy()
self.k = {}
self.d = {}
self.j = {}
def main(self):
Histroy_record = exchange.GetRecords(PERIOD_D1)
dopen = Histroy_record[-1].Open # 每日的开盘价
LastDay = get_day(Histroy_record[-1].Time / 1000) # 上一次获取行情的日期
Nperiod = Histroy_record[-1 * self.param['N'] - 1:][:-1] # 前N日的K数据
Track = self.get_track(Nperiod,dopen) # 获取上下轨数据
LastState = 'IDLE'
self.k[LastDay] = 50
self.d[LastDay] = 50
self.price = 0
while True:
ticker = exchange.GetTicker()
#print(time.strftime("%b %d %Y %H:%M:%S",time.localtime(int(str(ticker['Time'])[:-3]))))
#print(ticker.Last)
NowPrice = ticker.Last # 当前市场最后成交价格
NowDay = get_day(ticker.Time) # 当前日期
#print(time.strftime("%b %d %Y %H:%M:%S", time.localtime(int(str(ticker['Time'])[:-3]))))
if (NowDay != LastDay):
# 进入了新的一天,重新更新前N天数据与今日开盘价,与今日上下轨点数
Histroy_record = exchange.GetRecords(PERIOD_D1)
#for tick in Histroy_record:
# print(time.strftime("%b %d %Y %H:%M:%S", time.localtime(int(str(tick['Time'])[:-3]))))
#exit()
Nperiod = Histroy_record[-1 * self.param['N'] - 1:][:-1]
dopen = Histroy_record[-1].Open
self.KDJ(NowDay)
if self.k[NowDay] > 50 and self.d[NowDay] > 50 and self.j[NowDay] > 50:
self.param['k1'] = self.defaultparam['k1'] - 0.1
self.param['k2'] = self.defaultparam['k2']
#为做多市场,调整下轨
if self.k[NowDay] < 50 and self.d[NowDay] < 50 and self.j[NowDay] < 50:
self.param['k1'] = self.defaultparam['k1']
self.param['k2'] = self.defaultparam['k2'] - 0.1
Track = self.get_track(Nperiod, dopen)
Log(NowDay, ':', Log(exchange.GetAccount()))
#止损
if LastState == 'LONG' and self.price - ticker.Buy*0.999 >= self.param['stop']:
exchange.Sell(ticker.Buy * 0.999, 1 * self.param['p'])
LastState = "IDLE"
if LastState == 'SHORT' and ticker.Sell*1.001 - self.price >= self.param['stop']:
exchange.Buy(ticker.Sell *1.001, 1 * self.param['p'])
LastState = "IDLE"
#KDJ条件判断市场
if (NowPrice > Track['uptrack'] and LastState != 'LONG'):
Log('当前市价格', NowPrice, '突破做多触发价:', Track['downtrack'])
# cancel_pending_orders(ORDER_TYPE_SELL) # 撤回所有卖单
if LastState == "IDLE":
exchange.Buy(ticker.Sell * 1.001, self.param['p'])
else:
exchange.Buy(ticker.Sell * 1.001, 2 * self.param['p'])
# days_operation = True
self.price = ticker.Sell * 1.001
LastState = 'LONG'
if (NowPrice < Track['downtrack'] and LastState != 'SHORT'):
Log('当前市价格', NowPrice, '突破做空触发价:', Track['downtrack'])
# cancel_pending_orders(ORDER_TYPE_BUY) # 撤回所有买单
if LastState == "IDLE":
exchange.Sell(ticker.Buy * 0.999, self.param['p'])
else:
exchange.Sell(ticker.Buy * 0.999, 2 * self.param['p'])
self.price = ticker.Buy * 0.999
LastState = 'SHORT'
LastDay = NowDay # 更新上次获取行情的日期
def get_track(self,Nperiod,dopen):
'获得上下轨点数'
HH = max([day.High for day in Nperiod])
LC = min([day.Close for day in Nperiod])
HC = max([day.Close for day in Nperiod])
LL = min([day.Low for day in Nperiod])
RANGE = max(HH - LC, HC - LL)
return {'uptrack': dopen + self.param['k1'] * RANGE, 'downtrack': dopen - self.param['k2'] * RANGE}
def RSV(self,Nperiod):
Cn = Nperiod[-1].Close
Ln = min([day.Close for day in Nperiod])
Hn = max([day.High for day in Nperiod])
return (Cn - Ln) / (Hn - Ln) * 100
def KDJ(self,today):
records = exchange.GetRecords(PERIOD_D1);
kdj = TA.KDJ(records,self.param['KDJ_N'], 3, 3);
#Log("k:", kdj[0], "d:", kdj[1], "j:", kdj[2]);
self.k[today] = kdj[0][-1]
self.d[today] = kdj[0][-1]
self.j[today] = kdj[0][-1]
class HMM(strategy):
def __init__(self):
self.name = "Hiden Markov "
self.winratio = []
self.param = {
"p":0.1,
'gap':2,
"days":200,
}
def testmetric(self):
global ALL_day
periods = []
total_real = []
total_metr= []
ALL_day = ALL_day[-10:]
for i in range(len(ALL_day)-1):
periods.append((ALL_day[i],ALL_day[i+1]))
labels = []
this_dir = self.dir + self.param['metrics']+ "/"
try:
os.mkdir(this_dir)
except:
pass
for period in periods:
start = period[0]
end = period[1]
if start.endswith("00:00:00"):
start = start[:10]
if end.endswith("00:00:00"):
end = end[:10]
print('period:',start + " - " + end)
self.backtest(start,end,False,False)
labels.append(start)
total_real += self.real
total_metr += self.metrics
samplenum = len(total_real)
fig = plt.figure(figsize=(30,8))
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 指定下边的边作为 x 轴 指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0)) # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
ax.plot(list(range(samplenum)), total_real, label='真实成交价变动')
ax2 = ax.twinx()
ax2.set_xlabel("sample")
ax2.set_ylabel("number")
# ax2.set_ylim((0,100))
plt.title(self.param['title'])
if self.param['metrics'] == 'MACD':
DIF = []
DEA = []
MACD = []
for item in total_metr:
DIF.append(item[0])
DEA.append(item[1])
MACD.append(item[2])
ax2.plot(list(range(samplenum)), DIF, color='red',
label="DIF")
ax2.plot(list(range(samplenum)), DEA, color='blue',
label="DEA")
ax2.plot(list(range(samplenum)), MACD, color='green',
label="MACD")
ax2.legend(loc='upper right')
plt.savefig(this_dir +self.param['title'] + ".png")
print("done!:")
if self.param['metrics'] == 'RSI':
ax2.plot(list(range(samplenum)), total_metr, color='red',
label="该时刻前{}条1分钟k线所求出{}值".format(self.param['metrics_period'], self.param['metrics']))
ax2.legend(loc='upper right')
plt.savefig(this_dir + str(self.param['metrics_period'])+'_'+self.param['title'] + ".png")
print("done!:")
if self.param['metrics'] == 'OBV':
ax2.plot(list(range(samplenum)), total_metr, color='red',
label="前一时刻所求出{}值".format(self.param['metrics']))
ax2.legend(loc='upper right')
plt.savefig(this_dir +self.param['title'] + ".png")
print("done!:")
def testall(self):
global ALL_day
periods = []
total_real = []
total_pred = []
ratio = []
profits = []
ALL_day = ALL_day[-1*self.param['days']:]
for i in range(len(ALL_day)-1):
periods.append((ALL_day[i],ALL_day[i+1]))
labels = []
this_dir = self.dir + "algorithm2" + "/"
try:
os.mkdir(this_dir)
except:
pass
if self.param['gap'] != None:
this_dir = self.dir +"algorithm2" + "/" + str(self.param['gap']) + '/'
else:
this_dir = self.dir + "algorithm2" + "/"
try:
os.mkdir(this_dir)
except:
pass
for period in periods:
start = period[0]
end = period[1]
if start.endswith("00:00:00"):
start = start[:10]
if end.endswith("00:00:00"):
end = end[:10]
print('period:',start + " - " + end)
self.backtest(start,end,False,False)
self.predict = self.predict[:len(self.real)]
if len(self.predict) == 0:
print('跳过一天')
continue
labels.append(start)
total_real += self.real
total_pred += self.predict
ratio.append(self.cal_trend_correct_ratio(self.real,self.predict))
profits.append(self.realizible_profit)
sum_profit = [profits[0]]
for i in range(1,len(profits)):
sum_profit.append(sum_profit[-1] + profits[i])
samplenum = len(total_real)
fig = plt.figure(figsize=(30,5))
above_figure, axs = plt.subplots(2,1)
change_ax = axs[0]
correct_ax = axs[1]
ax = change_ax
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 指定下边的边作为 x 轴 指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0)) # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
ax.plot(list(range(samplenum)),total_real,label = '真实成交价变动')
ax.plot(list(range(samplenum)),total_pred,label = "前一时刻指标值")
ax.legend(loc = 'upper right')
ax.set_xlabel("sample")
ax.set_ylabel("number")
ax.set_title("{}日内成交价真实变动与预测变动曲线".format(self.param['days']))
ax = correct_ax
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 指定下边的边作为 x 轴 指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0)) # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
ax.plot(list(range(len(ratio))),ratio,label = '趋势预测正确率')
ax.plot(list(range(len(ratio))),[np.average(ratio)]*len(ratio),label = '样本平均预测正确率')
ax.legend(loc = 'upper right')
ax.set_xlabel("sample")
ax.set_ylabel("ratio")
ax.set_title("{}日预测正确率".format(self.param['days']))
plt.savefig(this_dir + "compare_change_and_correct_ratio_{}.png".format(self.param_suffix_str))
fig = plt.figure()
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 指定下边的边作为 x 轴 指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0)) # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
plt.plot(list(range(len(labels))),profits,label = '每日收益曲线')
plt.plot(list(range(len(labels))),sum_profit,label = "累积收益曲线")
plt.legend(loc = 'upper left')
plt.xlabel("day")
plt.ylabel("number")
plt.title("{}日收益曲线".format(self.param['days']))
plt.savefig(this_dir + "profit_curve_{}.png".format(self.param_suffix_str))
print("done!:")
def testprofit(self):
global ALL_day
periods = []
profits = []
labels = []
ALL_day = ALL_day[-100:]
for i in range(len(ALL_day)-1):
periods.append((ALL_day[i],ALL_day[i+1]))
for period in periods:
start = period[0]
end = period[1]
if start.endswith("00:00:00"):
start = start[:10]
if end.endswith("00:00:00"):
end = end[:10]
print('period:',start + " - " + end)
labels.append(start)
self.backtest(start,end,False,False)
profits.append(self.realizible_profit)
this_dir = self.dir +"algorithm2" + "/"
sum_profit = [profits[0]]
for i in range(1,len(profits)):
sum_profit.append(sum_profit[-1] + profits[i])
try:
os.mkdir(this_dir)
except:
pass
samplenum = len(labels)
fig = plt.figure()
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left') # 指定下边的边作为 x 轴 指定左边的边为 y 轴
ax.spines['bottom'].set_position(('data', 0)) # 指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['left'].set_position(('data', 0))
plt.plot(list(range(samplenum)),profits,label = '每日收益曲线')
plt.plot(list(range(samplenum)),sum_profit,label = "累积收益曲线")
plt.legend(loc = 'upper right')
plt.xlabel("day")
plt.ylabel("number")
plt.title("100日收益曲线")
plt.savefig(this_dir + "profit_curve_{}.png".format(self.param_suffix_str))
def plot(self):
pass
def exit_f2(self):
self.predict = self.predict[:len(self.real)]
self.winratio.append(self.cal_trend_correct_ratio(self.real,self.predict))
def exit_f1(self):
fig = plt.figure(figsize=(12,8))
plt.plot(self.real,label = "真实的变化")
plt.plot(self.preditct,label = "预测的变化")
plt.title("用10点-12点的数据喂模型")
plt.legend(loc = "upper_left")
suffix = ""
for key in self.param:
suffix += str(key) +"="+str(self.param[key]) + "&"
g = lambda x: 1 if x > 0 else -1
f = lambda x,y: 1 if x == y else 0
trend_real = [g(x) for x in self.real]
trend_predic = [g(x) for x in self.preditct]
ratio = sum([f(trend_real[i],trend_predic[i]) for i in range(len(trend_predic))])/(len(trend_predic))
suffix += "趋势正确预测率_"+str(round(ratio,2))
plt.savefig(self.dir + "10点-12点数据训练模型_"+suffix + ".png")
def cal_trend_correct_ratio(self,real,predict):
g = lambda x: 1 if x > 0 else -1
f = lambda x, y: 1 if x == y else 0
trend_real = [g(x) for x in real]
trend_predic = [g(x) for x in predict]
if len(trend_real) == 0:
return np.nan
ratio = sum([f(trend_real[i], trend_predic[i]) for i in range(len(trend_predic))]) / (len(trend_predic))
return round(ratio,2)
def algorithm1(self):
'''用早上10点-12点的数据喂HMM'''
self.param['n'] = 10
self.param['obeserve'] = 6
self.param['threhold'] = 70
LastDay = ""
STATE = 'IDLE'
X = []
History = exchange.GetRecords(PERIOD_M1)
lastticker = History[-2]
lts = lastticker.Time / 1000
train_model = False
self.accumulate_profit = 0
self.real = []
self.predict = []
self.total_ratio = []
COMPARE = False
self.profits = []
NeedStop = False
self.stop = {
"LONG":[],
"SHORT":[],
}
hastart = False
while True:
ticker = exchange.GetTicker()
#优先止损 亏损保持在5美元以内
self.stop["LONG"] = list(sorted(self.stop["LONG"],key = lambda x:x[0]))
self.stop["SHORT"] = list(sorted(self.stop["SHORT"], key=lambda x: x[0],reverse=True))
for item in self.stop["LONG"]:
if ticker.Buy * 0.9999 - item[0] >= -1*item[1] * 5 / 0.1:
exchange.Sell(ticker.Buy * 0.9999, 1 * item[1])
self.stop["LONG"].remove(item)
for item in self.stop["SHORT"]:
if item[0] - ticker.Sell * 1.0001 >= -1*item[1] * 5 / 0.1:
exchange.Buy(ticker.Sell * 1.0001, 1 * item[1])
self.stop["SHORT"].remove(item)
tickertime = strategy.ticktime(ticker)
NowDay = get_day(ticker.Time)
if NowDay != LastDay:
Log("进入新的一天")
dts = ticker.Time / 1000
LastDay = NowDay
train_model = False
O = []
if ticker.Time/1000 - dts >= 36000 and ticker.Time/1000 - dts < 43200:
"十点至十二点的数据喂养模型"
change = ticker.Last - lastticker.Last
X.append([change])
else:
if not hastart:
last_check_ticker = ticker
hastart = True
if ticker.Time/1000 - dts >= 43200 and ticker.Time/1000 - dts < 64800:
if ticker.Time/1000 - last_check_ticker.Time/1000 < self.param['gap']*60:
#需要跳过
continue
lastticker = last_check_ticker
change = ticker.Last - lastticker.Last
O.append([change])
"十二点到下午六点进行交易"
if COMPARE:
self.real.append(change)
#print("预测:", self.predict[-1], " 实际:", change,end = " ")
#print("当前成交价:",ticker.Last ," 上一时刻成交价:",lastticker.Last)
#print("买一:",ticker.Buy," 卖一:",ticker.Sell,end=" ")
if STATE == 'LONG':
print("买: ",price)
if price - ticker.Buy * 0.9999 >= self.param['p'] * 30 / 0.1 :
print("变动过大,止损,有仓位需要后续止损")
self.stop[STATE].append((price,self.param['p']))
else:
exchange.Sell(ticker.Buy * 0.9999, 1 * self.param['p'])
print("收益:",ticker.Buy * 0.9999 - price)
self.profits.append(ticker.Buy * 0.9999 - price)
STATE = "IDLE"
if STATE == 'SHORT':
print("卖: ", price)
if ticker.Sell * 1.0001 - price>= self.param['p'] * 30 / 0.1 :
print("变动过大,止损,有仓位需要后续止损")
self.stop[STATE].append((price,self.param['p']))
else:
exchange.Buy(ticker.Sell * 1.0001, 1 * self.param['p'])
print("收益:",price - ticker.Sell * 1.000 )
self.profits.append(price - ticker.Sell * 1.000)
STATE = "IDLE"
COMPARE = False
#print()
if not train_model:
"先训练模型"
m = hmm.GaussianHMM(n_components=self.param['n'],covariance_type="full")
seen = np.array(X).reshape(-1, 1)
try:
m.fit(seen)
except:
return 0
train_model = True
trans = m.transmat_
else:
"训练好模型,观察到5个就交易,就对下一个进行交易"
if len(O) == self.param['obeserve'] and len(self.stop["LONG"]) + len(self.stop["SHORT"]) <= 2:
"最多只能有两个未止损"
try:
I = m.predict_proba(O)
except:
return 0
TRAN = I[-1:]
next_state_pro = (TRAN @ trans).T
distriution_means = m.means_
expected_change = (next_state_pro.T @ distriution_means)[0][0]
if expected_change > self.param['threhold'] and STATE == "IDLE":
exchange.Buy(ticker.Sell *1.0001, 1 * self.param['p'])
STATE = "LONG"
price = ticker.Sell *1.0001
if expected_change < -1*self.param['threhold'] and STATE == "IDLE":
exchange.Sell(ticker.Buy *0.9999, 1 * self.param['p'])
STATE = "SHORT"
price = ticker.Buy *0.9999
self.predict.append(expected_change)
COMPARE = True
O = O[1::]
#O = []
last_check_ticker = ticker
if ticker.Time/1000 - dts >= 86250:
Log("进入一天末尾准备平仓")
for item in self.stop["LONG"]:
exchange.Sell(ticker.Buy * 0.9999, 1 * item[1])
self.stop["LONG"].remove(item)
for item in self.stop["SHORT"]:
exchange.Buy(ticker.Sell * 1.0001, 1 * item[1])
self.stop["SHORT"].remove(item)
STATE = 'IDLE'
Log(exchange.GetAccount())
print(self.profits)
print(sum(self.profits))
lastticker = ticker
def algorithm2(self):
'''用早上10点-12点的数据喂HMM'''
self.param['n'] = 18
self.param['obeserve'] = 5
self.param['threhold'] = 20
self.param['stop'] = True
LastDay = ""
STATE = 'IDLE'
X = []
History = exchange.GetRecords(PERIOD_M1)
lastticker = History[-2]
lts = lastticker.Time / 1000
train_model = False
self.accumulate_profit = 0
self.real = []
self.predict = []
self.total_ratio = []
COMPARE = False
self.profits = []
NeedStop = False
self.stop = {
"LONG": [],
"SHORT": [],
}
hastart = False
hastrainstart = False
while True:
ticker = exchange.GetTicker()
# 优先止损 亏损保持在5美元以内
'''
self.stop["LONG"] = list(sorted(self.stop["LONG"], key=lambda x: x[0]))
self.stop["SHORT"] = list(sorted(self.stop["SHORT"], key=lambda x: x[0], reverse=True))
for item in self.stop["LONG"]: