vnpy/vn.trader/ctaStrategy/strategy/__init__.py
chenxy123 9f6ea1fd5c 1. ctaAlgo模块改名ctaStrategy
2. 将vn.trader下的所有接口统一放到了一个单独的gateway文件夹中,并实现自动识别和加载
2017-03-18 12:19:51 +08:00

32 lines
953 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# encoding: UTF-8
'''
动态载入所有的策略类
'''
import os
import importlib
# 用来保存策略类的字典
STRATEGY_CLASS = {}
# 获取目录路径
path = os.path.abspath(os.path.dirname(__file__))
# 遍历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', '')
# 使用importlib动态载入模块
module = importlib.import_module(moduleName)
# 遍历模块下的对象,只有名称中包含'Strategy'的才是策略类
for k in dir(module):
if 'Strategy' in k:
v = module.__getattribute__(k)
STRATEGY_CLASS[k] = v