重构目录,将应用模块放到单独的app文件夹下

This commit is contained in:
vn.py 2017-06-01 00:10:07 +08:00
parent 06ab98e95b
commit 1fb9eaf3ab
72 changed files with 308 additions and 226 deletions

View File

@ -0,0 +1 @@
# encoding: UTF-8

View File

Can't render this file because it is too large.

View File

@ -0,0 +1,9 @@
# encoding: UTF-8
from ctaEngine import CtaEngine
from uiCtaWidget import CtaEngineManager
appName = 'CtaStrategy'
appDisplayName = u'CTA策略'
appEngine = CtaEngine
appWidget = CtaEngineManager

View File

@ -17,7 +17,7 @@ from vnpy.trader.vtObject import VtTickData, VtBarData
from vnpy.trader.vtConstant import * from vnpy.trader.vtConstant import *
from vnpy.trader.vtGateway import VtOrderData, VtTradeData from vnpy.trader.vtGateway import VtOrderData, VtTradeData
from vnpy.trader.ctaStrategy.ctaBase import * from vnpy.trader.app.ctaStrategy.ctaBase import *
######################################################################## ########################################################################

View File

@ -31,8 +31,8 @@ from vnpy.trader.vtObject import VtTickData, VtBarData
from vnpy.trader.vtGateway import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData from vnpy.trader.vtGateway import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData
from vnpy.trader.vtFunction import todayDate from vnpy.trader.vtFunction import todayDate
from vnpy.trader.ctaStrategy.ctaBase import * from vnpy.trader.app.ctaStrategy.ctaBase import *
from vnpy.trader.ctaStrategy.strategy import STRATEGY_CLASS from vnpy.trader.app.ctaStrategy.strategy import STRATEGY_CLASS
######################################################################## ########################################################################

View File

@ -17,7 +17,7 @@ from vnpy.trader.vtGlobal import globalSetting
from vnpy.trader.vtConstant import * from vnpy.trader.vtConstant import *
from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtObject import VtBarData
from vnpy.trader.ctaStrategy.datayesClient import DatayesClient from vnpy.trader.app.ctaStrategy.datayesClient import DatayesClient
# 以下为vn.trader和通联数据规定的交易所代码映射 # 以下为vn.trader和通联数据规定的交易所代码映射

View File

@ -6,7 +6,7 @@
from vnpy.trader.vtConstant import * from vnpy.trader.vtConstant import *
from vnpy.trader.ctaStrategy.ctaBase import * from vnpy.trader.app.ctaStrategy.ctaBase import *
######################################################################## ########################################################################
class CtaTemplate(object): class CtaTemplate(object):

View File

@ -0,0 +1,13 @@
# encoding: UTF-8
import json
import os
import traceback
# 默认设置
from chinese import text
# 是否要使用英文
from vnpy.trader.vtGlobal import globalSetting
if globalSetting['language'] == 'english':
from english import text

View File

@ -20,7 +20,7 @@ for root, subdirs, files in os.walk(path):
# 只有文件名中包含strategy且非.pyc的文件才是策略文件 # 只有文件名中包含strategy且非.pyc的文件才是策略文件
if 'strategy' in name and '.pyc' not in name: if 'strategy' in name and '.pyc' not in name:
# 模块名称需要上前缀 # 模块名称需要上前缀
moduleName = 'vnpy.trader.ctaStrategy.strategy.' + name.replace('.py', '') moduleName = 'vnpy.trader.app.ctaStrategy.strategy.' + name.replace('.py', '')
# 使用importlib动态载入模块 # 使用importlib动态载入模块
try: try:

View File

@ -15,7 +15,7 @@ import numpy as np
from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtObject import VtBarData
from vnpy.trader.vtConstant import EMPTY_STRING from vnpy.trader.vtConstant import EMPTY_STRING
from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate from vnpy.trader.app.ctaStrategy.ctaTemplate import CtaTemplate
######################################################################## ########################################################################
@ -240,7 +240,7 @@ class AtrRsiStrategy(CtaTemplate):
if __name__ == '__main__': if __name__ == '__main__':
# 提供直接双击回测的功能 # 提供直接双击回测的功能
# 导入PyQt4的包是为了保证matplotlib使用PyQt4而不是PySide防止初始化出错 # 导入PyQt4的包是为了保证matplotlib使用PyQt4而不是PySide防止初始化出错
from vnpy.trader.ctaStrategy.ctaBacktesting import * from vnpy.trader.app.ctaStrategy.ctaBacktesting import *
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
# 创建回测引擎 # 创建回测引擎

