Exemplo n.º 1
0
 async def get_open_orders(self) -> List[OpenOrder]:
     result = await self._api_request("GET",
                                      Constants.ENDPOINT["USER_ORDERS"],
                                      is_auth_required=True,
                                      limit_id=Constants.RL_ID_USER_ORDERS)
     ret_val = []
     for order in result:
         if order["state"] in Constants.ORDER_STATES['DONE']:
             # Skip done orders
             continue
         exchange_order_id = str(order["id"])
         client_order_id = order["client_id"]
         if order["ord_type"] != OrderType.LIMIT.name.lower():
             self.logger().info(
                 f"Unsupported order type found: {order['type']}")
             # Skip and report non-limit orders
             continue
         ret_val.append(
             OpenOrder(client_order_id=client_order_id,
                       trading_pair=convert_from_exchange_trading_pair(
                           order["market"]),
                       price=Decimal(str(order["price"])),
                       amount=Decimal(str(order["origin_volume"])),
                       executed_amount=Decimal(str(
                           order["executed_volume"])),
                       status=order["state"],
                       order_type=OrderType.LIMIT,
                       is_buy=True if order["side"].lower()
                       == TradeType.BUY.name.lower() else False,
                       time=str_date_to_ts(order["created_at"]),
                       exchange_order_id=exchange_order_id))
     return ret_val
Exemplo n.º 2
0
 def _format_trading_rules(self, symbols_info: Dict[str, Any]) -> Dict[str, TradingRule]:
     """
     Converts json API response into a dictionary of trading rules.
     :param symbols_info: The json API response
     :return A dictionary of trading rules.
     Response Example:
     [
         {
             id: "btcusdt",
             name: "BTC/USDT",
             base_unit: "btc",
             quote_unit: "usdt",
             min_price: "0.01",
             max_price: "200000.0",
             min_amount: "0.00000001",
             amount_precision: 8,
             price_precision: 2,
             state: "enabled"
         }
     ]
     """
     result = {}
     for rule in symbols_info:
         try:
             trading_pair = convert_from_exchange_trading_pair(rule["id"])
             min_amount = Decimal(rule["min_amount"])
             min_notional = min(Decimal(rule["min_price"]) * min_amount, Decimal("0.00000001"))
             result[trading_pair] = TradingRule(trading_pair,
                                                min_order_size=min_amount,
                                                min_base_amount_increment=Decimal(f"1e-{rule['amount_precision']}"),
                                                min_notional_size=min_notional,
                                                min_price_increment=Decimal(f"1e-{rule['price_precision']}"))
         except Exception:
             self.logger().error(f"Error parsing the trading pair rule {rule}. Skipping.", exc_info=True)
     return result
 def get_mid_price(trading_pair: str) -> Optional[Decimal]:
     resp = requests.get(url=Constants.EXCHANGE_ROOT_API +
                         Constants.TICKER_URI)
     records = resp.json()
     result = None
     for tag in list(records.keys()):
         record = records[tag]
         pair = convert_from_exchange_trading_pair(tag)
         if trading_pair == pair and record["ticker"][
                 "open"] is not None and record["ticker"][
                     "last"] is not None:
             result = ((Decimal(record["ticker"]["open"]) * Decimal('1')) +
                       (Decimal(record["ticker"]["last"]) *
                        Decimal('3'))) / Decimal("4")
             if result <= 0:
                 result = Decimal('0.00000001')
             break
     return result
Exemplo n.º 4
0
 async def get_open_orders(self) -> List[OpenOrder]:
     tracked_orders = list(self._in_flight_orders.values())
     # for order in tracked_orders:
     #     await order.get_exchange_order_id()
     result = await self._api_request("GET", Constants.ENDPOINT["USER_ORDERS"], is_auth_required=True)
     ret_val = []
     for order in result:
         if order["state"] in Constants.ORDER_STATES['DONE']:
             # Skip done orders
             continue
         exchange_order_id = str(order["id"])
         # AltMarkets doesn't support client order ids yet so we must find it from the tracked orders.
         track_order = [o for o in tracked_orders if exchange_order_id == o.exchange_order_id]
         if not track_order or len(track_order) < 1:
             # Skip untracked orders
             continue
         client_order_id = track_order[0].client_order_id
         if order["ord_type"] != OrderType.LIMIT.name.lower():
             self.logger().info(f"Unsupported order type found: {order['type']}")
             # Skip and report non-limit orders
             continue
         ret_val.append(
             OpenOrder(
                 client_order_id=client_order_id,
                 trading_pair=convert_from_exchange_trading_pair(order["market"]),
                 price=Decimal(str(order["price"])),
                 amount=Decimal(str(order["origin_volume"])),
                 executed_amount=Decimal(str(order["executed_volume"])),
                 status=order["state"],
                 order_type=OrderType.LIMIT,
                 is_buy=True if order["side"].lower() == TradeType.BUY.name.lower() else False,
                 time=str_date_to_ts(order["created_at"]),
                 exchange_order_id=exchange_order_id
             )
         )
     return ret_val