-
Notifications
You must be signed in to change notification settings - Fork 28
/
07CHANNEL.py
142 lines (127 loc) · 3.83 KB
/
07CHANNEL.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
# 实现经典量化策略
# 通道策略 Channel。
# 参考 ZuraKakushadze,JuanAndrésSerur. 151 Trading Strategies.
import tradesys as ts
import run
import sys
import akshare as ak
import efinance as ef
import pandas as pd
import numpy as np
import os
import datetime
import backtrader as bt
# 策略类
class CHAStrategy(ts.Strategy):
"""
bprint, 是否输出交易过程
"""
params = (
("bprint", False),
("T", 20), )
def __init__(self):
self.close = self.datas[0].close
self.H = bt.ind.Highest(self.datas[0].high, period = self.p.T)
self.L = bt.ind.Highest(self.datas[0].high, period = self.p.T)
self.order = None
self.price = 0.0
# 测试用
# print("参数", self.p.T1, self.p.T2, self.p.stoprate)
def next(self):
if self.order:
return
if not self.position:
if self.close[0] <= self.L[0]:
cash = self.broker.get_cash()
amount = self.downcast(cash*0.9/self.close[0], 100)
self.order = self.buy(size = amount)
self.price = self.close[0]
else:
pos = self.getposition()
if self.close[0] >= self.H[0]:
self.order = self.sell(size = pos.size)
self.price = 0.0
if self.is_lastday(data = self.datas[0]):
self.close()
# 主函数
@run.change_dir
def channel():
ts.init_display()
start_date = "20100108"
end_date = "20201231"
# codes = init_data(start_date = start_date, end_date = end_date, retry = False)
codes = ["000100"]
backtest = ts.BackTest(
strategy = CHAStrategy,
codes = codes,
bk_code = "000001",
start_date = start_date,
end_date = end_date,
rf = 0.03,
start_cash = 10000000,
stamp_duty=0.005,
commission=0.0001,
adjust = "hfq",
period = "daily",
refresh = True,
bprint = True,
bdraw = True,
T = 20, )
results = backtest.run()
print("回测结果", results[:-2])
# 调参试试
@run.change_dir
def opt_channel():
ts.init_display()
start_date = "20100108"
end_date = "20201231"
# codes = init_data(start_date = start_date, end_date = end_date, retry = False)
codes = ["000100"]
backtest = ts.OptStrategy(
strategy = CHAStrategy,
codes = codes,
bk_code = "000001",
start_date = start_date,
end_date = end_date,
rf = 0.03,
start_cash = 10000000,
stamp_duty=0.005,
commission=0.0001,
adjust = "hfq",
period = "daily",
refresh = False,
bprint = False,
bdraw = False,
T = range(10, 60))
results = backtest.run()
print("回测结果", results.loc[:,["参数", "年化收益率"]])
# 对整个市场回测
@run.change_dir
def research_channel():
ts.init_display()
start_date = "20100108"
end_date = "20201231"
# codes = init_data(start_date = start_date, end_date = end_date, retry = False)
backtest = ts.Research(
strategy = CHAStrategy,
bk_code = "000001",
start_date = start_date,
end_date = end_date,
start_cash = 10000000,
min_len = 2000,
adjust = "hfq",
period = "daily",
refresh = True,
bprint = False,
retest = True,
T = 17)
results = backtest.run()
# print("测试3")
# print(results.info())
results.sort_values(by = "年化收益率", inplace = True, ascending = False)
print("回测结果", results.loc[:, ["年化收益率"]])
if __name__ == "__main__":
# channel()
# opt_channel()
# research_channel()
pass