View File

@ -8,7 +8,7 @@ from datetime import time
from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtObject import VtBarData
from vnpy.trader.vtConstant import EMPTY_STRING from vnpy.trader.vtConstant import EMPTY_STRING
from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate from vnpy.trader.app.ctaStrategy.ctaTemplate import CtaTemplate
######################################################################## ########################################################################

View File

@ -15,7 +15,7 @@ from __future__ import division
from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtObject import VtBarData
from vnpy.trader.vtConstant import EMPTY_STRING, EMPTY_FLOAT from vnpy.trader.vtConstant import EMPTY_STRING, EMPTY_FLOAT
from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate from vnpy.trader.app.ctaStrategy.ctaTemplate import CtaTemplate
######################################################################## ########################################################################

View File

@ -17,7 +17,7 @@ import numpy as np
from vnpy.trader.vtObject import VtBarData from vnpy.trader.vtObject import VtBarData
from vnpy.trader.vtConstant import EMPTY_STRING from vnpy.trader.vtConstant import EMPTY_STRING
from vnpy.trader.ctaStrategy.ctaTemplate import CtaTemplate from vnpy.trader.app.ctaStrategy.ctaTemplate import CtaTemplate
######################################################################## ########################################################################

View File

@ -9,7 +9,7 @@ from vnpy.event import Event
from vnpy.trader.vtEvent import * from vnpy.trader.vtEvent import *
from vnpy.trader.uiBasicWidget import QtGui, QtCore, QtWidgets, BasicCell from vnpy.trader.uiBasicWidget import QtGui, QtCore, QtWidgets, BasicCell
from vnpy.trader.ctaStrategy.language import text from vnpy.trader.app.ctaStrategy.language import text
######################################################################## ########################################################################

View File

@ -0,0 +1,9 @@
# encoding: UTF-8
from drEngine import DrEngine
from uiDrWidget import DrEngineManager
appName = 'DataRecorder'
appDisplayName = u'行情记录'
appEngine = DrEngine
appWidget = DrEngineManager

View File

@ -19,8 +19,8 @@ from vnpy.trader.vtEvent import *
from vnpy.trader.vtFunction import todayDate from vnpy.trader.vtFunction import todayDate
from vnpy.trader.vtObject import VtSubscribeReq, VtLogData, VtBarData, VtTickData from vnpy.trader.vtObject import VtSubscribeReq, VtLogData, VtBarData, VtTickData
from vnpy.trader.dataRecorder.drBase import * from vnpy.trader.app.dataRecorder.drBase import *
from vnpy.trader.dataRecorder.language import text from vnpy.trader.app.dataRecorder.language import text
######################################################################## ########################################################################

View File

@ -0,0 +1,13 @@
# encoding: UTF-8
import json
import os
import traceback
# 默认设置
from chinese import text
# 是否要使用英文
from vnpy.trader.vtGlobal import globalSetting
if globalSetting['language'] == 'english':
from english import text

View File

@ -11,7 +11,7 @@ from qtpy import QtWidgets, QtGui, QtCore
from vnpy.event import Event from vnpy.event import Event
from vnpy.trader.vtEvent import * from vnpy.trader.vtEvent import *
from vnpy.trader.dataRecorder.language import text from vnpy.trader.app.dataRecorder.language import text
######################################################################## ########################################################################

View File

@ -0,0 +1,9 @@
# encoding: UTF-8
from rmEngine import RmEngine
from uiRmWidget import RmEngineManager
appName = 'RiskManager'
appDisplayName = u'风险管理'
appEngine = RmEngine
appWidget = RmEngineManager

View File

@ -0,0 +1,13 @@
# encoding: UTF-8
import json
import os
import traceback
# 默认设置
from chinese import text
# 是否要使用英文
from vnpy.trader.vtGlobal import globalSetting
if globalSetting['language'] == 'english':
from english import text

View File

@ -31,6 +31,9 @@ class RmEngine(object):
"""Constructor""" """Constructor"""
self.mainEngine = mainEngine self.mainEngine = mainEngine
self.eventEngine = eventEngine self.eventEngine = eventEngine
# 绑定自身到主引擎的风控引擎引用上
mainEngine.rmEngine = self
# 是否启动风控 # 是否启动风控
self.active = False self.active = False

View File

@ -8,7 +8,7 @@
from vnpy.event import Event from vnpy.event import Event
from vnpy.trader.uiBasicWidget import QtGui, QtWidgets, QtCore from vnpy.trader.uiBasicWidget import QtGui, QtWidgets, QtCore
from vnpy.trader.riskManager.language import text from vnpy.trader.app.riskManager.language import text
######################################################################## ########################################################################

