Python Client
GitHub - tardis-dev/tardis-python: Python client for tardis.dev - historical tick-level cryptocurrency market data replay API.
GitHub
Python tardis-client GitHub repository
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 feeds, 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+.
1
import asyncio
2
from tardis_client import TardisClient, Channel
3
4
async def replay():
5
tardis_client = TardisClient()
6
7
# replay method returns Async Generator
8
# https://rickyhan.com/jekyll/update/2018/01/27/python36.html
9
messages = tardis_client.replay(
10
exchange="bitmex",
11
from_date="2019-06-01",
12
to_date="2019-06-02",
13
filters=[
14
Channel(name="trade", symbols=["XBTUSD","ETHUSD"]),
15
Channel("orderBookL2", ["XBTUSD"])
16
],
17
)
18
19
# this will print all trades and orderBookL2 messages for XBTUSD
20
# and all trades for ETHUSD for bitmex exchange
21
# between 2019-06-01T00:00:00.000Z and 2019-06-02T00:00:00.000Z
22
#(whole first day of June 2019)
23
async for local_timestamp, message in messages:
24
# local timestamp is a Python datetime that marks timestamp
25
# when given message has been received
26
# message is a message object as provided by exchange real-time stream
27
print(message)
28
29
asyncio.run(replay())
Copied!

Examples

■ BitMEX historical trades and order book data replay

1
import asyncio
2
from tardis_client import TardisClient, Channel
3
4
async def replay():
5
tardis_client = TardisClient()
6
7
# replay method returns Async Generator
8
# https://rickyhan.com/jekyll/update/2018/01/27/python36.html
9
messages = tardis_client.replay(
10
exchange="bitmex",
11
from_date="2019-06-01",
12
to_date="2019-06-02",
13
filters=[
14
Channel(name="trade", symbols=["XBTUSD","ETHUSD"]),
15
Channel("orderBookL2", ["XBTUSD"])
16
],
17
)
18
19
# this will print all trades and orderBookL2 messages for XBTUSD
20
# and all trades for ETHUSD for bitmex exchange
21
# between 2019-06-01T00:00:00.000Z and 2019-06-02T00:00:00.000Z
22
#(whole first day of June 2019)
23
async for local_timestamp, message in messages:
24
# local timestamp is a Python datetime that marks timestamp
25
# when given message has been received
26
# message is a message object as provided by exchange real-time stream
27
print(message)
28
29
asyncio.run(replay())
Copied!

■ Saving Deribit historical index data to CSV file

1
import asyncio
2
import csv
3
from tardis_client import TardisClient, Channel
4
5
6
async def save_historical_deribit_index_data_to_csv():
7
tardis_client = TardisClient()
8
9
messages = tardis_client.replay(
10
exchange="deribit",
11
from_date="2019-06-01",
12
to_date="2019-06-02",
13
filters=[Channel(name="deribit_price_index", symbols=["btc_usd", "eth_usd"])],
14
)
15
with open("./deribit_index_data.csv", mode="w") as csv_file:
16
fieldnames = ["symbol", "price", "timestamp"]
17
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
18
writer.writeheader()
19
async for local_timestamp, message in messages:
20
data = message["params"]["data"]
21
writer.writerow({"symbol": data["index_name"], "price": data["price"], "timestamp": data["timestamp"]})
22
23
print("finished")
24
25
26
asyncio.run(save_historical_deribit_index_data_to_csv())
27
Copied!