Skip to content

Commit

Permalink
adjust run option
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWang committed Mar 27, 2017
1 parent 9257883 commit f443355
Show file tree
Hide file tree
Showing 26 changed files with 41 additions and 47 deletions.
13 changes: 6 additions & 7 deletions rqalpha/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,28 @@ def update_bundle(data_bundle_path, locale):
@click.option('-f', '--strategy-file', 'base__strategy_file', type=click.Path(exists=True))
@click.option('-s', '--start-date', 'base__start_date', type=Date())
@click.option('-e', '--end-date', 'base__end_date', type=Date())
@click.option('-r', '--rid', 'base__run_id', type=click.STRING)
@click.option('-sc', '--stock-starting-cash', 'base__stock_starting_cash', type=click.FLOAT)
@click.option('-fc', '--future-starting-cash', 'base__future_starting_cash', type=click.FLOAT)
@click.option('-bm', '--benchmark', 'base__benchmark', type=click.STRING, default=None)
@click.option('-mm', '--margin-multiplier', 'base__margin_multiplier', type=click.FLOAT)
@click.option('-st', '--strategy-type', 'base__strategy_type', type=click.Choice(['stock', 'future', 'stock_future']))
@click.option('-st', '--security', 'base__securities', multiple=True, type=click.Choice(['stock', 'future']))
@click.option('-fq', '--frequency', 'base__frequency', type=click.Choice(['1d', '1m']))
@click.option('-rt', '--run-type', 'base__run_type', type=click.Choice(['b', 'p']), default="b")
@click.option('--resume', 'base__resume_mode', is_flag=True)
@click.option('--handle-split/--not-handle-split', 'base__handle_split', default=None, help="handle split")
# -- Extra Configuration
@click.option('-l', '--log-level', 'extra__log_level', type=click.Choice(['verbose', 'debug', 'info', 'error', 'none']))
@click.option('--locale', 'extra__locale', type=click.Choice(['cn', 'en']), default="cn")
@click.option('--disable-user-system-log', 'extra__user_system_log_disabled', is_flag=True, help='disable user system log')
@click.option('--extra-vars', 'extra__context_vars', type=click.STRING, help="override context vars")
@click.option("--enable-profiler", "extra__enable_profiler", is_flag=True,
help="add line profiler to profile your strategy")
@click.option("--enable-profiler", "extra__enable_profiler", is_flag=True, help="add line profiler to profile your strategy")
@click.option('--config', 'config_path', type=click.STRING, help="config file path")
# -- Mod Configuration
@click.option('-mc', '--mod-config', 'mod_configs', nargs=2, multiple=True, type=click.STRING, help="mod extra config")
# -- DEPRECATED ARGS && WILL BE REMOVED AFTER VERSION 3.0.0
@click.option('-i', '--init-cash', 'base__stock_starting_cash', type=click.FLOAT)
@click.option('-k', '--kind', 'base__strategy_type', type=click.Choice(['stock', 'future', 'stock_future']))
@click.option('-i', '--init-cash', 'base__stock_starting_cash', type=click.FLOAT, help="[Deprecated]")
@click.option('-k', '--kind', 'base__securities', type=click.Choice(['stock', 'future', 'stock_future']), help="[Deprecated]")
@click.option('--strategy-type', 'base__securities', type=click.Choice(['stock', 'future']), help="[Deprecated]")
# -- Moving to Simulation
def run(**kwargs):
"""
Start to run a strategy
Expand Down
8 changes: 0 additions & 8 deletions rqalpha/core/strategy_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class RunInfo(object):
__repr__ = property_repr

def __init__(self, config):
self._run_id = config.base.run_id
self._start_date = config.base.start_date
self._end_date = config.base.end_date
self._frequency = config.base.frequency
Expand All @@ -46,13 +45,6 @@ def __init__(self, config):
self._slippage = config.mod.simulation.slippage
self._commission_multiplier = config.mod.simulation.commission_multiplier

@property
def run_id(self):
"""
:property getter: 标识策略每次运行的唯一id
"""
return self._run_id

@property
def start_date(self):
"""
Expand Down
6 changes: 2 additions & 4 deletions rqalpha/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ version: 0.1.5
whitelist: [base, extra, validator, mod]

base:
# 可以指定回测的唯一ID,用户区分多次回测的结果
run_id: 9999
# 数据源所存储的文件路径
data_bundle_path: ~
# 启动的策略文件路径
Expand All @@ -18,8 +16,8 @@ base:
stock_starting_cash: 0
# 期货起始资金,默认为0
future_starting_cash: 0
# 设置策略类型,目前支持 `stock` (股票策略)、`future` (期货策略)及 `stock_future` (混合策略)
strategy_type: stock
# 设置策略可交易品种,目前支持 `stock` (股票策略)、`future` (期货策略)tst
securities: [stock]
# 设置保证金乘数,默认为1
margin_multiplier: 1
# 运行类型,`b` 为回测,`p` 为模拟交易, `r` 为实盘交易。
Expand Down
3 changes: 1 addition & 2 deletions rqalpha/mod/rqalpha_mod_sys_analyser/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ def tear_down(self, code, exception=None):
'start_date': self._env.config.base.start_date.strftime('%Y-%m-%d'),
'end_date': self._env.config.base.end_date.strftime('%Y-%m-%d'),
'strategy_file': self._env.config.base.strategy_file,
'strategy_type': self._env.config.base.strategy_type,
'run_id': self._env.config.base.run_id,
'securities': self._env.config.base.securities,
'run_type': self._env.config.base.run_type.value,
'stock_starting_cash': self._env.config.base.stock_starting_cash,
'future_starting_cash': self._env.config.base.future_starting_cash,
Expand Down
10 changes: 10 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_simulation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


