-
Notifications
You must be signed in to change notification settings - Fork 28
/
05TMA.py
152 lines (137 loc) · 4.29 KB
/
05TMA.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
# 实现经典量化策略
# 双移动均线 Two Moving Average。
# 参考 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 SMAStrategy(ts.Strategy):
"""
T1, 短周期
T2, 长周期
stoprate, 止损位
bprint, 是否输出交易过程
"""
params = (("T1", 10),
("T2", 20),
("stoprate", 0.05),
("bprint", False),)
def __init__(self):
self.close = self.datas[0].close
# self.sma1 = bt.indicators.SimpleMovingAverage(self.datas[0], period = self.p.T1)
# self.sma2 = bt.indicators.SimpleMovingAverage(self.datas[0], period = self.p.T2)
self.sma1 = bt.talib.KAMA(self.close, timeperiod = self.p.T1)
self.sma2 = bt.talib.KAMA(self.close, timeperiod = self.p.T2)
self.order = None
self.price = 0.0
# 测试用
# print("参数", self.p.T1, self.p.T2, self.p.stoprate)
def next(self):
if self.order:
return
print(self.sma1[0], self.sma2[0])
if not self.position:
if self.sma1 > self.sma2:
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.sma1 < self.sma2 or self.close[0] < self.price*(1-self.p.stoprate):
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 tma():
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 = SMAStrategy,
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 = False,
bdraw = True)
results = backtest.run()
print("回测结果", results[:-2])
# 调参试试
@run.change_dir
def opt_tma():
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 = SMAStrategy,
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,
num_params = 3,
T1 = range(10, 20),
T2 = range(30, 60),
stoprate = np.arange(0.01, 0.1, 0.01))
results = backtest.run()
print("回测结果", results.loc[:,["参数", "年化收益率"]])
# 对整个市场回测
@run.change_dir
def research_tma():
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 = SMAStrategy,
bk_code = "000001",
start_date = start_date,
end_date = end_date,
start_cash = 10000000,
min_len = 2000,
adjust = "hfq",
period = "daily",
refresh = False,
bprint = False,
retest = True,
T1 = 16,
T2 = 55,
stoprate = 0.08)
results = backtest.run()
# print("测试3")
# print(results.info())
results.sort_values(by = "年化收益率", inplace = True, ascending = False)
print("回测结果", results.loc[:, ["年化收益率"]])
if __name__ == "__main__":
tma()
# opt_tma()
# research_tma()