View File

@ -1,23 +0,0 @@
# encoding: UTF-8
import json
import os
import traceback
# 默认设置
from chinese import text
# 获取目录上级路径
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
SETTING_FILENAME = 'VT_setting.json'
SETTING_FILENAME = os.path.join(path, SETTING_FILENAME)
# 打开配置文件,读取语言配置
try:
f = file(SETTING_FILENAME)
setting = json.load(f)
if setting['language'] == 'english':
from english import text
f.close()
except:
traceback.print_exc()

View File

@ -1,23 +0,0 @@
# encoding: UTF-8
import json
import os
import traceback
# 默认设置
from chinese import text
# 获取目录上级路径
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
SETTING_FILENAME = 'VT_setting.json'
SETTING_FILENAME = os.path.join(path, SETTING_FILENAME)
# 打开配置文件,读取语言配置
try:
f = file(SETTING_FILENAME)
setting = json.load(f)
if setting['language'] == 'english':
from english import text
f.close()
except:
traceback.print_exc()

View File

@ -1,33 +1 @@
# encoding: UTF-8 # encoding: UTF-8
'''
动态载入所有的Gateway
'''
import os
import importlib
import traceback
# 用来保存Gateway类的字典
GATEWAY_DICT = {}
# 获取目录路径
path = os.path.abspath(os.path.dirname(__file__))
# 遍历strategy目录下的文件
for root, subdirs, files in os.walk(path):
if path != root:
continue
for foldername in subdirs:
# 接口目录名中必须含有Gateway
if 'Gateway' in foldername:
# 模块名称需要上前缀
moduleName = 'vnpy.trader.gateway.' + foldername
try:
# 使用importlib动态载入模块并保存到字典中
module = importlib.import_module(moduleName)
GATEWAY_DICT[module.gatewayName] = module
except:
traceback.print_exc()

View File

@ -1,8 +1,9 @@
# encoding: UTF-8 # encoding: UTF-8
from vnpy.trader import vtConstant from vnpy.trader import vtConstant
from cshshlpGateway import CshshlpGateway as gateway from cshshlpGateway import CshshlpGateway
gatewayClass = CshshlpGateway
gatewayName = 'CSHSHLP' gatewayName = 'CSHSHLP'
gatewayDisplayName = u'中信期权' gatewayDisplayName = u'中信期权'
gatewayType = vtConstant.GATEWAYTYPE_EQUITY gatewayType = vtConstant.GATEWAYTYPE_EQUITY

View File

@ -1,10 +1,10 @@
# encoding: UTF-8 # encoding: UTF-8
from vnpy.trader import vtConstant from vnpy.trader import vtConstant
from ctpGateway import CtpGateway as gateway from ctpGateway import CtpGateway
gatewayClass = CtpGateway
gatewayName = 'CTP' gatewayName = 'CTP'
gatewayDisplayName = gatewayName gatewayDisplayName = 'CTP'
gatewayType = vtConstant.GATEWAYTYPE_FUTURES gatewayType = vtConstant.GATEWAYTYPE_FUTURES
gatewayQryEnabled = True gatewayQryEnabled = True

View File

@ -16,6 +16,7 @@ from datetime import datetime
from vnpy.api.ctp import MdApi, TdApi, defineDict from vnpy.api.ctp import MdApi, TdApi, defineDict
from vnpy.trader.vtGateway import * from vnpy.trader.vtGateway import *
from vnpy.trader.gateway.ctpGateway.language import text from vnpy.trader.gateway.ctpGateway.language import text
from vnpy.trader.vtConstant import GATEWAYTYPE_FUTURES
# 以下为一些VT类型和CTP类型的映射字典 # 以下为一些VT类型和CTP类型的映射字典
@ -88,7 +89,7 @@ class CtpGateway(VtGateway):
self.mdConnected = False # 行情API连接状态登录完成后为True self.mdConnected = False # 行情API连接状态登录完成后为True
self.tdConnected = False # 交易API连接状态 self.tdConnected = False # 交易API连接状态
self.qryEnabled = False # 是否要启动循环查询 self.qryEnabled = False # 循环查询
self.requireAuthentication = False self.requireAuthentication = False

View File

