For information on changes for our US clients, please visit our Support Center article.

Search
Python code to retrieve historical time and sales (trading history).
Historical time and sales for our markets can be retrieved via the REST API Trades endpoint. The entire trading history is available, from the very first trade to the most recent trade.
The following Python 3 code implements a command line API client specifically for retrieving historical time and sales in CSV format, and is provided as an example of how the Trades endpoint can be used:
#!/usr/bin/env python

# Kraken Historical Time and Sales
# Usage: ./krakenhistory symbol start [end]
# Example: ./krakenhistory XXBTZUSD 1559347200 1559433600

import sys
import time
import urllib.request
import json

api_domain = "https://api.kraken.com"
api_path = "/0/public/"
api_method = "Trades"
api_data = ""

if len(sys.argv) < 3:
sys.exit(1)

api_symbol = sys.argv[1].upper()
api_start = str(int(sys.argv[2]) - 1) + "999999999"

if len(sys.argv) > 3:
api_end = sys.argv[3]
else:
api_end = "9999999999"

try:
while True:
api_data = "?pair=%(pair)s&since=%(since)s" % {"pair":api_symbol, "since":api_start}
api_request = urllib.request.Request(api_domain + api_path + api_method + api_data)
try:
api_data = urllib.request.urlopen(api_request).read()
except Exception:
time.sleep(3)
continue
api_data = json.loads(api_data)
if len(api_data["error"]) != 0:
time.sleep(3)
continue
for trade in api_data["result"][api_symbol]:
if int(trade[2]) < int(api_end):
print("%(datetime)d,%(price)s,%(volume)s" % {"datetime":trade[2], "price":trade[0], "volume":trade[1]})
else:
sys.exit(0)
api_start = api_data["result"]["last"]
except KeyboardInterrupt:
None

sys.exit(0)