Skip to content

Commit

Permalink
use ~/.rqalpha/config.yml as default config file && add `rqalpha gene…
Browse files Browse the repository at this point in the history
…rate_config` click option
  • Loading branch information
EricWang committed Feb 23, 2017
1 parent 3e828c5 commit dee632e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
6 changes: 4 additions & 2 deletions docs/source/api/config.rst
Expand Up @@ -36,7 +36,7 @@
-sp `- -` slippage 设置滑点
-cm `- -` commission-multiplier 设置手续费乘数,默认为1
-mm `- -` margin-multiplier 设置保证金乘数,默认为1
-k `- -` kind 设置策略类型,目前支持 :code:`stock` (股票策略)、:code:`future` (期货策略)及 :code:`stock_future` (混合策略)
-st `- -` strategy-type 设置策略类型,目前支持 :code:`stock` (股票策略)、:code:`future` (期货策略)及 :code:`stock_future` (混合策略)
-fq `- -` frequency 目前支持 :code:`1d` (日线回测) 和 :code:`1m` (分钟线回测),如果要进行分钟线,请注意是否拥有对应的数据源,目前开源版本是不提供对应的数据源的
-me `- -` match-engine 启用的回测引擎,目前支持 :code:`current_bar` (当前Bar收盘价撮合) 和 :code:`next_bar` (下一个Bar开盘价撮合)
-rt `- -` run-type 运行类型,:code:`b` 为回测,:code:`p` 为模拟交易, :code:`r` 为实盘交易
Expand All @@ -45,10 +45,10 @@ N/A `- -` handle-split 开启自动处理, 默认不开
N/A `- -` not-handle-split 不开启自动处理, 默认不开启
N/A `- -` risk-grid 开启Alpha/Beta 等风险指标的实时计算,默认开启
N/A `- -` no-risk-grid 不开启Alpha/Beta 等风险指标的实时计算,默认开启
N/A `- -` disable-user-system-log 关闭用户策略产生的系统日志(比如订单未成交等提示)
-l `- -` log-level 选择日期的输出等级,有 :code:`verbose` | :code:`info` | :code:`warning` | :code:`error` 等选项,您可以通过设置 :code:`verbose` 来查看最详细的日志,或者设置 :code:`error` 只查看错误级别的日志输出
-p `- -` plot 在回测结束后,查看图形化的收益曲线
N/A `- -` no-plot 在回测结束后,不查看图形化的收益曲线
N/A `- -` fast-match 默认关闭,如果当前撮合引擎为 :code:`current_bar` 开启该选项会立刻进行撮合,不会等到当前bar结束
N/A `- -` progress 开启命令行显示回测进度条
N/A `- -` no-progress 关闭命令行查看回测进度
N/A `- -` enable-profiler 启动策略逐行性能分析,启动后,在回测结束,会打印策略的运行性能分析报告,可以看到每一行消耗的时间
Expand All @@ -72,6 +72,8 @@ N/A `- -` config 设置配置文件路径

除了在启动策略的时候传入参数,还可以通过指定配置文件的方式来进行参数的配置, 配置文件的配置信息优先级低于启动参数,也就是说启动参数会覆盖配置文件的配置项,配置文件的格式如下:

注: 如果没有指定 `config.yml`, RQAlpha 在运行时会自动在 `~/.rqalpha/` 文件夹下创建 `config.yml` 文件作为默认配置文件。您也可以使用 `$ rqalpha generate_config` 来生成一份默认的配置文件。

.. code-block:: bash
:linenos:
Expand Down
12 changes: 12 additions & 0 deletions docs/source/intro/install.rst
Expand Up @@ -76,3 +76,15 @@ bundle 默认存放在 `~/.rqalpha` 下,您也可以指定 bundle 的存放位
$ rqalpha run -d target_bundle_path .....
详细参数配置请查看 :ref:`api-config`

获取配置文件
==================

如果运行 RQAlpha 时不指定配置文件,会在 `~/.rqalpha/` 文件夹下创建 `config.yml` 文件作为默认配置文件。

如果您想要直接获得一份配置文件,也可以通过如下命令来获得。

.. code-block:: bash
$ rqalpha generate_config
25 changes: 21 additions & 4 deletions rqalpha/__main__.py
Expand Up @@ -40,6 +40,9 @@ def entry_point():
@cli.command()
@click.option('-d', '--data-bundle-path', default=os.path.expanduser("~/.rqalpha"), type=click.Path(file_okay=False))
def update_bundle(data_bundle_path):
"""
Sync Data Bundle
"""
from . import main
main.update_bundle(data_bundle_path)

