2016-07-02 03:12:44 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
|
from uiBasicWidget import *
|
2017-04-28 14:10:07 +00:00
|
|
|
|
from ctaStrategy.uiCtaWidget import CtaEngineManager
|
2016-07-02 03:12:44 +00:00
|
|
|
|
from dataRecorder.uiDrWidget import DrEngineManager
|
|
|
|
|
from riskManager.uiRmWidget import RmEngineManager
|
2017-06-08 09:20:36 +00:00
|
|
|
|
#from uiFullMonitorWidget import MonitorWidget
|
|
|
|
|
#from uiKChartWidget import CandleForm
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-08-04 16:08:29 +00:00
|
|
|
|
import os
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
########################################################################
|
|
|
|
|
class MainWindow(QtGui.QMainWindow):
|
|
|
|
|
"""主窗口"""
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self, mainEngine, eventEngine):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(MainWindow, self).__init__()
|
|
|
|
|
|
|
|
|
|
self.mainEngine = mainEngine
|
|
|
|
|
self.eventEngine = eventEngine
|
|
|
|
|
|
|
|
|
|
self.widgetDict = {} # 用来保存子窗口的字典
|
2016-11-30 06:28:30 +00:00
|
|
|
|
|
|
|
|
|
self.connectGatewayDict = {}
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
self.initUi()
|
2016-11-30 06:28:30 +00:00
|
|
|
|
#self.loadWindowSettings()
|
2016-07-13 15:40:59 +00:00
|
|
|
|
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connected = False
|
2016-10-26 10:27:55 +00:00
|
|
|
|
self.autoDisConnect = False
|
|
|
|
|
|
2016-10-28 07:22:31 +00:00
|
|
|
|
self.orderSaveDate = EMPTY_STRING
|
|
|
|
|
self.barSaveDate = EMPTY_STRING
|
|
|
|
|
|
2016-11-30 06:28:30 +00:00
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def initUi(self):
|
|
|
|
|
"""初始化界面"""
|
2016-11-30 06:28:30 +00:00
|
|
|
|
path = os.getcwd().rsplit('\\')[-1]
|
|
|
|
|
|
|
|
|
|
self.setWindowTitle(path)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
self.initCentral()
|
|
|
|
|
self.initMenu()
|
|
|
|
|
self.initStatusBar()
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def initCentral(self):
|
|
|
|
|
"""初始化中心区域"""
|
|
|
|
|
widgetMarketM, dockMarketM = self.createDock(MarketMonitor, u'行情', QtCore.Qt.RightDockWidgetArea)
|
|
|
|
|
widgetLogM, dockLogM = self.createDock(LogMonitor, u'日志', QtCore.Qt.BottomDockWidgetArea)
|
|
|
|
|
widgetErrorM, dockErrorM = self.createDock(ErrorMonitor, u'错误', QtCore.Qt.BottomDockWidgetArea)
|
2016-09-12 02:58:24 +00:00
|
|
|
|
self.widgetTradeM, dockTradeM = self.createDock(TradeMonitor, u'成交', QtCore.Qt.BottomDockWidgetArea)
|
2016-08-04 16:08:29 +00:00
|
|
|
|
self.widgetOrderM, dockOrderM = self.createDock(OrderMonitor, u'委托', QtCore.Qt.RightDockWidgetArea)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
widgetPositionM, dockPositionM = self.createDock(PositionMonitor, u'持仓', QtCore.Qt.BottomDockWidgetArea)
|
|
|
|
|
widgetAccountM, dockAccountM = self.createDock(AccountMonitor, u'资金', QtCore.Qt.BottomDockWidgetArea)
|
|
|
|
|
widgetTradingW, dockTradingW = self.createDock(TradingWidget, u'交易', QtCore.Qt.LeftDockWidgetArea)
|
|
|
|
|
|
|
|
|
|
self.tabifyDockWidget(dockTradeM, dockErrorM)
|
|
|
|
|
self.tabifyDockWidget(dockTradeM, dockLogM)
|
|
|
|
|
self.tabifyDockWidget(dockPositionM, dockAccountM)
|
|
|
|
|
|
|
|
|
|
dockTradeM.raise_()
|
|
|
|
|
dockPositionM.raise_()
|
|
|
|
|
|
|
|
|
|
# 连接组件之间的信号
|
|
|
|
|
widgetPositionM.itemDoubleClicked.connect(widgetTradingW.closePosition)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def initMenu(self):
|
|
|
|
|
"""初始化菜单"""
|
|
|
|
|
# 创建操作
|
2016-07-03 16:59:48 +00:00
|
|
|
|
connectCtpProdAction = QtGui.QAction(u'上海中期CTP', self)
|
|
|
|
|
connectCtpProdAction.triggered.connect(self.connectCtpProd)
|
|
|
|
|
|
|
|
|
|
connectCtpPostAction = QtGui.QAction(u'中期盘后CTP', self)
|
|
|
|
|
connectCtpPostAction.triggered.connect(self.connectCtpPost)
|
|
|
|
|
|
|
|
|
|
connectCtpTestAction = QtGui.QAction(u'光大CTP', self)
|
|
|
|
|
connectCtpTestAction.triggered.connect(self.connectCtpEBF)
|
|
|
|
|
|
2017-04-20 12:29:24 +00:00
|
|
|
|
connectCtpJRAction = QtGui.QAction(u'金瑞CTP', self)
|
|
|
|
|
connectCtpJRAction.triggered.connect(self.connectCtpJR)
|
|
|
|
|
|
2017-06-08 09:20:36 +00:00
|
|
|
|
connectCtpJR2Action = QtGui.QAction(u'金瑞CTP2', self)
|
|
|
|
|
connectCtpJR2Action.triggered.connect(self.connectCtpJR2)
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
connectCtpAction = QtGui.QAction(u'连接CTP', self)
|
|
|
|
|
connectCtpAction.triggered.connect(self.connectCtp)
|
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectLtsAction = QtGui.QAction(u'连接LTS', self)
|
|
|
|
|
#connectLtsAction.triggered.connect(self.connectLts)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectKsotpAction = QtGui.QAction(u'连接金仕达期权', self)
|
|
|
|
|
#connectKsotpAction.triggered.connect(self.connectKsotp)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectFemasAction = QtGui.QAction(u'连接飞马', self)
|
|
|
|
|
#connectFemasAction.triggered.connect(self.connectFemas)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectXspeedAction = QtGui.QAction(u'连接飞创', self)
|
|
|
|
|
#connectXspeedAction.triggered.connect(self.connectXspeed)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectKsgoldAction = QtGui.QAction(u'连接金仕达黄金', self)
|
|
|
|
|
#connectKsgoldAction.triggered.connect(self.connectKsgold)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectSgitAction = QtGui.QAction(u'连接飞鼠', self)
|
|
|
|
|
#connectSgitAction.triggered.connect(self.connectSgit)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectWindAction = QtGui.QAction(u'连接Wind', self)
|
|
|
|
|
#connectWindAction.triggered.connect(self.connectWind)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectIbAction = QtGui.QAction(u'连接IB', self)
|
|
|
|
|
#connectIbAction.triggered.connect(self.connectIb)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#connectOandaAction = QtGui.QAction(u'连接OANDA', self)
|
|
|
|
|
#connectOandaAction.triggered.connect(self.connectOanda)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
connectDbAction = QtGui.QAction(u'连接数据库', self)
|
|
|
|
|
connectDbAction.triggered.connect(self.mainEngine.dbConnect)
|
2016-10-26 10:27:55 +00:00
|
|
|
|
|
|
|
|
|
autoDisconnetAction = QtGui.QAction(u'自动断开重连', self)
|
|
|
|
|
autoDisconnetAction.triggered.connect(self.setAutoDisconnect)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
testAction = QtGui.QAction(u'测试', self)
|
2017-06-08 09:43:07 +00:00
|
|
|
|
testAction.triggered.connect(self.test)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
exitAction = QtGui.QAction(u'退出', self)
|
|
|
|
|
exitAction.triggered.connect(self.close)
|
|
|
|
|
|
|
|
|
|
aboutAction = QtGui.QAction(u'关于', self)
|
|
|
|
|
aboutAction.triggered.connect(self.openAbout)
|
|
|
|
|
|
|
|
|
|
contractAction = QtGui.QAction(u'查询合约', self)
|
|
|
|
|
contractAction.triggered.connect(self.openContract)
|
|
|
|
|
|
|
|
|
|
drAction = QtGui.QAction(u'行情数据记录', self)
|
|
|
|
|
drAction.triggered.connect(self.openDr)
|
|
|
|
|
|
|
|
|
|
ctaAction = QtGui.QAction(u'CTA策略', self)
|
|
|
|
|
ctaAction.triggered.connect(self.openCta)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
2017-06-08 09:20:36 +00:00
|
|
|
|
#monitorAction = QtGui.QAction(u'周期监控', self)
|
|
|
|
|
#monitorAction.triggered.connect(self.openMonitor)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
2017-06-08 09:20:36 +00:00
|
|
|
|
#kChart = QtGui.QAction(u'K线图', self)
|
|
|
|
|
#kChart.triggered.connect(self.openKChart)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
rmAction = QtGui.QAction(u'风险管理', self)
|
|
|
|
|
rmAction.triggered.connect(self.openRm)
|
|
|
|
|
|
|
|
|
|
# 创建菜单
|
|
|
|
|
menubar = self.menuBar()
|
|
|
|
|
|
|
|
|
|
# 设计为只显示存在的接口
|
|
|
|
|
sysMenu = menubar.addMenu(u'系统')
|
2016-07-03 16:59:48 +00:00
|
|
|
|
sysMenu.addAction(connectCtpProdAction)
|
|
|
|
|
sysMenu.addAction(connectCtpPostAction)
|
|
|
|
|
sysMenu.addAction(connectCtpTestAction)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
sysMenu.addAction(connectCtpJRAction)
|
2017-06-08 09:20:36 +00:00
|
|
|
|
sysMenu.addAction(connectCtpJR2Action)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
if 'CTP' in self.mainEngine.gatewayDict:
|
|
|
|
|
sysMenu.addAction(connectCtpAction)
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#if 'LTS' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectLtsAction)
|
|
|
|
|
#if 'FEMAS' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectFemasAction)
|
|
|
|
|
#if 'XSPEED' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectXspeedAction)
|
|
|
|
|
#if 'KSOTP' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectKsotpAction)
|
|
|
|
|
#if 'KSGOLD' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectKsgoldAction)
|
|
|
|
|
#if 'SGIT' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectSgitAction)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
sysMenu.addSeparator()
|
2016-10-26 10:27:55 +00:00
|
|
|
|
sysMenu.addAction(autoDisconnetAction)
|
2016-07-13 15:40:59 +00:00
|
|
|
|
#if 'IB' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectIbAction)
|
|
|
|
|
#if 'OANDA' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectOandaAction)
|
|
|
|
|
#sysMenu.addSeparator()
|
|
|
|
|
#if 'Wind' in self.mainEngine.gatewayDict:
|
|
|
|
|
# sysMenu.addAction(connectWindAction)
|
2016-07-02 03:12:44 +00:00
|
|
|
|
sysMenu.addSeparator()
|
|
|
|
|
sysMenu.addAction(connectDbAction)
|
|
|
|
|
sysMenu.addSeparator()
|
|
|
|
|
sysMenu.addAction(exitAction)
|
|
|
|
|
|
|
|
|
|
functionMenu = menubar.addMenu(u'功能')
|
|
|
|
|
functionMenu.addAction(contractAction)
|
|
|
|
|
functionMenu.addAction(drAction)
|
|
|
|
|
functionMenu.addAction(rmAction)
|
|
|
|
|
|
|
|
|
|
# 算法相关
|
|
|
|
|
algoMenu = menubar.addMenu(u'算法')
|
|
|
|
|
algoMenu.addAction(ctaAction)
|
2017-06-08 09:20:36 +00:00
|
|
|
|
#algoMenu.addAction(monitorAction)
|
|
|
|
|
#algoMenu.addAction(kChart)
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
# 帮助
|
|
|
|
|
helpMenu = menubar.addMenu(u'帮助')
|
|
|
|
|
helpMenu.addAction(aboutAction)
|
|
|
|
|
helpMenu.addAction(testAction)
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def initStatusBar(self):
|
|
|
|
|
"""初始化状态栏"""
|
|
|
|
|
self.statusLabel = QtGui.QLabel()
|
|
|
|
|
self.statusLabel.setAlignment(QtCore.Qt.AlignLeft)
|
|
|
|
|
|
|
|
|
|
self.statusBar().addPermanentWidget(self.statusLabel)
|
|
|
|
|
self.statusLabel.setText(self.getCpuMemory())
|
|
|
|
|
|
|
|
|
|
self.sbCount = 0
|
|
|
|
|
self.sbTrigger = 10 # 10秒刷新一次
|
|
|
|
|
self.eventEngine.register(EVENT_TIMER, self.updateStatusBar)
|
2016-11-30 06:28:30 +00:00
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def updateStatusBar(self, event):
|
2016-11-30 06:28:30 +00:00
|
|
|
|
"""1、在状态栏更新CPU和内存信息"""
|
|
|
|
|
# 2、定时断开服务器连接
|
|
|
|
|
# 3、定时重连服务器
|
|
|
|
|
# 4、定时保存每日的委托单
|
|
|
|
|
# 5、定时执行策略的保存事件
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
self.sbCount += 1
|
2016-07-13 15:40:59 +00:00
|
|
|
|
|
|
|
|
|
# 更新任务栏
|
2016-07-02 03:12:44 +00:00
|
|
|
|
if self.sbCount == self.sbTrigger:
|
|
|
|
|
self.sbCount = 0
|
2016-10-26 10:27:55 +00:00
|
|
|
|
info = self.getCpuMemory()
|
|
|
|
|
|
|
|
|
|
if self.autoDisConnect:
|
|
|
|
|
info = info + u'[自动断开重连]'
|
|
|
|
|
|
|
|
|
|
self.statusLabel.setText(info)
|
2016-07-13 15:40:59 +00:00
|
|
|
|
|
2016-11-30 06:28:30 +00:00
|
|
|
|
if self.connectGatewayDict:
|
2016-10-19 12:41:30 +00:00
|
|
|
|
s = u''.join(str(e) for e in self.connectGatewayDict.values())
|
2016-11-30 06:28:30 +00:00
|
|
|
|
|
|
|
|
|
if not self.connected:
|
|
|
|
|
s = s + u' [已断开]'
|
|
|
|
|
|
|
|
|
|
self.setWindowTitle(s)
|
|
|
|
|
|
|
|
|
|
# 定时断开
|
|
|
|
|
if self.connected and self.trade_off() and self.autoDisConnect:
|
|
|
|
|
self.disconnect()
|
|
|
|
|
self.mainEngine.writeLog(u'断开连接{0}'.format(self.connectGatewayDict.values()))
|
|
|
|
|
self.mainEngine.writeLog(u'清空数据引擎')
|
|
|
|
|
self.mainEngine.clearData()
|
|
|
|
|
self.mainEngine.writeLog(u'清空委托列表')
|
|
|
|
|
self.widgetOrderM.clearData()
|
|
|
|
|
self.mainEngine.writeLog(u'清空交易列表')
|
|
|
|
|
self.widgetTradeM.clearData()
|
|
|
|
|
|
|
|
|
|
# 定时重连
|
|
|
|
|
if not self.connected \
|
|
|
|
|
and self.autoDisConnect \
|
|
|
|
|
and not self.trade_off()\
|
|
|
|
|
and len(self.connectGatewayDict) > 0:
|
|
|
|
|
|
|
|
|
|
self.mainEngine.writeLog(u'清空数据引擎')
|
|
|
|
|
self.mainEngine.clearData()
|
|
|
|
|
self.mainEngine.writeLog(u'清空委托列表')
|
|
|
|
|
self.widgetOrderM.clearData()
|
|
|
|
|
self.mainEngine.writeLog(u'清空交易列表')
|
|
|
|
|
self.widgetTradeM.clearData()
|
|
|
|
|
s = u''.join(str(e) for e in self.connectGatewayDict.values())
|
|
|
|
|
self.mainEngine.writeLog(u'重新连接{0}'.format(s))
|
|
|
|
|
|
|
|
|
|
for key in self.connectGatewayDict.keys():
|
|
|
|
|
self.mainEngine.connect(key)
|
|
|
|
|
|
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
|
|
# 交易日收盘后保存所有委托记录,
|
|
|
|
|
dt = datetime.now()
|
|
|
|
|
today = datetime.now().strftime('%y%m%d')
|
|
|
|
|
if dt.hour == 15 and dt.minute == 1 and len(self.connectGatewayDict) > 0 and today!=self.orderSaveDate:
|
|
|
|
|
self.orderSaveDate = today
|
|
|
|
|
self.mainEngine.writeLog(u'保存所有委托记录')
|
|
|
|
|
orderfile = os.getcwd() +'/orders/{0}.csv'.format(self.orderSaveDate)
|
|
|
|
|
if os.path.exists(orderfile):
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
self.widgetOrderM.saveToCsv(path=orderfile)
|
|
|
|
|
|
|
|
|
|
# 调用各策略保存数据
|
|
|
|
|
if ((dt.hour == 15 and dt.minute == 1) or (dt.hour == 2 and dt.minute == 31)) \
|
|
|
|
|
and len(self.connectGatewayDict) > 0 \
|
|
|
|
|
and today != self.barSaveDate \
|
|
|
|
|
and self.connected:
|
|
|
|
|
self.barSaveDate = today
|
|
|
|
|
self.mainEngine.writeLog(u'调用各策略保存数据')
|
|
|
|
|
self.mainEngine.saveData()
|
|
|
|
|
|
|
|
|
|
if not (dt.hour == 15 or dt.hour == 2):
|
|
|
|
|
self.barSaveDate = EMPTY_STRING
|
2016-10-28 07:22:31 +00:00
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def getCpuMemory(self):
|
|
|
|
|
"""获取CPU和内存状态信息"""
|
|
|
|
|
cpuPercent = psutil.cpu_percent()
|
|
|
|
|
memoryPercent = psutil.virtual_memory().percent
|
|
|
|
|
return u'CPU使用率:%d%% 内存使用率:%d%%' % (cpuPercent, memoryPercent)
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
def connectCtpProd(self):
|
2016-07-13 15:40:59 +00:00
|
|
|
|
"""连接上海中期生产环境CTP接口"""
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
self.mainEngine.connect('CTP_Prod')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['CTP_Prod'] = u'上海中期CTP'
|
|
|
|
|
self.connected = True
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
def connectCtpPost(self):
|
2016-07-13 15:40:59 +00:00
|
|
|
|
"""连接上海中期盘后CTP接口"""
|
2016-07-03 16:59:48 +00:00
|
|
|
|
self.mainEngine.connect('CTP_Post')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['CTP_Post'] = u'中期盘后CTP'
|
|
|
|
|
self.connected = True
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
|
|
|
|
def connectCtpEBF(self):
|
|
|
|
|
"""连接光大期货CTP接口"""
|
|
|
|
|
self.mainEngine.connect('CTP_EBF')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['CTP_EBF'] = u'光大CTP'
|
|
|
|
|
self.connected = True
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
2017-04-20 12:29:24 +00:00
|
|
|
|
def connectCtpJR(self):
|
|
|
|
|
"""连接金瑞期货CTP接口"""
|
|
|
|
|
self.mainEngine.connect('CTP_JR')
|
|
|
|
|
self.connectGatewayDict['CTP_JR'] = u'金瑞CTP'
|
|
|
|
|
self.connected = True
|
|
|
|
|
|
2017-06-08 09:20:36 +00:00
|
|
|
|
def connectCtpJR2(self):
|
|
|
|
|
"""连接金瑞期货CTP接口"""
|
|
|
|
|
self.mainEngine.connect('CTP_JR2')
|
|
|
|
|
self.connectGatewayDict['CTP_JR2'] = u'金瑞CTP2'
|
|
|
|
|
self.connected = True
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
def connectCtpTest(self):
|
2016-07-13 15:40:59 +00:00
|
|
|
|
"""连接SNOW测试环境CTP接口"""
|
2016-07-03 16:59:48 +00:00
|
|
|
|
self.mainEngine.connect('CTP_Test')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['CTP_Test'] = u'SNOW测试'
|
|
|
|
|
self.connected = True
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def connectCtp(self):
|
|
|
|
|
"""连接CTP接口"""
|
|
|
|
|
self.mainEngine.connect('CTP')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['CTP'] = u'CTP'
|
|
|
|
|
self.connected = True
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def connectLts(self):
|
|
|
|
|
"""连接LTS接口"""
|
2016-07-13 15:40:59 +00:00
|
|
|
|
self.mainEngine.connect('LTS')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['LTS'] = u'LTS'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectKsotp(self):
|
|
|
|
|
"""连接金仕达期权接口"""
|
2016-07-13 15:40:59 +00:00
|
|
|
|
self.mainEngine.connect('KSOTP')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['KSOTP'] = u'金仕达期权'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectFemas(self):
|
|
|
|
|
"""连接飞马接口"""
|
2016-07-13 15:40:59 +00:00
|
|
|
|
self.mainEngine.connect('FEMAS')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['FEMAS'] = u'FEMAS飞马'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectXspeed(self):
|
|
|
|
|
"""连接飞马接口"""
|
2016-07-13 15:40:59 +00:00
|
|
|
|
self.mainEngine.connect('XSPEED')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['XSPEED'] = u'XSPEED飞马接口'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectKsgold(self):
|
|
|
|
|
"""连接金仕达黄金接口"""
|
2016-07-13 15:40:59 +00:00
|
|
|
|
self.mainEngine.connect('KSGOLD')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict ['KSGOLD'] = u'金仕达黄金'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectSgit(self):
|
|
|
|
|
"""连接飞鼠接口"""
|
2016-07-13 15:40:59 +00:00
|
|
|
|
self.mainEngine.connect('SGIT')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['SGIT'] = u'飞鼠'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectWind(self):
|
|
|
|
|
"""连接Wind接口"""
|
|
|
|
|
self.mainEngine.connect('Wind')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['Wind'] = u'Wind'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def connectIb(self):
|
|
|
|
|
"""连接Ib"""
|
|
|
|
|
self.mainEngine.connect('IB')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['IB'] = u'盈透'
|
|
|
|
|
self.connected = True
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def connectOanda(self):
|
|
|
|
|
"""连接OANDA"""
|
|
|
|
|
self.mainEngine.connect('OANDA')
|
2016-10-19 12:41:30 +00:00
|
|
|
|
self.connectGatewayDict['OANDA'] = u'OANDA'
|
|
|
|
|
self.connected = True
|
2016-10-26 10:27:55 +00:00
|
|
|
|
|
|
|
|
|
def setAutoDisconnect(self):
|
|
|
|
|
|
|
|
|
|
if self.autoDisConnect:
|
|
|
|
|
self.autoDisConnect = False
|
|
|
|
|
else:
|
|
|
|
|
self.autoDisConnect = True
|
|
|
|
|
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def test(self):
|
|
|
|
|
"""测试按钮用的函数"""
|
|
|
|
|
# 有需要使用手动触发的测试函数可以写在这里
|
2016-10-26 10:27:55 +00:00
|
|
|
|
self.mainEngine.saveData()
|
2016-07-02 03:12:44 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def openAbout(self):
|
|
|
|
|
"""打开关于"""
|
|
|
|
|
try:
|
|
|
|
|
self.widgetDict['aboutW'].show()
|
|
|
|
|
except KeyError:
|
|
|
|
|
self.widgetDict['aboutW'] = AboutWidget(self)
|
|
|
|
|
self.widgetDict['aboutW'].show()
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def openContract(self):
|
|
|
|
|
"""打开合约查询"""
|
|
|
|
|
try:
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
self.widgetDict['contractM'].show()
|
|
|
|
|
except KeyError:
|
|
|
|
|
self.widgetDict['contractM'] = ContractMonitor(self.mainEngine)
|
|
|
|
|
self.widgetDict['contractM'].show()
|
2016-07-03 16:59:48 +00:00
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def openCta(self):
|
|
|
|
|
"""打开CTA组件"""
|
|
|
|
|
try:
|
|
|
|
|
self.widgetDict['ctaM'].showMaximized()
|
|
|
|
|
except KeyError:
|
|
|
|
|
self.widgetDict['ctaM'] = CtaEngineManager(self.mainEngine.ctaEngine, self.eventEngine)
|
|
|
|
|
self.widgetDict['ctaM'].showMaximized()
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
2017-06-08 09:20:36 +00:00
|
|
|
|
#def openMonitor(self):
|
|
|
|
|
# try:
|
|
|
|
|
# self.widgetDict['Monitor'].showMaximized()
|
|
|
|
|
# except KeyError:
|
|
|
|
|
# self.widgetDict['Monitor'] = MonitorWidget(self.mainEngine.ctaEngine, self.eventEngine)
|
|
|
|
|
# self.widgetDict['Monitor'].showMaximized()
|
|
|
|
|
#
|
|
|
|
|
#def openKChart(self):
|
|
|
|
|
# try:
|
|
|
|
|
# self.widgetDict['kChart'].showMaximized()
|
|
|
|
|
# except KeyError:
|
|
|
|
|
# self.widgetDict['kChart'] = CandleForm(self.mainEngine.ctaEngine, self.eventEngine, symbol='p1705', period='minute',interval=5)
|
|
|
|
|
# self.widgetDict['kChart'].showMaximized()
|
2017-04-20 12:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def openDr(self):
|
|
|
|
|
"""打开行情数据记录组件"""
|
|
|
|
|
try:
|
|
|
|
|
self.widgetDict['drM'].showMaximized()
|
|
|
|
|
except KeyError:
|
|
|
|
|
self.widgetDict['drM'] = DrEngineManager(self.mainEngine.drEngine, self.eventEngine)
|
|
|
|
|
self.widgetDict['drM'].showMaximized()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def openRm(self):
|
|
|
|
|
"""打开组件"""
|
|
|
|
|
try:
|
|
|
|
|
self.widgetDict['rmM'].show()
|
|
|
|
|
except KeyError:
|
|
|
|
|
self.widgetDict['rmM'] = RmEngineManager(self.mainEngine.rmEngine, self.eventEngine)
|
|
|
|
|
self.widgetDict['rmM'].show()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def closeEvent(self, event):
|
|
|
|
|
"""关闭事件"""
|
|
|
|
|
reply = QtGui.QMessageBox.question(self, u'退出',
|
|
|
|
|
u'确认退出?', QtGui.QMessageBox.Yes |
|
|
|
|
|
QtGui.QMessageBox.No, QtGui.QMessageBox.No)
|
|
|
|
|
|
|
|
|
|
if reply == QtGui.QMessageBox.Yes:
|
|
|
|
|
for widget in self.widgetDict.values():
|
|
|
|
|
widget.close()
|
|
|
|
|
self.saveWindowSettings()
|
|
|
|
|
|
|
|
|
|
self.mainEngine.exit()
|
|
|
|
|
event.accept()
|
|
|
|
|
else:
|
|
|
|
|
event.ignore()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def createDock(self, widgetClass, widgetName, widgetArea):
|
|
|
|
|
"""创建停靠组件"""
|
|
|
|
|
widget = widgetClass(self.mainEngine, self.eventEngine)
|
|
|
|
|
dock = QtGui.QDockWidget(widgetName)
|
|
|
|
|
dock.setWidget(widget)
|
|
|
|
|
dock.setObjectName(widgetName)
|
|
|
|
|
dock.setFeatures(dock.DockWidgetFloatable|dock.DockWidgetMovable)
|
|
|
|
|
self.addDockWidget(widgetArea, dock)
|
|
|
|
|
return widget, dock
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def saveWindowSettings(self):
|
|
|
|
|
"""保存窗口设置"""
|
|
|
|
|
settings = QtCore.QSettings('vn.py', 'vn.trader')
|
|
|
|
|
settings.setValue('state', self.saveState())
|
|
|
|
|
settings.setValue('geometry', self.saveGeometry())
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def loadWindowSettings(self):
|
|
|
|
|
"""载入窗口设置"""
|
|
|
|
|
settings = QtCore.QSettings('vn.py', 'vn.trader')
|
2016-07-25 00:13:26 +00:00
|
|
|
|
# 这里由于PyQt4的版本不同,settings.value('state')调用返回的结果可能是:
|
|
|
|
|
# 1. None(初次调用,注册表里无相应记录,因此为空)
|
|
|
|
|
# 2. QByteArray(比较新的PyQt4)
|
|
|
|
|
# 3. QVariant(以下代码正确执行所需的返回结果)
|
|
|
|
|
# 所以为了兼容考虑,这里加了一个try...except,如果是1、2的情况就pass
|
|
|
|
|
# 可能导致主界面的设置无法载入(每次退出时的保存其实是成功了)
|
|
|
|
|
try:
|
|
|
|
|
self.restoreState(settings.value('state').toByteArray())
|
|
|
|
|
self.restoreGeometry(settings.value('geometry').toByteArray())
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
2016-07-02 03:12:44 +00:00
|
|
|
|
|
|
|
|
|
|
2016-10-19 12:41:30 +00:00
|
|
|
|
def disconnect(self):
|
|
|
|
|
""""断开底层gateway的连接"""
|
|
|
|
|
self.mainEngine.disconnect()
|
|
|
|
|
self.connected = False
|
|
|
|
|
|
|
|
|
|
def trade_off(self):
|
|
|
|
|
"""检查现在是否为非交易时间"""
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
a = datetime.now().replace(hour=2, minute=35, second=0, microsecond=0)
|
|
|
|
|
b = datetime.now().replace(hour=8, minute=30, second=0, microsecond=0)
|
|
|
|
|
c = datetime.now().replace(hour=15, minute=30, second=0, microsecond=0)
|
|
|
|
|
d = datetime.now().replace(hour=20, minute=30, second=0, microsecond=0)
|
|
|
|
|
weekend = (now.isoweekday() == 6 and now >= a) or (now.isoweekday() == 7)
|
|
|
|
|
off = (a<= now <= b) or (c <= now <= d) or weekend
|
|
|
|
|
return off
|
|
|
|
|
|
2016-07-02 03:12:44 +00:00
|
|
|
|
########################################################################
|
|
|
|
|
class AboutWidget(QtGui.QDialog):
|
|
|
|
|
"""显示关于信息"""
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(AboutWidget, self).__init__(parent)
|
|
|
|
|
|
|
|
|
|
self.initUi()
|
|
|
|
|
|
2016-07-03 16:59:48 +00:00
|
|
|
|
# ----------------------------------------------------------------------
|
2016-07-02 03:12:44 +00:00
|
|
|
|
def initUi(self):
|
|
|
|
|
""""""
|
|
|
|
|
self.setWindowTitle(u'关于VnTrader')
|
|
|
|
|
|
|
|
|
|
text = u"""
|
|
|
|
|
Developed by traders, for traders.
|
|
|
|
|
|
|
|
|
|
License:MIT
|
|
|
|
|
|
|
|
|
|
Website:www.vnpy.org
|
|
|
|
|
|
|
|
|
|
Github:www.github.com/vnpy/vnpy
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
label = QtGui.QLabel()
|
|
|
|
|
label.setText(text)
|
|
|
|
|
label.setMinimumWidth(500)
|
|
|
|
|
|
|
|
|
|
vbox = QtGui.QVBoxLayout()
|
|
|
|
|
vbox.addWidget(label)
|
|
|
|
|
|
|
|
|
|
self.setLayout(vbox)
|
|
|
|
|
|