def get_current_orderbook(self, equity_pair, depth): current_ts = ts() connected = current_ts - self.last_updated_ts < 500 if not connected: return None if equity_pair not in self.orderbooks: return None if not (len(self.orderbooks[equity_pair]['asks']) > 0 and len(self.orderbooks[equity_pair]['bids']) > 0): return None cex_asks = self.orderbooks[equity_pair]['asks'] cex_bids = self.orderbooks[equity_pair]['bids'] timestamp = self.orderbooks[equity_pair]['timestamp'] if current_ts - timestamp > 3000: # cex 서버 타임스탬프가 3초 이상 오래된 경우에는 최신이라고 확신할 수 없기 때문에 그냥 사용하지 않습니다. return None asks_result = [] for ask_price in cex_asks: ask_volume = cex_asks[ask_price] asks_result.append([ask_price, ask_volume]) asks_result.sort() asks_result = asks_result[0:min(depth, len(asks_result))] asks = map(lambda elem: {'price': elem[0], 'volume': elem[1]}, asks_result) bids_result = [] for bid_price in cex_bids: bid_volume = cex_bids[bid_price] bids_result.append([bid_price, bid_volume]) bids_result.sort(reverse=True) bids_result = bids_result[0:min(depth, len(bids_result))] bids = map(lambda elem: {'price': elem[0], 'volume': elem[1]}, bids_result) return Orderbook('cex', equity_pair=equity_pair, asks=asks, bids=bids, timestamp=timestamp)
def parse_orderbook_response(cls, response, poloniex_currency_pair): validate_response(response, required_keys=['asks', 'bids', 'timestamp']) assert len(response['asks']) != 0, 'INVALID_VALUE: orderbook:asks should not be empty' assert len(response['bids']) != 0, 'INVALID_VALUE: orderbook:bids should not be empty' equity_pair = EQUITY_PAIRS.from_exchange_pair(poloniex_currency_pair) asks = map(lambda elem: {'price': float(elem[0]), 'volume': float(elem[1])}, response['asks']) bids = map(lambda elem: {'price': float(elem[0]), 'volume': float(elem[1])}, response['bids']) timestamp = int(response['timestamp']) * 1000 return Orderbook('poloniex', equity_pair, asks, bids, timestamp)
def parse_orderbook_response(cls, response, binance_equity_pair): validate_response(response, required_keys=['asks', 'bids', 'lastUpdateId']) assert len(response['asks'] ) != 0, 'INVALID_VALUE: orderbook:asks should not be empty' assert len(response['bids'] ) != 0, 'INVALID_VALUE: orderbook:bids should not be empty' equity_pair = EQUITY_PAIRS.from_exchange_pair(binance_equity_pair) asks = map( lambda elem: { 'price': float(elem[0]), 'volume': float(elem[1]) }, response['asks']) bids = map( lambda elem: { 'price': float(elem[0]), 'volume': float(elem[1]) }, response['bids']) timestamp = ts() return Orderbook('binance', equity_pair, asks, bids, timestamp)
def parse_orderbook_response(cls, response, okex_equity_pair, contract_type=None): validate_response(response, required_keys=['asks', 'bids']) assert len(response['asks'] ) != 0, 'INVALID_VALUE: orderbook:asks should not be empty' assert len(response['bids'] ) != 0, 'INVALID_VALUE: orderbook:bids should not be empty' equity_pair = EQUITY_PAIRS.from_exchange_pair(okex_equity_pair) # 만약 contract_type이 주어진 경우 선물에 대한 오더북이다. # 이 경우에는 equity_pair를 선물 포맷에 맞게 변형해서 넣어줘야 한다. if contract_type is not None: expiry_yymmdd = get_future_expiry_symbol(contract_type) equity_pair = 'f_{}_{}'.format(equity_pair, expiry_yymmdd) asks = sorted(response['asks'], key=lambda b: b[0]) bids = sorted(response['bids'], key=lambda b: b[0], reverse=True) asks = map(lambda elem: {'price': elem[0], 'volume': elem[1]}, asks) bids = map(lambda elem: {'price': elem[0], 'volume': elem[1]}, bids) timestamp = ts() return Orderbook('okex', equity_pair, asks, bids, timestamp)