-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBoll_Std_Vix.py
170 lines (135 loc) · 4.52 KB
/
Boll_Std_Vix.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
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
from vnpy.app.cta_strategy.new_strategy import NewBarGenerator
class Boll_Std_vix(CtaTemplate):
""""""
author = "yunya"
win_open = 15
boll_window = 80
atr_window = 30
sl_multiplier = 10.0
fixed_size = 1
entry_crossover = 0
atr_value = 0
intra_trade_high = 0
intra_trade_low = 0
long_stop = 0
short_stop = 0
parameters = [
"win_open",
"boll_window",
"sl_multiplier",
"fixed_size",
]
variables = [
"entry_crossover",
"atr_value",
"intra_trade_high",
"intra_trade_low",
"long_stop",
"short_stop"
]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super().__init__(cta_engine, strategy_name, vt_symbol, setting)
self.bg = NewBarGenerator(self.on_bar, self.win_open, self.on_xmin_bar)
self.am = ArrayManager(self.boll_window + 100)
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")
self.load_bar(10)
def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log("策略启动")
self.put_event()
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")
self.put_event()
def on_tick(self, tick: TickData):
"""
Callback of new tick data update.
"""
self.bg.update_tick(tick)
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.bg.update_bar(bar)
def on_xmin_bar(self, bar: BarData):
""""""
self.cancel_all()
am = self.am
am.update_bar(bar)
if not am.inited:
return
# Calculate array 计算数组
self.sma_array = am.sma(self.boll_window, True)
std_array = am.std(self.boll_window, True)
dev = abs(self.am.close[:-1] - self.sma_array[:-1]) / std_array[:-1]
dev_max = dev[-self.boll_window:].max()
self.boll_up_array = self.sma_array + std_array * dev_max
self.boll_down_array = self.sma_array - std_array * dev_max
# Get current and last index
current_sma = self.sma_array[-1]
last_sma = self.sma_array[-2]
last_close = self.am.close[-2]
currnet_boll_up = self.boll_up_array[-1]
last_boll_up = self.boll_up_array[-2]
current_boll_down = self.boll_down_array[-1]
last_boll_down = self.boll_down_array[-2]
# Get crossover
if (bar.close_price > currnet_boll_up and last_close <= last_boll_up):
self.entry_crossover = 1
elif(bar.close_price < current_boll_down and last_close >=last_boll_down):
self.entry_crossover = -1
self.atr_value = am.atr(self.atr_window)
if not self.pos:
self.intra_trade_high = bar.high_price
self.intra_trade_low = bar.low_price
if self.entry_crossover > 0:
self.buy(bar.close_price, self.fixed_size, True)
elif self.entry_crossover < 0:
self.short(bar.close_price, self.fixed_size, True)
elif self.pos > 0:
self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
self.intra_trade_low = bar.low_price
self.long_stop = self.intra_trade_high - self.atr_value * self.sl_multiplier
self.sell(self.long_stop, abs(self.pos), True)
elif self.pos < 0:
self.intra_trade_high = bar.high_price
self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
self.short_stop = self.intra_trade_low + self.atr_value * self.sl_multiplier
self.cover(self.short_stop, abs(self.pos), True)
self.put_event()
def on_order(self, order: OrderData):
"""
Callback of new order data update.
"""
self.put_event()
pass
def on_trade(self, trade: TradeData):
"""
Callback of new trade data update.
"""
self.put_event()
def on_stop_order(self, stop_order: StopOrder):
"""
Callback of stop order update.
"""
self.put_event()
pass