Profile picture

Co-founder @ RMOTR

Alpha Vantage Cryptos Live Docs

Last updated: December 6th, 20182018-12-06Project preview

Alpha Vantage is today one of the best sources to get financial data. It's not just for cryptos. Also offers Stock prices, Forex rates and other indicators.

Alpha Vantage strong selling point is its simplicity. It has a couple of simple to use methods that give you access to prices with just a few requests. The limitation is that it doesn't offer data "per exchange". That is, you can't access Bitcoin prices in both Coinbase and Bitstamp (for example). There's only one price for Bitcoin (probably a weighted average).

In [1]:
import os
import requests
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline

API Authentication

Before we begin, you MUST get a free API key from Cryptocompare: https://min-api.cryptocompare.com/. After getting the API Key, you can set it as an environment variable (in the project edit page) or just replace it where it says 'YOUR-API-KEY'.

In [2]:
AV_API_KEY = os.environ.get('ALPHAVANTAGE_API_KEY', 'YOUR-API-KEY')
In [3]:
assert AV_API_KEY != 'YOUR-API-KEY'

we'll use this API Key in each request sending it as a regular parameter.

Supported cryptos and fiat currencies

With this project I've included 2 csv files with the cryptocurrencies supported by Alpha Vantage. Fork it to see it by yourself. Here's a quick summary:

In [7]:
cryptos = pd.read_csv('digital_currency_list.csv')
cryptos.head()
Out[7]:
currency code currency name
0 1ST FirstBlood
1 2GIVE GiveCoin
2 808 808Coin
3 ABT ArcBlock
4 ABY ArtByte
In [14]:
cryptos[cryptos['currency name'].str.contains('Bitcoin')]
Out[14]:
currency code currency name
41 BCD Bitcoin-Diamond
42 BCH Bitcoin-Cash
45 BCX BitcoinX
77 BTC Bitcoin
78 BTCD BitcoinDark
79 BTCP Bitcoin-Private
80 BTG Bitcoin-Gold
394 SBTC Super-Bitcoin
500 XBC Bitcoin-Plus
In [15]:
cryptos[cryptos['currency name'].str.contains('Ethereum')]
Out[15]:
currency code currency name
166 ETC Ethereum-Classic
167 ETH Ethereum
168 ETHD Ethereum-Dark
In [16]:
fiat = pd.read_csv('physical_currency_list.csv')
In [17]:
fiat.head()
Out[17]:
currency code currency name
0 AED United Arab Emirates Dirham
1 AFN Afghan Afghani
2 ALL Albanian Lek
3 AMD Armenian Dram
4 ANG Netherlands Antillean Guilder
In [18]:
fiat[fiat['currency code'] == 'USD']
Out[18]:
currency code currency name
139 USD United States Dollar

Realtime exchange rates

The simplest function exposed by Alpha Vantage API is CURRENCY_EXCHANGE_RATE, which returns the current exchange rate (the price) for a given crypto. Here's an example with Bitcoin:

In [24]:
resp = requests.get('https://www.alphavantage.co/query', params={
    'function': 'CURRENCY_EXCHANGE_RATE',
    'from_currency': 'BTC',
    'to_currency': 'USD',
    'apikey': AV_API_KEY
})
In [25]:
resp
Out[25]:
<Response [200]>
In [27]:
resp.json()
Out[27]:
{'Realtime Currency Exchange Rate': {'1. From_Currency Code': 'BTC',
  '2. From_Currency Name': 'Bitcoin',
  '3. To_Currency Code': 'USD',
  '4. To_Currency Name': 'United States Dollar',
  '5. Exchange Rate': '3633.94000000',
  '6. Last Refreshed': '2018-12-06 19:05:01',
  '7. Time Zone': 'UTC'}}

And here's one for Ethereum:

In [28]:
resp = requests.get('https://www.alphavantage.co/query', params={
    'function': 'CURRENCY_EXCHANGE_RATE',
    'from_currency': 'ETH',
    'to_currency': 'USD',
    'apikey': AV_API_KEY
})
In [29]:
resp
Out[29]:
<Response [200]>
In [30]:
resp.json()
Out[30]:
{'Realtime Currency Exchange Rate': {'1. From_Currency Code': 'ETH',
  '2. From_Currency Name': 'Ethereum',
  '3. To_Currency Code': 'USD',
  '4. To_Currency Name': 'United States Dollar',
  '5. Exchange Rate': '96.22000000',
  '6. Last Refreshed': '2018-12-06 19:06:56',
  '7. Time Zone': 'UTC'}}
