vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 52
声望: 0

下面这个generate_trade_pairs应该是生成交易对的,没完全看明白,程序是怎么判断两个交易是一对的。如果有加仓,开和平仓并不是相邻的,显示会不会有BUG。

因为我编写的策略有加仓,感觉显示出来的交易对有问题

\vnpy\app\cta_backtester\ui\widget.py

def generate_trade_pairs(trades: list) -> list:
""""""
long_trades = []
short_trades = []
trade_pairs = []

for trade in trades:
    trade = copy(trade)

    if trade.direction == Direction.LONG:
        same_direction = long_trades
        opposite_direction = short_trades
    else:
        same_direction = short_trades
        opposite_direction = long_trades

    while trade.volume and opposite_direction:
        open_trade = opposite_direction[0]

        close_volume = min(open_trade.volume, trade.volume)
        d = {
            "open_dt": open_trade.datetime,
            "open_price": open_trade.price,
            "close_dt": trade.datetime,
            "close_price": trade.price,
            "direction": open_trade.direction,
            "volume": close_volume,
        }
        trade_pairs.append(d)

        open_trade.volume -= close_volume
        if not open_trade.volume:
            opposite_direction.pop(0)

        trade.volume -= close_volume

    if trade.volume:
        same_direction.append(trade)

return trade_pairs
Member
avatar
加入于:
帖子: 52
声望: 0

这个opposite_direction一直没有查到有赋值的地方,opposite_direction = short_trades或者opposite_direction = long_trades,short_trades,long_trades也是空集合啊,后面怎么基于它来操作的,没看明白

Member
avatar
加入于:
帖子: 52
声望: 0

应该是在这里重新指向和赋值的 ,不过感觉有点问题,如果有加仓的话,开平仓不相邻,形成的交易对好像有问题
if trade.direction == Direction.LONG:
same_direction = long_trades
opposite_direction = short_trades
else:
same_direction = short_trades
opposite_direction = long_trades

Member
avatar
加入于:
帖子: 2013
声望: 133

vnpy的规则是FIFO,逐笔对冲配对的,即使加仓结果也能保证正确

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号