@ -1,8 +1,9 @@
# encoding: UTF-8 # encoding: UTF-8
from vnpy.trader import vtConstant from vnpy.trader import vtConstant
from femasGateway import FemasGateway as gateway from femasGateway import FemasGateway
gatewayClass = FemasGateway
gatewayName = 'FEMAS' gatewayName = 'FEMAS'
gatewayDisplayName = u'飞马' gatewayDisplayName = u'飞马'
gatewayType = vtConstant.GATEWAYTYPE_FUTURES gatewayType = vtConstant.GATEWAYTYPE_FUTURES

View File

@ -1,8 +1,9 @@
# encoding: UTF-8 # encoding: UTF-8
from vnpy.trader import vtConstant from vnpy.trader import vtConstant
from huobiGateway import HuobiGateway as gateway from huobiGateway import HuobiGateway
gatewayClass = HuobiGateway
gatewayName = 'HUOBI' gatewayName = 'HUOBI'
gatewayDisplayName = u'火币' gatewayDisplayName = u'火币'
gatewayType = vtConstant.GATEWAYTYPE_BTC gatewayType = vtConstant.GATEWAYTYPE_BTC

View File

@ -1,8 +1,9 @@
# encoding: UTF-8 # encoding: UTF-8
from vnpy.trader import vtConstant from vnpy.trader import vtConstant
from ibGateway import IbGateway as gateway from ibGateway import IbGateway
gatewayClass = IbGateway
gatewayName = 'IB' gatewayName = 'IB'
gatewayDisplayName = gatewayName gatewayDisplayName = gatewayName
gatewayType = vtConstant.GATEWAYTYPE_INTERNATIONAL gatewayType = vtConstant.GATEWAYTYPE_INTERNATIONAL

View File

@ -6,17 +6,7 @@ import os
# 默认设置 # 默认设置
from chinese import text, constant from chinese import text, constant
# 获取目录上级路径 # 是否要使用英文
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) from vnpy.trader.vtGlobal import globalSetting
SETTING_FILENAME = 'VT_setting.json' if globalSetting['language'] == 'english':
SETTING_FILENAME = os.path.join(path, SETTING_FILENAME) from english import text, constant
# 打开配置文件,读取语言配置
try:
f = file(SETTING_FILENAME)
setting = json.load(f)
if setting['language'] == 'english':
from english import text, constant
f.close()
except:
pass

View File

@ -117,7 +117,7 @@ STRATEGY = u'策略'
CTA_STRATEGY = u'CTA策略' CTA_STRATEGY = u'CTA策略'
HELP = u'帮助' HELP = u'帮助'
RESTORE = u'还原' RESTORE = u'还原窗口'
ABOUT = u'关于' ABOUT = u'关于'
TEST = u'测试' TEST = u'测试'
CONNECT = u'连接' CONNECT = u'连接'

View File

@ -117,7 +117,7 @@ STRATEGY = u'Strategy'
CTA_STRATEGY = u'CTA Strategy' CTA_STRATEGY = u'CTA Strategy'
HELP = u'Help' HELP = u'Help'
RESTORE = u'Restore' RESTORE = u'Restore Window'
ABOUT = u'About' ABOUT = u'About'
TEST = u'Test' TEST = u'Test'
CONNECT = u'Connect ' CONNECT = u'Connect '

View File

@ -1,23 +0,0 @@
# encoding: UTF-8
import json
import os
import traceback
# 默认设置
from chinese import text
# 获取目录上级路径
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
SETTING_FILENAME = 'VT_setting.json'
SETTING_FILENAME = os.path.join(path, SETTING_FILENAME)
# 打开配置文件,读取语言配置
try:
f = file(SETTING_FILENAME)
setting = json.load(f)
if setting['language'] == 'english':
from english import text
f.close()
except:
traceback.print_exc()

View File

@ -3,6 +3,7 @@
import json import json
import csv import csv
import os import os
import platform
from collections import OrderedDict from collections import OrderedDict
from qtpy import QtWidgets, QtGui, QtCore from qtpy import QtWidgets, QtGui, QtCore
@ -18,6 +19,12 @@ COLOR_RED = QtGui.QColor('red')
COLOR_GREEN = QtGui.QColor('green') COLOR_GREEN = QtGui.QColor('green')
# 设置Windows底部任务栏图标
if 'Windows' in platform.uname():
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.trader')
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def loadFont(): def loadFont():
"""载入字体设置""" """载入字体设置"""
@ -723,7 +730,9 @@ class TradingWidget(QtWidgets.QFrame):
self.symbol = '' self.symbol = ''
# 添加交易接口 # 添加交易接口
self.gatewayList.extend(mainEngine.getAllGatewayNames()) l = mainEngine.getAllGatewayDetails()
gatewayNameList = [d['gatewayName'] for d in l]
self.gatewayList.extend(gatewayNameList)
self.initUi() self.initUi()
self.connectSignal() self.connectSignal()

