sync vnpy1.6.2
This commit is contained in:
parent
314023553f
commit
9287136356
@ -1,357 +0,0 @@
|
|||||||
# encoding: UTF-8
|
|
||||||
|
|
||||||
# 系统模块
|
|
||||||
from Queue import Queue, Empty
|
|
||||||
from threading import Thread
|
|
||||||
from time import sleep
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
# 第三方模块
|
|
||||||
from PyQt4.QtCore import QTimer
|
|
||||||
|
|
||||||
# 自己开发的模块
|
|
||||||
from eventType import *
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class EventEngine(object):
|
|
||||||
"""
|
|
||||||
事件驱动引擎
|
|
||||||
|
|
||||||
事件驱动引擎中所有的变量都设置为了私有,这是为了防止不小心
|
|
||||||
从外部修改了这些变量的值或状态,导致bug。
|
|
||||||
|
|
||||||
变量说明
|
|
||||||
__queue:私有变量,事件队列
|
|
||||||
__active:私有变量,事件引擎开关
|
|
||||||
__thread:私有变量,事件处理线程
|
|
||||||
__timer:私有变量,计时器
|
|
||||||
__handlers:私有变量,事件处理函数字典
|
|
||||||
|
|
||||||
|
|
||||||
方法说明
|
|
||||||
__run: 私有方法,事件处理线程连续运行用
|
|
||||||
__process: 私有方法,处理事件,调用注册在引擎中的监听函数
|
|
||||||
__onTimer:私有方法,计时器固定事件间隔触发后,向事件队列中存入计时器事件
|
|
||||||
start: 公共方法,启动引擎
|
|
||||||
stop:公共方法,停止引擎
|
|
||||||
register:公共方法,向引擎中注册监听函数
|
|
||||||
unregister:公共方法,向引擎中注销监听函数
|
|
||||||
put:公共方法,向事件队列中存入新的事件
|
|
||||||
|
|
||||||
事件监听函数必须定义为输入参数仅为一个event对象,即:
|
|
||||||
|
|
||||||
函数
|
|
||||||
def func(event)
|
|
||||||
...
|
|
||||||
|
|
||||||
对象方法
|
|
||||||
def method(self, event)
|
|
||||||
...
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __init__(self):
|
|
||||||
"""初始化事件引擎"""
|
|
||||||
# 事件队列
|
|
||||||
self.__queue = Queue()
|
|
||||||
|
|
||||||
# 事件引擎开关
|
|
||||||
self.__active = False
|
|
||||||
|
|
||||||
# 事件处理线程
|
|
||||||
self.__thread = Thread(target = self.__run)
|
|
||||||
|
|
||||||
# 计时器,用于触发计时器事件
|
|
||||||
self.__timer = QTimer()
|
|
||||||
self.__timer.timeout.connect(self.__onTimer)
|
|
||||||
|
|
||||||
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
|
||||||
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
|
||||||
self.__handlers = defaultdict(list)
|
|
||||||
|
|
||||||
# __generalHandlers是一个列表,用来保存通用回调函数(所有事件均调用)
|
|
||||||
self.__generalHandlers = []
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __run(self):
|
|
||||||
"""引擎运行"""
|
|
||||||
while self.__active == True:
|
|
||||||
try:
|
|
||||||
event = self.__queue.get(block = True, timeout = 1) # 获取事件的阻塞时间设为1秒
|
|
||||||
self.__process(event)
|
|
||||||
except Empty:
|
|
||||||
pass
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __process(self, event):
|
|
||||||
"""处理事件"""
|
|
||||||
# 检查是否存在对该事件进行监听的处理函数
|
|
||||||
if event.type_ in self.__handlers:
|
|
||||||
# 若存在,则按顺序将事件传递给处理函数执行
|
|
||||||
[handler(event) for handler in self.__handlers[event.type_]]
|
|
||||||
|
|
||||||
# 以上语句为Python列表解析方式的写法,对应的常规循环写法为:
|
|
||||||
#for handler in self.__handlers[event.type_]:
|
|
||||||
#handler(event)
|
|
||||||
|
|
||||||
# 调用通用处理函数进行处理
|
|
||||||
if self.__generalHandlers:
|
|
||||||
[handler(event) for handler in self.__generalHandlers]
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __onTimer(self):
|
|
||||||
"""向事件队列中存入计时器事件"""
|
|
||||||
# 创建计时器事件
|
|
||||||
event = Event(type_=EVENT_TIMER)
|
|
||||||
|
|
||||||
# 向队列中存入计时器事件
|
|
||||||
self.put(event)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def start(self, timer=True):
|
|
||||||
"""
|
|
||||||
引擎启动
|
|
||||||
timer:是否要启动计时器
|
|
||||||
"""
|
|
||||||
# 将引擎设为启动
|
|
||||||
self.__active = True
|
|
||||||
|
|
||||||
# 启动事件处理线程
|
|
||||||
self.__thread.start()
|
|
||||||
|
|
||||||
# 启动计时器,计时器事件间隔默认设定为1秒
|
|
||||||
if timer:
|
|
||||||
self.__timer.start(1000)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def stop(self):
|
|
||||||
"""停止引擎"""
|
|
||||||
# 将引擎设为停止
|
|
||||||
self.__active = False
|
|
||||||
|
|
||||||
# 停止计时器
|
|
||||||
self.__timer.stop()
|
|
||||||
|
|
||||||
# 等待事件处理线程退出
|
|
||||||
self.__thread.join()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def register(self, type_, handler):
|
|
||||||
"""注册事件处理函数监听"""
|
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无defaultDict会自动创建新的list
|
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
|
|
||||||
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
|
||||||
if handler not in handlerList:
|
|
||||||
handlerList.append(handler)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def unregister(self, type_, handler):
|
|
||||||
"""注销事件处理函数监听"""
|
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
|
|
||||||
# 如果该函数存在于列表中,则移除
|
|
||||||
if handler in handlerList:
|
|
||||||
handlerList.remove(handler)
|
|
||||||
|
|
||||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
|
||||||
if not handlerList:
|
|
||||||
del self.__handlers[type_]
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def put(self, event):
|
|
||||||
"""向事件队列中存入事件"""
|
|
||||||
self.__queue.put(event)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def registerGeneralHandler(self, handler):
|
|
||||||
"""注册通用事件处理函数监听"""
|
|
||||||
if handler not in self.__generalHandlers:
|
|
||||||
self.__generalHandlers.append(handler)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def unregisterGeneralHandler(self, handler):
|
|
||||||
"""注销通用事件处理函数监听"""
|
|
||||||
if handler in self.__generalHandlers:
|
|
||||||
self.__generalHandlers.remove(handler)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class EventEngine2(object):
|
|
||||||
"""
|
|
||||||
计时器使用python线程的事件驱动引擎
|
|
||||||
"""
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __init__(self):
|
|
||||||
"""初始化事件引擎"""
|
|
||||||
# 事件队列
|
|
||||||
self.__queue = Queue()
|
|
||||||
|
|
||||||
# 事件引擎开关
|
|
||||||
self.__active = False
|
|
||||||
|
|
||||||
# 事件处理线程
|
|
||||||
self.__thread = Thread(target = self.__run)
|
|
||||||
|
|
||||||
# 计时器,用于触发计时器事件
|
|
||||||
self.__timer = Thread(target = self.__runTimer)
|
|
||||||
self.__timerActive = False # 计时器工作状态
|
|
||||||
self.__timerSleep = 1 # 计时器触发间隔(默认1秒)
|
|
||||||
|
|
||||||
# 这里的__handlers是一个字典,用来保存对应的事件调用关系
|
|
||||||
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
|
|
||||||
self.__handlers = defaultdict(list)
|
|
||||||
|
|
||||||
# __generalHandlers是一个列表,用来保存通用回调函数(所有事件均调用)
|
|
||||||
self.__generalHandlers = []
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __run(self):
|
|
||||||
"""引擎运行"""
|
|
||||||
while self.__active == True:
|
|
||||||
try:
|
|
||||||
event = self.__queue.get(block = True, timeout = 1) # 获取事件的阻塞时间设为1秒
|
|
||||||
self.__process(event)
|
|
||||||
except Empty:
|
|
||||||
pass
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __process(self, event):
|
|
||||||
"""处理事件"""
|
|
||||||
# 检查是否存在对该事件进行监听的处理函数
|
|
||||||
if event.type_ in self.__handlers:
|
|
||||||
# 若存在,则按顺序将事件传递给处理函数执行
|
|
||||||
[handler(event) for handler in self.__handlers[event.type_]]
|
|
||||||
|
|
||||||
# 以上语句为Python列表解析方式的写法,对应的常规循环写法为:
|
|
||||||
#for handler in self.__handlers[event.type_]:
|
|
||||||
#handler(event)
|
|
||||||
|
|
||||||
# 调用通用处理函数进行处理
|
|
||||||
if self.__generalHandlers:
|
|
||||||
[handler(event) for handler in self.__generalHandlers]
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __runTimer(self):
|
|
||||||
"""运行在计时器线程中的循环函数"""
|
|
||||||
while self.__timerActive:
|
|
||||||
# 创建计时器事件
|
|
||||||
event = Event(type_=EVENT_TIMER)
|
|
||||||
|
|
||||||
# 向队列中存入计时器事件
|
|
||||||
self.put(event)
|
|
||||||
|
|
||||||
# 等待
|
|
||||||
sleep(self.__timerSleep)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def start(self, timer=True):
|
|
||||||
"""
|
|
||||||
引擎启动
|
|
||||||
timer:是否要启动计时器
|
|
||||||
"""
|
|
||||||
# 将引擎设为启动
|
|
||||||
self.__active = True
|
|
||||||
|
|
||||||
# 启动事件处理线程
|
|
||||||
self.__thread.start()
|
|
||||||
|
|
||||||
# 启动计时器,计时器事件间隔默认设定为1秒
|
|
||||||
if timer:
|
|
||||||
self.__timerActive = True
|
|
||||||
self.__timer.start()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def stop(self):
|
|
||||||
"""停止引擎"""
|
|
||||||
# 将引擎设为停止
|
|
||||||
self.__active = False
|
|
||||||
|
|
||||||
# 停止计时器
|
|
||||||
self.__timerActive = False
|
|
||||||
self.__timer.join()
|
|
||||||
|
|
||||||
# 等待事件处理线程退出
|
|
||||||
self.__thread.join()
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def register(self, type_, handler):
|
|
||||||
"""注册事件处理函数监听"""
|
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无defaultDict会自动创建新的list
|
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
|
|
||||||
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
|
|
||||||
if handler not in handlerList:
|
|
||||||
handlerList.append(handler)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def unregister(self, type_, handler):
|
|
||||||
"""注销事件处理函数监听"""
|
|
||||||
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
|
|
||||||
handlerList = self.__handlers[type_]
|
|
||||||
|
|
||||||
# 如果该函数存在于列表中,则移除
|
|
||||||
if handler in handlerList:
|
|
||||||
handlerList.remove(handler)
|
|
||||||
|
|
||||||
# 如果函数列表为空,则从引擎中移除该事件类型
|
|
||||||
if not handlerList:
|
|
||||||
del self.__handlers[type_]
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def put(self, event):
|
|
||||||
"""向事件队列中存入事件"""
|
|
||||||
self.__queue.put(event)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def registerGeneralHandler(self, handler):
|
|
||||||
"""注册通用事件处理函数监听"""
|
|
||||||
if handler not in self.__generalHandlers:
|
|
||||||
self.__generalHandlers.append(handler)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def unregisterGeneralHandler(self, handler):
|
|
||||||
"""注销通用事件处理函数监听"""
|
|
||||||
if handler in self.__generalHandlers:
|
|
||||||
self.__generalHandlers.remove(handler)
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class Event:
|
|
||||||
"""事件对象"""
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def __init__(self, type_=None):
|
|
||||||
"""Constructor"""
|
|
||||||
self.type_ = type_ # 事件类型
|
|
||||||
self.dict_ = {} # 字典用于保存具体的事件数据
|
|
||||||
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
|
||||||
def test():
|
|
||||||
"""测试函数"""
|
|
||||||
import sys
|
|
||||||
from datetime import datetime
|
|
||||||
from PyQt4.QtCore import QCoreApplication
|
|
||||||
|
|
||||||
def simpletest(event):
|
|
||||||
print u'处理每秒触发的计时器事件:%s' % str(datetime.now())
|
|
||||||
|
|
||||||
app = QCoreApplication(sys.argv)
|
|
||||||
|
|
||||||
ee = EventEngine2()
|
|
||||||
#ee.register(EVENT_TIMER, simpletest)
|
|
||||||
ee.registerGeneralHandler(simpletest)
|
|
||||||
ee.start()
|
|
||||||
|
|
||||||
app.exec_()
|
|
||||||
|
|
||||||
|
|
||||||
# 直接运行脚本可以进行测试
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test()
|
|
@ -1,16 +0,0 @@
|
|||||||
# encoding: UTF-8
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
|
|
||||||
import trader.vtConstant
|
|
||||||
|
|
||||||
print u'init {0}'.format(os.path.dirname(__file__))
|
|
||||||
|
|
||||||
from ctpGateway import CtpGateway as gateway
|
|
||||||
|
|
||||||
gatewayName = 'CTP'
|
|
||||||
gatewayDisplayName = gatewayName
|
|
||||||
gatewayType = trader.vtConstant.GATEWAYTYPE_FUTURES
|
|
||||||
gatewayQryEnabled = True
|
|
||||||
|
|
@ -1 +0,0 @@
|
|||||||
存放每天委托日志
|
|
@ -1,459 +0,0 @@
|
|||||||
# encoding: UTF-8
|
|
||||||
|
|
||||||
import time
|
|
||||||
from datetime import datetime
|
|
||||||
from eventEngine import *
|
|
||||||
|
|
||||||
from vtConstant import *
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class VtGateway(object):
|
|
||||||
"""交易接口"""
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def __init__(self, eventEngine, gatewayName):
|
|
||||||
"""Constructor"""
|
|
||||||
self.eventEngine = eventEngine
|
|
||||||
self.gatewayName = gatewayName
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def onTick(self, tick):
|
|
||||||
"""市场行情推送"""
|
|
||||||
# 通用事件
|
|
||||||
event1 = Event(type_=EVENT_TICK)
|
|
||||||
event1.dict_['data'] = tick
|
|
||||||
self.eventEngine.put(event1)
|
|
||||||
|
|
||||||
# 特定合约代码的事件
|
|
||||||
event2 = Event(type_=EVENT_TICK+tick.vtSymbol)
|
|
||||||
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.vtSymbol)
|
|
||||||
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.vtOrderID)
|
|
||||||
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.vtSymbol)
|
|
||||||
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):
|
|
||||||
"""错误信息推送"""
|
|
||||||
# 通用事件
|
|
||||||
event1 = Event(type_=EVENT_ERROR)
|
|
||||||
event1.dict_['data'] = error
|
|
||||||
self.eventEngine.put(event1)
|
|
||||||
|
|
||||||
logMsg = u'{0}:[{1}]:{2}'.format(error.gatewayName, error.errorID,error.errorMsg )
|
|
||||||
# 写入本地log日志
|
|
||||||
logging.info(logMsg)
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def onLog(self, log):
|
|
||||||
"""日志推送"""
|
|
||||||
# 通用事件
|
|
||||||
event1 = Event(type_=EVENT_LOG)
|
|
||||||
event1.dict_['data'] = log
|
|
||||||
self.eventEngine.put(event1)
|
|
||||||
|
|
||||||
# 写入本地log日志
|
|
||||||
logging.info(log.logContent)
|
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def onContract(self, contract):
|
|
||||||
"""合约基础信息推送"""
|
|
||||||
# 通用事件
|
|
||||||
event1 = Event(type_=EVENT_CONTRACT)
|
|
||||||
event1.dict_['data'] = contract
|
|
||||||
self.eventEngine.put(event1)
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
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 checkStatus(self):
|
|
||||||
"""查询状态"""
|
|
||||||
return True
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
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.vtSymbol = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
|
|
||||||
|
|
||||||
# 成交数据
|
|
||||||
self.lastPrice = EMPTY_FLOAT # 最新成交价
|
|
||||||
self.lastVolume = EMPTY_INT # 最新成交量
|
|
||||||
self.volume = EMPTY_INT # 今天总成交量
|
|
||||||
self.preOpenInterest = EMPTY_INT # 昨持仓量
|
|
||||||
self.openInterest = EMPTY_INT # 持仓量
|
|
||||||
self.time = EMPTY_STRING # 时间 11:20:56.5
|
|
||||||
self.date = EMPTY_STRING # 日期 20151009
|
|
||||||
self.tradingDay = EMPTY_STRING # 交易日期
|
|
||||||
|
|
||||||
# 常规行情
|
|
||||||
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.vtSymbol = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
|
|
||||||
|
|
||||||
self.tradeID = EMPTY_STRING # 成交编号
|
|
||||||
self.vtTradeID = EMPTY_STRING # 成交在vt系统中的唯一编号,通常是 Gateway名.成交编号
|
|
||||||
|
|
||||||
self.orderID = EMPTY_STRING # 订单编号
|
|
||||||
self.vtOrderID = 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.vtSymbol = EMPTY_STRING # 合约在vt系统中的唯一代码,通常是 合约代码.交易所代码
|
|
||||||
|
|
||||||
self.orderID = EMPTY_STRING # 订单编号
|
|
||||||
self.vtOrderID = EMPTY_STRING # 订单在vt系统中的唯一编号,通常是 Gateway名.订单编号
|
|
||||||
|
|
||||||
# 报单相关
|
|
||||||
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.updateTime = 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.vtSymbol = EMPTY_STRING # 合约在vt系统中的唯一代码,合约代码.交易所代码
|
|
||||||
|
|
||||||
# 持仓相关
|
|
||||||
self.direction = EMPTY_STRING # 持仓方向
|
|
||||||
self.position = EMPTY_INT # 持仓量
|
|
||||||
self.frozen = EMPTY_INT # 冻结数量
|
|
||||||
self.price = EMPTY_FLOAT # 持仓均价
|
|
||||||
self.vtPositionName = EMPTY_STRING # 持仓在vt系统中的唯一代码,通常是vtSymbol.方向
|
|
||||||
self.ydPosition = EMPTY_INT # 昨持仓
|
|
||||||
self.positionProfit = EMPTY_FLOAT # 持仓盈亏
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
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 # 补充信息
|
|
||||||
|
|
||||||
self.errorTime = time.strftime('%X', time.localtime()) # 错误生成时间
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class VtLogData(VtBaseData):
|
|
||||||
"""日志数据类"""
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def __init__(self):
|
|
||||||
"""Constructor"""
|
|
||||||
super(VtLogData, self).__init__()
|
|
||||||
|
|
||||||
#self.logTime = time.strftime('%X:%f', time.localtime()) # 日志生成时间
|
|
||||||
self.logTime = datetime.now().strftime('%X:%f')
|
|
||||||
self.logContent = EMPTY_UNICODE # 日志信息
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
class VtContractData(VtBaseData):
|
|
||||||
"""合约详细信息类"""
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
|
||||||
def __init__(self):
|
|
||||||
"""Constructor"""
|
|
||||||
super(VtContractData, self).__init__()
|
|
||||||
|
|
||||||
self.symbol = EMPTY_STRING # 代码
|
|
||||||
self.exchange = EMPTY_STRING # 交易所代码
|
|
||||||
self.vtSymbol = 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 # 期权类型
|
|
||||||
self.lastTradeDateOrContractMonth = EMPTY_STRING # 合约月,IB专用
|
|
||||||
self.multiplier = EMPTY_STRING # 乘数,IB专用
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
|
||||||
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 # 会话号
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -204,13 +204,13 @@ class CtpGateway(VtGateway):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def close(self):
|
def close(self):
|
||||||
"""关闭"""
|
"""关闭"""
|
||||||
if self.mdConnected and self.mdApi is not None:
|
if self.mdApi is not None:
|
||||||
tmp1 = self.mdApi
|
tmp1 = self.mdApi
|
||||||
self.mdApi = None
|
self.mdApi = None
|
||||||
tmp1.close()
|
tmp1.close()
|
||||||
self.mdConnected = False
|
self.mdConnected = False
|
||||||
|
|
||||||
if self.tdConnected and self.tdApi is not None:
|
if self.tdApi is not None:
|
||||||
tmp2 = self.tdApi
|
tmp2 = self.tdApi
|
||||||
self.tdApi = None
|
self.tdApi = None
|
||||||
tmp2.close()
|
tmp2.close()
|
||||||
|
@ -4,12 +4,12 @@ import json
|
|||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
print u'init {0}'.format(os.path.dirname(__file__))
|
#print u'init {0}'.format(os.path.dirname(__file__))
|
||||||
|
|
||||||
# 默认设置
|
# 默认设置
|
||||||
from chinese import text
|
from chinese import text
|
||||||
|
|
||||||
# 是否要使用英文
|
# 是否要使用英文
|
||||||
from trader.vtGlobal import globalSetting
|
from vnpy.trader.vtGlobal import globalSetting
|
||||||
if globalSetting['language'] == 'english':
|
if globalSetting['language'] == 'english':
|
||||||
from english import text
|
from english import text
|
@ -1,13 +1,9 @@
|
|||||||
# encoding: UTF-8
|
# encoding: UTF-8
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
# 默认设置
|
# 默认设置
|
||||||
from chinese import text, constant
|
from chinese import text, constant
|
||||||
|
|
||||||
# 是否要使用英文
|
# 是否要使用英文
|
||||||
from trader.vtGlobal import globalSetting
|
from vnpy.trader.vtGlobal import globalSetting
|
||||||
if globalSetting['language'] == 'english':
|
if globalSetting['language'] == 'english':
|
||||||
from english import text, constant
|
from vnpy.trader.language.english import text, constant
|
0
vnpy/trader/language/chinese/__init__.py
Normal file
0
vnpy/trader/language/chinese/__init__.py
Normal file
0
vnpy/trader/language/english/__init__.py
Normal file
0
vnpy/trader/language/english/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user