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

平仓时,有时会出现连续两笔平仓单都能成交的情况 #94

Closed
wanghc02 opened this issue Apr 16, 2017 · 5 comments
Closed
Assignees

Comments

@wanghc02
Copy link
Contributor

测试场景: 经典的dualthrust策略,IF99合约,分钟线,2016-5-09至2016-5-18,每笔交易1手
成交记录在2016-5-11 11:00与11:01 出现了两次平仓成交,如下图:
image
查询仓位发现,在11:01处buy_quantity与sell_quantity都是1,即11:00下单的平仓单未能成交。
那么,就存在两个问题:
1、市价单不是涨跌停挂单吗,数量都是1手,未能成交的原因是什么?
2、为什么会出现两个成交的平仓记录?

策略代码附后:

from rqalpha.api import *
from rqalpha.model.position.future_position import FuturePosition

def init(context):
    context.k = 0.7
    context.window=1
    context.lots = 1
    context.s = 'IF99'
    context.lastDay=None
    context.open = 0
    context.range = 0
    context.isOpenRecord=False
    context.flag=0
    subscribe(context.s)

def before_trading(context):
    daybar=history_bars(context.s, context.window, '1d', ['high','low','close'])
    HH=max(daybar['high'])
    LL=min(daybar['low'])
    HC=max(daybar['close'])
    LC =min(daybar['close'])
    context.range=max(HH-LC,HC-LL)
    context.isOpenRecord = False


def handle_bar(context, bar_dict):
    bar=bar_dict[context.s]
    if context.isOpenRecord == False:
        context.isOpenRecord = True
        context.open=bar.open

    if bar.close > context.open + context.range * context.k:
        context.flag = 1
    elif bar.close < context.open - context.range * context.k:
        context.flag = -1


    q=context.portfolio.positions[context.s]
    if q is None:
        q=FuturePosition(context.s)
    if context.flag==1:
        if q.sell_quantity>0:
            buy_close(context.s, q.sell_quantity)
        else:
            pass
        if q.buy_quantity>0:
            pass
        else:
            buy_open(context.s,context.lots)
    elif context.flag==-1:
        if q.buy_quantity>0:
            sell_close(context.s, q.buy_quantity)
        else:
            pass
        if q.sell_quantity>0:
            pass
        else:
            sell_open(context.s,context.lots)
    else:
        if q.buy_quantity>0:
            sell_close(context.s, q.buy_quantity)
        else:
            pass
        if q.sell_quantity>0:
            buy_close(context.s, q.sell_quantity)
        else:
            pass
@handsomu
Copy link
Contributor

开源版本并未提供期货分钟线数据。不知道是否是在替换数据源中出现了问题。
本身平仓单是会在position_validator中进行验证的。您可以查看下代码,或者提供更多的信息(比如是否进行了MOD的二次开发等等),方便我们进行定位。
https://github.com/ricequant/rqalpha/blob/master/rqalpha/mod/rqalpha_mod_sys_risk/position_validator.py

image

@wanghc02
Copy link
Contributor Author

是的,我用mod扩展了本地1分钟的数据源,测试中用的数据是从ricequant研究中导出的一段数据,所有字段应该都是完全一致的。你们可以用ricequant的数据测试下能不能重现这个交易记录。我也再检查一下代码。

@wanghc02
Copy link
Contributor Author

@handsomu
经检查,future_position.py中的__close_holding方法有bug,第386行应改为:
consumed_quantity = old_quantity
image

@wanghc02
Copy link
Contributor Author

还有409行,同理

@wh1100717
Copy link
Member

@wanghc02 确实是个Regression Bug,Ricequant 线上还没有更新最新版本的RQAlpha 所以并没有这个问题。

line 386/409 应该修改为 consumed_quantity = old_quantity

如果可以的话,希望您提交一个Pull Request来解决这个Bug 我们会尽快 Merge PR

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

3 participants