Exemplo n.º 1
0
    def get_order_status(self, order_id) -> ExchangeResponse:
        """ Get the order status from the exchange"""
        payload = {
            "request": API_BITFINEX_ORDER_STATUS,
            "nonce": nonceGenerator.getNonce(),
            "order_id": order_id
        }

        headers = self.process_payload(payload)
        try:
            response = requests.post(url=API_BITFINEX_ENDPOINT +
                                     API_BITFINEX_ORDER_STATUS,
                                     data={},
                                     headers=headers,
                                     timeout=REQUEST_TIMEOUT)
            response_json = response.json()
        except:
            self.logger.error("Exception during handling exchange response.",
                              exc_info=True)
            return ExchangeResponse(False, exception=traceback.format_exc())

        if "is_live" not in response_json:
            self.bot.logger.error(
                "Failed to get order status for order ID {} . Message: {}".
                format(order_id, response_json["message"]))
            return ExchangeResponse(False,
                                    message=str(response_json["message"]))
        else:
            return ExchangeResponse(True, json=response_json)
Exemplo n.º 2
0
    def cancel_order(self, order_id):
        payload = {
            "request": API_BITFINEX_CANCEL_ORDER,
            "nonce": nonceGenerator.getNonce(),
            "order_id": order_id
        }

        headers = self.process_payload(payload)
        response = requests.post(url=API_BITFINEX_ENDPOINT +
                                 API_BITFINEX_CANCEL_ORDER,
                                 data={},
                                 headers=headers)

        response_json = response.json()

        for _ in range(5):
            status = self.get_order_status(order_id)
            if not status.get("is_cancelled", False):
                time.sleep(1)
            else:
                self.bot.logger.info("Cancelled order: {}".format(order_id))
                return True

        if "is_cancelled" not in response_json:
            self.bot.logger.error(
                "Failed to get order status for order ID {} . Message: {}".
                format(order_id, response_json["message"]))
        elif not response_json["is_cancelled"]:
            self.bot.logger.error(
                "Order not cancelled. Order ID {} . Json: {}".format(
                    order_id, str(response_json)))
        else:
            return True
Exemplo n.º 3
0
    def place_limit_order(self, side, amount=0.00, price=0.00):
        side = side.lower()
        assert (side in ("buy", "sell"))
        #
        payload = {
            'request': API_BITFINEX_NEW_ORDER,
            'nonce': nonceGenerator.getNonce(),
            'symbol': self.bot.symbol,
            'amount': str(amount),
            'price': str(price),
            'exchange': 'bitfinex',
            'type': 'exchange limit',
            'side': side
        }

        headers = self.process_payload(payload)
        response = requests.post(url=API_BITFINEX_ENDPOINT +
                                 API_BITFINEX_NEW_ORDER,
                                 data={},
                                 headers=headers)
        response_json = response.json()

        if 'order_id' not in response_json:
            self.bot.logger.error(
                "Failed to place [%s] limit order: [%s]" %
                (payload['side'], str(response_json["message"])))
            return {"successful": False}
        else:
            order_id = response_json["order_id"]
            self.bot.logger.info(
                "Successfully placed limit order: {} {} {} @ {}. Order ID {}:".
                format(side, amount, self.bot.symbol, price, order_id))

            return {"successful": True, "order_id": order_id}
Exemplo n.º 4
0
    def place_sell_limit_order(self, amount=0.00, price=0.00):
        #
        payload_object = {
            'request': API_BITFINEX_ORDER,
            'nonce': nonceGenerator.getNonce(),
            'symbol': self.symbol,
            'amount': str(amount),
            'price': str(price),
            'exchange': 'bitfinex',
            'side': 'sell',
            'type': 'exchange limit'
        }
        payload_text = json.dumps(payload_object)
        payload_base64_encoded = base64.b64encode(str.encode(payload_text))
        headers = {
            'X-BFX-APIKEY': BITFINEX_API_KEY,
            'X-BFX-PAYLOAD': payload_base64_encoded,
            'X-BFX-SIGNATURE': self.__get_signature(payload_base64_encoded)
        }

        response = requests.post(url=API_BITFINEX_ENDPOINT +
                                 API_BITFINEX_ORDER,
                                 data={},
                                 headers=headers)
        response_json = response.json()

        if 'order_id' not in response_json:
            self.logger.error("Failed to place [SELL] limit order: [" +
                              response_json["message"] + "]")
            return {"successful": False}
        else:
            self.logger.info("Successfully placed [SELL] limit order: [" +
                             str(response_json) + "]")
            return {"successful": True, "order_id": response_json["order_id"]}
