Repository navigation
Expand file tree
/
Copy pathlogger.py
More file actions
374 lines (343 loc) · 12.8 KB
/
Copy pathlogger.py
File metadata and controls
374 lines (343 loc) · 12.8 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
365
366
367
368
369
370
371
372
373
374
# -*- coding: utf-8 -*-
from _ctypes import Structure
from datetime import datetime
import sys
from base.callbacks import CallBacks
from base.comm import get_depth_market_data, get_all_bids, get_all_asks
__author__ = 'Chunyou<snowtigersoft@126.com>'
class Logger(CallBacks):
is_flushing = False
market_data = []
trading_data = []
gbk_decode_key = ['Text', 'ErrorMsg', 'InstrumentName']
ord_decode_key = ['Type', 'OptionsType', 'Side', 'OpenClose', 'HedgeFlag', 'TimeInForce', 'Status', 'ExecType']
def __init__(self):
"""
Initialize current callback.
"""
CallBacks.__init__(self)
# The kinds of items to be logged.
self.data_kinds = [
# The following are callbacks.
"market_connected",
"market_rsp_error",
"market_rtn_depth_market_data_n",
"trading_connected",
"trading_rsp_error",
"trading_rsp_qry_depth_market_data",
"trading_rsp_qry_instrument",
"trading_rsp_qry_investor_position",
"trading_rsp_qry_investor_position_detail",
"trading_rsp_qry_trading_account",
"trading_rtn_order",
"trading_rtn_trade",
# The following are method invocations.
"invoke_market_connect",
"invoke_market_disconnect",
"invoke_market_subscribe",
"invoke_market_unsubscribe",
"invoke_trading_connect",
"invoke_trading_disconnect",
"invoke_trading_send_order",
"invoke_trading_cancel_order",
"invoke_trading_get_investor_position",
"invoke_trading_get_investor_position_details",
"invoke_trading_get_order",
"invoke_trading_get_trading_account",
"invoke_trading_get_instrument",
"invoke_trading_get_commission_rate",
"invoke_trading_get_margin_rate",
"invoke_trading_get_deep_market_data"
]
# The maximum number of records to keep in memory before
# writing them into permanent storage.
self.in_memory_records = 100
# The time formatter function pointer.
# If not None, it should be a function which takes a datetime object as input and returns an adapted
# datetime object. Mainly used for adapting time zone time information.
self.datetime_adapter = None
def _format(self, k, v):
if isinstance(v, Structure):
result = {}
for k1, _ in v._fields_:
result[k1] = getattr(v, k1).decode('gbk') if k1 in self.gbk_decode_key else (ord(getattr(v, k1)) if k1 in self.ord_decode_key else getattr(v, k1))
elif 'ctypes' in str(type(v)) or 'c_char_Array_' in str(type(v)):
result = v.value
elif k == 'status':
result = ord(v)
else:
result = v
return result
def _do_log(self, content, data_kind=None):
data_kind = data_kind if data_kind else sys._getframe(1).f_code.co_name[3:]
if data_kind in self.data_kinds:
for k, v in content.iteritems():
if isinstance(v, list):
content[k] = [self._format(k, vv) for vv in v]
else:
content[k] = self._format(k, v)
now = datetime.now()
if self.datetime_adapter is not None:
now = self.datetime_adapter(now)
if data_kind in ['market_rtn_depth_market_data_n']:
self.market_data.append([now, data_kind, content])
else:
self.trading_data.append([now, data_kind, content])
if len(self.market_data) + len(self.trading_data) >= self.in_memory_records:
self.flush()
#handel on_invoke_*
def __getattr__(self, item):
if item[:10] == 'on_invoke_':
def on_invoke(kwargs):
self._do_log(kwargs, data_kind=item[3:])
return on_invoke
def on_market_connected(self, p_api, rsp_user_login, status):
"""
callback on market server connect status change
:param p_api: c_void_p
:param rsp_user_login: RspUserLoginField
:param status: ConnectionStatus int
:return:
"""
self._do_log({
"p_api": p_api,
"rsp_user_login": rsp_user_login,
"status": status
})
def on_market_rsp_error(self, p_api, rsp_info, b_is_last):
"""
callback on market server when error occurred
:param p_api: c_void_p
:param rsp_info: ErrorField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"rsp_info": rsp_info,
"b_is_last": b_is_last
})
def on_market_rtn_depth_market_data_n(self, p_api, p_depth_market_data_n):
"""
callback on market server when receive market data
:param p_api: c_void_p
:param p_depth_market_data_n: DepthMarketDataNField
:return:
"""
depth_market_data = get_depth_market_data(p_depth_market_data_n)
bids = get_all_bids(p_depth_market_data_n)
asks = get_all_asks(p_depth_market_data_n)
self._do_log({
"p_api": p_api,
"depth_market_data": depth_market_data,
"bids": bids,
"asks": asks
})
def on_trading_connected(self, p_api, rsp_user_login, status):
"""
callback on trading server connect status change
:param p_api: c_void_p
:param rsp_user_login: CThostFtdcRspUserLoginField
:param status: ConnectionStatus int
:return:
"""
self._do_log({
"p_api": p_api,
"rsp_user_login": rsp_user_login,
"status": status
})
def on_trading_rsp_error(self, p_api, rsp_info, b_is_last):
"""
callback on trading server when error occurred
:param p_api: c_void_p
:param rsp_info: CThostFtdcRspInfoField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"rsp_info": rsp_info,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_depth_market_data(self, p_api, depth_market_data, rsp_info, b_is_last):
"""
请求查询行情响应。当客户端发出请求查询行情指令后,交易托管系统返回响应时,该方法会被调用。
:param p_api: c_void_p
:param depth_market_data: CThostFtdcDepthMarketDataField
:param rsp_info: CThostFtdcRspInfoField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"depth_market_data": depth_market_data,
"rsp_info": rsp_info,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_instrument(self, p_api, instrument, b_is_last):
"""
请求查询合约响应。当客户端发出请求查询合约指令后,交易托管系统返回响应时,该方法会被调用。
:param p_api: c_void_p
:param instrument: InstrumentField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"instrument": instrument,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_investor_position(self, p_api, investor_position, b_is_last):
"""
投资者持仓查询应答。当客户端发出投资者持仓查询指令后,后交易托管系统返回响应时,该方法会被调用。
:param p_api: c_void_p
:param investor_position: PositionField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"investor_position": investor_position,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_trading_account(self, p_api, trading_account, b_is_last):
"""
请求查询资金账户响应。当客户端发出请求查询资金账户指令后,交易托管系统返回响应时,该方法会被调用。
:param p_api: c_void_p
:param trading_account: AccountField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"trading_account": trading_account,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_settlement_info(self, p_api, settlement_info, b_is_last):
"""
请求查询结算信息响应。当客户端发出请求查询结算信息指令后,交易托管系统返回响应时,该方法会被调用。
:param p_api: c_void_p
:param settlement_info: SettlementInfoField
:param b_is_last: boolean, True if it is the last response for this request
:return:
"""
self._do_log({
"p_api": p_api,
"settlement_info": settlement_info,
"b_is_last": b_is_last
})
def on_trading_rtn_order(self, p_api, order):
"""
报单回报。当客户端进行报单录入、报单操作及其它原因(如部分成交)导致报单状态发生变化时,交易托管系统会主动通知客户端,该方法会被调用。
:param p_api: c_void_p
:param order: OrderField
"""
self._do_log({
"p_api": p_api,
"order": order
})
def on_trading_rtn_trade(self, p_api, trade):
"""
成交回报。当发生成交时交易托管系统会通知客户端,该方法会被调用。
:param p_api: c_void_p
:param trade:TradeField
:return:
"""
self._do_log({
"p_api": p_api,
"trade": trade
})
def on_trading_rtn_quote(self, p_api, quote):
"""
Quote
:param p_api: c_void_p
:param quote: QuoteField
"""
self._do_log({
"p_api": p_api,
"quote": quote
})
def on_trading_rtn_quote_request(self, p_api, quote_request):
"""
Quote Request
:param p_api: c_void_p
:param quote_request: QuoteRequestField
"""
self._do_log({
"p_api": p_api,
"quote_request": quote_request
})
def on_trading_rsp_qry_historical_ticks(self, p_api, tick, historical_data_request, b_is_last):
"""
Historical Data
:param p_api:
:param tick: TickField
:param historical_data_request: HistoricalDataRequestField
:param b_is_last:
:return:
"""
self._do_log({
"p_api": p_api,
"tick": tick,
"historical_data_request": historical_data_request,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_historical_bars(self, p_api, bar, historical_data_request, b_is_last):
"""
Historical Data
:param p_api:
:param bar: BarField
:param historical_data_request: HistoricalDataRequestField
:param b_is_last:
:return:
"""
self._do_log({
"p_api": p_api,
"bar": bar,
"historical_data_request": historical_data_request,
"b_is_last": b_is_last
})
def on_trading_rsp_qry_investor(self, p_api, investor):
"""
Qry investor
:param p_api:
:param investor:
:return:
"""
self._do_log({
"p_api": p_api,
"investor": investor
})
def on_trading_filter_subscribe(self, p_api, exchange, instrument_part1, instrument_part2, instrument_part3, pInstrument):
"""
Filter Subscribe
:param p_api:
:param exchange:
:param instrument_part1:
:param instrument_part2:
:param instrument_part3:
:param pInstrument:
:return:
"""
self._do_log({
"p_api": p_api,
"exchange": exchange,
"instrument_part1": instrument_part1,
"instrument_part2": instrument_part2,
"instrument_part3": instrument_part3,
"pInstrument": pInstrument
})
def flush(self):
"""
Flush unwritten data into permanent storage. Can be used when the trading program ends.
"""
if not self.is_flushing:
self.is_flushing = True
self.flush_impl()
self.is_flushing = False
def flush_impl(self):
"""
Sub-class should redefine this to implement permanent storage..
:return:
"""
pass