Tardis.dev Documentation
  • Welcome
  • Frequently Asked Questions
    • General
    • Data
    • Billing and Subscriptions
  • Downloadable CSV files
  • Historical Data Details
    • BitMEX
    • Deribit
    • Binance USDT Futures
    • Binance COIN Futures
    • Binance Spot
    • FTX
    • OKX Futures
    • OKX Swap
    • OKX Options
    • OKX Spot
    • Huobi Futures
    • Huobi COIN Swaps
    • Huobi USDT Swaps
    • Huobi Global
    • Bitfinex Derivatives
    • Bitfinex
    • Coinbase Pro
    • Kraken Futures
    • Kraken
    • Bitstamp
    • Gemini
    • Bybit Derivatives
    • Bybit Spot
    • dYdX
    • WOO X
    • Kucoin Spot
    • Blockchain.com
    • Upbit
    • Phemex
    • Delta
    • AscendEX (BitMax)
    • FTX US
    • Binance US
    • Gate.io Futures
    • Gate.io
    • Bitnomial
    • Crypto.com
    • OKCoin
    • bitFlyer
    • HitBTC (high caps)
    • CoinFLEX (2.0)
    • Binance Jersey
    • Binance DEX
    • Poloniex
  • API
    • Getting Started
    • Python Client
    • Node.js Client
    • Tardis Machine Server
    • Instruments Metadata API
    • HTTP API Reference
  • Legal
    • Privacy Policy
    • Terms of Service
  • External Links
    • Public Roadmap
    • Open Source
    • Newsletter
    • Contact Us
    • Get API Key
    • Tardis.dev Home
Powered by GitBook
On this page
  • Examples
  • ■ BitMEX historical trades and order book data replay
  • ■ Saving Deribit historical index data to CSV file
Export as PDF
  1. API

Python Client

PreviousGetting StartedNextNode.js Client

Last updated 5 years ago

provides simple and intuitive way of accessing Tardis.dev historical market data API. Detailed and most up to date documentation & installation instructions can be found on , but the gist of it is that you provide exchange name, historical date ranges and optional filters (channel names are the same as exchange's channels in real-time WebSocket feeds, same for symbols) and you receive an that provides market data message for each iteration. Local disk-based caching is being done transparently in the background. Cached data is stored on disk in compressed form (GZIP) and decompressed on demand when reading the data. See example snippet below that shows how to replay some of the historical BitMEX data.

Requires Python 3.7.0+.

import asyncio
from tardis_client import TardisClient, Channel

async def replay():
    tardis_client = TardisClient()

    # replay method returns Async Generator
    # https://rickyhan.com/jekyll/update/2018/01/27/python36.html
    messages = tardis_client.replay(
        exchange="bitmex",
        from_date="2019-06-01",
        to_date="2019-06-02",
        filters=[
          Channel(name="trade", symbols=["XBTUSD","ETHUSD"]), 
          Channel("orderBookL2", ["XBTUSD"])
        ],
    )

    # this will print all trades and orderBookL2 messages for XBTUSD
    # and all trades for ETHUSD for bitmex exchange
    # between 2019-06-01T00:00:00.000Z and 2019-06-02T00:00:00.000Z 
    #(whole first day of June 2019)
    async for local_timestamp, message in messages:
        # local timestamp is a Python datetime that marks timestamp 
        # when given message has been received
        # message is a message object as provided by exchange real-time stream
        print(message)

asyncio.run(replay())

Examples

■ BitMEX historical trades and order book data replay

import asyncio
from tardis_client import TardisClient, Channel

async def replay():
    tardis_client = TardisClient()

    # replay method returns Async Generator
    # https://rickyhan.com/jekyll/update/2018/01/27/python36.html
    messages = tardis_client.replay(
        exchange="bitmex",
        from_date="2019-06-01",
        to_date="2019-06-02",
        filters=[
          Channel(name="trade", symbols=["XBTUSD","ETHUSD"]), 
          Channel("orderBookL2", ["XBTUSD"])
        ],
    )

    # this will print all trades and orderBookL2 messages for XBTUSD
    # and all trades for ETHUSD for bitmex exchange
    # between 2019-06-01T00:00:00.000Z and 2019-06-02T00:00:00.000Z 
    #(whole first day of June 2019)
    async for local_timestamp, message in messages:
        # local timestamp is a Python datetime that marks timestamp 
        # when given message has been received
        # message is a message object as provided by exchange real-time stream
        print(message)

asyncio.run(replay())

■ Saving Deribit historical index data to CSV file

import asyncio
import csv
from tardis_client import TardisClient, Channel


async def save_historical_deribit_index_data_to_csv():
    tardis_client = TardisClient()

    messages = tardis_client.replay(
        exchange="deribit",
        from_date="2019-06-01",
        to_date="2019-06-02",
        filters=[Channel(name="deribit_price_index", symbols=["btc_usd", "eth_usd"])],
    )
    with open("./deribit_index_data.csv", mode="w") as csv_file:
        fieldnames = ["symbol", "price", "timestamp"]
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        async for local_timestamp, message in messages:
            data = message["params"]["data"]
            writer.writerow({"symbol": data["index_name"], "price": data["price"], "timestamp": data["timestamp"]})

    print("finished")


asyncio.run(save_historical_deribit_index_data_to_csv())

Python tardis-client
GitHub
Async Generator
Try on repl.it
Try on repl.it
Try on repl.it
GitHub - tardis-dev/tardis-python: Python client for tardis.dev - historical tick-level cryptocurrency market data replay API.GitHub
Python tardis-client GitHub repository
Logo