Skip to content

Commit

Permalink
move subscribe_event into api_base
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuizi7 committed Oct 10, 2018
1 parent 5952c05 commit 02b10b3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
14 changes: 1 addition & 13 deletions rqalpha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import pkgutil

from rqalpha.__main__ import cli
from rqalpha.api.api_base import export_as_api
from rqalpha.api.api_base import export_as_api, subscribe_event

__all__ = [
'__version__',
Expand Down Expand Up @@ -141,15 +141,3 @@ def run_func(**kwargs):
config = parse_config(config, user_funcs=user_funcs)
clear_all_cached_functions()
return main.run(config, user_funcs=user_funcs)


def subscribe_event(event_type, handler):
import types
from rqalpha.events import EVENT
from rqalpha.environment import Environment

assert isinstance(handler, types.FunctionType)
assert isinstance(event_type, EVENT)

env = Environment.get_instance()
env.event_bus.add_listener(event_type, handler)
11 changes: 11 additions & 0 deletions rqalpha/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import datetime
import inspect
import sys
import types
from collections import Iterable
from functools import wraps
from types import FunctionType
Expand Down Expand Up @@ -928,3 +929,13 @@ def get_position(order_book_id, direction):
raise RuntimeError(_("Booking has not been set, please check your broker configuration."))

return booking.get_position(order_book_id, direction)


@export_as_api
@apply_rules(
verify_that('event_type').is_instance_of(EVENT),
verify_that('handler').is_instance_of(types.FunctionType)
)
def subscribe_event(event_type, handler):
env = Environment.get_instance()
env.event_bus.add_listener(event_type, handler)
4 changes: 1 addition & 3 deletions tests/test_s_tick_size.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from rqalpha import subscribe_event, events

SLIPPAGE = 10
price = 0
stock = "000001.XSHE"


def init(context):
context.count = 0
subscribe_event(events.EVENT.TRADE, on_trade)
subscribe_event(EVENT.TRADE, on_trade)


def on_trade(event):
Expand Down

0 comments on commit 02b10b3

Please sign in to comment.