Skip to content

Commit

Permalink
validate benchmark in mod instead of main
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuizi7 committed Dec 4, 2018
1 parent ab47399 commit 8840e2f
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 29 deletions.
2 changes: 0 additions & 2 deletions rqalpha/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ base:
run_type: b
# 目前支持 `1d` (日线回测) 和 `1m` (分钟线回测),如果要进行分钟线,请注意是否拥有对应的数据源,目前开源版本是不提供对应的数据源的。
frequency: 1d
# Benchmark,如果不设置,默认没有基准参照。
benchmark: ~
# 在模拟交易和实盘交易中,RQAlpha支持策略的pause && resume,该选项表示开启 resume 功能
resume_mode: false
# 在模拟交易和实盘交易中,RQAlpha支持策略的pause && resume,该选项表示开启 persist 功能呢,
Expand Down
26 changes: 0 additions & 26 deletions rqalpha/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,6 @@ def _adjust_start_date(config, data_proxy):
config.base.timezone = pytz.utc


def _validate_benchmark(config, data_proxy):
benchmark = config.base.benchmark
if benchmark is None:
return
instrument = data_proxy.instruments(benchmark)
if instrument is None:
raise patch_user_exc(ValueError(_(u"invalid benchmark {}").format(benchmark)))

if instrument.order_book_id == "000300.XSHG":
# 000300.XSHG 数据进行了补齐,因此认为只要benchmark设置了000300.XSHG,就存在数据,不受限于上市日期。
return
config = Environment.get_instance().config
start_date = config.base.start_date
end_date = config.base.end_date
if instrument.listed_date.date() > start_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has not been listed on {start_date}").format(benchmark=benchmark,
start_date=start_date)))
if instrument.de_listed_date.date() < end_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has been de_listed on {end_date}").format(benchmark=benchmark,
end_date=end_date)))


def create_base_scope(copy_scope=False):
from rqalpha.utils.logger import user_print, user_log
from . import user_module
Expand Down Expand Up @@ -199,8 +175,6 @@ def run(config, source_code=None, user_funcs=None):

_adjust_start_date(env.config, env.data_proxy)

_validate_benchmark(env.config, env.data_proxy)

# FIXME
start_dt = datetime.datetime.combine(config.base.start_date, datetime.datetime.min.time())
env.calendar_dt = start_dt
Expand Down
1 change: 0 additions & 1 deletion rqalpha/mod/rqalpha_mod_sys_analyser/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def tear_down(self, code, exception=None):
'end_date': self._env.config.base.end_date.strftime('%Y-%m-%d'),
'strategy_file': self._env.config.base.strategy_file,
'run_type': self._env.config.base.run_type.value,
'benchmark': self._env.config.base.benchmark,
}
for account_type, starting_cash in six.iteritems(self._env.config.base.accounts):
summary[account_type] = starting_cash
Expand Down
27 changes: 27 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_benchmark/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

from rqalpha.interface import AbstractMod
from rqalpha.utils.logger import system_log
from rqalpha.utils.exception import patch_user_exc
from rqalpha.utils.i18n import gettext as _
from rqalpha.const import RUN_TYPE
from rqalpha.events import EVENT


class BenchmarkMod(AbstractMod):
Expand All @@ -33,6 +36,8 @@ def start_up(self, env, mod_config):
system_log.info("No order_book_id set, BenchmarkMod disabled.")
return

env.event_bus.add_listener(EVENT.POST_SYSTEM_INIT, lambda e: self._validate_benchmark(order_book_id, env))

if env.config.base.run_type == RUN_TYPE.BACKTEST:
from .benchmark_provider import BackTestPriceSeriesBenchmarkProvider as BTProvider
env.set_benchmark_provider(BTProvider(order_book_id))
Expand All @@ -42,3 +47,25 @@ def start_up(self, env, mod_config):

def tear_down(self, code, exception=None):
pass

@staticmethod
def _validate_benchmark(bechmark_order_book_id, env):
instrument = env.data_proxy.instruments(bechmark_order_book_id)
if instrument is None:
raise patch_user_exc(ValueError(_(u"invalid benchmark {}").format(bechmark_order_book_id)))

if instrument.order_book_id == "000300.XSHG":
# 000300.XSHG 数据进行了补齐,因此认为只要benchmark设置了000300.XSHG,就存在数据,不受限于上市日期。
return

config = env.config
start_date = config.base.start_date
end_date = config.base.end_date
if instrument.listed_date.date() > start_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has not been listed on {start_date}").format(benchmark=bechmark_order_book_id,
start_date=start_date)))
if instrument.de_listed_date.date() < end_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has been de_listed on {end_date}").format(benchmark=bechmark_order_book_id,
end_date=end_date)))
15 changes: 15 additions & 0 deletions tests/api/test_api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,18 @@ def handle_bar(context, bar_dict):
assert_position(pos, "RB1701", POSITION_DIRECTION.SHORT, 0, 3, context.expected_avg_price)

return init, handle_bar


@as_test_strategy
def test_subscribe_event():
def init(_):
subscribe_event(EVENT.BEFORE_TRADING, on_before_trading)

def before_trading(_):
g.before_trading_ran = True

def on_before_trading(_):
assert g.before_trading_ran
g.before_trading_ran = False

return init, before_trading
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_macd_signal.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_tick_size.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle_signal.pkl
Binary file not shown.
Binary file modified tests/outs/test_sf_buy_and_hold.pkl
Binary file not shown.

0 comments on commit 8840e2f

Please sign in to comment.