-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIEX_API.py
More file actions
72 lines (56 loc) · 1.93 KB
/
IEX_API.py
File metadata and controls
72 lines (56 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# coding: utf-8
import os
import requests
base_url = 'https://cloud.iexapis.com/v1'
sandbox_url = 'https://sandbox.iexapis.com/stable'
token = os.environ.get('IEX_TOKEN')
params = {'token': token}
resp = requests.get(base_url + '/status')
print(resp)
print(resp.json())
# Historical prices endpoint
resp = requests.get(base_url+'/stock/AAPL/chart', params=params)
resp.raise_for_status()
print(resp.json())
# Converting response to a Pandas DataFrame
import pandas as pd
df = pd.DataFrame(resp.json())
print(df.head())
# Create a function to access historical data
def historical_data(_symbol, _range=None, _date=None):
endpoint = f'{base_url}/stock/{_symbol}/chart'
if _range:
endpoint += f'/{_range}'
elif _date:
endpoint += f'/date/{_date}'
resp = requests.get(endpoint, params=params)
resp.raise_for_status()
return pd.DataFrame(resp.json())
appl_3m_df = historical_data('AAPL', _range='3m')
print(appl_3m_df.head())
appl_april_20_df = historical_data('AAPL', _date=20200420)
print(appl_april_20_df.head())
# Using the sandbox environment
''' This requires a new sandbox token. You can save it as an environment variable (recommended)
or hard code it. You will also need to activate the sanbox environment from within the IEX console.'''
sandbox_params = {'token': 'your_sandbox_api_token'}
resp = requests.get(sandbox_url+'/stock/AAPL/chart', params=sandbox_params)
resp.raise_for_status()
print(resp.json())
# Earnings Today
endpoint = '/stock/market/today-earnings'
resp = requests.get(base_url+endpoint, params=params)
resp.raise_for_status()
print(resp.json())
# News
endpoint = '/stock/TSLA/news'
resp = requests.get(base_url+endpoint, params=params)
resp.raise_for_status()
print(resp.json())
# Investor Exchange Data
payload = params.copy()
payload['symbols'] = 'TSLA, NFLX'
resp = requests.get(base_url+'/tops/last', params=payload)
resp.raise_for_status()
print(resp.json())