diff --git a/vnpy/trader/ctaStrategy/ctaBacktesting.py b/vnpy/trader/ctaStrategy/ctaBacktesting.py index 9e8ef52d..488812cc 100644 --- a/vnpy/trader/ctaStrategy/ctaBacktesting.py +++ b/vnpy/trader/ctaStrategy/ctaBacktesting.py @@ -12,10 +12,10 @@ from itertools import product import multiprocessing import pymongo +import vtGlobal from ctaBase import * from vtConstant import * from vtGateway import VtOrderData, VtTradeData -from vtFunction import loadMongoSetting ######################################################################## @@ -119,9 +119,7 @@ class BacktestingEngine(object): #---------------------------------------------------------------------- def loadHistoryData(self): """载入历史数据""" - host, port, logging = loadMongoSetting() - - self.dbClient = pymongo.MongoClient(host, port) + self.dbClient = pymongo.MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT) collection = self.dbClient[self.dbName][self.symbol] self.output(u'开始载入数据') diff --git a/vnpy/trader/ctaStrategy/ctaBase.py b/vnpy/trader/ctaStrategy/ctaBase.py index 6b16a85e..b6b7133e 100644 --- a/vnpy/trader/ctaStrategy/ctaBase.py +++ b/vnpy/trader/ctaStrategy/ctaBase.py @@ -41,6 +41,8 @@ ENGINETYPE_TRADING = 'trading' # 实盘 # CTA引擎中涉及的数据类定义 from vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT +from vnpy.trader.vtObject import VtBarData as CtaBarData +from vnpy.trader.vtObject import VtTickData as CtaTickData ######################################################################## @@ -60,77 +62,3 @@ class StopOrder(object): self.strategy = None # 下停止单的策略对象 self.stopOrderID = EMPTY_STRING # 停止单的本地编号 self.status = EMPTY_STRING # 停止单状态 - - -######################################################################## -class CtaBarData(object): - """K线数据""" - - #---------------------------------------------------------------------- - def __init__(self): - """Constructor""" - self.vtSymbol = EMPTY_STRING # vt系统代码 - self.symbol = EMPTY_STRING # 代码 - self.exchange = EMPTY_STRING # 交易所 - - self.open = EMPTY_FLOAT # OHLC - self.high = EMPTY_FLOAT - self.low = EMPTY_FLOAT - self.close = EMPTY_FLOAT - - self.date = EMPTY_STRING # bar开始的时间,日期 - self.time = EMPTY_STRING # 时间 - self.datetime = None # python的datetime时间对象 - - self.volume = EMPTY_INT # 成交量 - self.openInterest = EMPTY_INT # 持仓量 - - -######################################################################## -class CtaTickData(object): - """Tick数据""" - - #---------------------------------------------------------------------- - def __init__(self): - """Constructor""" - self.vtSymbol = EMPTY_STRING # vt系统代码 - self.symbol = EMPTY_STRING # 合约代码 - self.exchange = EMPTY_STRING # 交易所代码 - - # 成交数据 - self.lastPrice = EMPTY_FLOAT # 最新成交价 - self.volume = EMPTY_INT # 最新成交量 - self.openInterest = EMPTY_INT # 持仓量 - - self.upperLimit = EMPTY_FLOAT # 涨停价 - self.lowerLimit = EMPTY_FLOAT # 跌停价 - - # tick的时间 - self.date = EMPTY_STRING # 日期 - self.time = EMPTY_STRING # 时间 - self.datetime = None # python的datetime时间对象 - - # 五档行情 - 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 \ No newline at end of file diff --git a/vnpy/trader/ctaStrategy/ctaEngine.py b/vnpy/trader/ctaStrategy/ctaEngine.py index b6117de6..e55e8811 100644 --- a/vnpy/trader/ctaStrategy/ctaEngine.py +++ b/vnpy/trader/ctaStrategy/ctaEngine.py @@ -27,7 +27,7 @@ from datetime import datetime, timedelta from ctaBase import * from strategy import STRATEGY_CLASS from vnpy.event.eventEngine import * -from vnpy.trader.eventType import * +from vnpy.trader.vtEvent import * from vtConstant import * from vtGateway import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData from vtFunction import todayDate diff --git a/vnpy/trader/ctaStrategy/ctaHistoryData.py b/vnpy/trader/ctaStrategy/ctaHistoryData.py index 0ddc0d7a..3a7168eb 100644 --- a/vnpy/trader/ctaStrategy/ctaHistoryData.py +++ b/vnpy/trader/ctaStrategy/ctaHistoryData.py @@ -12,9 +12,9 @@ import pymongo from time import time from multiprocessing.pool import ThreadPool +import vtGlobal from ctaBase import * from vtConstant import * -from vtFunction import loadMongoSetting from datayesClient import DatayesClient @@ -34,9 +34,7 @@ class HistoryDataEngine(object): #---------------------------------------------------------------------- def __init__(self): """Constructor""" - host, port, logging = loadMongoSetting() - - self.dbClient = pymongo.MongoClient(host, port) + self.dbClient = pymongo.MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT) self.datayesClient = DatayesClient() #---------------------------------------------------------------------- @@ -363,6 +361,8 @@ def downloadEquityDailyBarts(self, symbol): print u'%s下载完成' %symbol else: print u'找不到合约%s' %symbol + + #---------------------------------------------------------------------- def loadMcCsv(fileName, dbName, symbol): """将Multicharts导出的csv格式的历史数据插入到Mongo数据库中""" @@ -372,9 +372,7 @@ def loadMcCsv(fileName, dbName, symbol): print u'开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol) # 锁定集合,并创建索引 - host, port, logging = loadMongoSetting() - - client = pymongo.MongoClient(host, port) + client = pymongo.MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT) collection = client[dbName][symbol] collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True) @@ -408,9 +406,7 @@ def loadTdxCsv(fileName, dbName, symbol): print u'开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol) # 锁定集合,并创建索引 - host, port, logging = loadMongoSetting() - - client = pymongo.MongoClient(host, port) + client = pymongo.MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT) collection = client[dbName][symbol] collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True) @@ -449,9 +445,7 @@ def loadTBCsv(fileName, dbName, symbol): print u'开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol) # 锁定集合,并创建索引 - host, port, logging = loadMongoSetting() - - client = pymongo.MongoClient(host, port) + client = pymongo.MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT) collection = client[dbName][symbol] collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True) diff --git a/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py b/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py index b378cfb1..b9080b34 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyAtrRsi.py @@ -13,8 +13,8 @@ import talib import numpy as np -from ctaBase import * -from ctaTemplate import CtaTemplate +from vnpy.trader.vtConstant import EMPTY_STRING +from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate ######################################################################## diff --git a/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py b/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py index 54b4428f..46f8c74b 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyDualThrust.py @@ -6,8 +6,8 @@ DualThrust交易策略 from datetime import time -from ctaBase import * -from ctaTemplate import CtaTemplate +from vnpy.trader.vtConstant import EMPTY_STRING +from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate ######################################################################## diff --git a/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py b/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py index 024163f6..6f8d64dd 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyEmaDemo.py @@ -12,8 +12,8 @@ """ -from ctaBase import * -from ctaTemplate import CtaTemplate +from vnpy.trader.vtConstant import EMPTY_STRING, EMPTY_FLOAT +from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate ######################################################################## diff --git a/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py b/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py index 6c1e64aa..57e37ce3 100644 --- a/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py +++ b/vnpy/trader/ctaStrategy/strategy/strategyKingKeltner.py @@ -12,12 +12,12 @@ from __future__ import division -from ctaBase import * -from ctaTemplate import CtaTemplate - import talib import numpy as np +from vnpy.trader.vtConstant import EMPTY_STRING +from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate + ######################################################################## class KkStrategy(CtaTemplate): diff --git a/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py b/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py index f9cc0c0d..9827ec12 100644 --- a/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py +++ b/vnpy/trader/ctaStrategy/tools/multiTimeFrame/ctaBacktestMultiTF.py @@ -8,7 +8,7 @@ same as CTA engine. Real trading code can be directly used for backtesting. ''' from __future__ import division -from vtFunction import loadMongoSetting +import vtGlobal from ctaBacktesting import * @@ -70,11 +70,7 @@ class BacktestEngineMultiTF(BacktestingEngine): # ---------------------------------------------------------------------- def loadHistoryData(self): """载入历史数据""" - """load historical data""" - - host, port, logging = loadMongoSetting() - - self.dbClient = pymongo.MongoClient(host, port) + self.dbClient = pymongo.MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT) collection = self.dbClient[self.dbName][self.symbol] # Load historical data of information symbols, construct a dictionary of Database diff --git a/vnpy/trader/dataRecorder/drEngine.py b/vnpy/trader/dataRecorder/drEngine.py index 84010c77..a0363a13 100644 --- a/vnpy/trader/dataRecorder/drEngine.py +++ b/vnpy/trader/dataRecorder/drEngine.py @@ -15,7 +15,7 @@ from Queue import Queue from threading import Thread from vnpy.event.eventEngine import * -from vnpy.trader.eventType import * +from vnpy.trader.vtEvent import * from vtGateway import VtSubscribeReq, VtLogData from drBase import * from vtFunction import todayDate diff --git a/vnpy/trader/riskManager/rmEngine.py b/vnpy/trader/riskManager/rmEngine.py index e28570b8..86134d82 100644 --- a/vnpy/trader/riskManager/rmEngine.py +++ b/vnpy/trader/riskManager/rmEngine.py @@ -12,7 +12,7 @@ import os import platform from vnpy.event.eventEngine import * -from vnpy.trader.eventType import * +from vnpy.trader.vtEvent import * from vtConstant import * from vtGateway import VtLogData diff --git a/vnpy/trader/uiBasicWidget.py b/vnpy/trader/uiBasicWidget.py index 86400d62..9e0a7ee5 100644 --- a/vnpy/trader/uiBasicWidget.py +++ b/vnpy/trader/uiBasicWidget.py @@ -8,7 +8,7 @@ from collections import OrderedDict from PyQt4 import QtGui, QtCore from vnpy.event.eventEngine import * -from vnpy.trader.eventType import * +from vnpy.trader.vtEvent import * from vtFunction import * from vtGateway import * import vtText diff --git a/vnpy/trader/vtClient.py b/vnpy/trader/vtClient.py index d5c38cfa..1ded48e7 100644 --- a/vnpy/trader/vtClient.py +++ b/vnpy/trader/vtClient.py @@ -9,7 +9,7 @@ import vtPath from uiMainWindow import * from eventEngine import * -from vnrpc import RpcClient +from vnpy.rpc.vnrpc import RpcClient from ctaStrategy.ctaEngine import CtaEngine from dataRecorder.drEngine import DrEngine diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index 7c2569f5..ccd8b1ce 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -7,10 +7,10 @@ from datetime import datetime from pymongo import MongoClient from pymongo.errors import ConnectionFailure +import vtGlobal from vnpy.event.eventEngine import * -from vnpy.trader.eventType import * +from vnpy.trader.vtEvent import * from vtGateway import * -from vtFunction import loadMongoSetting from language import text from gateway import GATEWAY_DICT @@ -163,11 +163,9 @@ class MainEngine(object): """连接MongoDB数据库""" if not self.dbClient: # 读取MongoDB的设置 - host, port, logging = loadMongoSetting() - try: # 设置MongoDB操作的超时时间为0.5秒 - self.dbClient = MongoClient(host, port, connectTimeoutMS=500) + self.dbClient = MongoClient(vtGlobal.MONGO_HOST, vtGlobal.MONGO_PORT, connectTimeoutMS=500) # 调用server_info查询服务器状态,防止服务器异常并未连接成功 self.dbClient.server_info() @@ -175,7 +173,7 @@ class MainEngine(object): self.writeLog(text.DATABASE_CONNECTING_COMPLETED) # 如果启动日志记录,则注册日志事件监听函数 - if logging: + if vtGlobal.MONGO_LOGGING: self.eventEngine.register(EVENT_LOG, self.dbLogging) except ConnectionFailure: diff --git a/vnpy/trader/eventType.py b/vnpy/trader/vtEvent.py similarity index 100% rename from vnpy/trader/eventType.py rename to vnpy/trader/vtEvent.py diff --git a/vnpy/trader/vtFunction.py b/vnpy/trader/vtFunction.py index c299d327..5f8c6840 100644 --- a/vnpy/trader/vtFunction.py +++ b/vnpy/trader/vtFunction.py @@ -28,26 +28,6 @@ def safeUnicode(value): return unicode(value) -#---------------------------------------------------------------------- -def loadMongoSetting(): - """载入MongoDB数据库的配置""" - fileName = 'VT_setting.json' - path = os.path.abspath(os.path.dirname(__file__)) - fileName = os.path.join(path, fileName) - - try: - f = file(fileName) - setting = json.load(f) - host = setting['mongoHost'] - port = setting['mongoPort'] - logging = setting['mongoLogging'] - except: - host = 'localhost' - port = 27017 - logging = False - - return host, port, logging - #---------------------------------------------------------------------- def todayDate(): """获取当前本机电脑时间的日期""" diff --git a/vnpy/trader/vtGateway.py b/vnpy/trader/vtGateway.py index 16a525a1..ede612ed 100644 --- a/vnpy/trader/vtGateway.py +++ b/vnpy/trader/vtGateway.py @@ -3,9 +3,10 @@ import time from vnpy.event.eventEngine import * -from vnpy.trader.eventType import * +from vnpy.trader.vtEvent import * from vtConstant import * +from vtObject import * ######################################################################## @@ -141,296 +142,6 @@ class VtGateway(object): 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.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.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.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', time.localtime()) # 日志生成时间 - 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 # 会话号 - diff --git a/vnpy/trader/vtGlobal.py b/vnpy/trader/vtGlobal.py new file mode 100644 index 00000000..670ec295 --- /dev/null +++ b/vnpy/trader/vtGlobal.py @@ -0,0 +1,42 @@ +# encoding: UTF-8 + +""" +存放全局变量的文件 +""" + +# 数据库 +MONGO_HOST = "localhost" +MONGO_PORT = 27017 +MONGO_LOGGING = False + +# 语言 +LANGUAGE = "chinese" + +# 界面 +DARK_STYLE = True +FONT_FAMILY = u"微软雅黑" +FONT_SIZE = 12 + + +import os + +settingFileName = "VT_setting.json" +path = os.path.abspath(os.path.dirname(__file__)) +settingFileName = os.path.join(path, settingFileName) + +try: + f = file(settingFileName) + setting = json.load(f) + + MONGO_HOST = setting["mongoHost"] + MONGO_PORT = setting["mongoPort"] + MONGO_LOGGING = setting["mongoLogging"] + + LANGUAGE = setting["language"] + + DARK_STYLE = setting["darkStyle"] + FONT_FAMILY = setting["fontFamily"] + FONT_SIZE = setting["fontSize"] + +except: + pass diff --git a/vnpy/trader/vtMain.py b/vnpy/trader/vtMain.py index dc0e5f2b..e7c7e79c 100644 --- a/vnpy/trader/vtMain.py +++ b/vnpy/trader/vtMain.py @@ -2,10 +2,9 @@ import sys import os -import ctypes import platform -import vtPath +import vtGlobal from vtEngine import MainEngine from uiMainWindow import * @@ -14,8 +13,6 @@ path = os.path.abspath(os.path.dirname(__file__)) ICON_FILENAME = 'vnpy.ico' ICON_FILENAME = os.path.join(path, ICON_FILENAME) -SETTING_FILENAME = 'VT_setting.json' -SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) #---------------------------------------------------------------------- def main(): @@ -25,7 +22,8 @@ def main(): sys.setdefaultencoding('utf8') # 设置Windows底部任务栏图标 - if 'Windows' in platform.uname() : + if 'Windows' in platform.uname(): + import ctypes ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.trader') # 初始化Qt应用对象 @@ -34,15 +32,9 @@ def main(): app.setFont(BASIC_FONT) # 设置Qt的皮肤 - try: - f = file(SETTING_FILENAME) - setting = json.load(f) - if setting['darkStyle']: - import qdarkstyle - app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False)) - f.close() - except: - pass + if vtGlobal.DARK_STYLE: + import qdarkstyle + app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False)) # 初始化主引擎和主窗口对象 mainEngine = MainEngine() diff --git a/vnpy/trader/vtObject.py b/vnpy/trader/vtObject.py new file mode 100644 index 00000000..5f118f74 --- /dev/null +++ b/vnpy/trader/vtObject.py @@ -0,0 +1,326 @@ +# encoding: UTF-8 + +import time + +from vtConstant import * + + +######################################################################## +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.openInterest = EMPTY_INT # 持仓量 + self.time = EMPTY_STRING # 时间 11:20:56.5 + self.date = EMPTY_STRING # 日期 20151009 + self.datetime = None # python的datetime时间对象 + + # 常规行情 + 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 VtBarData(VtBaseData): + """K线数据""" + + #---------------------------------------------------------------------- + def __init__(self): + """Constructor""" + super(VtBarData, self).__init__() + + self.vtSymbol = EMPTY_STRING # vt系统代码 + self.symbol = EMPTY_STRING # 代码 + self.exchange = EMPTY_STRING # 交易所 + + self.open = EMPTY_FLOAT # OHLC + self.high = EMPTY_FLOAT + self.low = EMPTY_FLOAT + self.close = EMPTY_FLOAT + + self.date = EMPTY_STRING # bar开始的时间,日期 + self.time = EMPTY_STRING # 时间 + self.datetime = None # python的datetime时间对象 + + self.volume = EMPTY_INT # 成交量 + self.openInterest = 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.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', time.localtime()) # 日志生成时间 + 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 # 会话号 + + + + + diff --git a/vnpy/trader/vtPath.py b/vnpy/trader/vtPath.py deleted file mode 100644 index 9b5aada3..00000000 --- a/vnpy/trader/vtPath.py +++ /dev/null @@ -1,24 +0,0 @@ -# encoding: UTF-8 - -""" -用于vn.trader的运行目录环境设置 -""" - -import os -import sys - -# 将根目录路径添加到环境变量中 -ROOT_PATH = os.path.abspath(os.path.dirname(__file__)) -sys.path.append(ROOT_PATH) - -# 将功能模块的目录路径添加到环境变量中 -# 若各目录下存在同名文件可能导致异常,请注意测试 -MODULE_PATH = {} -MODULE_PATH['CTA'] = os.path.join(ROOT_PATH, 'ctaStrategy') -MODULE_PATH['RM'] = os.path.join(ROOT_PATH, 'riskManager') -MODULE_PATH['DR'] = os.path.join(ROOT_PATH, 'dataRecorder') - -# 添加到环境变量中 -for path in MODULE_PATH.values(): - if path not in sys.path: - sys.path.append(path) diff --git a/vnpy/trader/vtServer.py b/vnpy/trader/vtServer.py index 16698105..f89b0113 100644 --- a/vnpy/trader/vtServer.py +++ b/vnpy/trader/vtServer.py @@ -7,9 +7,8 @@ from datetime import datetime from time import sleep from threading import Thread -import vtPath import eventType -from vnrpc import RpcServer +from vnpy.rpc.vnrpc import RpcServer from vtEngine import MainEngine