How to use the cryptofeed.defines.TRADES 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 / bitfinex.py View on Github external
period = None
            ts = timestamp_normalize(self.id, ts)
            side = SELL if amount < 0 else BUY
            amount = abs(amount)
            if period:
                await self.callback(FUNDING, feed=self.id,
                                    pair=pair,
                                    side=side,
                                    amount=Decimal(amount),
                                    price=Decimal(price),
                                    order_id=order_id,
                                    timestamp=ts,
                                    receipt_timestamp=timestamp,
                                    period=period)
            else:
                await self.callback(TRADES, feed=self.id,
                                    pair=pair,
                                    side=side,
                                    amount=Decimal(amount),
                                    price=Decimal(price),
                                    order_id=order_id,
                                    timestamp=ts,
                                    receipt_timestamp=timestamp)
github bmoscon / cryptofeed / cryptofeed / exchange / coinbene.py View on Github external
"""
        if pair not in self.last_trade_update:
            async with session.get(f"{self.address}trades?symbol={pair}") as response:
                data = await response.json()
                self.last_trade_update[pair] = timestamp_normalize(self.id, data['trades'][-1]['time'])
        else:
            async with session.get(f"{self.address}trades?symbol={pair}&size=2000") as response:
                data = await response.json()
                for trade in data['trades']:
                    if timestamp_normalize(self.id, trade['time']) <= self.last_trade_update[pair]:
                        continue
                    price = Decimal(trade['price'])
                    amount = Decimal(trade['quantity'])
                    side = BUY if trade['take'] == 'buy' else SELL

                    await self.callback(TRADES, feed=self.id,
                                        pair=pair_exchange_to_std(pair),
                                        side=side,
                                        amount=amount,
                                        price=price,
                                        order_id=trade['tradeId'],
                                        timestamp=timestamp_normalize(self.id, trade['time']))
                self.last_trade_update[pair] = timestamp_normalize(self.id, data['trades'][-1]['time'])
github bmoscon / cryptofeed / examples / demo_open_interest.py View on Github external
def main():
    f = FeedHandler()
    f.add_feed(Bitmex(pairs=['XBTUSD'], channels=[TRADES, OPEN_INTEREST], callbacks={
               OPEN_INTEREST: OpenInterestArctic('cryptofeed-test2'), TRADES: TradeArctic('cryptofeed-test2')}))

    f.run()
github bmoscon / cryptofeed / examples / demo_kafka.py View on Github external
def main():
    f = FeedHandler()
    cbs = {TRADES: TradeKafka(), L2_BOOK: BookKafka()}

    f.add_feed(Coinbase(max_depth=10, channels=[TRADES, L2_BOOK], pairs=['BTC-USD'], callbacks=cbs))

    f.run()
github bmoscon / cryptofeed / examples / demo_postgres.py View on Github external
def main():
    f = FeedHandler()
    f.add_feed(Coinbase(channels=[L2_BOOK, TRADES, TICKER], pairs=['BTC-USD'], callbacks={L2_BOOK: BookPostgres(**postgres_cfg), BOOK_DELTA: BookDeltaPostgres(**postgres_cfg), TICKER: TickerPostgres(**postgres_cfg), TRADES: TradePostgres(**postgres_cfg)}))
    f.run()
github bmoscon / cryptofeed / cryptofeed / exchange / deribit.py View on Github external
"tick_direction": 3,
                        "price": 3948.69,
                        "instrument_name": "BTC-PERPETUAL",
                        "index_price": 3930.73,
                        "direction": "sell",
                        "amount": 10
                    }
                ],
                "channel": "trades.BTC-PERPETUAL.raw"
            },
            "method": "subscription",
            "jsonrpc": "2.0"
        }
        """
        for trade in msg["params"]["data"]:
            await self.callback(TRADES,
                                feed=self.id,
                                pair=trade["instrument_name"],
                                order_id=trade['trade_id'],
                                side=BUY if trade['direction'] == 'buy' else SELL,
                                amount=Decimal(trade['amount']),
                                price=Decimal(trade['price']),
                                timestamp=timestamp_normalize(self.id, trade['timestamp']),
                                receipt_timestamp=timestamp,
                                )
            if 'liquidation' in trade:
                await self.callback(LIQUIDATIONS,
                                    feed=self.id,
                                    pair=trade["instrument_name"],
                                    side=BUY if trade['direction'] == 'buy' else SELL,
                                    leaves_qty=Decimal(trade['amount']),
                                    price=Decimal(trade['price']),
github bmoscon / cryptofeed / cryptofeed / exchange / kraken.py View on Github external
async def _trade(self, msg: dict, pair: str, timestamp: float):
        """
        example message:

        [1,[["3417.20000","0.21222200","1549223326.971661","b","l",""]]]
        channel id, price, amount, timestamp, size, limit/market order, misc
        """
        for trade in msg[1]:
            price, amount, server_timestamp, side, _, _ = trade
            await self.callback(TRADES, feed=self.id,
                                pair=pair,
                                side=BUY if side == 'b' else SELL,
                                amount=Decimal(amount),
                                price=Decimal(price),
                                order_id=None,
                                timestamp=float(server_timestamp),
                                receipt_timestamp=timestamp)
github bmoscon / cryptofeed / examples / demo_ohlcv.py View on Github external
def main():
    f = FeedHandler()
    f.add_feed(Coinbase(pairs=['BTC-USD', 'ETH-USD', 'BCH-USD'], channels=[TRADES], callbacks={TRADES: OHLCV(Callback(ohlcv), window=300)}))

    f.run()
github bmoscon / cryptofeed / cryptofeed / exchange / bittrex.py View on Github external
async def subscribe(self, websocket):
        self.__reset()
        # H: Hub, M: Message, A: Args, I: Internal ID
        # For more signalR info see:
        # https://blog.3d-logic.com/2015/03/29/signalr-on-the-wire-an-informal-description-of-the-signalr-protocol/
        # http://blogs.microsoft.co.il/applisec/2014/03/12/signalr-message-format/
        for channel in set(self.channels) if not self.config else set(self.config):
            symbols = self.pairs if not self.config else list(self.config[channel])
            i = 0
            if channel == 'SubscribeToExchangeDeltas':
                for symbol in symbols:
                    msg = {'A': [symbol], 'H': 'c2', 'I': i, 'M': 'QueryExchangeState'}
                    await websocket.send(json.dumps(msg))
                    i += 1
            if channel == TRADES:
                channel = 'SubscribeToExchangeDeltas'
            for symbol in symbols:
                msg = {'A': [symbol] if channel != 'SubscribeToSummaryDeltas' else [], 'H': 'c2', 'I': i, 'M': channel}
                i += 1
                await websocket.send(json.dumps(msg))