We have released new API endpoints for the decentralised exchange candle data making it easier to develop algorithmic trading strategies.
Preface
Trading Strategy is offering both real-time and historical dataset API for decentralised exchanges. We are currently using the same real-time API endpoints to render trading pair charts with ChartIQ, meaning we have candle data available. However, this time we optimised the API endpoint to be suitable for the backtesting of trading strategies. This is based on our early user feedback and the pain points we found in focus group testing.
JSONL streaming candle data
The new OHLCV data endpoint is called candles-jsonl. The highlights of this API are:
- No API key needed - limited to 200 req/minutes and limited by response size (100 megabytes)
- Optimised for fetching the historical data, instead of displaying the latest price action as one does with trading pair charts
- Streaming data delivery method instead of pre-compressed dataset files
- Dynamic filtering - makes datasets smaller
- Web-native citizen - works in JavaScript and Python alike
- Uses JSON Lines streaming data format - it's an easy to parse and human-readable format where each candle is a JSON object on its own line
- Low latency - suitable for both real-time and backtesting usage
- Supports fetching of data for multiple trading pairs simultaneously if one is to developer multi trading pair strategies
An example of a response:
{"p":1,"ts":1588712400,"o":205.58758738302666,"h":205.58758738302666,"l":201.48625086106713,"c":201.48625086106713,"v":0.011,"xr":1.0,"b":0,"s":2,"bv":0.0,"sv":0.011,"sb":10008566,"eb":10008585}
{"p":1,"ts":1588781700,"o":201.07839057310534,"h":201.07839057310534,"l":201.07839057310534,"c":201.07839057310534,"v":0.001,"xr":1.0,"b":0,"s":1,"bv":0.0,"sv":0.001,"sb":10013764,"eb":10013764}
{"p":1,"ts":1588790700,"o":201.3584584250841,"h":201.3584584250841,"l":201.3584584250841,"c":201.3584584250841,"v":0.0006889999999999999,"xr":1.0,"b":1,"s":0,"bv":0.0006889999999999999,"sv":0.0,"sb":10014418,"eb":10014418}
{"p":1,"ts":1589202000,"o":199.06541070480117,"h":211.27000707204232,"l":199.06541070480117,"c":211.27000707204232,"v":0.397513,"xr":1.0,"b":1,"s":1,"bv":0.19264299999999998,"sv":0.20487,"sb":10045107,"eb":10045110}
For more information about the new candles API, see the OpenAPI explorer.
Example Python script
Here is a pain Python example that fetches data for two trading pairs over candles-jsonl
endpoint.
- Uses 15 minutes candles
- BNB-BUSD trading pair on PancakeSwap on Binance Smart Chain
- ETH-USD trading pair on Uniswap v2 on Ethereum mainnet
"""A sample script to download JSONL candle data.
Request all historical data for
- 15m candles
- ETH/USDC
- BNB/BUSD
The download JSONL binary size is 28 Mbytes
"""
import datetime
from collections import defaultdict
import requests
import jsonlines
api_url = "https://tradingstrategy.ai/api"
bnb_busd_params = {
"chain_slug": "binance",
"exchange_slug": "pancakeswap-v2",
"pair_slug": "bnb-busd",
}
resp = requests.get(f"{api_url}/pair-details", bnb_busd_params)
assert resp.status_code == 200, f"Got {resp.text}"
bnb_busd = resp.json()
print("Pair #1", bnb_busd["summary"]["pair_name"], bnb_busd["summary"]["pair_id"])
eth_usdc_params = {
"chain_slug": "ethereum",
"exchange_slug": "uniswap-v2",
"pair_slug": "eth-usdc",
}
resp = requests.get(f"{api_url}/pair-details", eth_usdc_params)
assert resp.status_code == 200, f"Got {resp.text}"
eth_usdc = resp.json()
print("Pair #2", eth_usdc["summary"]["pair_name"], eth_usdc["summary"]["pair_id"])
id_list = (eth_usdc["summary"]["pair_id"], bnb_busd["summary"]["pair_id"])
params = {
"pair_ids": ",".join(str(i) for i in id_list),
"time_bucket": "15m",
}
# Iterate the resulting response
# using jsonlines reader.
# We start to decode incoming data on the first arrived byte
# and keep decoding while streaming the response.
# https://stackoverflow.com/a/60846477/315168
print("Loading OHLCV data")
resp = requests.get(f"{api_url}/candles-jsonl", params=params, stream=True)
reader = jsonlines.Reader(resp.raw)
print("Iterating response")
candle_data = defaultdict(list)
for item in reader:
pair_id = item["p"]
candle_data[pair_id].append(item)
eth_usdc_candles = candle_data[eth_usdc["summary"]["pair_id"]]
first_candle = datetime.datetime.utcfromtimestamp(eth_usdc_candles[0]["ts"])
last_candle = datetime.datetime.utcfromtimestamp(eth_usdc_candles[-1]["ts"])
bnb_busd_candles = candle_data[bnb_busd["summary"]["pair_id"]]
print(f"ETH-USDC has {len(eth_usdc_candles):,} candles from {first_candle} to {last_candle}")
print(f"BNB-BUSD has {len(bnb_busd_candles):,} candles")
If you are programming in Python we recommend you to use our trading-strategy client library.
We are hiring
Trading Strategy is hiring Python developers. Check the blog post for more information.
More information
Join our Discord to find out more about DeFi markets and algorithmic trading.
Trading Strategy is an algorithmic trading protocol for decentralised markets, enabling automated trading on decentralised exchanges (DEXs). Learn more about algorithmic trading here.