From aadb59e08951eeb48aa2510b105bd10246169fbb Mon Sep 17 00:00:00 2001 From: EricWang Date: Tue, 11 Apr 2017 18:17:42 +0800 Subject: [PATCH] add extend_api example --- docs/source/development/data_source.rst | 7 ++- rqalpha/__init__.py | 1 + rqalpha/api/api_base.py | 1 + .../{data_source => }/IF1706_20161108.csv | 0 .../examples/data_source/get_csv_module.py | 2 +- .../examples/data_source/read_csv_as_df.py | 2 +- rqalpha/examples/extend_api/__init__.py | 16 ++++++ .../extend_api/rqalpha_mod_extend_api_demo.py | 49 +++++++++++++++++++ .../examples/extend_api/test_extend_api.py | 35 +++++++++++++ rqalpha/mod/rqalpha_mod_sys_progress/mod.py | 1 + 10 files changed, 110 insertions(+), 4 deletions(-) rename rqalpha/examples/{data_source => }/IF1706_20161108.csv (100%) create mode 100644 rqalpha/examples/extend_api/__init__.py create mode 100644 rqalpha/examples/extend_api/rqalpha_mod_extend_api_demo.py create mode 100644 rqalpha/examples/extend_api/test_extend_api.py diff --git a/docs/source/development/data_source.rst b/docs/source/development/data_source.rst index 0ac971938..d87851b29 100644 --- a/docs/source/development/data_source.rst +++ b/docs/source/development/data_source.rst @@ -30,6 +30,8 @@ RQAlpha 不限制本地运行的策略调使用哪些库,因此您可以直接 * 请在 `init`, `before_trading`, `handle_bar`, `handle_tick`, `after_trading` 等函数中读取自有数据,而不要在函数外执行数据获取的代码,否则可能会产生异常。 * RQAlpha 是读取策略代码并执行的,因此实际当前路径是运行 `rqalpha` 命令的路径,策略使用相对路径容易产生异常。如果您需要根据策略路径来定位相对路径可以通过 `context.config.base.strategy_file` 来获取策略路径,从而获取相对策略文件的其他路径,具体使用方式请看下面的示例代码。 +`read_csv_as_df `_ + .. code-block:: python3 from rqalpha.api import * @@ -87,7 +89,8 @@ RQAlpha 不限制本地运行的策略调使用哪些库,因此您可以直接 * 如果您的自定义模块是基于策略策略的相对路径,则需要在 `init` 函数中通过 `context.config.base.strategy_file` 获取到策略路径,然后再添加到 `sys.path` 中。 * RQAlpha 是读取策略代码并执行的,因此实际当前路径是执行 `rqalpha` 命令的路径,避免使用相对路径。 -get_csv_module.py +`get_csv_module `_ + .. code-block:: python3 @@ -104,7 +107,7 @@ get_csv_module.py csv_path = os.path.join(os.path.dirname(__file__), "./IF1706_20161108.csv") return read_csv_as_df(csv_path) -import_get_csv_module.py +`import_get_csv_module `_ .. code-block:: python3 diff --git a/rqalpha/__init__.py b/rqalpha/__init__.py index dd96cac6f..f0dd198f4 100644 --- a/rqalpha/__init__.py +++ b/rqalpha/__init__.py @@ -20,6 +20,7 @@ import pkgutil from .cmd import cmd_cli +from .api.api_base import export_as_api __all__ = [ '__version__', diff --git a/rqalpha/api/api_base.py b/rqalpha/api/api_base.py index 7983b9cbd..aca885cc9 100644 --- a/rqalpha/api/api_base.py +++ b/rqalpha/api/api_base.py @@ -120,6 +120,7 @@ def export_as_api(func): __all__.append(func.__name__) func = decorate_api_exc(func) + globals()[func.__name__] = func return func diff --git a/rqalpha/examples/data_source/IF1706_20161108.csv b/rqalpha/examples/IF1706_20161108.csv similarity index 100% rename from rqalpha/examples/data_source/IF1706_20161108.csv rename to rqalpha/examples/IF1706_20161108.csv diff --git a/rqalpha/examples/data_source/get_csv_module.py b/rqalpha/examples/data_source/get_csv_module.py index bc54e4085..637f83694 100644 --- a/rqalpha/examples/data_source/get_csv_module.py +++ b/rqalpha/examples/data_source/get_csv_module.py @@ -8,5 +8,5 @@ def read_csv_as_df(csv_path): def get_csv(): - csv_path = os.path.join(os.path.dirname(__file__), "./IF1706_20161108.csv") + csv_path = os.path.join(os.path.dirname(__file__), "../IF1706_20161108.csv") return read_csv_as_df(csv_path) diff --git a/rqalpha/examples/data_source/read_csv_as_df.py b/rqalpha/examples/data_source/read_csv_as_df.py index 408651e68..ff24855bb 100644 --- a/rqalpha/examples/data_source/read_csv_as_df.py +++ b/rqalpha/examples/data_source/read_csv_as_df.py @@ -10,7 +10,7 @@ def read_csv_as_df(csv_path): def init(context): import os strategy_file_path = context.config.base.strategy_file - csv_path = os.path.join(os.path.dirname(strategy_file_path), "./IF1706_20161108.csv") + csv_path = os.path.join(os.path.dirname(strategy_file_path), "../IF1706_20161108.csv") IF1706_df = read_csv_as_df(csv_path) context.IF1706_df = IF1706_df diff --git a/rqalpha/examples/extend_api/__init__.py b/rqalpha/examples/extend_api/__init__.py new file mode 100644 index 000000000..f7b4f888c --- /dev/null +++ b/rqalpha/examples/extend_api/__init__.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2017 Ricequant, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + diff --git a/rqalpha/examples/extend_api/rqalpha_mod_extend_api_demo.py b/rqalpha/examples/extend_api/rqalpha_mod_extend_api_demo.py new file mode 100644 index 000000000..39a67ffc4 --- /dev/null +++ b/rqalpha/examples/extend_api/rqalpha_mod_extend_api_demo.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2017 Ricequant, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import pandas as pd +from rqalpha.interface import AbstractMod + + +__config__ = { + "csv_path": None +} + + +def load_mod(): + return ExtendAPIDemoMod() + + +class ExtendAPIDemoMod(AbstractMod): + def __init__(self): + # 注入API 一定要在初始化阶段,否则无法成功注入 + self._csv_path = None + self._inject_api() + + def start_up(self, env, mod_config): + self._csv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), mod_config.csv_path)) + + def tear_down(self, code, exception=None): + pass + + def _inject_api(self): + from rqalpha import export_as_api + + @export_as_api + def get_csv_as_df(): + data = pd.read_csv(self._csv_path) + return data diff --git a/rqalpha/examples/extend_api/test_extend_api.py b/rqalpha/examples/extend_api/test_extend_api.py new file mode 100644 index 000000000..b3cbcc86f --- /dev/null +++ b/rqalpha/examples/extend_api/test_extend_api.py @@ -0,0 +1,35 @@ +from rqalpha.api import * + + +def init(context): + IF1706_df = get_csv_as_df() + context.IF1706_df = IF1706_df + + +def before_trading(context): + logger.info(context.IF1706_df) + + +__config__ = { + "base": { + "securities": "future", + "start_date": "2015-01-09", + "end_date": "2015-01-10", + "frequency": "1d", + "matching_type": "current_bar", + "future_starting_cash": 1000000, + "benchmark": None, + }, + "extra": { + "log_level": "verbose", + }, + "mod": { + "extend_api_demo": { + "enabled": True, + "lib": "rqalpha.examples.extend_api.rqalpha_mod_extend_api_demo", + "csv_path": "../IF1706_20161108.csv" + } + } +} + + diff --git a/rqalpha/mod/rqalpha_mod_sys_progress/mod.py b/rqalpha/mod/rqalpha_mod_sys_progress/mod.py index f4ae03554..4c8fdb65a 100644 --- a/rqalpha/mod/rqalpha_mod_sys_progress/mod.py +++ b/rqalpha/mod/rqalpha_mod_sys_progress/mod.py @@ -44,3 +44,4 @@ def _tick(self, event): def tear_down(self, success, exception=None): if self._show: self.progress_bar.render_finish() +