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 2 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 urllib2
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 = sys.argv[2] + "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 = urllib2.Request(api_domain + api_path + api_method, api_data)
		try:
			api_data = urllib2.urlopen(api_request).read()
		except Exception:
			time.sleep(5)
			continue
		api_data = json.loads(api_data)
		if len(api_data["error"]) != 0:
			time.sleep(5)
			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"]
		time.sleep(3)
except KeyboardInterrupt:
	None

sys.exit(0)