def market_buy(self, symbol: str, amount: Decimal) -> Order: self._check_is_empty(symbol) self._set_futures_settings(symbol, self.leverage) symbol_info = self.get_symbol_info(symbol) price = self.get_current_price(symbol) quantity = self._round(amount / price, symbol_info.quantity_precision) info = self._client.futures_create_order( side=Order.SIDE_BUY, type=Order.TYPE_MARKET, symbol=symbol, quantity=quantity, ) if info['status'] != Order.STATUS_FILLED: sleep(1) info = self._client.futures_get_order(symbol=info['symbol'], orderId=info['orderId']) order = Order.from_dict(info, quantity_key='executedQty', price_key='avgPrice', futures=True) assert order.status == Order.STATUS_FILLED, f'Got {order.status} status' return order
def get_oco_sell_orders(self, symbol: str) -> List[Tuple[Order, Order]]: all_orders = [ Order.from_dict(info, quantity_key='origQty') for info in self._client.get_open_orders(symbol=symbol) if info['side'] == Order.SIDE_SELL and info['status'] == Order.STATUS_NEW and info['type'] in (Order.TYPE_LIMIT_MAKER, Order.TYPE_STOP_LOSS_LIMIT) ] all_orders.sort(key=lambda o: o.type) grouped: Dict[int, List[Order]] = {} for order in all_orders: if order.order_list_id is not None: grouped.setdefault(order.order_list_id, []).append(order) oco_orders = [] for orders in grouped.values(): if len(orders) == 2: limit_maker, stop_loss_limit = orders assert limit_maker.type == Order.TYPE_LIMIT_MAKER, f'Got {limit_maker.type}' assert stop_loss_limit.type == Order.TYPE_STOP_LOSS_LIMIT, f'Got {stop_loss_limit.type}' oco_orders.append((limit_maker, stop_loss_limit)) return oco_orders
def limit_buy(self, symbol: str, price: Decimal, amount: Decimal) -> Order: symbol_info = self.get_symbol_info(symbol) quantity = self._round(amount / price, symbol_info.quantity_precision) info = self._client.order_limit_buy( symbol=symbol, price=price, quantity=quantity, ) order = Order.from_dict(info, quantity_key='origQty') assert order.status == Order.STATUS_NEW, f'Got {order.status}' return order
def process_api_spot_message(self, msg: dict) -> None: if msg['e'] == 'executionReport': order_list_id = msg['g'] if msg['g'] != -1 else None order = Order(symbol=msg['s'], side=msg['S'], order_type=msg['o'], status=msg['X'], order_id=msg['i'], order_list_id=order_list_id, quantity=parse_decimal(msg['l']), price=parse_decimal(msg['L'])) self._process_api_order(order)
def process_api_futures_message(self, message: dict) -> None: if message['data']['e'] == 'ORDER_TRADE_UPDATE': msg = message['data']['o'] order = Order(symbol=msg['s'], side=msg['S'], order_type=msg['o'], original_type=msg['ot'], status=msg['X'], order_id=msg['i'], order_list_id=None, quantity=parse_decimal(msg['l']), price=parse_decimal(msg['L']), futures=True) self._process_api_order(order)
def _get_last_buy_order(self, symbol: str) -> Optional[Order]: api_orders = self._client.get_all_orders(symbol=symbol) api_orders.sort(key=lambda o: o['updateTime'], reverse=True) for info in api_orders: if info['side'] == Order.SIDE_BUY and info[ 'status'] == Order.STATUS_FILLED: # price is zero in original response price = parse_decimal( info['cummulativeQuoteQty']) / parse_decimal( info['executedQty']) return Order.from_dict(info, price=price, quantity_key='executedQty') else: return None
def limit_buy(self, symbol: str, price: Decimal, amount: Decimal) -> Order: self._check_is_empty(symbol) self._set_futures_settings(symbol, self.leverage) symbol_info = self.get_symbol_info(symbol) quantity = self._round(amount / price, symbol_info.quantity_precision) info = self._client.futures_create_order( side=Order.SIDE_BUY, type=Order.TYPE_LIMIT, symbol=symbol, price=price, quantity=quantity, timeInForce=Client.TIME_IN_FORCE_GTC, ) order = Order.from_dict(info, quantity_key='origQty', futures=True) assert order.status == Order.STATUS_NEW, f'Got {order.status} status' return order
def market_sell(self, symbol: str, quantity: Decimal) -> Order: info = self._client.order_market_sell( symbol=symbol, quantity=quantity, ) if info['status'] != Order.STATUS_FILLED: sleep(1) info = self._client.get_order(symbol=info['symbol'], orderId=info['orderId']) # price is zero in original response price = parse_decimal(info['cummulativeQuoteQty']) / parse_decimal( info['executedQty']) order = Order.from_dict(info, price=price, quantity_key='executedQty') assert order.status == Order.STATUS_FILLED, f'Got {order.status}' return order
def market_sell(self, symbol: str, quantity: Decimal) -> Order: info = self._client.futures_create_order( side=Order.SIDE_SELL, type=Order.TYPE_MARKET, symbol=symbol, quantity=quantity, reduceOnly=True, ) if info['status'] != Order.STATUS_FILLED: sleep(1) info = self._client.futures_get_order(symbol=info['symbol'], orderId=info['orderId']) order = Order.from_dict(info, quantity_key='executedQty', price_key='avgPrice', futures=True) assert order.status == Order.STATUS_FILLED, f'Got {order.status} status' return order