In [ ]:
 
In [88]:
resp = requests.get('https://www.alphavantage.co/query', params={
    'function': 'DIGITAL_CURRENCY_DAILY',
    'symbol': 'BTC',
    'market': 'USD',
    'apikey': AV_API_KEY
})
In [38]:
resp
Out[38]:
<Response [200]>
In [39]:
resp
Out[39]:
<Response [200]>
In [43]:
doc = resp.json()
In [44]:
doc['Meta Data']
Out[44]:
{'1. Information': 'Daily Prices and Volumes for Digital Currency',
 '2. Digital Currency Code': 'BTC',
 '3. Digital Currency Name': 'Bitcoin',
 '4. Market Code': 'USD',
 '5. Market Name': 'United States Dollar',
 '6. Last Refreshed': '2018-12-05 (end of day)',
 '7. Time Zone': 'UTC'}

This endpoint returns more than 4 years of data. For example, today's data:

In [59]:
doc['Time Series (Digital Currency Daily)']['2018-12-05']
Out[59]:
{'1a. open (USD)': '3946.15699876',
 '1b. open (USD)': '3946.15699876',
 '2a. high (USD)': '3957.77102631',
 '2b. high (USD)': '3957.77102631',
 '3a. low (USD)': '3746.81399002',
 '3b. low (USD)': '3746.81399002',
 '4a. close (USD)': '3752.69330547',
 '4b. close (USD)': '3752.69330547',
 '5. volume': '145573.53513234',
 '6. market cap (USD)': '546292830.74472952'}

There are repeated values because the result will always include USD information. Now, I'll turn it into a pandas DataFrame to visualize it:

In [79]:
df = pd.DataFrame.from_dict(doc['Time Series (Digital Currency Daily)'], orient='index', dtype=np.float)
df.head()
Out[79]:
1a. open (USD) 1b. open (USD) 2a. high (USD) 2b. high (USD) 3a. low (USD) 3b. low (USD) 4a. close (USD) 4b. close (USD) 5. volume 6. market cap (USD)
2014-04-01 468.480106 468.480106 491.267273 491.267273 468.480106 468.480106 479.023434 479.023434 62894.683068 3.012803e+07
2014-04-02 479.679615 479.679615 494.111337 494.111337 430.725909 430.725909 438.377824 438.377824 96314.587552 4.222218e+07
2014-04-03 437.322089 437.322089 452.441405 452.441405 414.811104 414.811104 446.819755 446.819755 74292.749549 3.319547e+07
2014-04-04 448.597144 448.597144 457.327172 457.327172 429.184117 429.184117 451.645992 451.645992 39267.667348 1.773508e+07
2014-04-05 450.027798 450.027798 466.385470 466.385470 445.107783 445.107783 464.379322 464.379322 21574.144537 1.001859e+07

And we'll do some processing to "clean it" a little bit. First step is get rid of repeated columns:

In [80]:
[c for c in df.columns.values if 'b.' in c]
Out[80]:
['1b. open (USD)', '2b. high (USD)', '3b. low (USD)', '4b. close (USD)']
In [81]:
df.drop(columns=[c for c in df.columns.values if 'b.' in c], inplace=True)
In [82]:
df.head()
Out[82]:
1a. open (USD) 2a. high (USD) 3a. low (USD) 4a. close (USD) 5. volume 6. market cap (USD)
2014-04-01 468.480106 491.267273 468.480106 479.023434 62894.683068 3.012803e+07
2014-04-02 479.679615 494.111337 430.725909 438.377824 96314.587552 4.222218e+07
2014-04-03 437.322089 452.441405 414.811104 446.819755 74292.749549 3.319547e+07
2014-04-04 448.597144 457.327172 429.184117 451.645992 39267.667348 1.773508e+07
2014-04-05 450.027798 466.385470 445.107783 464.379322 21574.144537 1.001859e+07

And now turn the index into a DateTime index:

