From b845966b2a827c0262734cc06f461eec3b8b4815 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Sun, 14 Feb 2016 13:03:41 +0800 Subject: [PATCH] recent update --- vn.trader/ctaConstant.py | 16 ++++++- vn.trader/ctaEngine.py | 9 +++- vn.trader/ctaObject.py | 10 ++++- vn.trader/ctaStrategies.py | 14 ++++++- vn.trader/ctaStrategyTemplate.py | 71 ++++++++++++++++++++++++++++---- vn.trader/uiMainWindow.py | 18 ++++---- vn.trader/vtConstant.py | 5 +++ vn.trader/vtGateway.py | 5 ++- vn.trader/vtMain.py | 20 ++++++++- vn.trader/windGateway.py | 8 ++-- 10 files changed, 146 insertions(+), 30 deletions(-) diff --git a/vn.trader/ctaConstant.py b/vn.trader/ctaConstant.py index c01c0ed8..d43ea18a 100644 --- a/vn.trader/ctaConstant.py +++ b/vn.trader/ctaConstant.py @@ -6,10 +6,24 @@ CTAORDER_SELL = u'卖平' CTAORDER_SHORT = u'卖开' CTAORDER_COVER = u'买平' +CTAORDER_OPEN_REJECT = u'开单拒绝' +CTAORDER_OPEN_FAIL = u'开单失败' +CTAORDER_CLOSE_FAIL = u'平单失败' + + # 本地停止单状态 STOPORDER_WAITING = u'等待中' STOPORDER_CANCELLED = u'已撤销' STOPORDER_TRIGGERED = u'已触发' # 本地停止单前缀 -STOPORDERPREFIX = 'CtaStopOrder.' \ No newline at end of file +STOPORDERPREFIX = 'CtaStopOrder.' + +# 各类商品所在市场 +NIGHT_MARKET_SQ1 = {'AU': 0, 'AG': 0} +NIGHT_MARKET_SQ2 = {'CU': 0, 'PB': 0, 'AL': 0, 'ZN': 0, 'FU': 0, 'BU': 0, 'RB': 0, 'WR': 0, 'HC': 0} +NIGHT_MARKET_SQ3 = {'RU': 0} +NIGHT_MARKET_ZZ = {'TA': 0, 'JR': 0, 'OI': 0, 'RO': 0, 'PM': 0, 'WT': 0, 'WS': 0, 'WH': 0, 'CF': 0, 'SR': 0, 'FG': 0, + 'ME': 0, 'MA': 0, 'RS': 0, 'RM': 0, 'TC': 0, 'RI': 0, 'ER': 0} +NIGHT_MARKET_DL = {'V': 0, 'L': 0, 'BB': 0, 'I': 0, 'FB': 0, 'C': 0, 'PP': 0, 'A': 0, 'B': 0, 'M': 0, 'Y': 0, 'P': 0, + 'JM': 0, 'J': 0} diff --git a/vn.trader/ctaEngine.py b/vn.trader/ctaEngine.py index 48c26abc..753400b9 100644 --- a/vn.trader/ctaEngine.py +++ b/vn.trader/ctaEngine.py @@ -153,15 +153,20 @@ class CtaEngine(object): # ---------------------------------------------------------------------- def cancelStopOrder(self, stopOrderID): - """撤销停止单""" + """撤销停止单 + Incense Li modified 20160124: + 增加返回True 和 False + """ # 1.检查停止单是否存在 if stopOrderID in self.workingStopOrderDict: so = self.workingStopOrderDict[stopOrderID] so.status = STOPORDER_CANCELLED # STOPORDER_WAITING =》STOPORDER_CANCELLED del self.workingStopOrderDict[stopOrderID] # 删除 self.writeCtaLog(u'撤销停止单:{0}成功.'.format(stopOrderID)) + return True else: self.writeCtaLog(u'撤销停止单:{0}失败,不存在Id.'.format(stopOrderID)) + return False # ---------------------------------------------------------------------- def processStopOrder(self, tick): @@ -240,6 +245,8 @@ class CtaEngine(object): # 4.触发策略的委托推送事件方法 strategy.onOrder(order) + else: + self.writeCtaLog(u'当前订单不在字典中:OrderId:{0}'.format(order.vtOrderID)) # ---------------------------------------------------------------------- diff --git a/vn.trader/ctaObject.py b/vn.trader/ctaObject.py index 38f64384..67d78ed7 100644 --- a/vn.trader/ctaObject.py +++ b/vn.trader/ctaObject.py @@ -1,6 +1,6 @@ # encoding: UTF-8 -from vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT +from vtConstant import EMPTY_UNICODE, EMPTY_STRING, EMPTY_FLOAT, EMPTY_INT,COLOR_EQUAL ######################################################################## @@ -43,6 +43,14 @@ class CtaBarData(object): self.volume = EMPTY_INT # 成交量 self.openInterest = EMPTY_INT # 持仓量 + self.color = COLOR_EQUAL # k 线颜色,COLOR_RED,COLOR_BLUE + self.traded = False + self.tradeStatus = EMPTY_STRING # 当前bar的交易情况: CTAORDER_BUY 、CTAORDER_SELL、 + # CTAORDER_SHORT 、CTAORDER_COVER 、 CTAORDER_OPEN_REJECT 、 + # CTAORDER_OPEN_FAIL 、CTAORDER_CLOSE_FAIL + + self.seconds = EMPTY_INT # 当前Bar的秒数(针对RenkoBar) + ######################################################################## class CtaTickData(object): diff --git a/vn.trader/ctaStrategies.py b/vn.trader/ctaStrategies.py index 7a08cb61..1a16be39 100644 --- a/vn.trader/ctaStrategies.py +++ b/vn.trader/ctaStrategies.py @@ -12,8 +12,18 @@ from ctaStrategyTemplate import TestStrategy from ctaDataRecorder import DataRecorder +from ctaStrategy02 import Strategy02 +from ctaStrategy03 import Strategy03 +from ctaStrategy04 import Strategy04 +from ctaStrategy05 import Strategy05 +from ctaStrategy06 import Strategy06 strategyClassDict = {} -strategyClassDict['TestStrategy'] = TestStrategy -strategyClassDict['DataRecorder'] = DataRecorder +#strategyClassDict['TestStrategy'] = TestStrategy +#strategyClassDict['DataRecorder'] = DataRecorder +strategyClassDict['Strategy02'] = Strategy02 +strategyClassDict['Strategy03'] = Strategy03 +strategyClassDict['Strategy04'] = Strategy04 +strategyClassDict['Strategy05'] = Strategy05 +strategyClassDict['Strategy06'] = Strategy06 diff --git a/vn.trader/ctaStrategyTemplate.py b/vn.trader/ctaStrategyTemplate.py index ddfd583b..5ba808b1 100644 --- a/vn.trader/ctaStrategyTemplate.py +++ b/vn.trader/ctaStrategyTemplate.py @@ -4,6 +4,7 @@ from vtConstant import * from ctaConstant import * +from datetime import datetime ######################################################################## class CtaStrategyTemplate(object): @@ -26,12 +27,21 @@ class CtaStrategyTemplate(object): self.vtSymbol = EMPTY_STRING # 交易的合约vt系统代码 AU1606,SR605 self.symbol = EMPTY_STRING # 交易的合约代码(除郑商所外与vtSymbol一致,一般为两位代码+两位年份+两位月份)AU1606,SR1605 + self.shortSymbol = EMPTY_STRING # 合约类型代码 self.tickDbName = EMPTY_STRING # tick数据库名称 self.barDbName = EMPTY_STRING # bar数据库名称 self.trading = False # 控制是否启动交易 + # 委托单状态 + self.entrust = 0 # 0 表示没有委托,1 表示存在多仓的委托,-1 表示存在空仓的委托 + + # 保存委托单编号和相关委托单的字典 + # key为委托单编号 + # value为该合约相关的委托单 + self.uncompletedOrders = {} + #self.init() # 初始化策略 Move to inhert strategy #if setting: # self.setParam(setting) @@ -72,7 +82,7 @@ class CtaStrategyTemplate(object): raise NotImplementedError # ---------------------------------------------------------------------- - def buy(self, price, volume, stop=False): + def buy(self, price, volume, stop=False,orderTime=datetime.now()): """买开""" # 如果stop为True,则意味着发本地停止单 if self.trading: @@ -81,13 +91,24 @@ class CtaStrategyTemplate(object): orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_BUY, price, volume, self) else: self.writeCtaLog(u'Buy买开,Price:{0},Volume:{1}'.format(price, volume)) + orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_BUY, price, volume, self) + + if orderID: + self.entrust = 1 # 委托状态 + + self.uncompletedOrders[orderID] = {'DIRECTION': DIRECTION_LONG, + 'OFFSET': OFFSET_OPEN, + 'Volume': volume, + 'Price': price, + 'OrderTime': orderTime + } return orderID else: return None # ---------------------------------------------------------------------- - def sell(self, price, volume, stop=False): + def sell(self, price, volume, stop=False, orderTime=datetime.now()): """卖平""" # 如果stop为True,则意味着发本地停止单 if self.trading: @@ -96,13 +117,25 @@ class CtaStrategyTemplate(object): orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_SELL, price, volume, self) else: self.writeCtaLog(u'sell卖平,Price:{0},Volume:{1}'.format(price, volume)) - orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_SELL, price, volume, self) + + orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_SELL, price, volume, self) + + if orderID: + self.entrust = -1 # 置当前策略的委托单状态 + # 记录委托单 + self.uncompletedOrders[orderID] = {'DIRECTION': DIRECTION_SHORT, + 'OFFSET': OFFSET_CLOSE, + 'Volume': volume, + 'Price': price, + 'OrderTime': orderTime + } + return orderID else: return None # ---------------------------------------------------------------------- - def short(self, price, volume, stop=False): + def short(self, price, volume, stop=False, orderTime=datetime.now()): """卖开""" # 如果stop为True,则意味着发本地停止单 if self.trading: @@ -111,13 +144,23 @@ class CtaStrategyTemplate(object): orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_SHORT, price, volume, self) else: self.writeCtaLog(u'short卖开,Price:{0},Volume:{1}'.format(price, volume)) - orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_SHORT, price, volume, self) + orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_SHORT, price, volume, self) + + if orderID: + self.entrust = -1 # 委托状态 + self.uncompletedOrders[orderID] = {'DIRECTION': DIRECTION_SHORT, + 'OFFSET': OFFSET_OPEN, + 'Volume': volume, + 'Price': price, + 'OrderTime': orderTime + } + return orderID else: return None # ---------------------------------------------------------------------- - def cover(self, price, volume, stop=False): + def cover(self, price, volume, stop=False,orderTime=datetime.now()): """买平""" if self.trading: # 如果stop为True,则意味着发本地停止单 @@ -125,9 +168,20 @@ class CtaStrategyTemplate(object): self.writeCtaLog(u'本地停止单,cover,买平,Price:{0},Volume:{1}'.format(price, volume)) orderID = self.ctaEngine.sendStopOrder(self.vtSymbol, CTAORDER_COVER, price, volume, self) else: - self.writeCtaLog(u'Short卖开,cover:{0},Volume:{1}'.format(price, volume)) + self.writeCtaLog(u'Cover买平,Price:{0},Volume:{1}'.format(price, volume)) + + orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_COVER, price, volume, self) + + if orderID: + self.entrust = -1 # 置当前策略的委托单状态 + # 记录委托单 + self.uncompletedOrders[orderID] = {'DIRECTION': DIRECTION_SHORT, + 'OFFSET': OFFSET_CLOSE, + 'Volume': volume, + 'Price': price, + 'OrderTime': orderTime + } - orderID = self.ctaEngine.sendOrder(self.vtSymbol, CTAORDER_COVER, price, volume, self) return orderID else: return None @@ -179,7 +233,6 @@ class CtaStrategyTemplate(object): def writeCtaLog(self, content): """记录CTA日志""" self.ctaEngine.writeCtaLog(self.name+':'+content) - ######################################################################## diff --git a/vn.trader/uiMainWindow.py b/vn.trader/uiMainWindow.py index 71682c32..88f4e584 100644 --- a/vn.trader/uiMainWindow.py +++ b/vn.trader/uiMainWindow.py @@ -85,15 +85,15 @@ class MainWindow(QtGui.QMainWindow): #connectCtpAction = QtGui.QAction(u'连接CTP', self) #connectCtpAction.triggered.connect(self.connectCtp) - connectLtsAction = QtGui.QAction(u'连接LTS', self) - connectLtsAction.triggered.connect(self.connectLts) + #connectLtsAction = QtGui.QAction(u'连接LTS', self) + #connectLtsAction.triggered.connect(self.connectLts) - connectWindAction = QtGui.QAction(u'连接Wind', self) - connectWindAction.triggered.connect(self.connectWind) + #connectWindAction = QtGui.QAction(u'连接Wind', self) + #connectWindAction.triggered.connect(self.connectWind) - connectIbAction = QtGui.QAction(u'连接IB', self) - connectIbAction.triggered.connect(self.connectIb) + #connectIbAction = QtGui.QAction(u'连接IB', self) + #connectIbAction.triggered.connect(self.connectIb) connectDbAction = QtGui.QAction(u'连接数据库', self) connectDbAction.triggered.connect(self.mainEngine.dbConnect) @@ -125,10 +125,10 @@ class MainWindow(QtGui.QMainWindow): sysMenu.addAction(connectCtpTestAction) #sysMenu.addAction(connectCtpAction) - sysMenu.addAction(connectLtsAction) - sysMenu.addAction(connectWindAction) + #sysMenu.addAction(connectLtsAction) + #sysMenu.addAction(connectWindAction) - sysMenu.addAction(connectIbAction) + #sysMenu.addAction(connectIbAction) sysMenu.addSeparator() sysMenu.addAction(connectDbAction) diff --git a/vn.trader/vtConstant.py b/vn.trader/vtConstant.py index 228c7f1f..72cec90e 100644 --- a/vn.trader/vtConstant.py +++ b/vn.trader/vtConstant.py @@ -6,6 +6,11 @@ EMPTY_UNICODE = u'' EMPTY_INT = 0 EMPTY_FLOAT = 0.0 +# k线颜色 +COLOR_RED = u'Red' # 上升K线 +COLOR_BLUE = u'Blue' # 下降K线 +COLOR_EQUAL = u'Equal' # 平K线 + # 方向常量 DIRECTION_NONE = u'无方向' DIRECTION_LONG = u'多' diff --git a/vn.trader/vtGateway.py b/vn.trader/vtGateway.py index f968d78f..7e2d9d0f 100644 --- a/vn.trader/vtGateway.py +++ b/vn.trader/vtGateway.py @@ -2,6 +2,8 @@ import time +from datetime import datetime + from eventEngine import * from vtConstant import * @@ -342,7 +344,8 @@ class VtLogData(VtBaseData): """Constructor""" super(VtLogData, self).__init__() - self.logTime = time.strftime('%X', time.localtime()) # 日志生成时间 + #self.logTime = time.strftime('%X:%f', time.localtime()) # 日志生成时间 + self.logTime = datetime.now().strftime('%X:%f') self.logContent = EMPTY_UNICODE # 日志信息 diff --git a/vn.trader/vtMain.py b/vn.trader/vtMain.py index 9088ee76..3905c53d 100644 --- a/vn.trader/vtMain.py +++ b/vn.trader/vtMain.py @@ -2,15 +2,23 @@ import sys import ctypes +#import bsddb3 as bsddb +#sys.modules['bsddb'] = bsddb from vtEngine import MainEngine from uiMainWindow import * +from simple_monitor import * + +from setup_logger import setup_logger + +setup_logger(filename='vnpy.log',debug=True) + # ---------------------------------------------------------------------- def main(): """主程序入口""" # 设置底部任务栏图标,win7以下请注释掉 - ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.py demo') + ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.py demo') app = QtGui.QApplication(sys.argv) app.setWindowIcon(QtGui.QIcon('vnpy.ico')) @@ -19,7 +27,15 @@ def main(): mainEngine = MainEngine() mainWindow = MainWindow(mainEngine, mainEngine.eventEngine, mainEngine.dataEngine) mainWindow.showMaximized() - + + logM = LogMonitor(mainEngine.eventEngine) + ctaLogM = CtaLogMonitor(mainEngine.eventEngine) + errorM = ErrorMonitor(mainEngine.eventEngine) + tradeM = TradeMonitor(mainEngine.eventEngine) + orderM = OrderMonitor(mainEngine.eventEngine, mainEngine) + positionM = PositionMonitor(mainEngine.eventEngine) + accountM = AccountMonitor(mainEngine.eventEngine) + sys.exit(app.exec_()) if __name__ == '__main__': diff --git a/vn.trader/windGateway.py b/vn.trader/windGateway.py index 3587a214..72dc9ca1 100644 --- a/vn.trader/windGateway.py +++ b/vn.trader/windGateway.py @@ -6,10 +6,10 @@ Wind Python API的gateway接入 from copy import copy -try: - from WindPy import w -except ImportError: - print u'请先安装WindPy接口' +#try: +# from WindPy import w +#except ImportError: +# print u'请先安装WindPy接口' from vtGateway import *