Remote Data Access Tiingo Examples¶
Need a FREE TIINGO API key
In [52]:
import os
import pandas_datareader as pdr
import pandas as pd
import pandas
import matplotlib as mpl
%matplotlib inline
!pip3 install tiingo[pandas]¶
Set TIINGO_API_KEY in your environment variables in your .bash_profile, OR pass a dictionary with 'api_key' as a key into the TiingoClient.
In [53]:
config = {}
# To reuse the same HTTP Session across API calls (and have better performance), include a session key.
config['session'] = True
# If you don't have your API key as an environment variable,
# pass it in via a configuration dictionary.
config['api_key'] = "Your Tiingo API Key"
# Initialize
client = TiingoClient(config)
Get Ticker¶
In [54]:
ticker_metadata = client.get_ticker_metadata("GOOGL")
In [55]:
# Get latest prices, based on 3+ sources as JSON, sampled weekly
In [56]:
ticker_price = client.get_ticker_price("GOOGL", frequency="weekly")
In [57]:
# Get historical GOOGL prices from August 2017 as JSON, sampled daily
In [58]:
historical_prices = client.get_ticker_price("GOOGL",
fmt='json',
startDate='2019-08-01',
endDate='2019-08-31',
frequency='daily')
Create a text file and save results¶
In [59]:
with open("google_2019_file.txt", "w") as output:
output.write(str(historical_prices))
In [37]:
# To receive results in pandas format, use the get_dataframe() method:
#Get a pd.DataFrame of the price history of a single symbol (default is daily):
In [38]:
ticker_history = client.get_dataframe("GOOGL")
In [39]:
ticker_history.head()
Out[39]:
adjClose | adjHigh | adjLow | adjOpen | adjVolume | close | divCash | high | low | open | splitFactor | volume | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
date | ||||||||||||
2021-03-26 00:00:00+00:00 | 2024.73 | 2039.385 | 2003.85 | 2031.79 | 1398875 | 2024.73 | 0.0 | 2039.385 | 2003.85 | 2031.79 | 1.0 | 1398875 |
The method returns all of the available information on a symbol, such as open, high, low, close,¶
adjusted close, etc. This page in the tiingo api documentation lists the available information on each¶
symbol:¶
https://api.tiingo.com/docs/tiingo/daily#priceData.
Frequencies and start and end dates can be specified similarly to the json method above.
Get a pd.Series of only one column of the available response data by specifying one of the valid the 'metric_name' parameters:
In [40]:
ticker_history = client.get_dataframe("GOOGL", metric_name='adjClose')
In [41]:
ticker_history
Out[41]:
date 2021-03-26 00:00:00+00:00 2024.73 Name: adjClose, dtype: float64
In [42]:
#Get a pd.DataFrame for a list of symbols for a specified metric_name (default is adjClose if no
# metric_name is specified):
In [43]:
ticker_history2 = client.get_dataframe(['GOOGL', 'AAPL'],
frequency='weekly',
metric_name='volume',
startDate='2017-01-01',
endDate='2018-05-31')
In [44]:
ticker_history2
Out[44]:
GOOGL | AAPL | |
---|---|---|
2016-12-30 00:00:00+00:00 | 4983989 | 84828531 |
2017-01-06 00:00:00+00:00 | 6832004 | 103845468 |
2017-01-13 00:00:00+00:00 | 6572250 | 138810760 |
2017-01-20 00:00:00+00:00 | 4845240 | 116347987 |
2017-01-27 00:00:00+00:00 | 13053648 | 124748449 |
... | ... | ... |
2018-05-04 00:00:00+00:00 | 9034426 | 252805668 |
2018-05-11 00:00:00+00:00 | 8725368 | 148266951 |
2018-05-18 00:00:00+00:00 | 8096281 | 99248752 |
2018-05-25 00:00:00+00:00 | 5698651 | 94394844 |
2018-06-01 00:00:00+00:00 | 10366549 | 92129925 |
75 rows × 2 columns
In [ ]:
Comments
Post a Comment