View File

@ -2,28 +2,41 @@
import psutil import psutil
from vnpy.trader.gateway import GATEWAY_DICT from vnpy.trader.vtGlobal import globalSetting
from vnpy.trader.uiBasicWidget import * from vnpy.trader.uiBasicWidget import *
from vnpy.trader.ctaStrategy.uiCtaWidget import CtaEngineManager #from vnpy.trader.ctaStrategy.uiCtaWidget import CtaEngineManager
from vnpy.trader.dataRecorder.uiDrWidget import DrEngineManager #from vnpy.trader.dataRecorder.uiDrWidget import DrEngineManager
from vnpy.trader.riskManager.uiRmWidget import RmEngineManager #from vnpy.trader.riskManager.uiRmWidget import RmEngineManager
######################################################################## ########################################################################
class MainWindow(QtWidgets.QMainWindow): class MainWindow(QtWidgets.QMainWindow):
"""主窗口""" """主窗口"""
signalStatusBar = QtCore.Signal(type(Event())) signalStatusBar = QtCore.Signal(type(Event()))
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self, mainEngine, eventEngine): def __init__(self, mainEngine, eventEngine):
"""Constructor""" """Constructor"""
# 实例化主窗口对象时设置Qt的皮肤
if globalSetting['darkStyle']:
import qdarkstyle
app = QtWidgets.QApplication.instance()
app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False))
super(MainWindow, self).__init__() super(MainWindow, self).__init__()
self.mainEngine = mainEngine self.mainEngine = mainEngine
self.eventEngine = eventEngine self.eventEngine = eventEngine
l = self.mainEngine.getAllGatewayDetails()
self.gatewayNameList = [d['gatewayName'] for d in l]
self.widgetDict = {} # 用来保存子窗口的字典 self.widgetDict = {} # 用来保存子窗口的字典
# 获取主引擎中的上层应用信息
self.appDetailList = self.mainEngine.getAllAppDetails()
self.initUi() self.initUi()
self.loadWindowSettings('custom') self.loadWindowSettings('custom')
@ -67,36 +80,33 @@ class MainWindow(QtWidgets.QMainWindow):
menubar = self.menuBar() menubar = self.menuBar()
# 设计为只显示存在的接口 # 设计为只显示存在的接口
gatewayDetails = self.mainEngine.getAllGatewayDetails()
sysMenu = menubar.addMenu(vtText.SYSTEM) sysMenu = menubar.addMenu(vtText.SYSTEM)
for gatewayModule in GATEWAY_DICT.values():
if gatewayModule.gatewayType == GATEWAYTYPE_FUTURES:
self.addConnectAction(sysMenu, gatewayModule.gatewayName,
gatewayModule.gatewayDisplayName)
for d in gatewayDetails:
if d['gatewayType'] == GATEWAYTYPE_FUTURES:
self.addConnectAction(sysMenu, d['gatewayName'], d['gatewayDisplayName'])
sysMenu.addSeparator() sysMenu.addSeparator()
for gatewayModule in GATEWAY_DICT.values():
if gatewayModule.gatewayType == GATEWAYTYPE_EQUITY:
self.addConnectAction(sysMenu, gatewayModule.gatewayName,
gatewayModule.gatewayDisplayName)
sysMenu.addSeparator()
for gatewayModule in GATEWAY_DICT.values():
if gatewayModule.gatewayType == GATEWAYTYPE_INTERNATIONAL:
self.addConnectAction(sysMenu, gatewayModule.gatewayName,
gatewayModule.gatewayDisplayName)
for d in gatewayDetails:
if d['gatewayType'] == GATEWAYTYPE_EQUITY:
self.addConnectAction(sysMenu, d['gatewayName'], d['gatewayDisplayName'])
sysMenu.addSeparator() sysMenu.addSeparator()
for gatewayModule in GATEWAY_DICT.values():
if gatewayModule.gatewayType == GATEWAYTYPE_BTC: for d in gatewayDetails:
self.addConnectAction(sysMenu, gatewayModule.gatewayName, if d['gatewayType'] == GATEWAYTYPE_INTERNATIONAL:
gatewayModule.gatewayDisplayName) self.addConnectAction(sysMenu, d['gatewayName'], d['gatewayDisplayName'])
sysMenu.addSeparator() sysMenu.addSeparator()
for gatewayModule in GATEWAY_DICT.values():
if gatewayModule.gatewayType == GATEWAYTYPE_DATA: for d in gatewayDetails:
self.addConnectAction(sysMenu, gatewayModule.gatewayName, if d['gatewayType'] == GATEWAYTYPE_BTC:
gatewayModule.gatewayDisplayName) self.addConnectAction(sysMenu, d['gatewayName'], d['gatewayDisplayName'])
sysMenu.addSeparator()
for d in gatewayDetails:
if d['gatewayType'] == GATEWAYTYPE_DATA:
self.addConnectAction(sysMenu, d['gatewayName'], d['gatewayDisplayName'])
sysMenu.addSeparator() sysMenu.addSeparator()
sysMenu.addAction(self.createAction(vtText.CONNECT_DATABASE, self.mainEngine.dbConnect)) sysMenu.addAction(self.createAction(vtText.CONNECT_DATABASE, self.mainEngine.dbConnect))
@ -104,19 +114,28 @@ class MainWindow(QtWidgets.QMainWindow):
sysMenu.addAction(self.createAction(vtText.EXIT, self.close)) sysMenu.addAction(self.createAction(vtText.EXIT, self.close))
# 功能应用 # 功能应用
functionMenu = menubar.addMenu(vtText.APPLICATION) #functionMenu = menubar.addMenu(vtText.APPLICATION)
functionMenu.addAction(self.createAction(vtText.CONTRACT_SEARCH, self.openContract)) #functionMenu.addAction(self.createAction(vtText.DATA_RECORDER, self.openDr))
functionMenu.addAction(self.createAction(vtText.DATA_RECORDER, self.openDr)) #functionMenu.addAction(self.createAction(vtText.RISK_MANAGER, self.openRm))
functionMenu.addAction(self.createAction(vtText.RISK_MANAGER, self.openRm))
appMenu = menubar.addMenu(vtText.APPLICATION)
for appDetail in self.appDetailList:
function = self.createOpenAppFunction(appDetail)
action = self.createAction(appDetail['appDisplayName'], function)
appMenu.addAction(action)
# 算法相关 # 算法相关
strategyMenu = menubar.addMenu(vtText.STRATEGY) #strategyMenu = menubar.addMenu(vtText.STRATEGY)
strategyMenu.addAction(self.createAction(vtText.CTA_STRATEGY, self.openCta)) #strategyMenu.addAction(self.createAction(vtText.CTA_STRATEGY, self.openCta))
# 帮助 # 帮助
helpMenu = menubar.addMenu(vtText.HELP) helpMenu = menubar.addMenu(vtText.HELP)
helpMenu.addAction(self.createAction(vtText.CONTRACT_SEARCH, self.openContract))
helpMenu.addSeparator()
helpMenu.addAction(self.createAction(vtText.RESTORE, self.restoreWindow)) helpMenu.addAction(self.createAction(vtText.RESTORE, self.restoreWindow))
helpMenu.addAction(self.createAction(vtText.ABOUT, self.openAbout)) helpMenu.addAction(self.createAction(vtText.ABOUT, self.openAbout))
helpMenu.addSeparator()
helpMenu.addAction(self.createAction(vtText.TEST, self.test)) helpMenu.addAction(self.createAction(vtText.TEST, self.test))
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@ -152,12 +171,12 @@ class MainWindow(QtWidgets.QMainWindow):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def addConnectAction(self, menu, gatewayName, displayName=''): def addConnectAction(self, menu, gatewayName, displayName=''):
"""增加连接功能""" """增加连接功能"""
if gatewayName not in self.mainEngine.getAllGatewayNames(): if gatewayName not in self.gatewayNameList:
return return
def connect(): def connect():
self.mainEngine.connect(gatewayName) self.mainEngine.connect(gatewayName)
if not displayName: if not displayName:
displayName = gatewayName displayName = gatewayName
@ -171,6 +190,20 @@ class MainWindow(QtWidgets.QMainWindow):
action = QtWidgets.QAction(actionName, self) action = QtWidgets.QAction(actionName, self)
action.triggered.connect(function) action.triggered.connect(function)
return action return action
#----------------------------------------------------------------------
def createOpenAppFunction(self, appDetail):
"""创建打开应用UI的函数"""
def openAppFunction():
appName = appDetail['appName']
try:
self.widgetDict[appName].show()
except KeyError:
appEngine = self.mainEngine.appDict[appName]
self.widgetDict[appName] = appDetail['appWidget'](appEngine, self.eventEngine)
self.widgetDict[appName].show()
return openAppFunction
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def test(self): def test(self):

