How to use the cryptofeed.defines.BID function in cryptofeed

To help you get started, we’ve selected a few cryptofeed examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bmoscon / cryptofeed / cryptofeed / exchange / huobi.py View on Github external
async def _book(self, msg: dict, timestamp: float):
        pair = pair_exchange_to_std(msg['ch'].split('.')[1])
        data = msg['tick']
        forced = pair not in self.l2_book

        update = {
            BID: sd({
                Decimal(price): Decimal(amount)
                for price, amount in data['bids']
            }),
            ASK: sd({
                Decimal(price): Decimal(amount)
                for price, amount in data['asks']
            })
        }

        if not forced:
            self.previous_book[pair] = self.l2_book[pair]
        self.l2_book[pair] = update

        await self.book_callback(self.l2_book[pair], L2_BOOK, pair, forced, False, timestamp_normalize(self.id, msg['ts']), timestamp)
github bmoscon / cryptofeed / cryptofeed / exchange / coinbase.py View on Github external
async def _pair_level2_update(self, msg: dict, timestamp: float):
        pair = pair_exchange_to_std(msg['product_id'])
        delta = {BID: [], ASK: []}
        for side, price, amount in msg['changes']:
            side = BID if side == 'buy' else ASK
            price = Decimal(price)
            amount = Decimal(amount)
            bidask = self.l2_book[pair][side]

            if amount == 0:
                del bidask[price]
                delta[side].append((price, 0))
            else:
                bidask[price] = amount
                delta[side].append((price, amount))

        await self.book_callback(self.l2_book[pair], L2_BOOK, pair, False, delta, timestamp, timestamp)
github bmoscon / cryptofeed / cryptofeed / backends / backend.py View on Github external
async def __call__(self, *, feed: str, pair: str, book: dict, timestamp: float, receipt_timestamp: float):
        data = {'timestamp': timestamp, 'receipt_timestamp': receipt_timestamp, 'delta': False, BID: {}, ASK: {}}
        book_convert(book, data, convert=self.numeric_type)
        await self.write(feed, pair, timestamp, receipt_timestamp, data)
github bmoscon / cryptofeed / examples / demo_okex_swaps.py View on Github external
async def book(feed, pair, book, timestamp, receipt_timestamp):
    print(f'Timestamp: {timestamp} Feed: {feed} Pair: {pair} Book Bid Size is {len(book[BID])} Ask Size is {len(book[ASK])}')
github bmoscon / cryptofeed / cryptofeed / rest / bitmex.py View on Github external
def l2_book(self, symbol: str, retry=None, retry_wait=10):
        ret = {symbol: {BID: sd(), ASK: sd()}}
        data = next(self._get('orderBook/L2', symbol, None, None, retry, retry_wait))
        for update in data:
            side = ASK if update['side'] == 'Sell' else BID
            ret[symbol][side][update['price']] = update['size']
        return ret
github bmoscon / cryptofeed / cryptofeed / exchange / kraken.py View on Github external
async def _book(self, msg: dict, pair: str, timestamp: float):
        delta = {BID: [], ASK: []}
        msg = msg[1:-2]

        if 'as' in msg[0]:
            # Snapshot
            self.l2_book[pair] = {BID: sd({
                Decimal(update[0]): Decimal(update[1]) for update in msg[0]['bs']
            }), ASK: sd({
                Decimal(update[0]): Decimal(update[1]) for update in msg[0]['as']
            })}
            await self.book_callback(self.l2_book[pair], L2_BOOK, pair, True, delta, timestamp, timestamp)
        else:
            for m in msg:
                for s, updates in m.items():
                    side = False
                    if s == 'b':
                        side = BID
github bmoscon / cryptofeed / examples / demo_book_delta.py View on Github external
async def handle_l2_delta(self, feed, pair, update, timestamp, receipt_timestamp):
        """Handle L2 delta updates."""
        # handle updates for L2 books
        for side in (BID, ASK):
            for price, size in update[side]:
                if size == 0:
                    del self.book[side][price]
                else:
                    self.book[side][price] = size
github bmoscon / cryptofeed / cryptofeed / exchange / poloniex.py View on Github external
if msg_type == 'i':
            forced = True
            pair = msg[0][1]['currencyPair']
            pair = pair_exchange_to_std(pair)
            self.l2_book[pair] = {BID: sd(), ASK: sd()}
            # 0 is asks, 1 is bids
            order_book = msg[0][1]['orderBook']
            for key in order_book[0]:
                amount = Decimal(order_book[0][key])
                price = Decimal(key)
                self.l2_book[pair][ASK][price] = amount

            for key in order_book[1]:
                amount = Decimal(order_book[1][key])
                price = Decimal(key)
                self.l2_book[pair][BID][price] = amount
        else:
            pair = self.pair_mapping[chan_id]
            pair = pair_exchange_to_std(pair)
            for update in msg:
                msg_type = update[0]
                # order book update
                if msg_type == 'o':
                    side = ASK if update[1] == 0 else BID
                    price = Decimal(update[2])
                    amount = Decimal(update[3])
                    if amount == 0:
                        delta[side].append((price, 0))
                        del self.l2_book[pair][side][price]
                    else:
                        delta[side].append((price, amount))
                        self.l2_book[pair][side][price] = amount
github bmoscon / cryptofeed / cryptofeed / exchange / ftx.py View on Github external
"""
        example messages:

        snapshot:
        {"channel": "orderbook", "market": "BTC/USD", "type": "partial", "data": {"time": 1564834586.3382702,
        "checksum": 427503966, "bids": [[10717.5, 4.092], ...], "asks": [[10720.5, 15.3458], ...], "action": "partial"}}

        update:
        {"channel": "orderbook", "market": "BTC/USD", "type": "update", "data": {"time": 1564834587.1299787,
        "checksum": 3115602423, "bids": [], "asks": [[10719.0, 14.7461]], "action": "update"}}
        """
        if msg['type'] == 'partial':
            # snapshot
            pair = pair_exchange_to_std(msg['market'])
            self.l2_book[pair] = {
                BID: sd({
                    Decimal(price): Decimal(amount) for price, amount in msg['data']['bids']
                }),
                ASK: sd({
                    Decimal(price): Decimal(amount) for price, amount in msg['data']['asks']
                })
            }
            await self.book_callback(self.l2_book[pair], L2_BOOK, pair, True, None, float(msg['data']['time']), timestamp)
        else:
            # update
            delta = {BID: [], ASK: []}
            pair = pair_exchange_to_std(msg['market'])
            for side in ('bids', 'asks'):
                s = BID if side == 'bids' else ASK
                for price, amount in msg['data'][side]:
                    price = Decimal(price)
                    amount = Decimal(amount)