Exemple #1
0
    def internal_send_order(self, order: Order):
        order_type = "Market"
        if order.limit_price is not None:
            order_type = "Limit"
        result = None
        if order.stop_price is not None:
            # conditional order
            base_side = 1 if order.amount < 0 else -1  # buy stops are triggered when price goes higher (so it is
            # considered lower before)
            result = self._execute(
                self.bybit.Conditional.Conditional_new(
                    side=("Buy" if order.amount > 0 else "Sell"),
                    symbol=self.symbol,
                    order_type=order_type,
                    qty=abs(order.amount),
                    price=order.limit_price,
                    stop_px=order.stop_price,
                    order_link_id=order.id,
                    base_price=order.stop_price + base_side,
                    time_in_force="GoodTillCancel"))
            if result is not None:
                order.exchange_id = result['stop_order_id']

        else:
            result = self._execute(
                self.bybit.Order.Order_newV2(
                    side=("Buy" if order.amount > 0 else "Sell"),
                    symbol=self.symbol,
                    order_type=order_type,
                    qty=abs(order.amount),
                    price=order.limit_price,
                    order_link_id=order.id,
                    time_in_force="GoodTillCancel"))
            if result is not None:
                order.exchange_id = result['order_id']
    def internal_send_order(self, order: Order):
        order_type = "Market"
        if order.limit_price is not None:
            order_type = "Limit"
        if order.stop_price is not None and (self.last - order.stop_price) * order.amount >= 0:
            order.stop_price = None  # already triggered

        orderType = TradingBot.order_type_from_order_id(order.id)

        if order.stop_price is not None:
            # conditional order
            base_side = self.symbol_info.tickSize * (
                1 if order.amount < 0 else -1)  # buy stops are triggered when price goes higher (so it is
            # considered lower before)
            normalizedStop = self.symbol_info.normalizePrice(order.stop_price, order.amount > 0)
            result = self._execute(self.bybit.LinearConditional.LinearConditional_new(side=("Buy" if order.amount > 0 else "Sell"),
                                                                          symbol=self.symbol,
                                                                          order_type=order_type,
                                                                          qty=strOrNone(
                                                                              self.symbol_info.normalizeSize(
                                                                                  abs(order.amount))),
                                                                          price=strOrNone(
                                                                              self.symbol_info.normalizePrice(
                                                                                  order.limit_price, order.amount < 0)),
                                                                          stop_px=strOrNone(normalizedStop),
                                                                          order_link_id=order.id,
                                                                          base_price=strOrNone(round(
                                                                              normalizedStop + base_side,
                                                                              self.symbol_info.pricePrecision)),
                                                                          time_in_force="GoodTillCancel",
                                                                          reduce_only= orderType != OrderType.ENTRY,
                                                                          close_on_trigger= orderType != OrderType.ENTRY))
            if result is not None:
                order.exchange_id = result['stop_order_id']

        else:
            result = self._execute(self.bybit.LinearOrder.LinearOrder_new(side=("Buy" if order.amount > 0 else "Sell"),
                                                              symbol=self.symbol,
                                                              order_type=order_type,
                                                              qty=strOrNone(
                                                                              self.symbol_info.normalizeSize(
                                                                                  abs(order.amount))),
                                                              price=strOrNone(
                                                                  self.symbol_info.normalizePrice(order.limit_price,
                                                                                                  order.amount < 0)),
                                                              order_link_id=order.id,
                                                              time_in_force="GoodTillCancel",
                                                              reduce_only= orderType != OrderType.ENTRY,
                                                              close_on_trigger= orderType != OrderType.ENTRY))
            if result is not None:
                order.exchange_id = result['order_id']
Exemple #3
0
 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 internal_send_order(self, order: Order):
        order_type = "Market"
        if order.stop_price is not None and (
                self.last - order.stop_price) * order.amount >= 0:
            order.stop_price = None  # already triggered
        if order.limit_price is not None:
            if order.stop_price is not None:
                order_type = "StopLimit"
            else:
                order_type = "Limit"
        elif order.stop_price is not None:
            order_type = "Stop"
            if (order.stop_price >= self.last and order.amount < 0) or \
                    (order.stop_price <= self.last and order.amount > 0):  # prevent error of "would trigger immediatly"
                order_type = "Market"

        params = dict(symbol=self.symbol,
                      clOrdID=order.id,
                      side="Buy" if order.amount > 0 else "Sell",
                      orderQty=abs(order.amount),
                      ordType=order_type,
                      stopPxEp=self.scale_price(order.stop_price),
                      priceEp=self.scale_price(order.limit_price),
                      triggerType="ByLastPrice"
                      if order.stop_price is not None else None)
        result = self.client.place_order(params)
        if "data" in result.keys() and "orderID" in result["data"]["orderID"]:
            order.exchange_id = result["data"]["orderID"]
