forked from harvey1673/pyktrader
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvtGateway.py
524 lines (429 loc) · 20.7 KB
/
vtGateway.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# encoding: UTF-8
import time
import os
import instrument
import csv
import workdays
import json
from misc import *
from eventEngine import *
from vtConstant import *
########################################################################
class Gateway(object):
"""交易接口"""
#----------------------------------------------------------------------
def __init__(self, agent = None, gatewayName = 'Gateway'):
"""Constructor"""
self.gatewayName = gatewayName
self.agent = agent
self.eventEngine = None
if agent != None:
self.eventEngine = agent.event_engine
self.qry_account = {}
self.qry_pos = {}
self.ref2order = {}
self.folder = ''
self.positions = {}
self.eod_flag = False
self.acc_info = { 'available': 0,
'locked_margin': 0,
'used_margin': 0,
'margin_cap': 1500000,
'curr_capital': 1000000,
'prev_capital': 1000000,
'pnl_total': 0,
'yday_pnl': 0,
'tday_pnl': 0,
'available': 0,
}
#----------------------------------------------------------------------
def event_subscribe(self):
pass
def onTick(self, tick):
"""市场行情推送"""
# 通用事件
event1 = Event(type=EVENT_TICK)
event1.dict['data'] = tick
self.eventEngine.put(event1)
# 特定合约代码的事件
event2 = Event(type=EVENT_TICK + tick.instID)
event2.dict['data'] = tick
self.eventEngine.put(event2)
#----------------------------------------------------------------------
def onTrade(self, trade):
"""成交信息推送"""
# 通用事件
event1 = Event(type=EVENT_TRADE)
event1.dict['data'] = trade
self.eventEngine.put(event1)
# 特定合约的成交事件
event2 = Event(type=EVENT_TRADE + trade.order_ref)
event2.dict['data'] = trade
self.eventEngine.put(event2)
#----------------------------------------------------------------------
def onOrder(self, order):
"""订单变化推送"""
# 通用事件
event1 = Event(type=EVENT_ORDER)
event1.dict['data'] = order
self.eventEngine.put(event1)
# 特定订单编号的事件
event2 = Event(type=EVENT_ORDER + order.order_ref)
event2.dict['data'] = order
self.eventEngine.put(event2)
#----------------------------------------------------------------------
def onPosition(self, position):
"""持仓信息推送"""
# 通用事件
event1 = Event(type=EVENT_POSITION)
event1.dict['data'] = position
self.eventEngine.put(event1)
# 特定合约代码的事件
event2 = Event(type=EVENT_POSITION+position.instID)
event2.dict['data'] = position
self.eventEngine.put(event2)
#----------------------------------------------------------------------
def onAccount(self, account):
"""账户信息推送"""
# 通用事件
event1 = Event(type=EVENT_ACCOUNT)
event1.dict['data'] = account
self.eventEngine.put(event1)
# 特定合约代码的事件
event2 = Event(type=EVENT_ACCOUNT+account.vtAccountID)
event2.dict['data'] = account
self.eventEngine.put(event2)
#----------------------------------------------------------------------
def onError(self, error):
"""错误信息推送"""
# 通用事件
logContent = error.errorMsg
self.onLog(logContent, level = logging.WARNING)
#----------------------------------------------------------------------
def onLog(self, log_content, level = logging.DEBUG):
"""日志推送"""
# 通用事件
event = Event(type=EVENT_LOG)
event.dict['data'] = log_content
event.dict['gateway'] = self.gatewayName
event.dict['level'] = level
self.eventEngine.put(event)
#----------------------------------------------------------------------
def onContract(self, contract):
"""合约基础信息推送"""
# 通用事件
event1 = Event(type=EVENT_CONTRACT)
event1.dict['data'] = contract
self.eventEngine.put(event1)
def get_local_positions(self, tday):
pos_date = tday
logfile = self.folder + 'EOD_Pos_' + pos_date.strftime('%y%m%d')+'.csv'
if not os.path.isfile(logfile):
pos_date = workdays.workday(pos_date, -1, CHN_Holidays)
logfile = self.folder + 'EOD_Pos_' + pos_date.strftime('%y%m%d')+'.csv'
if not os.path.isfile(logfile):
print "no prior position file is found"
return False
else:
self.eod_flag = True
with open(logfile, 'rb') as f:
reader = csv.reader(f)
for idx, row in enumerate(reader):
if row[0] == 'capital':
self.prev_capital = float(row[1])
elif row[0] == 'pos':
inst = row[1]
if inst in self.positions:
self.positions[inst].pos_yday.long = int(row[2])
self.positions[inst].pos_yday.short = int(row[3])
return True
def save_local_positions(self, tday):
file_prefix = self.folder
logfile = file_prefix + 'EOD_Pos_' + tday.strftime('%y%m%d')+'.csv'
if os.path.isfile(logfile):
return False
else:
with open(logfile,'wb') as log_file:
file_writer = csv.writer(log_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL);
for inst in self.positions:
pos = self.positions[inst]
pos.re_calc()
self.calc_margin()
file_writer.writerow(['capital', self.curr_capital])
for inst in self.positions:
pos = self.positions[inst]
if abs(pos.curr_pos.long) + abs(pos.curr_pos.short) > 0:
file_writer.writerow(['pos', inst, pos.curr_pos.long, pos.curr_pos.short])
return True
def calc_margin(self):
locked_margin = 0
used_margin = 0
yday_pnl = 0
tday_pnl = 0
for instID in self.positions:
inst = self.agent.instruments[instID]
pos = self.positions[instID]
under_price = 0.0
if (inst.ptype == instrument.ProductType.Option):
under_price = self.agent.instruments[inst.underlying].price
locked_margin += pos.locked_pos.long * inst.calc_margin_amount(ORDER_BUY, under_price)
locked_margin += pos.locked_pos.short * inst.calc_margin_amount(ORDER_SELL, under_price)
used_margin += pos.curr_pos.long * inst.calc_margin_amount(ORDER_BUY, under_price)
used_margin += pos.curr_pos.short * inst.calc_margin_amount(ORDER_SELL, under_price)
yday_pnl += (pos.pos_yday.long - pos.pos_yday.short) * (inst.price - inst.prev_close) * inst.multiple
tday_pnl += pos.tday_pos.long * (inst.price-pos.tday_avp.long) * inst.multiple
tday_pnl -= pos.tday_pos.short * (inst.price-pos.tday_avp.short) * inst.multiple
self.acc_info['locked_margin'] = locked_margin
self.acc_info['used_margin'] = used_margin
self.acc_info['pnl_total'] = yday_pnl + tday_pnl
self.acc_info['curr_capital'] = self.prev_capital + self.pnl_total
self.acc_info['available'] = self.curr_capital - self.locked_margin
#----------------------------------------------------------------------
def connect(self):
"""连接"""
pass
#----------------------------------------------------------------------
def subscribe(self, subscribeReq):
"""订阅行情"""
pass
#----------------------------------------------------------------------
def sendOrder(self, orderReq):
"""发单"""
pass
#----------------------------------------------------------------------
def cancelOrder(self, cancelOrderReq):
"""撤单"""
pass
#----------------------------------------------------------------------
def qryAccount(self):
"""查询账户资金"""
pass
#----------------------------------------------------------------------
def qryPosition(self):
"""查询持仓"""
pass
#----------------------------------------------------------------------
def close(self):
"""关闭"""
pass
########################################################################
class VtBaseData(object):
"""回调函数推送数据的基础类,其他数据类继承于此"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.gatewayName = EMPTY_STRING # Gateway名称
self.rawData = None # 原始数据
########################################################################
class VtTickData(VtBaseData):
"""Tick行情数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtTickData, self).__init__()
# 代码相关
self.symbol = EMPTY_STRING # 合约代码
self.exchange = EMPTY_STRING # 交易所代码
self.instID = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
# 成交数据
self.lastPrice = EMPTY_FLOAT # 最新成交价
self.lastVolume = EMPTY_INT # 最新成交量
self.volume = EMPTY_INT # 今天总成交量
self.openInterest = EMPTY_INT # 持仓量
self.time = EMPTY_STRING # 时间 11:20:56.5
self.date = EMPTY_STRING # 日期 20151009
# 常规行情
self.openPrice = EMPTY_FLOAT # 今日开盘价
self.highPrice = EMPTY_FLOAT # 今日最高价
self.lowPrice = EMPTY_FLOAT # 今日最低价
self.preClosePrice = EMPTY_FLOAT
self.upperLimit = EMPTY_FLOAT # 涨停价
self.lowerLimit = EMPTY_FLOAT # 跌停价
# 五档行情
self.bidPrice1 = EMPTY_FLOAT
self.bidPrice2 = EMPTY_FLOAT
self.bidPrice3 = EMPTY_FLOAT
self.bidPrice4 = EMPTY_FLOAT
self.bidPrice5 = EMPTY_FLOAT
self.askPrice1 = EMPTY_FLOAT
self.askPrice2 = EMPTY_FLOAT
self.askPrice3 = EMPTY_FLOAT
self.askPrice4 = EMPTY_FLOAT
self.askPrice5 = EMPTY_FLOAT
self.bidVolume1 = EMPTY_INT
self.bidVolume2 = EMPTY_INT
self.bidVolume3 = EMPTY_INT
self.bidVolume4 = EMPTY_INT
self.bidVolume5 = EMPTY_INT
self.askVolume1 = EMPTY_INT
self.askVolume2 = EMPTY_INT
self.askVolume3 = EMPTY_INT
self.askVolume4 = EMPTY_INT
self.askVolume5 = EMPTY_INT
########################################################################
class VtTradeData(VtBaseData):
"""成交数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtTradeData, self).__init__()
# 代码编号相关
self.symbol = EMPTY_STRING # 合约代码
self.exchange = EMPTY_STRING # 交易所代码
self.instID = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
self.tradeID = EMPTY_STRING # 成交编号
self.vtTradeID = EMPTY_STRING # 成交在vt系统中的唯一编号,通常是 Gateway名.成交编号
self.orderID = EMPTY_STRING # 订单编号
self.order_ref = EMPTY_STRING # 订单在vt系统中的唯一编号,通常是 Gateway名.订单编号
# 成交相关
self.direction = EMPTY_UNICODE # 成交方向
self.offset = EMPTY_UNICODE # 成交开平仓
self.price = EMPTY_FLOAT # 成交价格
self.volume = EMPTY_INT # 成交数量
self.tradeTime = EMPTY_STRING # 成交时间
########################################################################
class VtOrderData(VtBaseData):
"""订单数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtOrderData, self).__init__()
# 代码编号相关
self.symbol = EMPTY_STRING # 合约代码
self.exchange = EMPTY_STRING # 交易所代码
self.instID = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
self.orderID = EMPTY_STRING # 订单编号 local order ID
self.order_ref = EMPTY_STRING # for Order class object ID
self.orderSysID = EMPTY_STRING # remote order ID
# 报单相关
self.direction = EMPTY_UNICODE # 报单方向
self.offset = EMPTY_UNICODE # 报单开平仓
self.price = EMPTY_FLOAT # 报单价格
self.totalVolume = EMPTY_INT # 报单总数量
self.tradedVolume = EMPTY_INT # 报单成交数量
self.status = EMPTY_UNICODE # 报单状态
self.orderTime = EMPTY_STRING # 发单时间
self.cancelTime = EMPTY_STRING # 撤单时间
# CTP/LTS相关
self.frontID = EMPTY_INT # 前置机编号
self.sessionID = EMPTY_INT # 连接编号
########################################################################
class VtPositionData(VtBaseData):
"""持仓数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtPositionData, self).__init__()
# 代码编号相关
self.symbol = EMPTY_STRING # 合约代码
self.exchange = EMPTY_STRING # 交易所代码
self.instID = EMPTY_STRING # 合约在vt系统中的唯一代码,合约代码.交易所代码
# 持仓相关
self.direction = EMPTY_STRING # 持仓方向
self.position = EMPTY_INT # 持仓量
self.frozen = EMPTY_INT # 冻结数量
self.price = EMPTY_FLOAT # 持仓均价
self.vtPositionName = EMPTY_STRING # 持仓在vt系统中的唯一代码,通常是instID.方向
# 20151020添加
self.ydPosition = EMPTY_INT # 昨持仓
########################################################################
class VtAccountData(VtBaseData):
"""账户数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtAccountData, self).__init__()
# 账号代码相关
self.accountID = EMPTY_STRING # 账户代码
self.vtAccountID = EMPTY_STRING # 账户在vt中的唯一代码,通常是 Gateway名.账户代码
# 数值相关
self.preBalance = EMPTY_FLOAT # 昨日账户结算净值
self.balance = EMPTY_FLOAT # 账户净值
self.available = EMPTY_FLOAT # 可用资金
self.commission = EMPTY_FLOAT # 今日手续费
self.margin = EMPTY_FLOAT # 保证金占用
self.closeProfit = EMPTY_FLOAT # 平仓盈亏
self.positionProfit = EMPTY_FLOAT # 持仓盈亏
########################################################################
class VtErrorData(VtBaseData):
"""错误数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtErrorData, self).__init__()
self.errorID = EMPTY_STRING # 错误代码
self.errorMsg = EMPTY_UNICODE # 错误信息
self.additionalInfo = EMPTY_UNICODE # 补充信息
########################################################################
class VtLogData(VtBaseData):
"""日志数据类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtLogData, self).__init__()
self.logTime = time.strftime('%X', time.localtime()) # 日志生成时间
self.logContent = EMPTY_UNICODE # 日志信息
self.level = logging.DEBUG
########################################################################
class VtContractData(VtBaseData):
"""合约详细信息类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
super(VtBaseData, self).__init__()
self.symbol = EMPTY_STRING # 代码
self.exchange = EMPTY_STRING # 交易所代码
self.instID = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
self.name = EMPTY_UNICODE # 合约中文名
self.productClass = EMPTY_UNICODE # 合约类型
self.size = EMPTY_INT # 合约大小
self.priceTick = EMPTY_FLOAT # 合约最小价格TICK
# 期权相关
self.strikePrice = EMPTY_FLOAT # 期权行权价
self.underlyingSymbol = EMPTY_STRING # 标的物合约代码
self.optionType = EMPTY_UNICODE # 期权类型
########################################################################
class VtSubscribeReq(object):
"""订阅行情时传入的对象类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.symbol = EMPTY_STRING # 代码
self.exchange = EMPTY_STRING # 交易所
# 以下为IB相关
self.productClass = EMPTY_UNICODE # 合约类型
self.currency = EMPTY_STRING # 合约货币
self.expiry = EMPTY_STRING # 到期日
self.strikePrice = EMPTY_FLOAT # 行权价
self.optionType = EMPTY_UNICODE # 期权类型
########################################################################
class VtOrderReq(object):
"""发单时传入的对象类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.symbol = EMPTY_STRING # 代码
self.exchange = EMPTY_STRING # 交易所
self.price = EMPTY_FLOAT # 价格
self.volume = EMPTY_INT # 数量
self.priceType = EMPTY_STRING # 价格类型
self.direction = EMPTY_STRING # 买卖
self.offset = EMPTY_STRING # 开平
# 以下为IB相关
self.productClass = EMPTY_UNICODE # 合约类型
self.currency = EMPTY_STRING # 合约货币
self.expiry = EMPTY_STRING # 到期日
self.strikePrice = EMPTY_FLOAT # 行权价
self.optionType = EMPTY_UNICODE # 期权类型
########################################################################
class VtCancelOrderReq(object):
"""撤单时传入的对象类"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.symbol = EMPTY_STRING # 代码
self.exchange = EMPTY_STRING # 交易所
# 以下字段主要和CTP、LTS类接口相关
self.orderID = EMPTY_STRING # 报单号
self.frontID = EMPTY_STRING # 前置机号
self.sessionID = EMPTY_STRING # 会话号