Expand All @@ -58,6 +61,7 @@ def update_bundle(data_bundle_path):
@click.option('-cm', '--commission-multiplier', 'base__commission_multiplier', type=click.FLOAT)
@click.option('-mm', '--margin-multiplier', 'base__margin_multiplier', type=click.FLOAT)
@click.option('-k', '--kind', 'base__strategy_type', type=click.Choice(['stock', 'future', 'stock_future']))
@click.option('-st', '--strategy-type', 'base__strategy_type', type=click.Choice(['stock', 'future', 'stock_future']))
@click.option('-fq', '--frequency', 'base__frequency', type=click.Choice(['1d', '1m']))
@click.option('-me', '--match-engine', 'base__matching_type', type=click.Choice(['current_bar', 'next_bar']))
@click.option('-rt', '--run-type', 'base__run_type', type=click.Choice(['b', 'p']), default="b")
Expand All @@ -70,7 +74,6 @@ def update_bundle(data_bundle_path):
@click.option('--report', 'mod__analyser__report_save_path', type=click.Path(writable=True), help="save report")
@click.option('-o', '--output-file', 'mod__analyser__output_file', type=click.Path(writable=True),
help="output result pickle file")
@click.option('--fast-match', 'validator__fast_match', is_flag=True)
@click.option('--progress/--no-progress', 'mod__progress__enabled', default=None, help="show progress bar")
@click.option('--extra-vars', 'extra__context_vars', type=click.STRING, help="override context vars")
@click.option("--enable-profiler", "extra__enable_profiler", is_flag=True,
Expand All @@ -79,6 +82,9 @@ def update_bundle(data_bundle_path):
@click.option('-mc', '--mod-config', 'mod_configs', nargs=2, multiple=True, type=click.STRING, help="mod extra config")
@click.help_option('-h', '--help')
def run(**kwargs):
"""
Start to run a strategy
"""
if kwargs.get('base__run_type') == 'p':
set_cache_policy(CachePolicy.MINIMUM)

Expand All @@ -103,7 +109,7 @@ def run(**kwargs):
@click.option('-d', '--directory', default="./", type=click.Path(), required=True)
def examples(directory):
"""
generate example strategies to target folder
Generate example strategies to target folder
"""
source_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "examples")

Expand All @@ -120,7 +126,7 @@ def examples(directory):
@click.option('--plot-save', 'plot_save_file', default=None, type=click.Path(), help="save plot result to file")
def plot(result_dict_file, is_show, plot_save_file):
"""
draw result DataFrame
Draw result DataFrame
"""
import pandas as pd
from rqalpha.plot import plot_result
Expand All @@ -137,7 +143,7 @@ def plot(result_dict_file, is_show, plot_save_file):
@click.argument('target_report_csv_path', type=click.Path(exists=True, writable=True), required=True)
def report(result_pickle_file_path, target_report_csv_path):
"""
generate report from backtest output file
Generate report from backtest output file
"""
import pandas as pd
result_dict = pd.read_pickle(result_pickle_file_path)
Expand All @@ -156,5 +162,16 @@ def version(**kwargs):
print("Current Version: ", version_info)


@cli.command()
@click.option('-d', '--directory', default="./", type=click.Path(), required=True)
def generate_config(directory):
"""
Generate default config file
"""
default_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.yml")
target_config_path = os.path.abspath(os.path.join(directory, 'config.yml'))
shutil.copy(default_config, target_config_path)
print("Config file has been generated in", target_config_path)

if __name__ == '__main__':
entry_point()
8 changes: 7 additions & 1 deletion rqalpha/utils/config.py
Expand Up @@ -33,7 +33,13 @@

def parse_config(config_args, base_config_path=None, click_type=True, source_code=None):
if base_config_path is None:
config_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../config.yml"))
config_path = os.path.abspath(os.path.expanduser("~/.rqalpha/config.yml"))
if not os.path.exists(config_path):
dir_path = os.path.dirname(config_path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
default_config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../config.yml")
open(config_path, "wb").write(open(default_config_path, "rb").read())
else:
config_path = base_config_path
if not os.path.exists(config_path):
Expand Down

0 comments on commit dee632e

Please sign in to comment.