In [83]:
df.reset_index(inplace=True)
df.head()
Out[83]:
index 1a. open (USD) 2a. high (USD) 3a. low (USD) 4a. close (USD) 5. volume 6. market cap (USD)
0 2014-04-01 468.480106 491.267273 468.480106 479.023434 62894.683068 3.012803e+07
1 2014-04-02 479.679615 494.111337 430.725909 438.377824 96314.587552 4.222218e+07
2 2014-04-03 437.322089 452.441405 414.811104 446.819755 74292.749549 3.319547e+07
3 2014-04-04 448.597144 457.327172 429.184117 451.645992 39267.667348 1.773508e+07
4 2014-04-05 450.027798 466.385470 445.107783 464.379322 21574.144537 1.001859e+07
In [84]:
df['index'] = pd.to_datetime(df['index'])
df.set_index('index', inplace=True)
In [85]:
df.head()
Out[85]:
1a. open (USD) 2a. high (USD) 3a. low (USD) 4a. close (USD) 5. volume 6. market cap (USD)
index
2014-04-01 468.480106 491.267273 468.480106 479.023434 62894.683068 3.012803e+07
2014-04-02 479.679615 494.111337 430.725909 438.377824 96314.587552 4.222218e+07
2014-04-03 437.322089 452.441405 414.811104 446.819755 74292.749549 3.319547e+07
2014-04-04 448.597144 457.327172 429.184117 451.645992 39267.667348 1.773508e+07
2014-04-05 450.027798 466.385470 445.107783 464.379322 21574.144537 1.001859e+07
In [86]:
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1710 entries, 2014-04-01 to 2018-12-05
Data columns (total 6 columns):
1a. open (USD)         1710 non-null float64
2a. high (USD)         1710 non-null float64
3a. low (USD)          1710 non-null float64
4a. close (USD)        1710 non-null float64
5. volume              1710 non-null float64
6. market cap (USD)    1710 non-null float64
dtypes: float64(6)
memory usage: 93.5 KB

We can now visualize the prices:

In [90]:
df['4a. close (USD)'].plot(figsize=(14, 7))
Out[90]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd0dbcb6c50>

Utility function

I've written a small function to simplify the process of pulling the daily data, let's use it to get Ether prices:

In [91]:
from utils import get_daily_data
In [92]:
df = get_daily_data(AV_API_KEY, 'ETH')
In [93]:
df.head()
Out[93]:
1a. open (USD) 2a. high (USD) 3a. low (USD) 4a. close (USD) 5. volume 6. market cap (USD)
index
2015-08-08 3.00000 3.00000 0.15000 1.19999 1942.888147 2331.446348
2015-08-09 1.19999 1.19999 1.19999 1.19999 0.000000 0.000000
2015-08-10 1.19999 1.19999 1.19999 1.19999 0.000000 0.000000
2015-08-11 1.19999 1.19999 0.65038 0.99000 9234.568705 9142.223018
2015-08-12 0.99000 1.28800 0.90500 1.28800 1736.114983 2236.116098
In [94]:
df['4a. close (USD)'].plot(figsize=(14, 7))
Out[94]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd0dee27b00>

A simple multi-currencies experiment

With the get_daily_data it's now simple to pull multiple currencies and work them together, here's a quick example using Bitcoin (BTC), Bitcoin Cash (BCH) and Bitcoin Gold (BTG):

In [95]:
btc = get_daily_data(AV_API_KEY, 'BTC')
bch = get_daily_data(AV_API_KEY, 'BCH')
btg = get_daily_data(AV_API_KEY, 'BTG')
In [96]:
df = pd.DataFrame(index=btc.index)
In [103]:
df = pd.concat([btc['4a. close (USD)'], bch['4a. close (USD)'], btg['4a. close (USD)']], axis=1, join='inner')
In [104]:
df.columns = ['BTC', 'BCH', 'BTG']
In [105]:
df.head()
Out[105]:
BTC BCH BTG
index
2017-10-24 5512.769492 322.941096 106.242199
2017-10-25 5739.145869 328.902454 111.763120
2017-10-26 5893.642088 335.314685 114.858331
2017-10-27 5761.508307 364.390464 102.277440
2017-10-28 5737.983387 417.587081 96.758188

Plotting them:

In [109]:
df[['BTG', 'BCH']].plot(figsize=(14, 7))
Out[109]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd0dee6e0f0>
In [110]:
df[['BTC', 'BCH']].plot(figsize=(14, 7))
Out[110]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd0dbb89470>
In [117]:
(df['BTC'] / df['BCH']).plot(figsize=(14, 7))
(df['BCH'] / df['BTG']).plot(figsize=(14, 7))
Out[117]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd0db614048>
Notebooks AI
Notebooks AI Profile20060