Historical data

This page contains some samples for downloading historical data of your quant trading system.

Yahoo Finance API

There are many libraries for downloading historical data of stock markets from Yahoo finance, you can try some of them to discovery which you prefer:

Exchange rates (forex) from Yahoo finance

[1]:
!pip install pandas_datareader
from pandas_datareader import data as pdr
cross_from = ['EUR', 'USD', 'CAD']
cross_to = ['USD', 'JPY', 'USD']
symbols = [x + y + '=X' for x, y in zip(cross_from, cross_to)]
data = pdr.DataReader(symbols, 'yahoo')
data
Requirement already satisfied: pandas_datareader in /opt/conda/lib/python3.8/site-packages (0.9.0)
Requirement already satisfied: requests>=2.19.0 in /opt/conda/lib/python3.8/site-packages (from pandas_datareader) (2.25.1)
Requirement already satisfied: lxml in /opt/conda/lib/python3.8/site-packages (from pandas_datareader) (4.6.2)
Requirement already satisfied: pandas>=0.23 in /opt/conda/lib/python3.8/site-packages (from pandas_datareader) (1.1.5)
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/lib/python3.8/site-packages (from pandas>=0.23->pandas_datareader) (2.8.1)
Requirement already satisfied: pytz>=2017.2 in /opt/conda/lib/python3.8/site-packages (from pandas>=0.23->pandas_datareader) (2020.5)
Requirement already satisfied: numpy>=1.15.4 in /opt/conda/lib/python3.8/site-packages (from pandas>=0.23->pandas_datareader) (1.19.4)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas>=0.23->pandas_datareader) (1.15.0)
Requirement already satisfied: chardet<5,>=3.0.2 in /opt/conda/lib/python3.8/site-packages (from requests>=2.19.0->pandas_datareader) (4.0.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.8/site-packages (from requests>=2.19.0->pandas_datareader) (2020.12.5)
Requirement already satisfied: idna<3,>=2.5 in /opt/conda/lib/python3.8/site-packages (from requests>=2.19.0->pandas_datareader) (2.10)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.8/site-packages (from requests>=2.19.0->pandas_datareader) (1.26.2)
[1]:
Attributes Adj Close Close High Low Open Volume
Symbols EURUSD=X USDJPY=X CADUSD=X EURUSD=X USDJPY=X CADUSD=X EURUSD=X USDJPY=X CADUSD=X EURUSD=X USDJPY=X CADUSD=X EURUSD=X USDJPY=X CADUSD=X EURUSD=X USDJPY=X CADUSD=X
Date
2016-01-13 1.084399 117.830002 0.701508 1.084399 117.830002 0.701508 1.088400 118.362999 0.704871 1.080600 117.779999 0.700334 1.084399 117.831001 0.701459 0.0 0.0 0.0
2016-01-14 1.088601 117.425003 0.696816 1.088601 117.425003 0.696816 1.095000 118.161003 0.697515 1.083999 117.309998 0.694734 1.088803 117.400002 0.696811 0.0 0.0 0.0
2016-01-15 1.085894 118.203003 0.697126 1.085894 118.203003 0.697126 1.098200 118.255997 0.697253 1.085776 116.650002 0.687474 1.085776 118.223000 0.697204 0.0 0.0 0.0
2016-01-18 1.092001 116.989998 0.685965 1.092001 116.989998 0.685965 1.092705 117.433998 0.690274 1.087600 116.931000 0.685857 1.091906 117.010002 0.685857 0.0 0.0 0.0
2016-01-19 1.089503 117.384003 0.687545 1.089503 117.384003 0.687545 1.091000 118.099998 0.692953 1.086200 117.266998 0.687238 1.089503 117.397003 0.687564 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2021-01-05 1.225160 103.125000 0.782411 1.225160 103.125000 0.782411 1.229483 103.180000 0.787402 1.224995 102.671997 0.781959 1.225295 103.141998 0.782473 0.0 0.0 0.0
2021-01-06 1.230027 102.678001 0.788973 1.230027 102.678001 0.788973 1.235025 103.431000 0.791766 1.226693 102.589996 0.786250 1.229861 102.699997 0.788868 0.0 0.0 0.0
2021-01-07 1.234111 103.024002 0.789553 1.234111 103.024002 0.789553 1.234568 103.950996 0.789640 1.224665 102.956001 0.785379 1.233776 103.028000 0.789472 0.0 0.0 0.0
2021-01-08 1.227144 103.790001 0.788407 1.227144 103.790001 0.788407 1.228215 104.078003 0.789952 1.221493 103.616997 0.786201 1.226873 103.794998 0.788308 0.0 0.0 0.0
2021-01-11 1.216841 104.209999 0.782411 1.216841 104.209999 0.782411 1.223990 104.249001 0.788644 1.215806 103.686996 0.782142 1.222643 103.948997 0.788277 0.0 0.0 0.0

1281 rows × 18 columns

Other asset classes from Yahoo finance

[2]:
symbol = 'AMZN'
start = '1997-05-15'
end = '2020-09-30'
period = 'daily'
[3]:
from pandas_datareader import data as pdr
data = pdr.DataReader(symbol, 'yahoo', start, end)
len(data) # 5884 # until 2020-09-30 included
#data.to_csv('pdr_data.csv')
data
[3]:
High Low Open Close Volume Adj Close
Date
1997-05-15 2.500000 1.927083 2.437500 1.958333 72156000.0 1.958333
1997-05-16 1.979167 1.708333 1.968750 1.729167 14700000.0 1.729167
1997-05-19 1.770833 1.625000 1.760417 1.708333 6106800.0 1.708333
1997-05-20 1.750000 1.635417 1.729167 1.635417 5467200.0 1.635417
1997-05-21 1.645833 1.375000 1.635417 1.427083 18853200.0 1.427083
... ... ... ... ... ... ...
2020-09-24 3069.300049 2965.000000 2977.790039 3019.790039 5529400.0 3019.790039
2020-09-25 3101.540039 2999.000000 3054.860107 3095.129883 4615200.0 3095.129883
2020-09-28 3175.040039 3117.169922 3148.850098 3174.050049 4224200.0 3174.050049
2020-09-29 3188.260010 3132.540039 3175.389893 3144.879883 3495800.0 3144.879883
2020-09-30 3212.879883 3133.989990 3141.139893 3148.729980 4896100.0 3148.729980

5884 rows × 6 columns

[4]:
import pandas
!pip install yfinance
import yfinance
data = yfinance.Ticker(symbol)
data.info
df = pandas.DataFrame(data.history(period='max'))
len(df) # 5885 # until 2020-09-30 included + headline
#df.to_csv('yfinance_data.ticker.csv')
df
Requirement already satisfied: yfinance in /opt/conda/lib/python3.8/site-packages (0.1.55)
Requirement already satisfied: multitasking>=0.0.7 in /opt/conda/lib/python3.8/site-packages (from yfinance) (0.0.9)
Requirement already satisfied: lxml>=4.5.1 in /opt/conda/lib/python3.8/site-packages (from yfinance) (4.6.2)
Requirement already satisfied: numpy>=1.15 in /opt/conda/lib/python3.8/site-packages (from yfinance) (1.19.4)
Requirement already satisfied: requests>=2.20 in /opt/conda/lib/python3.8/site-packages (from yfinance) (2.25.1)
Requirement already satisfied: pandas>=0.24 in /opt/conda/lib/python3.8/site-packages (from yfinance) (1.1.5)
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/lib/python3.8/site-packages (from pandas>=0.24->yfinance) (2.8.1)
Requirement already satisfied: pytz>=2017.2 in /opt/conda/lib/python3.8/site-packages (from pandas>=0.24->yfinance) (2020.5)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance) (1.15.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.8/site-packages (from requests>=2.20->yfinance) (1.26.2)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.8/site-packages (from requests>=2.20->yfinance) (2020.12.5)
Requirement already satisfied: chardet<5,>=3.0.2 in /opt/conda/lib/python3.8/site-packages (from requests>=2.20->yfinance) (4.0.0)
Requirement already satisfied: idna<3,>=2.5 in /opt/conda/lib/python3.8/site-packages (from requests>=2.20->yfinance) (2.10)
[4]:
Open High Low Close Volume Dividends Stock Splits
Date
1997-05-15 2.437500 2.500000 1.927083 1.958333 72156000 0 0.0
1997-05-16 1.968750 1.979167 1.708333 1.729167 14700000 0 0.0
1997-05-19 1.760417 1.770833 1.625000 1.708333 6106800 0 0.0
1997-05-20 1.729167 1.750000 1.635417 1.635417 5467200 0 0.0
1997-05-21 1.635417 1.645833 1.375000 1.427083 18853200 0 0.0
... ... ... ... ... ... ... ...
2021-01-04 3270.000000 3272.000000 3144.020020 3186.629883 4411400 0 0.0
2021-01-05 3166.010010 3223.379883 3165.060059 3218.510010 2655500 0 0.0
2021-01-06 3146.479980 3197.510010 3131.159912 3138.379883 4394800 0 0.0
2021-01-07 3157.000000 3208.540039 3155.000000 3162.159912 3514500 0 0.0
2021-01-08 3180.000000 3190.639893 3142.199951 3182.699951 3534300 0 0.0

5953 rows × 7 columns

[5]:
data = yfinance.download(symbol, start=start, end=end, group_by='ticker')
len(data) # 5883 # until 2020-09-30 excluded
#data.to_csv('yfinance_data.download.csv')
data
[*********************100%***********************]  1 of 1 completed
[5]:
Open High Low Close Adj Close Volume
Date
1997-05-15 2.437500 2.500000 1.927083 1.958333 1.958333 72156000
1997-05-16 1.968750 1.979167 1.708333 1.729167 1.729167 14700000
1997-05-19 1.760417 1.770833 1.625000 1.708333 1.708333 6106800
1997-05-20 1.729167 1.750000 1.635417 1.635417 1.635417 5467200
1997-05-21 1.635417 1.645833 1.375000 1.427083 1.427083 18853200
... ... ... ... ... ... ...
2020-09-23 3120.429932 3127.000000 2992.379883 2999.860107 2999.860107 5652700
2020-09-24 2977.790039 3069.300049 2965.000000 3019.790039 3019.790039 5529400
2020-09-25 3054.860107 3101.540039 2999.000000 3095.129883 3095.129883 4615200
2020-09-28 3148.850098 3175.040039 3117.169922 3174.050049 3174.050049 4224200
2020-09-29 3175.389893 3188.260010 3132.540039 3144.879883 3144.879883 3495800

5883 rows × 6 columns

[6]:
yfinance.pdr_override()
data = pdr.get_data_yahoo(symbol, start=start, end=end)
len(data) # 5883 # until 2020-09-30 excluded
#data.to_csv('yfpdr_data.csv')
data
[*********************100%***********************]  1 of 1 completed
[6]:
Open High Low Close Adj Close Volume
Date
1997-05-15 2.437500 2.500000 1.927083 1.958333 1.958333 72156000
1997-05-16 1.968750 1.979167 1.708333 1.729167 1.729167 14700000
1997-05-19 1.760417 1.770833 1.625000 1.708333 1.708333 6106800
1997-05-20 1.729167 1.750000 1.635417 1.635417 1.635417 5467200
1997-05-21 1.635417 1.645833 1.375000 1.427083 1.427083 18853200
... ... ... ... ... ... ...
2020-09-23 3120.429932 3127.000000 2992.379883 2999.860107 2999.860107 5652700
2020-09-24 2977.790039 3069.300049 2965.000000 3019.790039 3019.790039 5529400
2020-09-25 3054.860107 3101.540039 2999.000000 3095.129883 3095.129883 4615200
2020-09-28 3148.850098 3175.040039 3117.169922 3174.050049 3174.050049 4224200
2020-09-29 3175.389893 3188.260010 3132.540039 3144.879883 3144.879883 3495800

5883 rows × 6 columns

[7]:
!pip install yahoofinancials
from yahoofinancials import YahooFinancials
data = YahooFinancials(symbol)
json = data.get_historical_price_data(start_date=start, end_date=end, time_interval=period)
df = pandas.DataFrame(json[symbol]['prices'])
len(df) # 5883 # until 2020-09-30 excluded and seconds instead of date
#df.to_csv('yahoofinancials_data.csv')
df
Requirement already satisfied: yahoofinancials in /opt/conda/lib/python3.8/site-packages (1.6)
Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.8/site-packages (from yahoofinancials) (4.9.3)
Requirement already satisfied: pytz in /opt/conda/lib/python3.8/site-packages (from yahoofinancials) (2020.5)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.8/site-packages (from beautifulsoup4->yahoofinancials) (2.0.1)
[7]:
date high low open close volume adjclose formatted_date
0 863703000 2.500000 1.927083 2.437500 1.958333 72156000 1.958333 1997-05-15
1 863789400 1.979167 1.708333 1.968750 1.729167 14700000 1.729167 1997-05-16
2 864048600 1.770833 1.625000 1.760417 1.708333 6106800 1.708333 1997-05-19
3 864135000 1.750000 1.635417 1.729167 1.635417 5467200 1.635417 1997-05-20
4 864221400 1.645833 1.375000 1.635417 1.427083 18853200 1.427083 1997-05-21
... ... ... ... ... ... ... ... ...
5878 1600867800 3127.000000 2992.379883 3120.429932 2999.860107 5652700 2999.860107 2020-09-23
5879 1600954200 3069.300049 2965.000000 2977.790039 3019.790039 5529400 3019.790039 2020-09-24
5880 1601040600 3101.540039 2999.000000 3054.860107 3095.129883 4615200 3095.129883 2020-09-25
5881 1601299800 3175.040039 3117.169922 3148.850098 3174.050049 4224200 3174.050049 2020-09-28
5882 1601386200 3188.260010 3132.540039 3175.389893 3144.879883 3495800 3144.879883 2020-09-29

5883 rows × 8 columns

[8]:
!pip install yahoo_fin
from yahoo_fin import stock_info as si
data = si.get_data(symbol)
len(data) # 5885 # until 2020-09-30 included (+ headline)
#data.to_csv('yahoo_fin_data.csv')
data
Requirement already satisfied: yahoo_fin in /opt/conda/lib/python3.8/site-packages (0.8.6)
Warning - Certain functionality
             requires requests_html, which is not installed.

             Install using:
             pip install requests_html

             After installation, you may have to restart your Python session.
[8]:
open high low close adjclose volume ticker
1997-05-15 2.437500 2.500000 1.927083 1.958333 1.958333 72156000 AMZN
1997-05-16 1.968750 1.979167 1.708333 1.729167 1.729167 14700000 AMZN
1997-05-19 1.760417 1.770833 1.625000 1.708333 1.708333 6106800 AMZN
1997-05-20 1.729167 1.750000 1.635417 1.635417 1.635417 5467200 AMZN
1997-05-21 1.635417 1.645833 1.375000 1.427083 1.427083 18853200 AMZN
... ... ... ... ... ... ... ...
2021-01-04 3270.000000 3272.000000 3144.020020 3186.629883 3186.629883 4411400 AMZN
2021-01-05 3166.010010 3223.379883 3165.060059 3218.510010 3218.510010 2655500 AMZN
2021-01-06 3146.479980 3197.510010 3131.159912 3138.379883 3138.379883 4394800 AMZN
2021-01-07 3157.000000 3208.540039 3155.000000 3162.159912 3162.159912 3514500 AMZN
2021-01-08 3180.000000 3190.639893 3142.199951 3182.699951 3182.699951 3534300 AMZN

5953 rows × 7 columns

Exchange rates (forex) from Oanda

Oanda API allows to download the historical data with the API key of a free Oanda account: you can find the Oanda library or you can use many other backtesting libraries for this.

But this is a general tutorial, so you can find the data by oanda.URL that the getSymbols method of R quantmod library uses. When you use this method, you can only download the historical data of the last six months.

[9]:
import json
import pandas
import requests
from datetime import datetime, timedelta
base_url = 'https://www.oanda.com/fx-for-business/historical-rates/api/data/update/?&source=OANDA&adjustment=0&base_currency={to_currency}&start_date={start_date}&end_date={end_date}&period={period}&price=mid&view=table&quote_currency_0={from_currency}'
today = datetime.strftime(datetime.now(), '%Y-%m-%d')
def get_oanda_currency_historical_rates(base_url, start, end, quote_currency, base_currency, period):
    url = base_url.format(from_currency=quote_currency, to_currency=base_currency, start_date=start, end_date=end, period=period)
    response = json.loads(requests.get(url).content.decode('utf-8'))
    return pandas.DataFrame(response['widget'][0]['data'])
# the last 6 months
df = get_oanda_currency_historical_rates(base_url, datetime.strftime(datetime.now() - timedelta(180), '%Y-%m-%d'), today, 'EUR', 'USD', period)
df
[9]:
0 1
0 1610236800000 0.818336
1 1610150400000 0.818270
2 1610064000000 0.816854
3 1609977600000 0.813786
4 1609891200000 0.812080
... ... ...
175 1595116800000 0.875000
176 1595030400000 0.875028
177 1594944000000 0.876130
178 1594857600000 0.877077
179 1594771200000 0.875957

180 rows × 2 columns

[10]:
# the last 60 days
df = get_oanda_currency_historical_rates(base_url, datetime.strftime(datetime.now() - timedelta(60), '%Y-%m-%d'), today, 'EUR', 'USD', period)
df.head()
[10]:
0 1
0 1610236800000 0.818336
1 1610150400000 0.818270
2 1610064000000 0.816854
3 1609977600000 0.813786
4 1609891200000 0.812080

Crypto currencies from CoinMarketCap

CoinMarketCap API allows to download the historical data with the API key of a free CoinMarketCap account: you can find the CoinMarketCap library or you can use many other backtesting libraries for this.

Like for the all assets, there are many libraries that they implement Coinmarketcap API, but many are open source so the updates could be slowly:

Below you can find a sample with sandbox API of CoinMarketCap.

[11]:
import json
import pandas
import requests
base_url = 'https://sandbox-api.coinmarketcap.com/v1/'
def get_coinmarketcap_data(url):
    response = json.loads(requests.get(url).content.decode('utf-8'))
    return pandas.DataFrame(response['data'])

# returns all active cryptocurrencies
url = base_url + 'cryptocurrency/map'
marketcap_map = get_coinmarketcap_data(url)
marketcap_map
[11]:
id name symbol slug is_active rank platform
0 1 Bitcoin BTC bitcoin 1 1 None
1 2 Litecoin LTC litecoin 1 5 None
2 3 Namecoin NMC namecoin 1 310 None
3 4 Terracoin TRC terracoin 1 926 None
4 5 Peercoin PPC peercoin 1 346 None
... ... ... ... ... ... ... ...
2322 4266 Monarch MT monarch 1 2059 {'id': 1027, 'name': 'Ethereum', 'symbol': 'ET...
2323 4268 NewYork Exchange NYE newyork-exchange 1 2012 None
2324 4273 Sessia KICKS sessia 1 2118 {'id': 1027, 'name': 'Ethereum', 'symbol': 'ET...
2325 4275 Cocos-BCX COCOS cocos-bcx 1 1960 {'id': 1027, 'name': 'Ethereum', 'symbol': 'ET...
2326 4276 Defi DEFI defi 1 1965 {'id': 1027, 'name': 'Ethereum', 'symbol': 'ET...

2327 rows × 7 columns

[12]:
# returns last market data of all active cryptocurrencies
url = base_url + 'cryptocurrency/listings/latest?convert=EUR'
last_marketcap = get_coinmarketcap_data(url)
last_marketcap
[12]:
id name symbol slug num_market_pairs date_added tags max_supply circulating_supply total_supply platform cmc_rank last_updated quote
0 1 Bitcoin BTC bitcoin 7919 2013-04-28T00:00:00.000Z [mineable] 2.100000e+07 1.790601e+07 1.790601e+07 None 1 2019-08-30T18:51:28.000Z {'EUR': {'price': 8708.442730269642, 'volume_2...
1 1027 Ethereum ETH ethereum 5629 2015-08-07T00:00:00.000Z [mineable] NaN 1.075379e+08 1.075379e+08 None 2 2019-08-30T18:51:21.000Z {'EUR': {'price': 153.68597253794135, 'volume_...
2 52 XRP XRP ripple 449 2013-08-04T00:00:00.000Z [] 1.000000e+11 4.293287e+10 9.999137e+10 None 3 2019-08-30T18:51:03.000Z {'EUR': {'price': 0.23181863120417767, 'volume...
3 1831 Bitcoin Cash BCH bitcoin-cash 378 2017-07-23T00:00:00.000Z [mineable] 2.100000e+07 1.797598e+07 1.797598e+07 None 4 2019-08-30T18:51:08.000Z {'EUR': {'price': 256.03512635975414, 'volume_...
4 2 Litecoin LTC litecoin 538 2013-04-28T00:00:00.000Z [mineable] 8.400000e+07 6.314712e+07 6.314712e+07 None 5 2019-08-30T18:51:04.000Z {'EUR': {'price': 58.62607480610576, 'volume_2...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
95 3709 Grin GRIN grin 53 2019-01-27T00:00:00.000Z [mineable] NaN 1.954596e+07 1.954596e+07 None 96 2019-08-30T18:51:18.000Z {'EUR': {'price': 1.7584409339670577, 'volume_...
96 2694 Nexo NEXO nexo 22 2018-05-01T00:00:00.000Z [] NaN 5.600000e+08 1.000000e+09 {'id': 1027, 'name': 'Ethereum', 'symbol': 'ET... 97 2019-08-30T18:51:12.000Z {'EUR': {'price': 0.06125607986488225, 'volume...
97 2900 Project Pai PAI project-pai 17 2018-07-05T00:00:00.000Z [] NaN 1.451234e+09 1.618050e+09 None 98 2019-08-30T18:51:14.000Z {'EUR': {'price': 0.022456686270928363, 'volum...
98 2137 Electroneum ETN electroneum 20 2017-11-02T00:00:00.000Z [mineable] 2.100000e+10 9.792946e+09 9.792946e+09 None 99 2019-08-30T18:51:08.000Z {'EUR': {'price': 0.0032193992228962184, 'volu...
99 1681 PRIZM PZM prizm 7 2017-05-19T00:00:00.000Z [] 6.000000e+09 9.820737e+07 9.820737e+07 None 100 2019-08-30T18:51:05.000Z {'EUR': {'price': 0.3204834704804656, 'volume_...

100 rows × 14 columns

[13]:
# returns last market data of Bitcoin
url = base_url + 'cryptocurrency/quotes/latest?id=1&convert=EUR'
last_bitcoin = get_coinmarketcap_data(url)
last_bitcoin
[13]:
1
circulating_supply 17906012
cmc_rank 1
date_added 2013-04-28T00:00:00.000Z
id 1
is_active 1
is_fiat 0
is_market_cap_included_in_calc 1
last_updated 2019-08-30T18:51:28.000Z
max_supply 21000000
name Bitcoin
num_market_pairs 7919
platform None
quote {'EUR': {'price': 8708.442730269642, 'volume_2...
slug bitcoin
symbol BTC
tags [mineable]
total_supply 17906012
[14]:
# returns historical data from a time_start to a time_end
url = base_url + 'cryptocurrency/quotes/historical?id=1&convert=EUR&time_start=2020-05-21T12:14&time_end=2020-05-21T12:44'
data = get_coinmarketcap_data(url)
data
[14]:
id name symbol is_fiat quotes
0 1 Bitcoin BTC 0 {'timestamp': '2020-05-21T12:19:03.000Z', 'quo...
1 1 Bitcoin BTC 0 {'timestamp': '2020-05-21T12:24:02.000Z', 'quo...
2 1 Bitcoin BTC 0 {'timestamp': '2020-05-21T12:29:03.000Z', 'quo...
3 1 Bitcoin BTC 0 {'timestamp': '2020-05-21T12:34:03.000Z', 'quo...
4 1 Bitcoin BTC 0 {'timestamp': '2020-05-21T12:39:01.000Z', 'quo...
5 1 Bitcoin BTC 0 {'timestamp': '2020-05-21T12:44:02.000Z', 'quo...

Other asset classes from Quandl

You can see details of asset classes of Quandl, by its research, for example crude oil.

Quandl API allows to download the historical data with the API key of a free Quandl account: you can find the Quandl library (doc) or you can use many other backtesting libraries for this.

However, it is possible to execute 50 calls per day without API key.

[15]:
symbol = 'CHRIS/CME_QM1'

import pandas
from datetime import datetime
base_url = 'https://www.quandl.com/api/v3/datasets/{symbol}.csv?start_date={start_date}&end_date={end_date}&collapse={period}'
today = datetime.strftime(datetime.now(), '%Y-%m-%d')
def get_quandl_financial_data(base_url, symbol, start, end, period):
    url = base_url.format(symbol=symbol, start_date=start, end_date=end, period=period)
    return pandas.read_csv(url)
# returns all free columns values
df = get_quandl_financial_data(base_url, symbol, start, end, period)
df
[15]:
Date Open High Low Last Change Settle Volume Previous Day Open Interest
0 2020-09-30 39.225 40.375 38.675 39.850 0.93 40.22 10605.0 1252.0
1 2020-09-29 40.575 40.700 38.425 39.075 -1.31 39.29 12589.0 784.0
2 2020-09-28 40.125 40.800 39.775 40.600 0.35 40.60 8194.0 787.0
3 2020-09-25 40.175 40.625 39.700 40.075 -0.06 40.25 7024.0 879.0
4 2020-09-24 39.600 40.350 39.125 40.200 0.38 40.31 8349.0 869.0
... ... ... ... ... ... ... ... ... ...
1646 2014-03-12 99.500 99.575 97.550 97.990 2.04 97.99 9437.0 2462.0
1647 2014-03-11 100.925 101.500 99.350 100.030 1.09 100.03 7337.0 2300.0
1648 2014-03-07 101.900 102.900 101.575 102.580 1.02 102.58 5304.0 2315.0
1649 2014-03-06 101.000 102.050 100.150 101.560 0.11 101.56 6850.0 2383.0
1650 2014-03-05 103.325 103.525 100.825 101.450 1.88 101.45 7459.0 2305.0

1651 rows × 9 columns

[16]:
# returns only open values
df['Open'] # or
df = get_quandl_financial_data(base_url + '&column_index=1', symbol, start, end, period)
df
[16]:
Date Open
0 2020-09-30 39.225
1 2020-09-29 40.575
2 2020-09-28 40.125
3 2020-09-25 40.175
4 2020-09-24 39.600
... ... ...
1646 2014-03-12 99.500
1647 2014-03-11 100.925
1648 2014-03-07 101.900
1649 2014-03-06 101.000
1650 2014-03-05 103.325

1651 rows × 2 columns

[17]:
!pip install quandl
import quandl
# returns all free columns values
data = quandl.get(symbol)
data
Requirement already satisfied: quandl in /opt/conda/lib/python3.8/site-packages (3.5.3)
Requirement already satisfied: requests>=2.7.0 in /opt/conda/lib/python3.8/site-packages (from quandl) (2.25.1)
Requirement already satisfied: six in /opt/conda/lib/python3.8/site-packages (from quandl) (1.15.0)
Requirement already satisfied: python-dateutil in /opt/conda/lib/python3.8/site-packages (from quandl) (2.8.1)
Requirement already satisfied: inflection>=0.3.1 in /opt/conda/lib/python3.8/site-packages (from quandl) (0.5.1)
Requirement already satisfied: more-itertools in /opt/conda/lib/python3.8/site-packages (from quandl) (8.6.0)
Requirement already satisfied: numpy>=1.8 in /opt/conda/lib/python3.8/site-packages (from quandl) (1.19.4)
Requirement already satisfied: pandas>=0.14 in /opt/conda/lib/python3.8/site-packages (from quandl) (1.1.5)
Requirement already satisfied: pytz>=2017.2 in /opt/conda/lib/python3.8/site-packages (from pandas>=0.14->quandl) (2020.5)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.8/site-packages (from requests>=2.7.0->quandl) (2020.12.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.8/site-packages (from requests>=2.7.0->quandl) (1.26.2)
Requirement already satisfied: chardet<5,>=3.0.2 in /opt/conda/lib/python3.8/site-packages (from requests>=2.7.0->quandl) (4.0.0)
Requirement already satisfied: idna<3,>=2.5 in /opt/conda/lib/python3.8/site-packages (from requests>=2.7.0->quandl) (2.10)
[17]:
Open High Low Last Change Settle Volume Previous Day Open Interest
Date
2014-03-05 103.325 103.525 100.825 101.450 1.88 101.45 7459.0 2305.0
2014-03-06 101.000 102.050 100.150 101.560 0.11 101.56 6850.0 2383.0
2014-03-07 101.900 102.900 101.575 102.580 1.02 102.58 5304.0 2315.0
2014-03-11 100.925 101.500 99.350 100.030 1.09 100.03 7337.0 2300.0
2014-03-12 99.500 99.575 97.550 97.990 2.04 97.99 9437.0 2462.0
... ... ... ... ... ... ... ... ...
2021-01-04 48.400 49.850 47.200 47.350 -0.90 47.62 21845.0 1350.0
2021-01-05 47.400 50.200 47.275 49.850 2.31 49.93 23473.0 1799.0
2021-01-06 49.825 50.925 49.475 50.525 0.70 50.63 16675.0 1908.0
2021-01-07 50.525 51.275 50.400 50.950 0.20 50.83 10814.0 1800.0
2021-01-08 50.925 52.750 50.825 52.750 1.41 52.24 10770.0 1833.0

1720 rows × 8 columns

[18]:
# returns only open values
data['Open']
[18]:
Date
2014-03-05    103.325
2014-03-06    101.000
2014-03-07    101.900
2014-03-11    100.925
2014-03-12     99.500
               ...
2021-01-04     48.400
2021-01-05     47.400
2021-01-06     49.825
2021-01-07     50.525
2021-01-08     50.925
Name: Open, Length: 1720, dtype: float64