예제 #1
0
 def update_position(self,
                     position_id: Hashable,
                     limit: float = None,
                     stop: float = None) -> Position:
     kwargs = no_none_dict(take_profit=limit, stop_loss=stop)
     res = self.private_put(f'/trades/{position_id}', **kwargs)
     return self._convert_position(res)
예제 #2
0
 def close_position(self,
                    position_id: Hashable,
                    *,
                    qty: float = None) -> Optional[Position]:
     kwargs = no_none_dict(closed_quantity=qty)
     res = self.private_put(f'/trades/{position_id}/close', **kwargs)
     return self._convert_position(res)
예제 #3
0
 def _update_order_op(self,
                      order_id: Hashable,
                      *,
                      gid: int = None,
                      price: float = None,
                      qty: float = None,
                      qty_delta: float = None,
                      flags: int = None,
                      **kwargs):
     orders = self._account_info.get_orders()
     order = orders[order_id]
     if not order:
         raise NotFoundError(f'{order_id} order not found')
     if price:
         price = str(price)
     if qty:
         if order.side == OrderSide.SELL:
             qty = -qty
         qty = str(qty)
     if qty_delta:
         if order.side == OrderSide.SELL:
             qty_delta = -qty_delta
         qty_delta = str(qty_delta)
     params = no_none_dict(id=order_id,
                           gid=gid,
                           price=price,
                           amount=qty,
                           delta=qty_delta,
                           flags=flags)
     return 'ou', params
예제 #4
0
 def _new_order_op(self,
                   *,
                   instrument: str,
                   order_type: str,
                   side: str,
                   price: float = None,
                   qty: float,
                   gid: int = None,
                   flags: int = None):
     cid = self._get_cid()
     symbol = self.instruments[instrument].name_id
     side = side.upper()
     if side == 'BUY':
         amount = qty
     else:
         assert side == 'SELL'
         amount = -qty
     if price:
         price = str(price)
     params = no_none_dict(cid=cid,
                           gid=gid,
                           symbol=symbol,
                           type=order_type,
                           amount=str(amount),
                           price=price,
                           flags=flags)
     return 'on', params
예제 #5
0
 def get_order_book(self,
                    instrument: str,
                    *_,
                    _depth: int = None) -> OrderBook:
     symbol = self.instruments[instrument].name_id
     timestamp = time.time()
     kwargs = no_none_dict(symbol=symbol, depth=_depth)
     res = self.public_get('/orderBook/L2', **kwargs)
     asks = []
     bids = []
     for x in res:
         if x['side'].upper() == 'BUY':
             bids.append((float(x['price']), float(x['size']), x['id']))
         else:
             asks.append((float(x['price']), float(x['size']), x['id']))
     return OrderBook(timestamp=timestamp,
                      instrument=instrument,
                      asks=asks,
                      bids=bids,
                      _data=res)
예제 #6
0
 def submit_order(self,
                  instrument: str,
                  order_type: str,
                  side: str,
                  price: Optional[float],
                  qty: float,
                  *,
                  margin: bool = False,
                  leverage: float = None,
                  params: dict = None) -> Order:
     if margin:
         raise NotSupportedError('margin trade not supported')
     pair = self.instruments[instrument].name_id
     order_type = self._order_type_map.get(order_type, order_type)
     side = side.lower()
     kwargs = no_none_dict(pair=pair,
                           side=side,
                           type=order_type,
                           price=price,
                           amount=qty)
     kwargs.update(params or {})
     res = self.private_post('/user/spot/order', **kwargs)
     return self._convert_order(res)
예제 #7
0
        return self._submit_order_op(order_op, timeout=timeout, async=async)

    def update_order(self,
                     order_id: Hashable,
                     *,
                     params: dict = None,
                     timeout: float = 30,
                     async: bool = False,
                     gid: int = None,
                     price: float = None,
                     qty: float = None,
                     qty_delta: float = None,
                     flags: int = None) -> Order:
        kwargs = no_none_dict(gid=gid,
                              price=price,
                              qty=qty,
                              qty_delta=qty_delta,
                              flags=flags)
        kwargs.update(params or {})
        order_op = self._update_order_op(order_id, **kwargs)
        return self._submit_order_op(order_op, timeout=timeout, async=async)

    def get_positions(
            self,
            *,
            instrument: str = None,
            active_only: bool = True,
            position_ids: Iterable[Hashable] = None) -> Iterator[Position]:
        if position_ids:
            raise NotSupportedError('position_ids not supported')
        if not active_only:
예제 #8
0
def test_no_none_dict():
    assert no_none_dict(dict(a=None, b=2)) == dict(b=2)
    assert no_none_dict(a=None, b=2) == dict(b=2)
    assert no_none_dict(dict(a=None, b=2), a=1, b=None) == dict(a=1)