def handle_order_execution(self, order: Order, intrabar: Bar): amount = order.amount - order.executed_amount order.executed_amount = order.amount fee = self.taker_fee if order.limit_price: price = order.limit_price fee = self.maker_fee elif order.stop_price: price = int( order.stop_price * (1 + math.copysign(self.market_slipage_percent, order.amount) / 100) / self.symbol.tickSize) * self.symbol.tickSize else: price = intrabar.open * ( 1 + math.copysign(self.market_slipage_percent, order.amount) / 100) price = min(intrabar.high, max( intrabar.low, price)) # only prices within the bar. might mean less slipage order.executed_price = price self.account.open_position.quantity += amount delta = amount * (price if not self.symbol.isInverse else -1 / price) self.account.open_position.walletBalance -= delta self.account.open_position.walletBalance -= math.fabs(delta) * fee order.active = False order.execution_tstamp = intrabar.tstamp order.final_reason = 'executed' self.account.order_history.append(order) self.account.open_orders.remove(order) logger.debug("executed order " + order.id + " | " + str(self.account.usd_equity) + " " + str(self.account.open_position.quantity))
def handle_order_execution(self, order: Order, intrabar: Bar): amount = order.amount - order.executed_amount order.executed_amount = order.amount fee = self.taker_fee if order.limit_price: price = order.limit_price fee = self.maker_fee elif order.stop_price: price = int( order.stop_price * (1 + math.copysign(self.market_slipage_percent, order.amount) / 100) / self.symbol.tickSize) * self.symbol.tickSize else: price = intrabar.open * ( 1 + math.copysign(self.market_slipage_percent, order.amount) / 100) price = min(intrabar.high, max( intrabar.low, price)) # only prices within the bar. might mean less slipage order.executed_price = price oldAmount = self.account.open_position.quantity if oldAmount != 0: oldavgentry = self.account.open_position.avgEntryPrice if oldAmount * amount > 0: self.account.open_position.avgEntryPrice = ( oldavgentry * oldAmount + price * amount) / (oldAmount + amount) if oldAmount * amount < 0: if abs(oldAmount) < abs(amount): profit = oldAmount * ( (price - oldavgentry) if not self.symbol.isInverse else (-1 / price + 1 / oldavgentry)) self.account.open_position.walletBalance += profit #close current, open new self.account.open_position.avgEntryPrice = price else: #closes the position by "-amount" cause amount is the side and direction of the close profit = -amount * ( (price - oldavgentry) if not self.symbol.isInverse else (-1 / price + 1 / oldavgentry)) self.account.open_position.walletBalance += profit else: self.account.open_position.avgEntryPrice = price self.account.open_position.quantity += amount volume = amount * (price if not self.symbol.isInverse else -1 / price) self.account.open_position.walletBalance -= math.fabs(volume) * fee order.active = False order.execution_tstamp = intrabar.tstamp order.final_reason = 'executed' self.account.order_history.append(order) self.account.open_orders.remove(order) self.logger.debug("executed order %s | %.0f %.2f | %.2f@ %.1f" % (order.id, self.account.usd_equity, self.account.open_position.quantity, order.executed_amount, order.executed_price))