[Mod]调整算法交易组件目录结构,增加自动加载运行时目录下算法的功能
This commit is contained in:
parent
1a75ef084a
commit
ca4a0cdafb
59
vnpy/trader/app/algoTrading/algo/__init__.py
Normal file
59
vnpy/trader/app/algoTrading/algo/__init__.py
Normal file
@ -0,0 +1,59 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
'''
|
||||
动态载入所有的策略类
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import importlib
|
||||
import traceback
|
||||
|
||||
|
||||
# 用来保存算法类和控件类的字典
|
||||
ALGO_DICT = {}
|
||||
WIDGET_DICT = {}
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def loadAlgoModule(path, prefix):
|
||||
"""使用importlib动态载入算法"""
|
||||
for root, subdirs, files in os.walk(path):
|
||||
for name in files:
|
||||
# 只有文件名以Algo.py结尾的才是算法文件
|
||||
if len(name)>7 and name[-7:] == 'Algo.py':
|
||||
try:
|
||||
# 模块名称需要模块路径前缀
|
||||
moduleName = prefix + name.replace('.py', '')
|
||||
module = importlib.import_module(moduleName)
|
||||
|
||||
# 获取算法类和控件类
|
||||
algo = None
|
||||
widget = None
|
||||
|
||||
for k in dir(module):
|
||||
# 以Algo结尾的类,是算法
|
||||
if k[-4:] == 'Algo':
|
||||
algo = module.__getattribute__(k)
|
||||
|
||||
# 以Widget结尾的类,是控件
|
||||
if k[-6:] == 'Widget':
|
||||
widget = module.__getattribute__(k)
|
||||
|
||||
# 保存到字典中
|
||||
if algo and widget:
|
||||
ALGO_DICT[algo.templateName] = algo
|
||||
WIDGET_DICT[algo.templateName] = widget
|
||||
except:
|
||||
print ('-' * 20)
|
||||
print ('Failed to import strategy file %s:' %moduleName)
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
# 遍历algo目录下的文件
|
||||
path1 = os.path.abspath(os.path.dirname(__file__))
|
||||
loadAlgoModule(path1, 'vnpy.trader.app.algoTrading.algo.')
|
||||
|
||||
# 遍历工作目录下的文件
|
||||
path2 = os.getcwd()
|
||||
loadAlgoModule(path2, '')
|
@ -8,9 +8,8 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
|
||||
PRICETYPE_LIMITPRICE, PRICETYPE_MARKETPRICE,
|
||||
STATUS_REJECTED, STATUS_CANCELLED, STATUS_ALLTRADED)
|
||||
from vnpy.trader.uiQt import QtWidgets
|
||||
|
||||
from .algoTemplate import AlgoTemplate
|
||||
from .uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate
|
||||
from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
|
||||
from six import text_type
|
||||
|
@ -6,9 +6,8 @@ from collections import OrderedDict
|
||||
from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
|
||||
OFFSET_OPEN, OFFSET_CLOSE)
|
||||
from vnpy.trader.uiQt import QtWidgets
|
||||
|
||||
from .algoTemplate import AlgoTemplate
|
||||
from .uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate
|
||||
from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
|
||||
|
||||
|
@ -9,9 +9,8 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
|
||||
OFFSET_OPEN, OFFSET_CLOSE,
|
||||
STATUS_ALLTRADED, STATUS_CANCELLED, STATUS_REJECTED)
|
||||
from vnpy.trader.uiQt import QtWidgets
|
||||
|
||||
from .algoTemplate import AlgoTemplate
|
||||
from .uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate
|
||||
from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
|
||||
|
||||
|
@ -7,9 +7,8 @@ from six import text_type
|
||||
|
||||
from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT)
|
||||
from vnpy.trader.uiQt import QtWidgets
|
||||
|
||||
from .algoTemplate import AlgoTemplate
|
||||
from .uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
from vnpy.trader.app.algoTrading.algoTemplate import AlgoTemplate
|
||||
from vnpy.trader.app.algoTrading.uiAlgoWidget import AlgoWidget, QtWidgets
|
||||
|
||||
|
||||
########################################################################
|
@ -15,13 +15,7 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
|
||||
OFFSET_CLOSETODAY, OFFSET_CLOSEYESTERDAY)
|
||||
from vnpy.trader.vtObject import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData
|
||||
|
||||
|
||||
from .twapAlgo import TwapAlgo
|
||||
from .dmaAlgo import DmaAlgo
|
||||
from .stopAlgo import StopAlgo
|
||||
from .stAlgo import StAlgo
|
||||
from .mmAlgo import MmAlgo
|
||||
|
||||
from .algo import ALGO_DICT
|
||||
|
||||
|
||||
EVENT_ALGO_LOG = 'eAlgoLog' # 算法日志事件
|
||||
@ -328,32 +322,3 @@ class AlgoEngine(object):
|
||||
event = Event(EVENT_ALGO_SETTING)
|
||||
event.dict_['data'] = algoSetting
|
||||
self.eventEngine.put(event)
|
||||
|
||||
|
||||
# 加载目录下的算法模板
|
||||
ALGO_DICT = {}
|
||||
WIDGET_DICT = {}
|
||||
|
||||
path = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
for root, subdirs, files in os.walk(path):
|
||||
for name in files:
|
||||
# 只有文件名以Algo.py结尾的才是算法文件
|
||||
if len(name)>7 and name[-7:] == 'Algo.py':
|
||||
# 模块名称需要模块路径前缀
|
||||
moduleName = 'vnpy.trader.app.algoTrading.' + name.replace('.py', '')
|
||||
module = importlib.import_module(moduleName)
|
||||
|
||||
# 获取算法类和控件类
|
||||
for k in dir(module):
|
||||
# 以Algo结尾的类,是算法
|
||||
if k[-4:] == 'Algo':
|
||||
algo = module.__getattribute__(k)
|
||||
|
||||
# 以Widget结尾的类,是控件
|
||||
if k[-6:] == 'Widget':
|
||||
widget = module.__getattribute__(k)
|
||||
|
||||
# 保存到字典中
|
||||
ALGO_DICT[algo.templateName] = algo
|
||||
WIDGET_DICT[algo.templateName] = widget
|
||||
|
@ -10,8 +10,9 @@ from vnpy.event import Event
|
||||
from vnpy.trader.uiQt import QtCore, QtWidgets
|
||||
|
||||
from .algoEngine import (EVENT_ALGO_LOG, EVENT_ALGO_PARAM,
|
||||
EVENT_ALGO_VAR, EVENT_ALGO_SETTING,
|
||||
WIDGET_DICT)
|
||||
EVENT_ALGO_VAR, EVENT_ALGO_SETTING)
|
||||
from .algo import WIDGET_DICT
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
|
Loading…
Reference in New Issue
Block a user