1

我的交易算法有点挣扎。在调用下一个 onBars() 之前,订单似乎无法完成,并且数量变得一团糟。我使用 enterLongLimit() 进入交易,它在完成时调用 onEnterOk() - 但我使用 limitOrder 根据一些技术指标退出部分头寸,这似乎没有调用 onExitOk()。

def onExitOk(self, position):
    print("Exit ok", position.getExitOrder().getExecutionInfo().getDateTime())

def onEnterOk(self, position):
    print("Enter ok", position.getEntryOrder().getExecutionInfo())

def _closePosition(self, price, qty, reason, date):
    print("Closing position with price", price, "and closing qty", qty)
    brk = self.getBroker()
    shares = brk.getShares(self.instrument) * qty
    print("Cash now before sell: ", brk.getCash(self.instrument))
    self.info("Sell BTC %s at %s because %s on %s " % (shares, price, reason, date))
    self.position = self.limitOrder(self.instrument, price, shares*-1)
    print("Cash now after sell: ", brk.getCash(self.instrument))

执行:

平仓价格 746.3 平仓数量 0.5


售前现款:17.423283999999967


售后现款:17.423283999999967

limitOrder 之前和之后的现金是一样的,所以我必须等待事件进来。想法?

4

1 回答 1

0

我遇到了类似的问题,然后经过一番挖掘,我发现只有在使用 enterLong 或 enterShort 方法之一打开仓位时才会触发 onEnter 和 onExit 事件。使用止损、限价或止损限价订单执行的交易不会触发 onEnter 或 onExit 事件,这真是令人遗憾。

这就是源代码中的注释所说的,即:

def onEnterOk(self, position):
    """Override (optional) to get notified when the order submitted to enter a position was filled. The default implementation is empty.
    :param position: A position returned by any of the enterLongXXX or enterShortXXX methods.
    :type position: :class:`pyalgotrade.strategy.position.Position`.
    """
    pass

请参阅策略类代码。

即使您没有收到通知,您的限价单仍应按预期工作。

于 2021-11-28T11:12:57.220 回答