Python

Python tardis-client 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 GitHub, 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 streams, same for symbols) and you receive an Async Generator 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())

Try on repl.it

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())

Try on repl.it

■ 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())

Try on repl.it