Struggelig a bit with my trading algo. It seems that the orders cant finish before the next onBars() is called, and the quantities become a mess. Im using the enterLongLimit() to enter a trade, which calls onEnterOk() when it finishes - but im using limitOrder to exit parts of a position based on some technical indicator, and that doesnt seem to call 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))
Execution:
Closing position with price 746.3 and closing qty 0.5
Cash now before sell: 17.423283999999967
Cash now after sell: 17.423283999999967
The cash before and after the limitOrder is the same, so i have to wait for the event to come in. Ideas?