Skip to content

Commit

Permalink
add extend_api example
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWang committed Apr 11, 2017
1 parent 8f1a498 commit aadb59e
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 4 deletions.
7 changes: 5 additions & 2 deletions docs/source/development/data_source.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/ricequant/rqalpha/blob/develop/rqalpha/examples/data_source/read_csv_as_df.py>`_

.. code-block:: python3
from rqalpha.api import *
Expand Down Expand Up @@ -87,7 +89,8 @@ RQAlpha 不限制本地运行的策略调使用哪些库,因此您可以直接
* 如果您的自定义模块是基于策略策略的相对路径,则需要在 `init` 函数中通过 `context.config.base.strategy_file` 获取到策略路径,然后再添加到 `sys.path` 中。
* RQAlpha 是读取策略代码并执行的,因此实际当前路径是执行 `rqalpha` 命令的路径,避免使用相对路径。

get_csv_module.py
`get_csv_module <https://github.com/ricequant/rqalpha/blob/develop/rqalpha/examples/data_source/get_csv_module.py>`_


.. code-block:: python3
Expand All @@ -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 <https://github.com/ricequant/rqalpha/blob/develop/rqalpha/examples/data_source/import_get_csv_module.py>`_

.. code-block:: python3
Expand Down
1 change: 1 addition & 0 deletions rqalpha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import pkgutil
from .cmd import cmd_cli
from .api.api_base import export_as_api

__all__ = [
'__version__',
Expand Down
1 change: 1 addition & 0 deletions rqalpha/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def export_as_api(func):
__all__.append(func.__name__)

func = decorate_api_exc(func)
globals()[func.__name__] = func

return func

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion rqalpha/examples/data_source/get_csv_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion rqalpha/examples/data_source/read_csv_as_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions rqalpha/examples/extend_api/__init__.py
Original file line number Diff line number Diff line change
@@ -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.

49 changes: 49 additions & 0 deletions rqalpha/examples/extend_api/rqalpha_mod_extend_api_demo.py
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions rqalpha/examples/extend_api/test_extend_api.py
Original file line number Diff line number Diff line change
@@ -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"
}
}
}


1 change: 1 addition & 0 deletions rqalpha/mod/rqalpha_mod_sys_progress/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ def _tick(self, event):
def tear_down(self, success, exception=None):
if self._show:
self.progress_bar.render_finish()

0 comments on commit aadb59e

Please sign in to comment.