Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

运行源代码中的test.py代码,策略里面的init,handle_bar方法没有调用 #70

Closed
yushaorong opened this issue Mar 23, 2017 · 2 comments

Comments

@yushaorong
Copy link

yushaorong commented Mar 23, 2017

在test_api()方法里单独调用一行代码

run(future_api_config, test_buy_open_code_new)

运行到main.py文件中 run() 方法里面

scope = env.strategy_loader.load(env.config.base.strategy_file if source_code is None else source_code, scope)

这条语句运行完成后,策略代码没有加载到scope里面,导致 user_strategy = Strategy(env.event_bus, scope, ucontext) 初始化的时候找不到 init, handle_bar 方法。具体策略:

def test_buy_open():
    from rqalpha.api import buy_open, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE
    def init(context):
        print "test_buy_open ladkfaldsfkjdsalfjldsajf1111111111111111"
        context.f1 = 'P88'
        context.amount = 1
        # context.marin_rate = 10
        subscribe(context.f1)
        context.order_count = 0
        context.order = None

    def handle_bar(context, bar_dict):
        order_id = buy_open(context.f1, 1)
        order = get_order(order_id)
        assert order.order_book_id == context.f1
        assert order.quantity == 1
        assert order.status == ORDER_STATUS.ACTIVE
        assert order.unfilled_quantity == 1
        assert order.unfilled_quantity + order.filled_quantity == order.quantity
        assert order.side == SIDE.BUY
        assert order.position_effect == POSITION_EFFECT.OPEN
test_buy_open_code_new = "".join(inspect.getsourcelines(test_buy_open)[0])

补充:感觉是strategy_loader_help.py中

def compile_strategy(source_code, strategy, scope):
    try:
        code = compile(source_code, strategy, 'exec')
        exec_(code, scope)
        return scope
    except Exception as e:
        pass

.
.
.
exec_(code, scope)这个执行的时候没有加载成功。

补充说明:

future_api_config = {
    "base": {
        "strategy_type": "future",
        "start_date": "2016-03-07",
        "end_date": "2016-03-08",
        "frequency": "1d",
        "matching_type": "next_bar",
        "future_starting_cash": 10000000000,
        "strategy_file": 'rqalpha/examples/buy_and_hold.py'
    },
    "extra": {
        "log_level": "error",
    },
}

在这里面加上"strategy_file": 'rqalpha/examples/buy_and_hold.py'这种形式,不传代码块是可以运行init,handle_bar方法的

@wh1100717
Copy link
Member

@yushaorong 在策略文件中是直接定义 init handle_bar 函数的,你定义的是 test_buy_open 函数,RQAlpha 在读取到源码的时候只是执行了这个函数的定义,因此实际上相当于在RQAlpha的scope中病没有 inithandle_bar 的定义,从而出现你描述的对应的方法没有执行的问题。

你在函数里面定义 inithandle_bar 是没有用的。

@wh1100717
Copy link
Member

通过 inspect.getsourcelines(test_buy_opne)[0] 获取的是如下代码:

def test_buy_open():
    from rqalpha.api import buy_open, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE
    def init(context):
        print("test_buy_open ladkfaldsfkjdsalfjldsajf1111111111111111")
        context.f1 = 'P88'
        context.amount = 1
        # context.marin_rate = 10
        subscribe(context.f1)
        context.order_count = 0
        context.order = None

    def handle_bar(context, bar_dict):
        order_id = buy_open(context.f1, 1)
        order = get_order(order_id)
        assert order.order_book_id == context.f1
        assert order.quantity == 1
        assert order.status == ORDER_STATUS.ACTIVE
        assert order.unfilled_quantity == 1
        assert order.unfilled_quantity + order.filled_quantity == order.quantity
        assert order.side == SIDE.BUY
        assert order.position_effect == POSITION_EFFECT.OPEN

而 RQAlpha 需要的是如下代码:

from rqalpha.api import buy_open, subscribe, get_order, ORDER_STATUS, POSITION_EFFECT, SIDE
def init(context):
    print("test_buy_open ladkfaldsfkjdsalfjldsajf1111111111111111")
    context.f1 = 'P88'
    context.amount = 1
    # context.marin_rate = 10
    subscribe(context.f1)
    context.order_count = 0
    context.order = None

def handle_bar(context, bar_dict):
    order_id = buy_open(context.f1, 1)
    order = get_order(order_id)
    assert order.order_book_id == context.f1
    assert order.quantity == 1
    assert order.status == ORDER_STATUS.ACTIVE
    assert order.unfilled_quantity == 1
    assert order.unfilled_quantity + order.filled_quantity == order.quantity
    assert order.side == SIDE.BUY
    assert order.position_effect == POSITION_EFFECT.OPEN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants