2015-09-28 03:42:35 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
import shelve
|
2015-10-19 07:04:03 +00:00
|
|
|
|
from collections import OrderedDict
|
2017-01-05 14:57:08 +00:00
|
|
|
|
from datetime import datetime
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
from pymongo import MongoClient
|
|
|
|
|
from pymongo.errors import ConnectionFailure
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
from eventEngine import *
|
2015-10-16 08:50:44 +00:00
|
|
|
|
from vtGateway import *
|
2016-04-20 15:14:21 +00:00
|
|
|
|
from vtFunction import loadMongoSetting
|
2016-04-15 12:20:49 +00:00
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
from ctaAlgo.ctaEngine import CtaEngine
|
2016-04-15 12:20:49 +00:00
|
|
|
|
from dataRecorder.drEngine import DrEngine
|
2016-05-03 12:35:59 +00:00
|
|
|
|
from riskManager.rmEngine import RmEngine
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class MainEngine(object):
|
|
|
|
|
"""主引擎"""
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""Constructor"""
|
2017-01-05 14:57:08 +00:00
|
|
|
|
# 记录今日日期
|
|
|
|
|
self.todayDate = datetime.now().strftime('%Y%m%d')
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
# 创建事件引擎
|
2017-01-15 10:59:30 +00:00
|
|
|
|
self.eventEngine = EventEngine2()
|
2015-09-28 03:42:35 +00:00
|
|
|
|
self.eventEngine.start()
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
# 创建数据引擎
|
|
|
|
|
self.dataEngine = DataEngine(self.eventEngine)
|
|
|
|
|
|
2015-12-30 14:01:17 +00:00
|
|
|
|
# MongoDB数据库相关
|
|
|
|
|
self.dbClient = None # MongoDB客户端对象
|
|
|
|
|
|
|
|
|
|
# 调用一个个初始化函数
|
|
|
|
|
self.initGateway()
|
2016-04-15 12:20:49 +00:00
|
|
|
|
|
|
|
|
|
# 扩展模块
|
|
|
|
|
self.ctaEngine = CtaEngine(self, self.eventEngine)
|
|
|
|
|
self.drEngine = DrEngine(self, self.eventEngine)
|
2016-05-03 12:35:59 +00:00
|
|
|
|
self.rmEngine = RmEngine(self, self.eventEngine)
|
2015-12-30 14:01:17 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initGateway(self):
|
|
|
|
|
"""初始化接口对象"""
|
2015-09-28 03:42:35 +00:00
|
|
|
|
# 用来保存接口对象的字典
|
2015-10-19 07:04:03 +00:00
|
|
|
|
self.gatewayDict = OrderedDict()
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
# 创建我们想要接入的接口对象
|
2015-11-20 06:11:29 +00:00
|
|
|
|
try:
|
2015-12-23 06:15:41 +00:00
|
|
|
|
from ctpGateway.ctpGateway import CtpGateway
|
2015-11-20 06:11:29 +00:00
|
|
|
|
self.addGateway(CtpGateway, 'CTP')
|
|
|
|
|
self.gatewayDict['CTP'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2015-11-20 06:11:29 +00:00
|
|
|
|
try:
|
2015-12-23 06:15:41 +00:00
|
|
|
|
from ltsGateway.ltsGateway import LtsGateway
|
2015-11-20 06:11:29 +00:00
|
|
|
|
self.addGateway(LtsGateway, 'LTS')
|
|
|
|
|
self.gatewayDict['LTS'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2016-10-02 15:19:51 +00:00
|
|
|
|
try:
|
|
|
|
|
from xtpGateway.xtpGateway import XtpGateway
|
|
|
|
|
self.addGateway(XtpGateway, 'XTP')
|
|
|
|
|
self.gatewayDict['XTP'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
|
|
|
|
|
2015-12-09 03:19:45 +00:00
|
|
|
|
try:
|
2015-12-23 06:15:41 +00:00
|
|
|
|
from ksotpGateway.ksotpGateway import KsotpGateway
|
2015-12-09 03:19:45 +00:00
|
|
|
|
self.addGateway(KsotpGateway, 'KSOTP')
|
|
|
|
|
self.gatewayDict['KSOTP'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2015-12-17 02:15:12 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2015-12-23 06:15:41 +00:00
|
|
|
|
from femasGateway.femasGateway import FemasGateway
|
2015-12-17 02:15:12 +00:00
|
|
|
|
self.addGateway(FemasGateway, 'FEMAS')
|
|
|
|
|
self.gatewayDict['FEMAS'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2016-04-02 09:04:58 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from xspeedGateway.xspeedGateway import XspeedGateway
|
|
|
|
|
self.addGateway(XspeedGateway, 'XSPEED')
|
|
|
|
|
self.gatewayDict['XSPEED'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2016-12-11 09:36:13 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from qdpGateway.qdpGateway import QdpGateway
|
|
|
|
|
self.addGateway(QdpGateway, 'QDP')
|
|
|
|
|
self.gatewayDict['QDP'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2016-04-02 09:04:58 +00:00
|
|
|
|
|
2015-11-20 06:11:29 +00:00
|
|
|
|
try:
|
2015-12-23 06:15:41 +00:00
|
|
|
|
from ksgoldGateway.ksgoldGateway import KsgoldGateway
|
|
|
|
|
self.addGateway(KsgoldGateway, 'KSGOLD')
|
|
|
|
|
self.gatewayDict['KSGOLD'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
|
|
|
|
|
2016-03-30 12:20:52 +00:00
|
|
|
|
try:
|
|
|
|
|
from sgitGateway.sgitGateway import SgitGateway
|
|
|
|
|
self.addGateway(SgitGateway, 'SGIT')
|
|
|
|
|
self.gatewayDict['SGIT'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
|
|
|
|
|
2015-12-23 06:15:41 +00:00
|
|
|
|
try:
|
|
|
|
|
from windGateway.windGateway import WindGateway
|
2015-11-20 06:11:29 +00:00
|
|
|
|
self.addGateway(WindGateway, 'Wind')
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2015-10-19 07:04:03 +00:00
|
|
|
|
|
2015-11-20 06:11:29 +00:00
|
|
|
|
try:
|
2015-12-23 06:15:41 +00:00
|
|
|
|
from ibGateway.ibGateway import IbGateway
|
2015-11-20 06:11:29 +00:00
|
|
|
|
self.addGateway(IbGateway, 'IB')
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2016-03-06 10:09:15 +00:00
|
|
|
|
|
2016-10-22 16:49:04 +00:00
|
|
|
|
try:
|
|
|
|
|
from shzdGateway.shzdGateway import ShzdGateway
|
|
|
|
|
self.addGateway(ShzdGateway, 'SHZD')
|
2016-10-25 14:31:23 +00:00
|
|
|
|
self.gatewayDict['SHZD'].setQryEnabled(True)
|
2016-10-22 16:49:04 +00:00
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
|
|
|
|
|
2016-03-06 10:09:15 +00:00
|
|
|
|
try:
|
|
|
|
|
from oandaGateway.oandaGateway import OandaGateway
|
|
|
|
|
self.addGateway(OandaGateway, 'OANDA')
|
|
|
|
|
self.gatewayDict['OANDA'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
2016-07-12 15:38:26 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from okcoinGateway.okcoinGateway import OkcoinGateway
|
|
|
|
|
self.addGateway(OkcoinGateway, 'OKCOIN')
|
|
|
|
|
self.gatewayDict['OKCOIN'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
2017-02-17 16:14:52 +00:00
|
|
|
|
print e
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2017-02-17 16:14:52 +00:00
|
|
|
|
try:
|
|
|
|
|
from huobiGateway.huobiGateway import HuobiGateway
|
|
|
|
|
self.addGateway(HuobiGateway, 'HUOBI')
|
|
|
|
|
self.gatewayDict['HUOBI'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
|
|
|
|
|
2017-02-24 18:05:06 +00:00
|
|
|
|
try:
|
|
|
|
|
from lhangGateway.lhangGateway import LhangGateway
|
|
|
|
|
self.addGateway(LhangGateway, 'LHANG')
|
|
|
|
|
self.gatewayDict['LHANG'].setQryEnabled(True)
|
|
|
|
|
except Exception, e:
|
|
|
|
|
print e
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-09-28 03:42:35 +00:00
|
|
|
|
def addGateway(self, gateway, gatewayName=None):
|
|
|
|
|
"""创建接口"""
|
|
|
|
|
self.gatewayDict[gatewayName] = gateway(self.eventEngine, gatewayName)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connect(self, gatewayName):
|
|
|
|
|
"""连接特定名称的接口"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if gatewayName in self.gatewayDict:
|
|
|
|
|
gateway = self.gatewayDict[gatewayName]
|
|
|
|
|
gateway.connect()
|
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'接口不存在:%s' %gatewayName)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def subscribe(self, subscribeReq, gatewayName):
|
|
|
|
|
"""订阅特定接口的行情"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if gatewayName in self.gatewayDict:
|
|
|
|
|
gateway = self.gatewayDict[gatewayName]
|
|
|
|
|
gateway.subscribe(subscribeReq)
|
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'接口不存在:%s' %gatewayName)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def sendOrder(self, orderReq, gatewayName):
|
|
|
|
|
"""对特定接口发单"""
|
2016-05-03 14:38:03 +00:00
|
|
|
|
# 如果风控检查失败则不发单
|
2017-03-08 14:27:08 +00:00
|
|
|
|
if not self.rmEngine.checkRisk(orderReq):
|
|
|
|
|
return ''
|
2017-03-07 14:22:21 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if gatewayName in self.gatewayDict:
|
|
|
|
|
gateway = self.gatewayDict[gatewayName]
|
|
|
|
|
return gateway.sendOrder(orderReq)
|
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'接口不存在:%s' %gatewayName)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def cancelOrder(self, cancelOrderReq, gatewayName):
|
|
|
|
|
"""对特定接口撤单"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if gatewayName in self.gatewayDict:
|
|
|
|
|
gateway = self.gatewayDict[gatewayName]
|
|
|
|
|
gateway.cancelOrder(cancelOrderReq)
|
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'接口不存在:%s' %gatewayName)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2017-02-13 02:17:10 +00:00
|
|
|
|
def qryAccount(self, gatewayName):
|
2015-09-28 03:42:35 +00:00
|
|
|
|
"""查询特定接口的账户"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if gatewayName in self.gatewayDict:
|
|
|
|
|
gateway = self.gatewayDict[gatewayName]
|
2016-05-28 14:31:47 +00:00
|
|
|
|
gateway.qryAccount()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'接口不存在:%s' %gatewayName)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2015-12-30 14:01:17 +00:00
|
|
|
|
def qryPosition(self, gatewayName):
|
2015-09-28 03:42:35 +00:00
|
|
|
|
"""查询特定接口的持仓"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if gatewayName in self.gatewayDict:
|
|
|
|
|
gateway = self.gatewayDict[gatewayName]
|
2016-05-28 14:31:47 +00:00
|
|
|
|
gateway.qryPosition()
|
2015-10-16 08:50:44 +00:00
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'接口不存在:%s' %gatewayName)
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def exit(self):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""退出程序前调用,保证正常退出"""
|
|
|
|
|
# 安全关闭所有接口
|
|
|
|
|
for gateway in self.gatewayDict.values():
|
|
|
|
|
gateway.close()
|
|
|
|
|
|
2015-09-28 03:42:35 +00:00
|
|
|
|
# 停止事件引擎
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.eventEngine.stop()
|
2015-09-28 03:42:35 +00:00
|
|
|
|
|
2016-06-13 14:48:23 +00:00
|
|
|
|
# 停止数据记录引擎
|
|
|
|
|
self.drEngine.stop()
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
# 保存数据引擎里的合约数据到硬盘
|
|
|
|
|
self.dataEngine.saveContracts()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def writeLog(self, content):
|
|
|
|
|
"""快速发出日志事件"""
|
|
|
|
|
log = VtLogData()
|
|
|
|
|
log.logContent = content
|
|
|
|
|
event = Event(type_=EVENT_LOG)
|
|
|
|
|
event.dict_['data'] = log
|
|
|
|
|
self.eventEngine.put(event)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def dbConnect(self):
|
|
|
|
|
"""连接MongoDB数据库"""
|
|
|
|
|
if not self.dbClient:
|
2016-04-20 15:14:21 +00:00
|
|
|
|
# 读取MongoDB的设置
|
2017-01-05 14:57:08 +00:00
|
|
|
|
host, port, logging = loadMongoSetting()
|
2016-04-20 15:14:21 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
try:
|
2016-05-19 15:33:09 +00:00
|
|
|
|
# 设置MongoDB操作的超时时间为0.5秒
|
2016-11-22 13:39:41 +00:00
|
|
|
|
self.dbClient = MongoClient(host, port, connectTimeoutMS=500)
|
2016-05-19 15:33:09 +00:00
|
|
|
|
|
|
|
|
|
# 调用server_info查询服务器状态,防止服务器异常并未连接成功
|
2016-05-18 13:15:58 +00:00
|
|
|
|
self.dbClient.server_info()
|
2016-05-19 15:33:09 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.writeLog(u'MongoDB连接成功')
|
2017-01-05 14:57:08 +00:00
|
|
|
|
|
|
|
|
|
# 如果启动日志记录,则注册日志事件监听函数
|
|
|
|
|
if logging:
|
|
|
|
|
self.eventEngine.register(EVENT_LOG, self.dbLogging)
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
except ConnectionFailure:
|
|
|
|
|
self.writeLog(u'MongoDB连接失败')
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def dbInsert(self, dbName, collectionName, d):
|
|
|
|
|
"""向MongoDB中插入数据,d是具体数据"""
|
|
|
|
|
if self.dbClient:
|
|
|
|
|
db = self.dbClient[dbName]
|
|
|
|
|
collection = db[collectionName]
|
2016-11-22 13:39:41 +00:00
|
|
|
|
collection.insert_one(d)
|
2017-01-05 14:31:42 +00:00
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'数据插入失败,MongoDB没有连接')
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def dbQuery(self, dbName, collectionName, d):
|
|
|
|
|
"""从MongoDB中读取数据,d是查询要求,返回的是数据库查询的指针"""
|
|
|
|
|
if self.dbClient:
|
|
|
|
|
db = self.dbClient[dbName]
|
|
|
|
|
collection = db[collectionName]
|
|
|
|
|
cursor = collection.find(d)
|
2017-01-15 10:59:30 +00:00
|
|
|
|
if cursor:
|
|
|
|
|
return list(cursor)
|
|
|
|
|
else:
|
|
|
|
|
return []
|
2015-10-16 08:50:44 +00:00
|
|
|
|
else:
|
2017-01-05 14:31:42 +00:00
|
|
|
|
self.writeLog(u'数据查询失败,MongoDB没有连接')
|
2016-12-15 14:04:09 +00:00
|
|
|
|
return []
|
2016-11-22 13:39:41 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def dbUpdate(self, dbName, collectionName, d, flt, upsert=False):
|
|
|
|
|
"""向MongoDB中更新数据,d是具体数据,flt是过滤条件,upsert代表若无是否要插入"""
|
|
|
|
|
if self.dbClient:
|
|
|
|
|
db = self.dbClient[dbName]
|
|
|
|
|
collection = db[collectionName]
|
|
|
|
|
collection.replace_one(flt, d, upsert)
|
2017-01-05 14:31:42 +00:00
|
|
|
|
else:
|
|
|
|
|
self.writeLog(u'数据更新失败,MongoDB没有连接')
|
2017-01-05 14:57:08 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def dbLogging(self, event):
|
|
|
|
|
"""向MongoDB中插入日志"""
|
|
|
|
|
log = event.dict_['data']
|
|
|
|
|
d = {
|
|
|
|
|
'content': log.logContent,
|
|
|
|
|
'time': log.logTime,
|
|
|
|
|
'gateway': log.gatewayName
|
|
|
|
|
}
|
|
|
|
|
self.dbInsert(LOG_DB_NAME, self.todayDate, d)
|
2015-12-30 14:01:17 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getContract(self, vtSymbol):
|
|
|
|
|
"""查询合约"""
|
|
|
|
|
return self.dataEngine.getContract(vtSymbol)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getAllContracts(self):
|
|
|
|
|
"""查询所有合约(返回列表)"""
|
|
|
|
|
return self.dataEngine.getAllContracts()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getOrder(self, vtOrderID):
|
|
|
|
|
"""查询委托"""
|
|
|
|
|
return self.dataEngine.getOrder(vtOrderID)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getAllWorkingOrders(self):
|
|
|
|
|
"""查询所有的活跃的委托(返回列表)"""
|
|
|
|
|
return self.dataEngine.getAllWorkingOrders()
|
|
|
|
|
|
2016-12-19 23:09:12 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getAllGatewayNames(self):
|
|
|
|
|
"""查询引擎中所有可用接口的名称"""
|
|
|
|
|
return self.gatewayDict.keys()
|
|
|
|
|
|
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class DataEngine(object):
|
|
|
|
|
"""数据引擎"""
|
|
|
|
|
contractFileName = 'ContractData.vt'
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self, eventEngine):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
self.eventEngine = eventEngine
|
|
|
|
|
|
|
|
|
|
# 保存合约详细信息的字典
|
|
|
|
|
self.contractDict = {}
|
|
|
|
|
|
|
|
|
|
# 保存委托数据的字典
|
|
|
|
|
self.orderDict = {}
|
|
|
|
|
|
|
|
|
|
# 保存活动委托数据的字典(即可撤销)
|
|
|
|
|
self.workingOrderDict = {}
|
|
|
|
|
|
|
|
|
|
# 读取保存在硬盘的合约数据
|
|
|
|
|
self.loadContracts()
|
|
|
|
|
|
|
|
|
|
# 注册事件监听
|
|
|
|
|
self.registerEvent()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def updateContract(self, event):
|
|
|
|
|
"""更新合约数据"""
|
|
|
|
|
contract = event.dict_['data']
|
|
|
|
|
self.contractDict[contract.vtSymbol] = contract
|
|
|
|
|
self.contractDict[contract.symbol] = contract # 使用常规代码(不包括交易所)可能导致重复
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getContract(self, vtSymbol):
|
|
|
|
|
"""查询合约对象"""
|
|
|
|
|
try:
|
|
|
|
|
return self.contractDict[vtSymbol]
|
|
|
|
|
except KeyError:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getAllContracts(self):
|
|
|
|
|
"""查询所有合约对象(返回列表)"""
|
|
|
|
|
return self.contractDict.values()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def saveContracts(self):
|
|
|
|
|
"""保存所有合约对象到硬盘"""
|
|
|
|
|
f = shelve.open(self.contractFileName)
|
|
|
|
|
f['data'] = self.contractDict
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def loadContracts(self):
|
|
|
|
|
"""从硬盘读取合约对象"""
|
|
|
|
|
f = shelve.open(self.contractFileName)
|
|
|
|
|
if 'data' in f:
|
|
|
|
|
d = f['data']
|
|
|
|
|
for key, value in d.items():
|
|
|
|
|
self.contractDict[key] = value
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def updateOrder(self, event):
|
|
|
|
|
"""更新委托数据"""
|
|
|
|
|
order = event.dict_['data']
|
|
|
|
|
self.orderDict[order.vtOrderID] = order
|
|
|
|
|
|
|
|
|
|
# 如果订单的状态是全部成交或者撤销,则需要从workingOrderDict中移除
|
|
|
|
|
if order.status == STATUS_ALLTRADED or order.status == STATUS_CANCELLED:
|
|
|
|
|
if order.vtOrderID in self.workingOrderDict:
|
|
|
|
|
del self.workingOrderDict[order.vtOrderID]
|
|
|
|
|
# 否则则更新字典中的数据
|
|
|
|
|
else:
|
|
|
|
|
self.workingOrderDict[order.vtOrderID] = order
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getOrder(self, vtOrderID):
|
|
|
|
|
"""查询委托"""
|
|
|
|
|
try:
|
|
|
|
|
return self.orderDict[vtOrderID]
|
|
|
|
|
except KeyError:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getAllWorkingOrders(self):
|
|
|
|
|
"""查询所有活动委托(返回列表)"""
|
|
|
|
|
return self.workingOrderDict.values()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def registerEvent(self):
|
|
|
|
|
"""注册事件监听"""
|
|
|
|
|
self.eventEngine.register(EVENT_CONTRACT, self.updateContract)
|
|
|
|
|
self.eventEngine.register(EVENT_ORDER, self.updateOrder)
|
|
|
|
|
|
|
|
|
|
|
2016-05-18 13:15:58 +00:00
|
|
|
|
|