forked from waditu/czsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_trader_base.py
289 lines (242 loc) · 10.7 KB
/
test_trader_base.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# -*- coding: utf-8 -*-
"""
author: zengbin93
email: [email protected]
create_dt: 2021/11/7 21:07
"""
import os
import pandas as pd
from copy import deepcopy
from czsc.utils.cache import home_path
from czsc.traders.base import CzscSignals, BarGenerator, CzscTrader
from czsc.traders.sig_parse import get_signals_config, get_signals_freqs
from czsc.objects import Signal, Factor, Event, Operate, Position
from test.test_analyze import read_daily
def test_object_position():
bars = read_daily()
bg = BarGenerator(base_freq='日线', freqs=['周线', '月线'])
for bar in bars[:1000]:
bg.update(bar)
opens = [
Event(name='开多', operate=Operate.LO, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D1B_BUY1_一买_任意_任意_0"),
])
]),
Event(name='开空', operate=Operate.SO, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D1B_BUY1_一卖_任意_任意_0"),
])
]),
]
# 没有出场条件的测试
pos = Position(name="测试A", symbol=bg.symbol, opens=opens, exits=[], interval=0, timeout=20, stop_loss=300)
signals_config = get_signals_config(pos.unique_signals)
cs = CzscSignals(deepcopy(bg), signals_config=signals_config)
for bar in bars[1000:]:
cs.update_signals(bar)
pos.update(cs.s)
df = pd.DataFrame(pos.pairs)
assert df.shape == (16, 11)
assert len(cs.s) == 12
exits = [
Event(name='平多', operate=Operate.LE, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看空_强_任意_0"),
])
]),
Event(name='平空', operate=Operate.SE, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看多_强_任意_0"),
])
]),
]
pos = Position(name="测试B", symbol=bg.symbol, opens=opens, exits=exits, interval=0, timeout=20, stop_loss=300)
assert len(pos.unique_signals) == 4
assert len(pos.events[0].unique_signals) == 1
signals_config = get_signals_config(pos.unique_signals)
cs = CzscSignals(deepcopy(bg), signals_config=signals_config)
for bar in bars[1000:]:
cs.update_signals(bar)
pos.update(cs.s)
df = pd.DataFrame(pos.pairs)
assert df.shape == (17, 11)
assert len(cs.s) == 13
# 测试 dump 和 load
pos_x = pos.dump()
assert isinstance(pos_x, dict)
pos_x['name'] = "测试C"
pos_y = Position.load(pos_x)
cs = CzscSignals(deepcopy(bg), signals_config=get_signals_config(pos_y.unique_signals))
for bar in bars[1000:]:
cs.update_signals(bar)
pos_y.update(cs.s)
df = pd.DataFrame(pos_y.pairs)
assert df.shape == (17, 11)
assert len(cs.s) == 13
def test_generate_czsc_signals():
from czsc.traders.base import generate_czsc_signals
bars = read_daily()
signals_seq = [
"日线_D1B_BUY1_一买_任意_任意_0",
"日线_D1B_BUY1_一卖_任意_任意_0",
"日线_D0停顿分型_BE辅助V230106_看空_强_任意_0"
]
signals_config = get_signals_config(signals_seq)
# 通过 signals_seq 得到 freqs
freqs = get_signals_freqs(signals_seq)
# 通过 signals_config 得到 freqs
freqs1 = get_signals_freqs(signals_config)
assert freqs == freqs1
res = generate_czsc_signals(bars, signals_config=signals_config, freqs=freqs, sdt="20100101", init_n=500)
rdf = generate_czsc_signals(bars, signals_config=signals_config, freqs=freqs, sdt="20100101", init_n=500, df=True)
assert len(res) == len(rdf)
def test_czsc_trader():
bars = read_daily()
sdt = "20100101"
init_n = 2000
sdt = pd.to_datetime(sdt)
bars_left = [x for x in bars if x.dt < sdt]
if len(bars_left) <= init_n:
bars_left = bars[:init_n]
bars_right = bars[init_n:]
else:
bars_right = [x for x in bars if x.dt >= sdt]
bg = BarGenerator(base_freq='日线', freqs=['周线', '月线'])
for bar in bars_left:
bg.update(bar)
def __create_sma5_pos():
opens = [
Event(name='开多', operate=Operate.LO, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D1B_BUY1_一买_任意_任意_0"),
])
]),
Event(name='开空', operate=Operate.SO, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D1B_BUY1_一卖_任意_任意_0"),
])
]),
]
exits = [
Event(name='平多', operate=Operate.LE, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看空_强_任意_0"),
])
]),
Event(name='平空', operate=Operate.SE, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看多_强_任意_0"),
])
]),
]
pos = Position(symbol=bg.symbol, opens=opens, exits=exits, interval=0, timeout=20, stop_loss=100, name="测试A")
return pos
def __create_sma10_pos():
opens = [
Event(name='开多', operate=Operate.LO, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D2B_BUY1_一买_任意_任意_0"),
])
]),
Event(name='开空', operate=Operate.SO, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D2B_BUY1_一卖_任意_任意_0"),
])
]),
]
exits = [
Event(name='平多', operate=Operate.LE, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看空_强_任意_0"),
])
]),
Event(name='平空', operate=Operate.SE, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看多_强_任意_0"),
])
]),
]
pos = Position(symbol=bg.symbol, opens=opens, exits=exits, interval=0, timeout=20, stop_loss=100, name="测试B")
return pos
def __create_sma20_pos():
opens = [
Event(name='开多', operate=Operate.LO, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D3B_BUY1_一买_任意_任意_0"),
])
]),
Event(name='开空', operate=Operate.SO, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D3B_BUY1_一卖_任意_任意_0"),
])
]),
]
exits = [
Event(name='平多', operate=Operate.LE, factors=[
Factor(name="跌破SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看空_强_任意_0"),
])
]),
Event(name='平空', operate=Operate.SE, factors=[
Factor(name="站上SMA5", signals_all=[
Signal("日线_D0停顿分型_BE辅助V230106_看多_强_任意_0"),
])
]),
]
pos = Position(symbol=bg.symbol, opens=opens, exits=exits, interval=0, timeout=20, stop_loss=100, name="测试C")
return pos
positions = [__create_sma5_pos(), __create_sma10_pos(), __create_sma20_pos()]
signals_seq = []
for _pos in positions:
signals_seq.extend(_pos.unique_signals)
signals_config = get_signals_config(list(set(signals_seq)))
# 通过 update 执行
ct = CzscTrader(deepcopy(bg), signals_config=signals_config,
positions=[__create_sma5_pos(), __create_sma10_pos(), __create_sma20_pos()])
for bar in bars_right:
ct.update(bar)
for _pos in ct.positions:
if _pos.pos_changed:
assert _pos.operates[-1]['dt'] == _pos.end_dt
print(_pos.name, _pos.operates[-1], _pos.end_dt, _pos.pos)
assert ct.pos_changed
assert list(ct.positions[0].dump(False).keys()) == ['symbol', 'name', 'opens', 'exits', 'interval', 'timeout',
'stop_loss', 'T0']
assert list(ct.positions[0].dump(True).keys()) == ['symbol', 'name', 'opens', 'exits', 'interval', 'timeout',
'stop_loss', 'T0', 'pairs', 'holds']
assert [x.pos for x in ct.positions] == [0, 0, 0]
# 测试自定义仓位集成
def _weighted_ensemble(poss):
return 0.5 * poss['测试A'] + 0.5 * poss['测试B']
assert ct.get_ensemble_pos(_weighted_ensemble) == 0
assert ct.get_ensemble_pos('vote') == 0
assert ct.get_ensemble_pos('max') == 0
assert ct.get_ensemble_pos('mean') == 0
dfw = ct.get_ensemble_weight(method='mean')
assert len(dfw) == len(bars_right)
res = ct.weight_backtest(method='mean', res_path=os.path.join(home_path, "test_trader"))
# 通过 on_bar 执行
ct1 = CzscTrader(deepcopy(bg), signals_config=signals_config,
positions=[__create_sma5_pos(), __create_sma10_pos(), __create_sma20_pos()])
for bar in bars_right:
ct1.on_bar(bar)
# print(ct1.s)
print(f"{ct1.end_dt}: pos_seq = {[x.pos for x in ct1.positions]}mean_pos = {ct1.get_ensemble_pos('mean')}; "
f"vote_pos = {ct1.get_ensemble_pos('vote')}; max_pos = {ct1.get_ensemble_pos('max')}")
assert [x.pos for x in ct1.positions] == [0, 0, 0]
assert len(ct1.positions[0].pairs) == len(ct.positions[0].pairs)
assert len(ct1.positions[1].pairs) == len(ct.positions[1].pairs)
assert len(ct1.positions[2].pairs) == len(ct.positions[2].pairs)
# 通过 on_sig 执行
from czsc.traders.base import generate_czsc_signals
res = generate_czsc_signals(bars, signals_config=signals_config, freqs=['周线', '月线'], sdt=sdt, init_n=init_n)
ct2 = CzscTrader(positions=[__create_sma5_pos(), __create_sma10_pos(), __create_sma20_pos()])
for sig in res:
ct2.on_sig(sig)
print(f"{ct2.end_dt}: pos_seq = {[x.pos for x in ct2.positions]}mean_pos = {ct2.get_ensemble_pos('mean')}; "
f"vote_pos = {ct2.get_ensemble_pos('vote')}; max_pos = {ct2.get_ensemble_pos('max')}")
assert [x.pos for x in ct2.positions] == [0, 0, 0]
assert len(ct1.positions[0].pairs) == len(ct2.positions[0].pairs)
assert len(ct1.positions[1].pairs) == len(ct2.positions[1].pairs)
assert len(ct1.positions[2].pairs) == len(ct2.positions[2].pairs)