In [ ]:
from rqalpha.api import *


# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。
def init(context):
    logger.info("init")

def before_trading(context):
    pass
    
# 你选择的证券的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新
def handle_bar(context, bar_dict):
    _fvalue = get_factors("testFactor",context.now.date(),context.now.date()).iloc[0]
    _fvalue = _fvalue[_fvalue>0]
#     print(context.now,_fvalue.sort_values())
    #买入低估值排名前10的票
    buy_codes = list(_fvalue.sort_values().index[:10])
#     holdings = [code for code in context.portfolio.positions]
    print(buy_codes)
    equalWeight_order(buy_codes,context)

    
def equalWeight_order(tobe_holding_codes = [],context=None):
    if len(tobe_holding_codes) < 1:
        for code,pos in context.portfolio.positions.items():
            if pos.sellable>0:
                order_shares(code,-1 * pos.sellable)
        return
#     print("positions",context.portfolio.positions)
    _target_percent = round(1.0/len(tobe_holding_codes),2)
    _targets = set(tobe_holding_codes)
    _tobe_sell = [pos for code,pos in context.portfolio.positions.items() if code not in _targets]
    for pos in _tobe_sell:
        if pos.sellable>0:
            order_shares(pos.order_book_id,-1 * pos.sellable)
    for code in tobe_holding_codes:
        _acount = context.portfolio.stock_account
        _cash_percent = round(_acount.cash/_acount.total_value,2)
        _real_percent = min(_cash_percent,_target_percent)
#         print(_acount.cash,_acount.total_value,_cash_percent,_real_percent)
        if _real_percent > 0:
            order_target_percent(code,_real_percent)
    return

In [2]:
# evaluateFileDemo
from rqalpha import run_file

config = {
  "base": {
    "start_date": "2017-01-01",
    "end_date": "2017-1-31",
  },
}

file_path = "./testStrategy.ipynb"#"./rqalpha/examples/testFactor.py"

run_file(strategy_file_path=file_path,config=config)


[2018-01-29 18:55:14.553671] ERROR: system_log: ******************************
[2018-01-29 18:55:14.561700] ERROR: system_log: Mod Import Error: rqalpha_mod_alphaStar_mgr, error: No module named 'rqalpha_mod_alphaStar_mgr'
[2018-01-29 18:55:14.568756] ERROR: system_log: ******************************
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\main.py in run(config, source_code, user_funcs)
    196         env.set_global_vars(GlobalVars())
--> 197         mod_handler.set_env(env)
    198         mod_handler.start_up()

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\mod\__init__.py in set_env(self, environment)
     50             system_log.debug(_(u"loading mod {}").format(lib_name))
---> 51             mod_module = import_mod(lib_name)
     52             if mod_module is None:

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\utils\package_helper.py in import_mod(mod_name)
     22         from importlib import import_module
---> 23         return import_module(mod_name)
     24     except Exception as e:

D:\Program Files\Anaconda3\envs\rqalpha\lib\importlib\__init__.py in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

D:\Program Files\Anaconda3\envs\rqalpha\lib\importlib\_bootstrap.py in _gcd_import(name, package, level)

D:\Program Files\Anaconda3\envs\rqalpha\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

D:\Program Files\Anaconda3\envs\rqalpha\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

ImportError: No module named 'rqalpha_mod_alphaStar_mgr'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-2-f5effe19506e> in <module>()
     11 file_path = "./testStrategy.ipynb"#"./rqalpha/examples/testFactor.py"
     12 
---> 13 run_file(strategy_file_path=file_path,config=config)

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\__init__.py in run_file(strategy_file_path, config)
     95     config = parse_config(config)
     96     clear_all_cached_functions()
---> 97     return main.run(config)
     98 
     99 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\main.py in run(config, source_code, user_funcs)
    311         user_exc = create_custom_exception(exc_type, exc_val, exc_tb, config.base.strategy_file)
    312 
--> 313         code = _exception_handler(user_exc)
    314         mod_handler.tear_down(code, user_exc)
    315     else:

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\main.py in _exception_handler(e)
    320 
    321 def _exception_handler(e):
--> 322     better_exceptions.excepthook(e.error.exc_type, e.error.exc_val, e.error.exc_tb)
    323     user_system_log.error(e.error)
    324     if not is_user_exc(e.error.exc_val):

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\better_exceptions-0.1.8-py3.5.egg\better_exceptions\__init__.py in excepthook(exc, value, tb)
    342 def excepthook(exc, value, tb):
    343     formatted = format_exception(exc, value, tb)
--> 344     write_stream(formatted)
    345 
    346 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\better_exceptions-0.1.8-py3.5.egg\better_exceptions\__init__.py in write_stream(data)
    323 
    324     if PY3:
--> 325         STREAM.buffer.write(data)
    326     else:
    327         STREAM.write(data)

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\colorama\ansitowin32.py in __getattr__(self, name)
     35 
     36     def __getattr__(self, name):
---> 37         return getattr(self.__wrapped, name)
     38 
     39     def write(self, text):

AttributeError: 'OutStream' object has no attribute 'buffer'