Python Stock API Example

It’s easy to download our JSON API with Python scripts, please do not forget to change the API token to yours, that you can get on the settings page after the free registration.

import urllib, json

url = "https://eodhistoricaldata.com/api/eod/AAPL.US?api_token=YOUR_API_TOKEN&order=d&fmt=json"

response = urllib.urlopen(url)

data = json.loads(response.read())

print data

There is also a simple Python example was written for us by Femto Trader. There is a small example, for more information you can find on GitHub, check python-eodhistoricaldata.

import requests

import pandas as pd

from io import StringIO

def get_eod_data(symbol=”AAPL.US”, api_token=”xxxx”, session=None):

if session is None:

session = requests.Session()

url = ‘https://eodhistoricaldata.com/api/eod/%s’ % symbol

params = {“api_token”: api_token}

r = session.get(url, params=params)

if r.status_code == requests.codes.ok:

df = pd.read_csv(StringIO(r.text), skipfooter=1, parse_dates=[0], index_col=0, engine=’python’)

return df

else:raise Exception(r.status_code, r.reason, url)

You can download data simply using

df = get_eod_data(“AAPL.US”)

And if you want to avoid too much data consumption, you can use a cache mechanism

import datetime

import requests_cache

expire_after = datetime.timedelta(days=1)

session = requests_cache.CachedSession(cache_name=’cache’, backend=’sqlite’, expire_after=expire_after)

df = get_eod_data(“AAPL.US“, session=session)

Register & Get Data

Great thanks to Femto Trader (Github) and don’t forget about the full python example python-eodhistoricaldata.