This quick interactive guide will show you how Bitcoinchart's API works. Bitcoinchart offers a free non-authenticated API to acccess Bitcoin prices.
Important! Bitcoincharts only offers Bitcoin data (doh!), for other cryptos see my other post about Cryptowatch API.
Bitcoincharts also offers historic data as complete CSVs, that you can just download as regular files: http://api.bitcoincharts.com/v1/csv/. Warning, files are large.
Let's get started with the most important endpoints:
import requests
from io import StringIO
import pandas as pd
%matplotlib inline
Weighted Prices¶
Weighted Prices offers a "summary" of the price of Bitcoin for different fiat currencies (US Dollars, Euros, etc).
resp = requests.get('http://api.bitcoincharts.com/v1/weighted_prices.json')
resp
resp.json()
This endpoint is probably not so useful, as aggregated price data isn't really actionable. The next ones, Markets Data and Historic Trade Data are more interesting.
Markets Data¶
Markets Data offers current market prices for multiple exchanges and fiat currencies (US Dollars, Euros). It's a snapshot of the current market for that particular exchange.
resp = requests.get('http://api.bitcoincharts.com/v1/markets.json')
resp
doc = resp.json()
print(f"There are {len(doc)} results")
An example of one of the results:
doc[0]
For example, here are the results for Bitstamp:
[d for d in doc if 'bitstamp' in d['symbol'].lower()]
Historic Trade Data¶
This endpoint returns historic data
resp = requests.get('http://api.bitcoincharts.com/v1/trades.csv', params={
'symbol': 'bitstampUSD'
})
resp
This endpoint is not JSON data. It's CSV, so the response will look something like:
print(resp.text[:90])
We can use Pandas to parse this data as a DataFrame
:
pd.to_datetime(1544023320)
df = pd.read_csv(StringIO(resp.text), header=None, names=['Timestamp', 'Price', 'Amount'])
df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='s')
df.set_index('Timestamp', inplace=True)
These are the first prices:
df.head()
Starting on:
df.index.min()
Up to:
df.index.max()
And this is the full chart displaying it the price:
df['Price'].plot(figsize=(14, 7))