pyalgotrade退出订单事件(onExitOk)的问题



我的交易算法有点问题。似乎订单不能在下一个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之前和之后的现金是相同的,所以我必须等待事件进入。想法吗?

我有一个类似的问题,然后经过一些挖掘,我发现onEnter和onExit事件只有在使用enterLong或enterShort方法之一打开头寸时才会触发。用止损、限价或止损限价单执行的交易不会触发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

参见策略类代码。

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

最新更新