xtquant.qmttools.stgentry

 1#coding:utf-8
 2
 3import os, sys
 4import types
 5
 6from .functions import *
 7from .contextinfo import ContextInfo
 8from .stgframe import StrategyLoader
 9
10
11def run_file(user_script, param = {}):
12    pypath = param.get('pythonpath')
13    if pypath:
14        lib_search = [os.path.abspath(p) for p in pypath.split(';')]
15        sys.path = lib_search + [p for p in sys.path if p not in lib_search]
16
17    user_module = compile(open(user_script, 'rb').read(), user_script, 'exec', optimize = 2)
18    #print({'user_module': user_module})
19
20    user_locals = {}
21
22    try:
23        pywentrance = param.get('pywentrance', '')
24        user_variable = compile(open(os.path.join(pywentrance, "..", "user_config.py"), "rb").read(),
25                                "user_config.py", 'exec', optimize=2)
26        exec(user_variable, globals(), user_locals)
27    except Exception as e:
28        pass
29
30    exec(user_module, globals(), user_locals)
31    #print('user_locals', user_locals)
32
33    globals().update(user_locals)
34
35    _C = ContextInfo()
36    _C._param = param
37    _C.user_script = user_script
38
39    def try_set_func(C, func_name):
40        func = user_locals.get(func_name)
41        if func:
42            C.__setattr__(func_name, types.MethodType(func, C))
43        return
44
45    try_set_func(_C, 'init')
46    try_set_func(_C, 'after_init')
47    try_set_func(_C, 'handlebar')
48    try_set_func(_C, 'stop')
49
50    try_set_func(_C, 'account_callback')
51    try_set_func(_C, 'order_callback')
52    try_set_func(_C, 'deal_callback')
53    try_set_func(_C, 'position_callback')
54    try_set_func(_C, 'orderError_callback')
55
56    loader = StrategyLoader()
57
58    loader.C = _C
59
60    loader.init()
61    loader.start()
62    loader.run()
63    loader.stop()
64    loader.shutdown()
65
66    return
def run_file(user_script, param={}):
12def run_file(user_script, param = {}):
13    pypath = param.get('pythonpath')
14    if pypath:
15        lib_search = [os.path.abspath(p) for p in pypath.split(';')]
16        sys.path = lib_search + [p for p in sys.path if p not in lib_search]
17
18    user_module = compile(open(user_script, 'rb').read(), user_script, 'exec', optimize = 2)
19    #print({'user_module': user_module})
20
21    user_locals = {}
22
23    try:
24        pywentrance = param.get('pywentrance', '')
25        user_variable = compile(open(os.path.join(pywentrance, "..", "user_config.py"), "rb").read(),
26                                "user_config.py", 'exec', optimize=2)
27        exec(user_variable, globals(), user_locals)
28    except Exception as e:
29        pass
30
31    exec(user_module, globals(), user_locals)
32    #print('user_locals', user_locals)
33
34    globals().update(user_locals)
35
36    _C = ContextInfo()
37    _C._param = param
38    _C.user_script = user_script
39
40    def try_set_func(C, func_name):
41        func = user_locals.get(func_name)
42        if func:
43            C.__setattr__(func_name, types.MethodType(func, C))
44        return
45
46    try_set_func(_C, 'init')
47    try_set_func(_C, 'after_init')
48    try_set_func(_C, 'handlebar')
49    try_set_func(_C, 'stop')
50
51    try_set_func(_C, 'account_callback')
52    try_set_func(_C, 'order_callback')
53    try_set_func(_C, 'deal_callback')
54    try_set_func(_C, 'position_callback')
55    try_set_func(_C, 'orderError_callback')
56
57    loader = StrategyLoader()
58
59    loader.C = _C
60
61    loader.init()
62    loader.start()
63    loader.run()
64    loader.stop()
65    loader.shutdown()
66
67    return