-
Notifications
You must be signed in to change notification settings - Fork 28
/
04SMA.py
134 lines (119 loc) · 3.59 KB
/
04SMA.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
# 实现经典量化策略
# 单移动均线 Signal 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):
"""
period, 调仓周期
bprint, 是否输出交易过程
"""
params = (("maperiod", 20),
("bprint", False),)
def __init__(self):
self.close = self.datas[0].close
self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period = self.p.maperiod)
self.order = None
def next(self):
if self.order:
return
if not self.position:
if self.close[0] > self.sma[0]:
cash = self.broker.get_cash()
amount = self.downcast(cash*0.9/self.close[0], 100)
self.order = self.buy(size = amount)
else:
pos = self.getposition()
if self.close[0] < self.sma[0]:
self.order = self.sell(size = pos.size)
if self.is_lastday(data = self.datas[0]):
self.close()
# 主函数
@run.change_dir
def sma():
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_sma():
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,
maperiod = range(10, 30))
results = backtest.run()
print("回测结果", results.loc[:,["参数", "年化收益率"]])
# 对整个市场回测
@run.change_dir
def research_sma():
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 = True,
bprint = False,
retest = True,
maperiod = 25)
results = backtest.run()
# print("测试3")
# print(results.info())
results.sort_values(by = "年化收益率", inplace = True, ascending = False)
print("回测结果", results.loc[:, ["年化收益率"]])
if __name__ == "__main__":
# sma()
# opt_sma()
research_sma()