Exemple #5
0
    def internal_send_order(self, order: Order):
        if order.limit_price is not None:
            order.limit_price = round(order.limit_price, self.symbol_object.pricePrecision)
            if order.stop_price is not None:
                order.stop_price = round(order.stop_price, self.symbol_object.pricePrecision)
                order_type = OrderType.STOP
            else:
                order_type = OrderType.LIMIT
        elif order.stop_price is not None:
            order.stop_price = round(order.stop_price, self.symbol_object.pricePrecision)
            order_type = OrderType.STOP_MARKET
        else:
            order_type = OrderType.MARKET

        order.amount = round(order.amount, self.symbol_object.quantityPrecision)
        quantityFormat = "{:." + str(self.symbol_object.quantityPrecision) + "f}"
        priceFormat = "{:." + str(self.symbol_object.pricePrecision) + "f}"
        # yes have to send the price and quantity in as str (although it wants float) cause otherwise it converts it
        # inernally and that sometimes f**k up the precision (0.023 -> 0.02299999999)
        resultOrder: binance_f.model.Order = self.client.post_order(
            symbol=self.symbol,
            side=OrderSide.BUY if order.amount > 0 else OrderSide.SELL,
            ordertype=order_type,
            timeInForce=TimeInForce.GTC if order_type in [OrderType.LIMIT, OrderType.STOP] else None,
            quantity=quantityFormat.format(abs(order.amount)),
            price=priceFormat.format(order.limit_price) if order.limit_price is not None else None,
            stopPrice=priceFormat.format(order.stop_price) if order.stop_price is not None else None,
            newClientOrderId=order.id)
        order.exchange_id = resultOrder.orderId
    def internal_send_order(self, order: Order):
        order_type = "Market"
        if order.limit_price is not None:
            order_type = "Limit"
        if order.stop_price is not None and (
                self.last - order.stop_price) * order.amount >= 0:
            order.stop_price = None  # already triggered

        if order.stop_price is not None:
            # conditional order
            base_side = self.symbol_info.tickSize * (
                1 if order.amount < 0 else -1
            )  # buy stops are triggered when price goes higher (so it is
            # considered lower before)
            normalizedStop = self.symbol_info.normalizePrice(
                order.stop_price, order.amount > 0)
            result = self.handle_result(
                lambda: self.pybit.place_conditional_order(
                    side=("Buy" if order.amount > 0 else "Sell"),
                    symbol=self.symbol,
                    order_type=order_type,
                    qty=strOrNone(int(abs(order.amount))),
                    price=strOrNone(
                        self.symbol_info.normalizePrice(
                            order.limit_price, order.amount < 0)),
                    stop_px=strOrNone(normalizedStop),
                    order_link_id=order.id,
                    base_price=strOrNone(
                        round(normalizedStop + base_side, self.symbol_info.
                              pricePrecision)),
                    time_in_force="GoodTillCancel"))
            if result is not None:
                order.exchange_id = result['stop_order_id']

        else:
            result = self.handle_result(lambda: self.pybit.place_active_order(
                side=("Buy" if order.amount > 0 else "Sell"),
                symbol=self.symbol,
                order_type=order_type,
                qty=strOrNone(int(abs(order.amount))),
                price=strOrNone(
                    self.symbol_info.normalizePrice(order.limit_price, order.
                                                    amount < 0)),
                order_link_id=order.id,
                time_in_force="GoodTillCancel"))
            if result is not None:
                order.exchange_id = result['order_id']
 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 convertOrder(self, apiOrder: binance_f.model.Order) -> Order:
     direction = 1 if apiOrder.side == OrderSide.BUY else -1
     order = Order(orderId=apiOrder.clientOrderId,
                   amount=apiOrder.origQty * direction,
                   limit=apiOrder.price,
                   stop=apiOrder.stopPrice)
     order.executed_amount = apiOrder.executedQty * direction
     order.executed_price = apiOrder.avgPrice
     order.active = apiOrder.status in ["NEW", "PARTIALLY_FILLED"]
     order.exchange_id = apiOrder.orderId
     return order
Exemple #9
0
    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
Exemple #10
0
    def internal_send_order(self, order: Order):
        if order.limit_price is not None:
            if order.stop_price is not None:
                order_type = OrderType.STOP
            else:
                order_type = OrderType.LIMIT
        else:
            order_type = OrderType.STOP_MARKET

        resultOrder: binance_f.model.Order = self.client.post_order(
            symbol=self.symbol,
            side=OrderSide.BUY if order.amount > 0 else OrderSide.SELL,
            ordertype=order_type,
            timeInForce=TimeInForce.GTC,
            quantity=abs(order.amount),
            price=order.limit_price,
            stopPrice=order.stop_price,
            newClientOrderId=order.id)
        order.exchange_id = resultOrder.orderId