forked from RoveAllOverTheWorld512/hyb_ta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backtrader3.py
211 lines (173 loc) · 6.94 KB
/
backtrader3.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
# -*- coding: utf-8 -*-
"""
Created on 2019-10-12 21:50:26
author: huangyunbin
email: huangyunbin@sina.com
QQ: 592440193
https://blog.csdn.net/qtlyx/article/details/70306256
"""
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime # For datetime objects
import pandas as pd
import backtrader as bt
import numpy as np
class ssa_index_ind(bt.Indicator):
lines = ('ssa',)
def __init__(self, ssa_window):
self.params.ssa_window = ssa_window
# 这个很有用,会有 not maturity生成
self.addminperiod(self.params.ssa_window * 2)
def get_window_matrix(self, input_array, t, m):
# 将时间序列变成矩阵
temp = []
n = t - m + 1
for i in range(n):
temp.append(input_array[i:i + m])
window_matrix = np.array(temp)
return window_matrix
def svd_reduce(self, window_matrix):
# svd分解
u, s, v = np.linalg.svd(window_matrix)
m1, n1 = u.shape
m2, n2 = v.shape
index = s.argmax() # get the biggest index
u1 = u[:, index]
v1 = v[index]
u1 = u1.reshape((m1, 1))
v1 = v1.reshape((1, n2))
value = s.max()
new_matrix = value * (np.dot(u1, v1))
return new_matrix
def recreate_array(self, new_matrix, t, m):
# 时间序列重构
ret = []
n = t - m + 1
for p in range(1, t + 1):
if p < m:
alpha = p
elif p > t - m + 1:
alpha = t - p + 1
else:
alpha = m
sigma = 0
for j in range(1, m + 1):
i = p - j + 1
if i > 0 and i < n + 1:
sigma += new_matrix[i - 1][j - 1]
ret.append(sigma / alpha)
return ret
def SSA(self, input_array, t, m):
window_matrix = self.get_window_matrix(input_array, t, m)
new_matrix = self.svd_reduce(window_matrix)
new_array = self.recreate_array(new_matrix, t, m)
return new_array
def next(self):
data_serial = self.data.get(size=self.params.ssa_window * 2)
self.lines.ssa[0] = self.SSA(data_serial, len(data_serial), int(len(data_serial) / 2))[-1]
# Create a Stratey
class MyStrategy(bt.Strategy):
params = (
('ssa_window', 15),
('maperiod', 15),
)
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close
# To keep track of pending orders and buy price/commission
self.order = None
self.buyprice = None
self.buycomm = None
# Add a MovingAverageSimple indicator
self.ssa = ssa_index_ind(ssa_window=self.params.ssa_window, subplot=False)
# bt.indicator.LinePlotterIndicator(self.ssa, name='ssa')
self.sma = bt.indicators.SimpleMovingAverage(period=self.params.maperiod)
def start(self):
print("the world call me!")
def prenext(self):
print("not mature")
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enougth cash
if order.status in [order.Completed]:
if order.isbuy():
self.log(
'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else: # Sell
self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
(order.executed.price,
order.executed.value,
order.executed.comm))
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
(trade.pnl, trade.pnlcomm))
def next(self):
# Simply log the closing price of the series from the reference
self.log('Close, %.2f' % self.dataclose[0])
# Check if an order is pending ... if yes, we cannot send a 2nd one
if self.order:
return
# Check if we are in the market
if not self.position:
# Not yet ... we MIGHT BUY if ...
if self.dataclose[0] > self.ssa[0]:
# BUY, BUY, BUY!!! (with all possible default parameters)
self.log('BUY CREATE, %.2f' % self.dataclose[0])
# Keep track of the created order to avoid a 2nd order
self.order = self.buy()
else:
if self.dataclose[0] < self.ssa[0]:
# SELL, SELL, SELL!!! (with all possible default parameters)
self.log('SELL CREATE, %.2f' % self.dataclose[0])
# Keep track of the created order to avoid a 2nd order
self.order = self.sell()
def stop(self):
print("death")
if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()
# Add a strategy
cerebro.addstrategy(MyStrategy)
# 本地数据,笔者用Wind获取的东风汽车数据以csv形式存储在本地。
# parase_dates = True是为了读取csv为dataframe的时候能够自动识别datetime格式的字符串,big作为index
# 注意,这里最后的pandas要符合backtrader的要求的格式
dataframe = pd.read_csv('sz000651.csv', index_col=0, parse_dates=True)
dataframe['openinterest'] = 0
data = bt.feeds.PandasData(dataname=dataframe,
fromdate = datetime.datetime(2018, 1, 1),
todate = datetime.datetime(2019, 12, 31)
)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
# Set our desired cash start
cerebro.broker.setcash(10000.0)
# 设置每笔交易交易的股票数量
cerebro.addsizer(bt.sizers.FixedSize, stake=10)
# Set the commission
cerebro.broker.setcommission(commission=0.0)
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Run over everything
cerebro.run()
# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()