View File

@ -11,9 +11,9 @@ from uiMainWindow import *
from vnpy.event import EventEngine from vnpy.event import EventEngine
from vnpy.rpc import RpcClient from vnpy.rpc import RpcClient
from vnpy.trader.ctaStrategy.ctaEngine import CtaEngine from vnpy.trader.app.ctaStrategy.ctaEngine import CtaEngine
from vnpy.trader.dataRecorder.drEngine import DrEngine from vnpy.trader.app.dataRecorder.drEngine import DrEngine
from vnpy.trader.riskManager.rmEngine import RmEngine from vnpy.trader.app.riskManager.rmEngine import RmEngine

View File

@ -13,10 +13,10 @@ from vnpy.trader.vtEvent import *
from vnpy.trader.vtGateway import * from vnpy.trader.vtGateway import *
from vnpy.trader.language import text from vnpy.trader.language import text
from vnpy.trader.gateway import GATEWAY_DICT #from vnpy.trader.gateway import GATEWAY_DICT
from vnpy.trader.ctaStrategy.ctaEngine import CtaEngine #from vnpy.trader.ctaStrategy.ctaEngine import CtaEngine
from vnpy.trader.dataRecorder.drEngine import DrEngine #from vnpy.trader.dataRecorder.drEngine import DrEngine
from vnpy.trader.riskManager.rmEngine import RmEngine from vnpy.trader.app import riskManager
######################################################################## ########################################################################
@ -24,13 +24,13 @@ class MainEngine(object):
"""主引擎""" """主引擎"""
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self): def __init__(self, eventEngine):
"""Constructor""" """Constructor"""
# 记录今日日期 # 记录今日日期
self.todayDate = datetime.now().strftime('%Y%m%d') self.todayDate = datetime.now().strftime('%Y%m%d')
# 创建事件引擎 # 绑定事件引擎
self.eventEngine = EventEngine2() self.eventEngine = eventEngine
self.eventEngine.start() self.eventEngine.start()
# 创建数据引擎 # 创建数据引擎
@ -40,12 +40,18 @@ class MainEngine(object):
self.dbClient = None # MongoDB客户端对象 self.dbClient = None # MongoDB客户端对象
# 调用一个个初始化函数 # 调用一个个初始化函数
self.initGateway() #self.initGateway()
# 扩展模块 # 接口实例
self.ctaEngine = CtaEngine(self, self.eventEngine) self.gatewayDict = OrderedDict()
self.drEngine = DrEngine(self, self.eventEngine) self.gatewayDetailList = []
self.rmEngine = RmEngine(self, self.eventEngine)
# 应用模块实例
self.appDict = OrderedDict()
self.appDetailList = []
# 风控引擎实例(特殊独立对象)
self.rmEngine = None
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def initGateway(self): def initGateway(self):
@ -63,9 +69,41 @@ class MainEngine(object):
print e print e
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def addGateway(self, gatewayClass, gatewayName=None): def addGateway(self, gatewayModule):
"""创建接口""" """添加底层接口"""
self.gatewayDict[gatewayName] = gatewayClass(self.eventEngine, gatewayName) gatewayName = gatewayModule.gatewayName
# 创建接口实例
self.gatewayDict[gatewayName] = gatewayModule.gatewayClass(self.eventEngine,
gatewayName)
# 设置接口轮询
if gatewayModule.gatewayQryEnabled:
self.gatewayDict[gatewayName].setQryEnabled(gatewayModule.gatewayQryEnabled)
# 保存接口详细信息
d = {
'gatewayName': gatewayModule.gatewayName,
'gatewayDisplayName': gatewayModule.gatewayDisplayName,
'gatewayType': gatewayModule.gatewayType
}
self.gatewayDetailList.append(d)
#----------------------------------------------------------------------
def addApp(self, appModule):
"""添加上层应用"""
appName = appModule.appName
# 创建应用实例
self.appDict[appName] = appModule.appEngine(self, self.eventEngine)
# 保存应用信息
d = {
'appName': appModule.appName,
'appDisplayName': appModule.appDisplayName,
'appWidget': appModule.appWidget
}
self.appDetailList.append(d)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def getGateway(self, gatewayName): def getGateway(self, gatewayName):
@ -98,8 +136,8 @@ class MainEngine(object):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def sendOrder(self, orderReq, gatewayName): def sendOrder(self, orderReq, gatewayName):
"""对特定接口发单""" """对特定接口发单"""
# 如果风控检查失败则不发单 # 如果创建了风控引擎,且风控检查失败则不发单
if not self.rmEngine.checkRisk(orderReq): if self.rmEngine and not self.rmEngine.checkRisk(orderReq):
return '' return ''
gateway = self.getGateway(gatewayName) gateway = self.getGateway(gatewayName)
@ -246,10 +284,14 @@ class MainEngine(object):
return self.dataEngine.getAllWorkingOrders() return self.dataEngine.getAllWorkingOrders()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def getAllGatewayNames(self): def getAllGatewayDetails(self):
"""查询引擎中所有可用接口的名称""" """查询引擎中所有底层接口的信息"""
return self.gatewayDict.keys() return self.gatewayDetailList
#----------------------------------------------------------------------
def getAllAppDetails(self):
"""查询引擎中所有上层应用的信息"""
return self.appDetailList
######################################################################## ########################################################################