Exemplo n.º 5
0
    def place_limit_order(self,
                          side,
                          amount=0.00,
                          price=0.00) -> ExchangeResponse:
        side = side.lower()
        assert (side in ("buy", "sell"))
        #
        payload = {
            'request': API_BITFINEX_NEW_ORDER,
            'nonce': nonceGenerator.getNonce(),
            'symbol': self.bot.symbol,
            'amount': no_exp_str(amount),
            'price': no_exp_str(price),
            'exchange': 'bitfinex',
            'type': 'exchange limit',
            'side': side
        }

        headers = self.process_payload(payload)

        try:
            response = requests.post(url=API_BITFINEX_ENDPOINT +
                                     API_BITFINEX_NEW_ORDER,
                                     data={},
                                     headers=headers,
                                     timeout=REQUEST_TIMEOUT)
            response_json = response.json()
        except:
            self.logger.error("Exception during handling exchange response.",
                              exc_info=True)
            return ExchangeResponse(False, exception=traceback.format_exc())

        if 'order_id' not in response_json:
            self.bot.logger.error(
                "Failed to place [%s] limit order: [%s]" %
                (payload['side'], str(response_json["message"])))
            return ExchangeResponse(False,
                                    message=str(response_json["message"]))

        else:
            order_id = response_json["order_id"]
            self.bot.logger.info(
                "Successfully placed limit order: {} {} {} @ {}. Order ID {}:".
                format(side, amount, self.bot.symbol, price, order_id))

            return ExchangeResponse(True,
                                    order_id=order_id,
                                    json=response_json)
Exemplo n.º 6
0
    def get_key_info(self) -> ExchangeResponse:
        payload = {
            "request": API_BITFINEX_KEY_INFO,
            "nonce": nonceGenerator.getNonce(),
        }

        headers = self.process_payload(payload)
        try:
            response = requests.post(url=API_BITFINEX_ENDPOINT +
                                     API_BITFINEX_KEY_INFO,
                                     data={},
                                     headers=headers,
                                     timeout=REQUEST_TIMEOUT)
            response_json = response.json()
        except:
            self.logger.error("Exception during handling exchange response.",
                              exc_info=True)
            return ExchangeResponse(False, exception=traceback.format_exc())

        return ExchangeResponse(True, json=response_json)
Exemplo n.º 7
0
    def cancel_order(self, order_id) -> ExchangeResponse:
        payload = {
            "request": API_BITFINEX_CANCEL_ORDER,
            "nonce": nonceGenerator.getNonce(),
            "order_id": order_id
        }

        headers = self.process_payload(payload)
        try:
            response = requests.post(url=API_BITFINEX_ENDPOINT +
                                     API_BITFINEX_CANCEL_ORDER,
                                     data={},
                                     headers=headers,
                                     timeout=REQUEST_TIMEOUT)
            response_json = response.json()
        except:
            self.logger.error("Exception during handling exchange response.",
                              exc_info=True)
            return ExchangeResponse(False, exception=traceback.format_exc())

        for _ in range(5):
            status = self.get_order_status(order_id)
            if not status.success or status.json.get("is_cancelled", False):
                time.sleep(5)
            else:
                self.bot.logger.info("Cancelled order: {}".format(order_id))
                return ExchangeResponse(True,
                                        order_id=order_id,
                                        json=response_json)

        if "is_cancelled" not in response_json:
            self.bot.logger.error(
                "Failed to get order status for order ID {} . Message: {}".
                format(order_id, response_json["message"]))
        elif not response_json["is_cancelled"]:
            self.bot.logger.error(
                "Order not cancelled. Order ID {} . Json: {}".format(
                    order_id, str(response_json)))

        return ExchangeResponse(False, order_id=order_id, json=response_json)
Exemplo n.º 8
0
    def place_stop_order(self,
                         amount: float,
                         stop_value: float = None) -> ExchangeResponse:

        payload = {
            'request': API_BITFINEX_NEW_ORDER,
            'nonce': nonceGenerator.getNonce(),
            'symbol': self.bot.symbol,
            'amount': no_exp_str(amount),
            'price': no_exp_str(stop_value),
            'exchange': 'bitfinex',
            'type': 'exchange stop',
            'side': "sell",
        }

        headers = self.process_payload(payload)

        try:
            response = requests.post(url=API_BITFINEX_ENDPOINT +
                                     API_BITFINEX_NEW_ORDER,
                                     data={},
                                     headers=headers,
                                     timeout=REQUEST_TIMEOUT)
            response_json = response.json()
        except:
            self.logger.error("Exception during handling exchange response.",
                              exc_info=True)
            return ExchangeResponse(False, exception=traceback.format_exc())

        if 'order_id' not in response_json:
            self.bot.logger.error("Failed to place stop order: [%s]" %
                                  (str(response_json["message"])))
            return ExchangeResponse(False,
                                    message=str(response_json["message"]))
        else:
            order_id = response_json["order_id"]
            self.bot.logger.info(f"Successfully placed stop order")
            return ExchangeResponse(True,
                                    order_id=order_id,
                                    json=response_json)
Exemplo n.º 9
0
    def get_order_status(self, order_id):
        """ Get the order status from the exchange"""
        payload = {
            "request": API_BITFINEX_ORDER_STATUS,
            "nonce": nonceGenerator.getNonce(),
            "order_id": order_id
        }

        headers = self.process_payload(payload)

        response = requests.post(url=API_BITFINEX_ENDPOINT +
                                 API_BITFINEX_ORDER_STATUS,
                                 data={},
                                 headers=headers)

        response_json = response.json()

        if "is_live" not in response_json:
            self.bot.logger.error(
                "Failed to get order status for order ID {} . Message: {}".
                format(order_id, response_json["message"]))
        else:
            return response_json