def manage_open_order(self, order, position, bars, to_update, to_cancel, open_positions): # first the modules super().manage_open_order(order, position, bars, to_update, to_cancel, open_positions) # now the channel stuff last_data: Data = self.channel.get_data(bars[2]) data: Data = self.channel.get_data(bars[1]) if data is not None: stopLong = data.longTrail stopShort = data.shortTrail if self.trail_to_swing and \ data.longSwing is not None and data.shortSwing is not None and \ (not self.delayed_swing_trail or (last_data is not None and last_data.longSwing is not None and last_data.shortSwing is not None)): stopLong = max(data.shortSwing, stopLong) stopShort = min(data.longSwing, stopShort) orderType = TradingBot.order_type_from_order_id(order.id) if position is not None and orderType == OrderType.SL: # trail newStop = order.stop_price isLong = position.amount > 0 if self.trail_active: trail = stopLong if isLong else stopShort if (trail - newStop) * position.amount > 0 or \ (self.trail_back and position.initial_stop is not None and (trail - position.initial_stop) * position.amount > 0): newStop = math.floor( trail) if not isLong else math.ceil(trail) if newStop != order.stop_price: order.stop_price = newStop to_update.append(order)
def manage_open_order(self, order, position, bars, to_update, to_cancel, open_positions): super().manage_open_order(order, position, bars, to_update, to_cancel, open_positions) data: Data = self.channel.get_data(bars[1]) if data is None: return orderType = TradingBot.order_type_from_order_id(order.id) # check for triggered but not filled if order.stop_triggered: # clear other side other_id = TradingBot.get_other_direction_id(posId=position.id) if other_id in open_positions.keys(): open_positions[other_id].markForCancel = bars[0].tstamp position.status = PositionStatus.TRIGGERED if not hasattr(position, 'waitingToFillSince'): position.waitingToFillSince = bars[0].tstamp if (bars[0].tstamp - position.waitingToFillSince) > self.bars_till_cancel_triggered * ( bars[0].tstamp - bars[1].tstamp): # cancel position.status = PositionStatus.MISSED position.exit_tstamp = bars[0].tstamp del open_positions[position.id] self.logger.info("canceling not filled position: " + position.id) to_cancel.append(order) if orderType == OrderType.ENTRY and \ (data.longSwing is None or data.shortSwing is None or (self.cancel_on_filter and not self.entries_allowed(bars))): if position.status == PositionStatus.PENDING: # don't delete if triggered self.logger.info("canceling cause channel got invalid: " + position.id) to_cancel.append(order) del open_positions[position.id]
def manage_open_order(self, order, position, bars, to_update, to_cancel, open_positions): # first the modules super().manage_open_order(order, position, bars, to_update, to_cancel, open_positions) # now the swing trail data: Data = self.swings.get_data(bars[1]) if data is not None: stopLong = data.swingLow stopShort = data.swingHigh orderType = TradingBot.order_type_from_order_id(order.id) if position is not None and orderType == OrderType.SL: # trail newStop = order.stop_price isLong = position.amount > 0 trail = stopLong if isLong else stopShort if trail is not None and (trail - newStop) * position.amount > 0: newStop = math.floor(trail) if not isLong else math.ceil( trail) if newStop != order.stop_price: order.stop_price = newStop to_update.append(order)
def manage_open_order(self, order, position, bars, to_update, to_cancel, open_positions): orderType = TradingBot.order_type_from_order_id(order.id) if orderType == OrderType.SL: for module in self.exitModules: module.manage_open_order(order, position, bars, to_update, to_cancel, open_positions)
def update_order(self, order: Order): if self.telegram_bot is not None and TradingBot.order_type_from_order_id( order.id) == OrderType.SL: self.telegram_bot.send_log( "updating (" + self.id + "): " + order.print_info(), order.id) self.exchange.update_order(order) self.exchange.on_tick_callback( True ) ##simulate tick to prevent early updates (need to wait for exchange to update order
def position_got_opened_or_changed(self, position: Position, bars: List[Bar], account: Account, open_positions): other_id = TradingBot.get_other_direction_id(position.id) if other_id in open_positions.keys(): open_positions[other_id].markForCancel = bars[0].tstamp # add stop gotStop = False # safety check needed to not add multiple SL in case of an error gotTp = False for order in account.open_orders: orderType = TradingBot.order_type_from_order_id(order.id) posId = TradingBot.position_id_from_order_id(order.id) if orderType == OrderType.SL and posId == position.id: gotStop = True if abs(order.amount + position.current_open_amount) > self.symbol.lotSize / 2: order.amount = -position.current_open_amount self.order_interface.update_order(order) elif self.tp_fac > 0 and orderType == OrderType.TP and posId == position.id: gotTp = True amount = self.symbol.normalizeSize( -position.current_open_amount + order.executed_amount) if abs(order.amount - amount) > self.symbol.lotSize / 2: order.amount = amount self.order_interface.update_order(order) if not gotStop: order = Order(orderId=TradingBot.generate_order_id( positionId=position.id, type=OrderType.SL), stop=position.initial_stop, amount=-position.amount) self.order_interface.send_order(order) if self.tp_fac > 0 and not gotTp: ref = position.filled_entry - position.initial_stop #tp = position.filled_entry + ref * self.tp_fac data: Data = self.channel.get_data(bars[1]) if order.amount < 0: tp = data.shortTrail else: tp = data.longTrail order = Order(orderId=TradingBot.generate_order_id( positionId=position.id, type=OrderType.TP), limit=tp, amount=-position.amount)
def position_got_opened(self, position: Position, bars: List[Bar], account: Account, open_positions): gotTP = False gotSL = False for order in position.connectedOrders: type = TradingBot.order_type_from_order_id(order.id) if type == OrderType.TP: gotTP = True amount = self.symbol.normalizeSize( -position.currentOpenAmount + order.executed_amount) if abs(order.amount - amount) > self.symbol.lotSize / 2: order.amount = amount self.order_interface.update_order(order) if type == OrderType.SL: gotSL = True amount = self.symbol.normalizeSize(-position.currentOpenAmount) if abs(order.amount - amount) > self.symbol.lotSize / 2: order.amount = amount self.order_interface.update_order(order) if not gotTP: slDiff = position.wanted_entry - position.initial_stop # reverse calc the std at time of signal and use tp factor accordingly tp = self.symbol.normalizePrice( position.wanted_entry + slDiff / (self.sl_factor - self.entry_factor) * (self.entry_factor - self.tp_factor), position.amount > 0) self.order_interface.send_order( Order(orderId=TradingBot.generate_order_id( position.id, OrderType.TP), amount=-position.currentOpenAmount, stop=None, limit=tp)) if not gotSL: order = Order(orderId=TradingBot.generate_order_id( positionId=position.id, type=OrderType.SL), stop=position.initial_stop, amount=-position.currentOpenAmount) self.order_interface.send_order(order) # added temporarily, cause sync with open orders is in the next loop and otherwise the orders vs # position check fails if order not in account.open_orders: # outside world might have already added it account.open_orders.append(order)
def position_got_opened(self, position: Position, bars: List[Bar], account: Account, open_positions): other_id = TradingBot.get_other_direction_id(position.id) if other_id in open_positions.keys(): open_positions[other_id].markForCancel = bars[0].tstamp # add stop gotStop= False # safety check needed to not add multiple SL in case of an error for order in account.open_orders: orderType = TradingBot.order_type_from_order_id(order.id) posId = TradingBot.position_id_from_order_id(order.id) if orderType == OrderType.SL and posId == position.id: gotStop= True break if not gotStop: order = Order(orderId=TradingBot.generate_order_id(positionId=position.id, type=OrderType.SL), stop=position.initial_stop, amount=-position.amount) self.order_interface.send_order(order) # added temporarily, cause sync with open orders is in the next loop and otherwise the orders vs # position check fails if order not in account.open_orders: # outside world might have already added it account.open_orders.append(order)
def update_order(self, order: Order): if self.telegram_bot is not None and TradingBot.order_type_from_order_id( order.id) == OrderType.SL: self.telegram_bot.send_log( "updating (" + self.id + "): " + order.print_info(), order.id) self.exchange.update_order(order)