__config__ = {
# 可以指定回测的唯一ID,用户区分多次回测的结果
"run_id": 9999,
# 是否开启信号模式
"signal": False,
# 启用的回测引擎,目前支持 `current_bar` (当前Bar收盘价撮合) 和 `next_bar` (下一个Bar开盘价撮合)
Expand Down Expand Up @@ -87,3 +89,11 @@ def load_mod():
help="[sys_simulation] set matching type"
)
)

cli.commands['run'].params.append(
click.Option(
('-r', '--rid', cli_prefix + "run_id"),
type=click.STRING,
help="[sys_simulation] set run id"
)
)
23 changes: 9 additions & 14 deletions rqalpha/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from . import RqAttrDict, logger
from .exception import patch_user_exc
from .logger import user_log, user_system_log, system_log, std_log, user_std_handler
from ..const import ACCOUNT_TYPE, MATCHING_TYPE, RUN_TYPE, PERSIST_MODE
from ..const import RUN_TYPE, PERSIST_MODE, ACCOUNT_TYPE
from ..utils.i18n import gettext as _, localization
from ..utils.dict_func import deep_update
from ..utils.py2 import to_utf8
Expand Down Expand Up @@ -169,7 +169,7 @@ def parse_config(config_args, config_path=None, click_type=True, source_code=Non
return

base_config.run_type = parse_run_type(base_config.run_type)
base_config.account_list = gen_account_list(base_config.strategy_type)
base_config.account_list = parse_account_list(base_config.securities)
base_config.persist_mode = parse_persist_mode(base_config.persist_mode)

if extra_config.log_level.upper() != "NONE":
Expand Down Expand Up @@ -228,18 +228,13 @@ def parse_user_config_from_code(config, source_code=None):
return config


def gen_account_list(account_list_str):
assert isinstance(account_list_str, six.string_types)
account_list = account_list_str.split("_")
return [parse_account_type(account_str) for account_str in account_list]


def parse_account_type(account_type_str):
assert isinstance(account_type_str, six.string_types)
if account_type_str == "stock":
return ACCOUNT_TYPE.STOCK
elif account_type_str == "future":
return ACCOUNT_TYPE.FUTURE
def parse_account_list(securities):
if isinstance(securities, (tuple, list)):
return [ACCOUNT_TYPE[security.upper()] for security in securities]
elif isinstance(securities, six.string_types):
return [ACCOUNT_TYPE[securities.upper()]]
else:
raise NotImplementedError


def parse_run_type(rt_str):
Expand Down
6 changes: 3 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_api():

base_api_config = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2016-12-01",
"end_date": "2016-12-31",
"frequency": "1d",
Expand All @@ -180,7 +180,7 @@ def test_api():

stock_api_config = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2016-03-07",
"end_date": "2016-03-08",
"frequency": "1d",
Expand All @@ -200,7 +200,7 @@ def test_api():

future_api_config = {
"base": {
"strategy_type": "future",
"securities": "future",
"start_date": "2016-03-07",
"end_date": "2016-03-08",
"frequency": "1d",
Expand Down
Binary file modified tests/outs/test_f_buy_and_hold.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_macd.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_mean_reverting.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_buy_and_hold.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_dma.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_dual_thrust.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_golden_cross.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_scheduler.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle.pkl
Binary file not shown.
1 change: 1 addition & 0 deletions tests/outs/time.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ date_time,time_spend
2017-03-21 20:23:51.415865,19.482884
2017-03-24 18:32:23.841078,14.941193
2017-03-25 18:10:15.051871,15.248598
2017-03-27 17:00:53.476269,15.105606
2 changes: 1 addition & 1 deletion tests/test_f_buy_and_hold.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "future",
"securities": "future",
"start_date": "2015-01-09",
"end_date": "2015-03-09",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_f_macd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "future",
"securities": "future",
"start_date": "2014-09-01",
"end_date": "2016-09-05",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_f_mean_reverting.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "future",
"securities": "future",
"start_date": "2014-06-01",
"end_date": "2015-08-01",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_buy_and_hold.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def after_trading(context):

__config__ = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2015-01-09",
"end_date": "2016-01-12",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_dma.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2008-07-01",
"end_date": "2017-01-01",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_dual_thrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2013-01-01",
"end_date": "2015-12-29",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_golden_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2008-07-01",
"end_date": "2017-01-01",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def rebalance(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2008-07-01",
"end_date": "2017-01-01",
"frequency": "1d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_s_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def handle_bar(context, bar_dict):

__config__ = {
"base": {
"strategy_type": "stock",
"securities": "stock",
"start_date": "2008-07-01",
"end_date": "2014-09-01",
"frequency": "1d",
Expand Down

0 comments on commit f443355

Please sign in to comment.