def orderDictToOrder(o): sideMulti = 1 if o["side"] == "Buy" else -1 ext = o['ext_fields'] if 'ext_fields' in o.keys() else None stop = o['trigger_price'] if 'trigger_price' in o.keys() else None if stop is None: stop = o['stop_px'] if 'stop_px' in o.keys() else None if stop is None and ext is not None and 'trigger_price' in ext.keys(): stop = ext['trigger_price'] order = Order( orderId=o["order_link_id"], stop=float(stop) if stop is not None else None, limit=float(o["price"]) if o['order_type'] == 'Limit' else None, amount=float(o["qty"] * sideMulti)) if "order_status" in o.keys(): order.stop_triggered = o[ "order_status"] == "New" and stop is not None order.active = o['order_status'] == 'New' or o[ 'order_status'] == 'Untriggered' elif "stop_order_status" in o.keys(): order.stop_triggered = o["stop_order_status"] == 'Triggered' or o[ 'stop_order_status'] == 'Active' order.active = o['stop_order_status'] == 'Triggered' or o[ 'stop_order_status'] == 'Untriggered' exec = o['cum_exec_qty'] if 'cum_exec_qty' in o.keys() else 0 order.executed_amount = float(exec) * sideMulti order.tstamp = parse_utc_timestamp(o['timestamp'] if 'timestamp' in o.keys() else o['created_at']) order.exchange_id = o["order_id"] if 'order_id' in o.keys( ) else o['stop_order_id'] order.executed_price = None if 'cum_exec_value' in o.keys() and 'cum_exec_qty' in o.keys( ) and float(o['cum_exec_value']) != 0: order.executed_price = o['cum_exec_qty'] / float( o["cum_exec_value"]) # cause of inverse return order
def send_order(self, order: Order): if order.amount == 0: self.logger.error("trying to send order without amount") return order.tstamp = self.bars[0].tstamp if order not in self.account.open_orders: # bot might add it himself temporarily. self.account.open_orders.append(order) self.exchange.send_order(order)
def update_order(self, order: Order): for existing_order in self.account.open_orders: if existing_order.id == order.id: self.account.open_orders.remove(existing_order) self.account.open_orders.append(order) order.tstamp = self.current_bars[0].last_tick_tstamp self.logger.debug("updated order %s" % (order.print_info())) break
def orderDictToOrder(self, o) -> Order: """ { "bizError": 0, "orderID": "9cb95282-7840-42d6-9768-ab8901385a67", "clOrdID": "7eaa9987-928c-652e-cc6a-82fc35641706", "symbol": "BTCUSD", "side": "Buy", "actionTimeNs": 1580533011677666800, "transactTimeNs": 1580533011677666800, "orderType": null, "priceEp": 84000000, "price": 8400, "orderQty": 1, "displayQty": 1, "timeInForce": null, "reduceOnly": false, "stopPxEp": 0, "closedPnlEv": 0, "closedPnl": 0, "closedSize": 0, "cumQty": 0, "cumValueEv": 0, "cumValue": 0, "leavesQty": 0, "leavesValueEv": 0, "leavesValue": 0, "stopPx": 0, "stopDirection": "Falling", "ordStatus": "Untriggered" }, """ sideMult = -1 if o['side'] == Client.SIDE_SELL else 1 stop = self.noneIfZero( o['stopPx']) if 'stopPx' in o else self.noneIfZero( o['stopPxEp'], True) price = self.noneIfZero( o['price']) if 'price' in o else self.noneIfZero( o['priceEp'], True) order = Order(orderId=o['clOrdID'], stop=stop, limit=price, amount=o['orderQty'] * sideMult) order.exchange_id = o['orderID'] order.tstamp = o['actionTimeNs'] / 1000000000 order.active = o['ordStatus'] in [ Client.ORDER_STATUS_NEW, Client.ORDER_STATUS_UNTRIGGERED, Client.ORDER_STATUS_TRIGGERED ] order.executed_amount = o['cumQty'] * sideMult val = o['cumValue'] if 'cumValue' in o else o[ 'cumValueEv'] / self.valueScale order.executed_price = o['cumQty'] / val if val != 0 else 0 if order.executed_amount != 0: order.execution_tstamp = o['transactTimeNs'] / 1000000000 order.stop_triggered = order.stop_price is not None and o[ 'ordStatus'] == Client.ORDER_STATUS_TRIGGERED return order
def send_order(self, order: Order): # check if order is val if order.amount == 0: self.logger.error("trying to send order without amount") return self.logger.debug("added order %s" % (order.print_info())) order.tstamp = self.current_bars[0].tstamp if order not in self.account.open_orders: # bot might add it himself temporarily. self.account.open_orders.append(order)
def send_order(self, order: Order): if order.amount == 0: self.logger.error("trying to send order without amount") return if self.telegram_bot is not None: self.telegram_bot.send_log( "Sending (" + self.id + "): " + order.print_info(), order.id) order.tstamp = self.bars[0].tstamp if order not in self.account.open_orders: # bot might add it himself temporarily. self.account.open_orders.append(order) self.exchange.send_order(order)
def get_orders(self) -> List[Order]: mexOrders = self.bitmex.open_orders() result: List[Order] = [] for o in mexOrders: sideMulti = 1 if o["side"] == "Buy" else -1 order = Order(orderId=o["clOrdID"], stop=o["stopPx"], limit=o["price"], amount=o["orderQty"] * sideMulti) order.stop_triggered = o["triggered"] == "StopOrderTriggered" order.executed_amount = (o["cumQty"]) * sideMulti order.tstamp = parse_utc_timestamp(o['timestamp']) order.execution_tstamp = order.tstamp order.active = o['ordStatus'] == 'New' order.exchange_id = o["orderID"] order.executed_price = o["avgPx"] result.append(order) return result
def send_order(self, order: Order): # check if order is val if order.amount == 0: self.logger.error("trying to send order without amount") return [posId, order_type] = TradingBot.position_id_and_type_from_order_id(order.id) if order_type == OrderType.ENTRY: [unused, direction] = TradingBot.split_pos_Id(posId) if direction == PositionDirection.LONG and order.amount < 0: self.logger.error("sending long entry with negative amount") if direction == PositionDirection.SHORT and order.amount > 0: self.logger.error("sending short entry with positive amount") self.logger.debug("added order %s" % (order.print_info())) order.tstamp = self.current_bars[0].tstamp if order not in self.account.open_orders: # bot might add it himself temporarily. self.account.open_orders.append(order)