forked from QuantFans/quantdigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
364 lines (311 loc) · 12.9 KB
/
Copy pathcontext.py
File metadata and controls
364 lines (311 loc) · 12.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# -*- coding: utf-8 -*-
##
# @file data.py
# @brief 数据上下文,交易上下文。
# @author wondereamer
# @version 0.2
# @date 2015-12-09
import six
import datetime
from quantdigger.datastruct import (
Direction,
PriceType,
Contract,
)
from quantdigger.engine.series import DateTimeSeries
from quantdigger.engine.context.data_context import DataContextAttributeHelper
class Context(object):
""" 上下文"""
def __init__(self, data, max_window):
self.ctx_dt_series = DateTimeSeries(
[datetime.datetime(2100, 1, 1)] * max_window,
'universal_time')
self.ctx_datetime = datetime.datetime(2100, 1, 1)
self.ctx_curbar = 0 # update by ExecuteUnit
self.on_bar = False # pass to on_bar function or on_symbol function
self._strategy_contexts = []
self._cur_strategy_context = None
self._cur_data_context = None
self._data_contexts = {} # str(PContract): DataContext
for key, value in six.iteritems(data):
self._data_contexts[key] = value
self._data_contexts[key.split('-')[0]] = value
self._data_contexts[key.split('.')[0]] = value
# latest price data
# Contract -> float
# Contract -> Bar
self._ticks = {}
self._bars = {}
def add_strategy_context(self, ctxs):
self._strategy_contexts.append(ctxs)
def switch_to_pcontract(self, pcon):
self._cur_data_context = self._data_contexts[pcon]
def switch_to_strategy(self, ith_comb, ith_strategy):
self._cur_strategy_context = self._strategy_contexts[ith_comb][ith_strategy]
if self.on_bar:
for data_context in six.itervalues(self._data_contexts):
data_context.ith_comb, data_context.ith_strategy = ith_comb, ith_strategy
else:
self._cur_data_context.ith_comb, self._cur_data_context.ith_strategy = ith_comb, ith_strategy
def time_aligned(self):
return (self._cur_data_context.datetime[0] <= self.ctx_datetime and
self._cur_data_context.next_datetime <= self.ctx_datetime)
# 第一根是必须运行
# return (self._cur_data_context.datetime[0] <= self.ctx_dt_series and
# self._cur_data_context.ctx_dt_series <= self.ctx_dt_series) or \
# self._cur_data_context.curbar == 0
def rolling_forward(self):
""" 更新最新tick价格,最新bar价格, 环境时间。 """
if self._cur_data_context.new_row:
self.ctx_dt_series.curbar = self.ctx_curbar
self.ctx_datetime = min(self._cur_data_context.next_datetime,
self.ctx_datetime)
try:
self.ctx_dt_series.data[self.ctx_curbar] = min(
self._cur_data_context.next_datetime, self.ctx_datetime)
except IndexError:
self.ctx_dt_series.data.append(
min(self._cur_data_context.next_datetime, self.ctx_datetime))
return True
hasnext, data = self._cur_data_context.rolling_forward()
if not hasnext:
return False
self.ctx_dt_series.curbar = self.ctx_curbar
try:
self.ctx_dt_series.data[self.ctx_curbar] = min(
self._cur_data_context.next_datetime, self.ctx_datetime)
except IndexError:
self.ctx_dt_series.data.append(min(
self._cur_data_context.next_datetime, self.ctx_datetime))
self.ctx_datetime = min(
self._cur_data_context.next_datetime, self.ctx_datetime)
return True
def update_user_vars(self):
""" 更新用户在策略中定义的变量, 如指标等。 """
self._cur_data_context.update_user_vars()
def update_system_vars(self):
""" 更新用户在策略中定义的变量, 如指标等。 """
self._cur_data_context.update_system_vars()
self._ticks[self._cur_data_context.contract] = \
self._cur_data_context.close[0]
self._bars[self._cur_data_context.contract] = \
self._cur_data_context.bar
oldbar = self._bars.setdefault(
self._cur_data_context.contract, self._cur_data_context.bar)
if self._cur_data_context.bar.datetime > oldbar.datetime:
# 处理不同周期时间滞后
self._bars[self._cur_data_context.contract] = \
self._cur_data_context.bar
def process_trading_events(self, at_baropen):
self._cur_strategy_context.update_environment(
self.ctx_datetime, self._ticks, self._bars)
self._cur_strategy_context.process_trading_events(at_baropen)
def __getitem__(self, strpcon):
""" 获取跨品种合约 """
return DataContextAttributeHelper(
self._data_contexts[strpcon.upper()])
def __getattr__(self, name):
return self._cur_data_context.get_item(name)
def __setattr__(self, name, value):
if name in [
'_data_contexts', '_cur_data_context', '_cur_strategy_context',
'_strategy_contexts', 'ctx_dt_series', '_ticks', '_bars',
'_trading', 'on_bar', 'ctx_curbar', 'ctx_datetime'
]:
super(Context, self).__setattr__(name, value)
else:
self._cur_data_context.add_item(name, value)
@property
def strategy(self):
""" 当前策略名 """
return self._cur_strategy_context.name
@property
def pcontract(self):
""" 当前周期合约 """
return self._cur_data_context.pcontract
@property
def symbol(self):
""" 当前合约 """
return str(self._cur_data_context.pcontract.contract)
@property
def curbar(self):
""" 当前是第几根k线, 从1开始 """
if self.on_bar:
return self.ctx_curbar + 1
else:
return self._cur_data_context.curbar
@property
def open(self):
""" k线开盘价序列 """
return self._cur_data_context.open
@property
def close(self):
""" k线收盘价序列 """
return self._cur_data_context.close
@property
def high(self):
""" k线最高价序列 """
return self._cur_data_context.high
@property
def low(self):
""" k线最低价序列 """
return self._cur_data_context.low
@property
def volume(self):
""" k线成交量序列 """
return self._cur_data_context.volume
@property
def datetime(self):
""" k线时间序列 """
if self.on_bar:
return self.ctx_dt_series
# return self._cur_data_context.datetime
else:
return self._cur_data_context.datetime
@property
def open_orders(self):
""" 未成交的订单 """
return list(self._cur_strategy_context.open_orders)
def buy(self, price, quantity, symbol=None):
""" 开多仓
Args:
price (float): 价格, 0表市价。
quantity (int): 数量。
symbol (str): 合约
"""
if not self.on_bar:
raise Exception('只有on_bar函数内能下单!')
if symbol:
contract = Contract(symbol) if isinstance(symbol, str) else symbol
else:
contract = self._cur_data_context.contract
price_type = PriceType.MKT if price == 0 else PriceType.LMT
self._cur_strategy_context.buy(price,
quantity, price_type,
contract)
def sell(self, price, quantity, symbol=None):
""" 平多仓。
Args:
price (float): 价格, 0表市价。
quantity (int): 数量。
symbol (str): 合约
"""
if not self.on_bar:
raise Exception('只有on_bar函数内能下单!')
if symbol:
contract = Contract(symbol) if isinstance(symbol, str) else symbol
else:
contract = self._cur_data_context.contract
price_type = PriceType.MKT if price == 0 else PriceType.LMT
self._cur_strategy_context.sell(price,
quantity, price_type,
contract)
def short(self, price, quantity, symbol=None):
""" 开空仓
Args:
price (float): 价格, 0表市价。
quantity (int): 数量。
symbol (str): 合约
"""
if not self.on_bar:
raise Exception('只有on_bar函数内能下单!')
if symbol:
contract = Contract(symbol) if isinstance(symbol, str) else symbol
else:
contract = self._cur_data_context.contract
price_type = PriceType.MKT if price == 0 else PriceType.LMT
self._cur_strategy_context.short(price,
quantity, price_type,
contract)
def cover(self, price, quantity, symbol=None):
""" 平空仓。
Args:
price (float): 价格, 0表市价。
quantity (int): 数量。
symbol (str): 合约
"""
if not self.on_bar:
raise Exception('只有on_bar函数内能下单!')
if symbol:
contract = Contract(symbol) if isinstance(symbol, str) else symbol
else:
contract = self._cur_data_context.contract
price_type = PriceType.MKT if price == 0 else PriceType.LMT
self._cur_strategy_context.cover(price,
quantity, price_type,
contract)
def position(self, direction='long', symbol=None):
""" 合约当前持仓仓位。
Args:
direction (str/int): 持仓方向。多头 - 'long' / 1 ;空头 - 'short' / 2
, 默认为多头。
symbol (str): 字符串合约,默认为None表示主合约。
Returns:
Position. 该合约的持仓
"""
if not self.on_bar:
raise Exception('只有on_bar函数内能查询当前持仓!')
direction = Direction.arg_to_type(direction)
contract = Contract(symbol) if symbol else \
self._cur_data_context.contract
# @TODO assert direction
return self._cur_strategy_context.position(contract, direction)
def all_positions(self):
""" 返回所有持仓列表 [Position] """
return self._cur_strategy_context.all_positions()
def pos(self, direction='long', symbol=None):
""" 合约的当前可平仓位。
Args:
direction (str/int): 持仓方向。多头 - 'long' / 1 ;空头 - 'short' / 2
, 默认为多头。
symbol (str): 字符串合约,默认为None表示主合约。
Returns:
int. 该合约的持仓数目。
"""
if not self.on_bar:
raise Exception('只有on_bar函数内能查询当前持仓!')
direction = Direction.arg_to_type(direction)
# @TODO symbol xxxxx
contract = Contract(symbol) if symbol else \
self._cur_data_context.contract
# @TODO assert direction
return self._cur_strategy_context.pos(contract, direction)
def cancel(self, orders):
""" 撤单 """
self._cur_strategy_context.cancel(orders)
def cash(self):
""" 现金。 """
if not self.on_bar:
raise Exception('只有on_bar函数内能查询可用资金!')
return self._cur_strategy_context.cash()
def equity(self):
""" 当前权益 """
if not self.on_bar:
raise Exception('只有on_bar函数内能查询当前权益!')
return self._cur_strategy_context.equity()
def profit(self, contract=None):
""" 当前持仓的历史盈亏 """
# if not self.on_bar:
# logger.warn('只有on_bar函数内能查询总盈亏!')
# return
pass
def plot_line(self, name, ith_window, x, y, styles, lw=1, ms=10, twinx=False):
self._cur_strategy_context.plot_line(name, ith_window - 1, x - 1, float(y),
styles, lw, ms, twinx)
def plot_text(self, name, ith_window, x, y, text, color='black', size=15, rotation=0):
self._cur_strategy_context.plot_text(name, ith_window-1, x-1, float(y),
text, color, size, rotation)
def day_profit(self, contract=None):
""" 当前持仓的浮动盈亏 """
#if not self.on_bar:
#logger.warn('只有on_bar函数内能查询浮动盈亏!')
#return
pass
def test_cash(self):
""" 当根bar时间终点撮合后的可用资金,用于测试。 """
self.process_trading_events(at_baropen=False)
return self.cash()
def test_equity(self):
""" 当根bar时间终点撮合后的权益,用于测试。 """
self.process_trading_events(at_baropen=False)
return self.equity()