-
Notifications
You must be signed in to change notification settings - Fork 19
/
doTest.py
128 lines (116 loc) · 4.45 KB
/
doTest.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
# coding:utf-8
# 1000元实盘练习程序
# 回测成交量辅助判断锤子线策略程序
import pandas as pd
import numpy as np
import talib
import tools
import run
import backtrader as bt
from backtest import BackTest
import strategy as st
import os
import matplotlib.pyplot as plt
from scipy import stats
# 回测主函数
@run.change_dir
def main(refresh = False, retest = False):
# 先初始化,准备数据
tools.init()
code = "600658"
benchmark = tools.getBenchmarkData(month = 60, refresh = refresh, path = "./stockdata/")
backtest = BackTest(codes = [code], strategy = st.ChuiziStrategy, benchmark = benchmark, month = 60, refresh = refresh, path = "./stockdata/")
results = backtest.getResults()
print(results)
backtest.drawResults(code + "result")
# 调参实验
def main2(refresh = False):
# 先初始化,准备数据
tools.init()
code = "600658"
benchmark = tools.getBenchmarkData(month = 60, refresh = refresh, path = "./stockdata/")
backtest = BackTest(codes = [code], strategy = st.ChuiziStrategy, benchmark = benchmark, month = 60, refresh = refresh, path = "./stockdata/", bOpt = True)
results = backtest.optRun(period = range(5,10), rate = np.arange(0.1, 0.5, 0.1), stopup = np.arange(0.05, 0.5, 0.05), stopdown = np.arange(0.05, 0.1, 0.05))
# results = backtest.optRun(period = range(5,10))
print("调参结果:")
print(results[:5])
# 回测主函数 refresh是否重新加载数据
# retest是否重新回测
@run.change_dir
def main3(refresh = False, retest = False):
# 先初始化,准备数据
tools.init()
codes = tools.Research(refresh = refresh, month = 60, highPrice = 10.0)
# codes = codes[:10]
benchmark = tools.getBenchmarkData(month = 60, refresh = refresh)
# print(benchmark.head())
datafilepath = "./backtest.csv"
if os.path.exists(datafilepath) and retest == False:
test_res = pd.read_csv(datafilepath, converters = {'股票代码':str})
else:
test_res = pd.DataFrame()
n = len(codes)
i = 0
for code in codes:
i += 1.0
print("回测进度:", i/n*100, "%")
backtest = BackTest(codes = [code], strategy = st.ChuiziStrategy, benchmark = benchmark)
results = backtest.getResults()
test_res = test_res.append(results)
test_res.to_csv(datafilepath, index = False)
# print(test_res.describe())
# print(test_res.股票代码)
print(test_res.info())
plt.figure()
test_res.胜率.plot(kind = "kde")
plt.savefig("./output/test_res1.png")
plt.close()
plt.figure()
test_res.年化收益率.plot(kind = "kde")
plt.legend(loc = "best")
plt.savefig("./output/test_res2.png")
print(test_res.head())
print(test_res[test_res.年化收益率 >= 0.7])
# 按年化收益率排序
test_res.sort_values(by = "年化收益率", inplace = True, ascending = False)
# 前10名
print(test_res.head(10).loc[:, ["股票代码", "年化收益率"]])
# 研究交易次数与年化收益率的相关性
# 剔除年化收益率绝对值大于40%的数据
new_res = test_res[np.abs(test_res.年化收益率) < 0.4]
trades = new_res.交易次数.values
gets = new_res.年化收益率.values
plt.figure()
plt.scatter(trades, gets)
plt.xlabel("交易次数")
plt.ylabel("年化收益率")
plt.savefig("./output/tradevsget.png")
# 计算交易次数与年化收益率的相关系数
r, p = stats.pearsonr(trades, gets)
print("交易次数与年化收益率的相关系数为:%6.3f,概率为:%6.3f" % (r, p))
# 研究交易次数与胜率的关系
trades = test_res.交易次数.values
winrates = test_res.胜率.values
plt.figure()
plt.scatter(trades, winrates)
plt.xlabel("交易次数")
plt.ylabel("胜率")
plt.savefig("./output/tradevswinrates.png")
# 计算交易次数与胜率的相关系数
r, p = stats.pearsonr(trades, winrates)
print("交易次数与胜率的相关系数为:%6.3f,概率为:%6.3f" % (r, p))
# 画alpha,beta值分布图
plt.figure()
test_res.Alpha.plot(kind = "kde")
plt.ylabel("α")
plt.savefig("./output/alpha.png")
plt.close()
plt.figure()
test_res.Beta.plot(kind = "kde")
plt.ylabel("β")
plt.savefig("./output/beta.png")
plt.close()
if __name__ == "__main__":
# main(refresh = False)
# main2(refresh = False)
main3(refresh = False, retest = False)