View File

@ -33,4 +33,23 @@ def todayDate():
"""获取当前本机电脑时间的日期""" """获取当前本机电脑时间的日期"""
return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
#----------------------------------------------------------------------
def loadIconPath():
"""加载程序图标路径"""
iconName = 'vnpy.ico'
# 首先尝试从当前目录加载图标
path = os.getcwd()
iconPath = os.path.join(path, iconName)
if os.path.isfile(iconPath):
return iconPath
# 如果失败则从vt根目录加载图标
else:
path = os.path.abspath(os.path.dirname(__file__))
iconPath = os.path.join(path, iconName)
return iconPath

View File

@ -20,6 +20,5 @@ try:
f = file(settingFileName) f = file(settingFileName)
globalSetting = json.load(f) globalSetting = json.load(f)
except: except:
print u'加载'
traceback.print_exc() traceback.print_exc()

View File

@ -1,50 +1,66 @@
# encoding: UTF-8 # encoding: UTF-8
# 重载sys模块设置默认字符串编码方式为utf8
import sys import sys
reload(sys)
sys.setdefaultencoding('utf8')
# Python内置模块
import os import os
import platform import platform
# Python三方模块
from qtpy import QtWidgets, QtGui from qtpy import QtWidgets, QtGui
# vn.trader模块
from vnpy.event import EventEngine2
from vnpy.trader.vtGlobal import globalSetting from vnpy.trader.vtGlobal import globalSetting
from vnpy.trader.vtEngine import MainEngine from vnpy.trader.vtEngine import MainEngine
from vnpy.trader.uiMainWindow import MainWindow, BASIC_FONT from vnpy.trader.uiMainWindow import MainWindow, BASIC_FONT
from vnpy.trader.vtFunction import loadIconPath
# 文件路径名 # 加载底层接口
path = os.path.abspath(os.path.dirname(__file__)) from vnpy.trader.gateway import (ctpGateway,
ICON_FILENAME = 'vnpy.ico' ibGateway,
ICON_FILENAME = os.path.join(path, ICON_FILENAME) huobiGateway)
# 加载上层应用
from vnpy.trader.app import (riskManager, dataRecorder,
ctaStrategy)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def main(): def main():
"""主程序入口""" """主程序入口"""
# 重载sys模块设置默认字符串编码方式为utf8 # 创建事件引擎
reload(sys) ee = EventEngine2()
sys.setdefaultencoding('utf8')
# 设置Windows底部任务栏图标 # 创建主引擎
if 'Windows' in platform.uname(): me = MainEngine(ee)
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('vn.trader') # 添加交易接口
me.addGateway(ctpGateway)
me.addGateway(ibGateway)
me.addGateway(huobiGateway)
# 添加上层应用
me.addApp(riskManager)
me.addApp(dataRecorder)
me.addApp(ctaStrategy)
# 初始化Qt应用对象 # 初始化Qt应用对象
app = QtWidgets.QApplication(sys.argv) app = QtWidgets.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon(ICON_FILENAME)) app.setWindowIcon(QtGui.QIcon(loadIconPath()))
app.setFont(BASIC_FONT) app.setFont(BASIC_FONT)
# 设置Qt的皮肤
if globalSetting['darkStyle']:
import qdarkstyle
app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False))
# 初始化主引擎和主窗口对象 # 初始化主引擎和主窗口对象
mainEngine = MainEngine() mw = MainWindow(me, ee)
mainWindow = MainWindow(mainEngine, mainEngine.eventEngine) mw.showMaximized()
mainWindow.showMaximized()
# 在主线程中启动Qt事件循环 # 在主线程中启动Qt事件循环
sys.exit(app.exec_()) sys.exit(app.exec_())
if __name__ == '__main__': if __name__ == '__main__':
main() main()