2017-05-03 10:03:17 +00:00
|
|
|
|
# encoding: UTF-8
|
2017-04-28 14:10:07 +00:00
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
动态载入所有的策略类
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import importlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 用来保存策略类的字典
|
|
|
|
|
STRATEGY_CLASS = {}
|
|
|
|
|
|
|
|
|
|
# 获取目录路径
|
|
|
|
|
path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
2017-05-03 10:03:17 +00:00
|
|
|
|
print 'init {0}'.format(path)
|
|
|
|
|
|
2017-04-28 14:10:07 +00:00
|
|
|
|
# 遍历strategy目录下的文件
|
|
|
|
|
for root, subdirs, files in os.walk(path):
|
|
|
|
|
for name in files:
|
|
|
|
|
# 只有文件名中包含strategy且非.pyc的文件,才是策略文件
|
|
|
|
|
if 'strategy' in name and '.pyc' not in name:
|
|
|
|
|
# 模块名称需要上前缀
|
|
|
|
|
moduleName = 'ctaStrategy.strategy.' + name.replace('.py', '')
|
|
|
|
|
|
2017-05-03 10:03:17 +00:00
|
|
|
|
print 'loading {0}'.format(moduleName)
|
|
|
|
|
try:
|
|
|
|
|
# 使用importlib动态载入模块
|
|
|
|
|
module = importlib.import_module(moduleName)
|
2017-04-28 14:10:07 +00:00
|
|
|
|
|
2017-05-03 10:03:17 +00:00
|
|
|
|
except:
|
|
|
|
|
print 'load fail'
|
|
|
|
|
continue
|
|
|
|
|
|
2017-04-28 14:10:07 +00:00
|
|
|
|
# 遍历模块下的对象,只有名称中包含'Strategy'的才是策略类
|
|
|
|
|
for k in dir(module):
|
|
|
|
|
if 'Strategy' in k:
|
2017-05-03 10:03:17 +00:00
|
|
|
|
print 'adding {0} into STRATEGY_CLASS'.format(k)
|
2017-04-28 14:10:07 +00:00
|
|
|
|
v = module.__getattribute__(k)
|
|
|
|
|
STRATEGY_CLASS[k] = v
|
2017-05-03 10:03:17 +00:00
|
|
|
|
|
|
|
|
|
print 'finished load modules'
|