This notebook shows basic usage of the tiingo-python
library. If you're running this on mybinder.org
, you can run this code without installing anything on your computer. You can find more information about what available at the Tiingo website, but this notebook will let you play around with real code samples in your browser.
If you've never used jupyter
before, I recommend this tutorial from Datacamp.
First, you'll need to provide your API key as a string in the cell below. If you forget to do this, the notebook cannot run. You can find your API key by visiting this link and logging in to your Tiingo account.
In [1]:
TIINGO_API_KEY = 'REPLACE-THIS-TEXT-WITH-A-REAL-API-KEY'
# This is here to remind you to change your API key.
if not TIINGO_API_KEY or (TIINGO_API_KEY == 'REPLACE-THIS-TEXT-WITH-A-REAL-API-KEY'):
raise Exception("Please provide a valid Tiingo API key!")
In [2]:
from tiingo import TiingoClient
config = {
'api_key': TIINGO_API_KEY,
'session': True # Reuse HTTP sessions across API calls for better performance
}
# Throughout the rest of this notebook, you'll use the "client" to interact with the Tiingo backend services.
client = TiingoClient(config)
In [3]:
# Get Ticker Metadata for the stock "GOOGL"
ticker_metadata = client.get_ticker_metadata("GOOGL")
print(ticker_metadata)
In [4]:
# Get latest prices, based on 3+ sources as JSON, sampled weekly
ticker_price = client.get_ticker_price("GOOGL", frequency="weekly")
print(ticker_price)
For values of frequency
:
You can specify any of the end of day frequencies (daily, weekly, monthly, and annually) or any intraday frequency for both the get_ticker_price
and get_dataframe
methods. Weekly frequencies resample to the end of day on Friday, monthly frequencies resample to the last day of the month, and annually frequencies resample to the end of day on 12-31 of each year. The intraday frequencies are specified using an integer followed by Min
or Hour
, for example 30Min
or 1Hour
.
In [5]:
# Get historical GOOGL prices from August 2017 as JSON, sampled daily
historical_prices = client.get_ticker_price("GOOGL",
fmt='json',
startDate='2017-08-01',
endDate='2017-08-31',
frequency='daily')
# Print the first 2 days of data, but you will find more days of data in the overall historical_prices variable.
print(historical_prices[:2])
In [6]:
# See what tickers are available
# Check what tickers are available, as well as metadata about each ticker
# including supported currency, exchange, and available start/end dates.
tickers = client.list_stock_tickers()
print(tickers[:2])
For each ticker, you may access
ticker
: The ticker's abbreviationexchange
: Which exchange it's traded onpriceCurrency
: Currency for the prices listed for this tickerstartDate
/ endDate
: Start / End Date for Tiingo's data about this tickerNote that Tiingo is constantly adding new data sources, so the values returned from this call will probably change every day.
In [7]:
# Search news articles about particular tickers
# This method will not work error if you do not have a paid Tiingo account associated with your API key.
articles = client.get_news(tickers=['GOOGL', 'AAPL'],
tags=['Laptops'],
sources=['washingtonpost.com'],
startDate='2017-01-01',
endDate='2017-08-31')
In [8]:
# Display a sample article
articles[0]
Out[8]:
Pandas is a popular python library for data analysis an manipulation. We provide out-of-the-box support for returning responses from Tiingo as Python Dataframes. If you are unfamiliar with pandas
, I recommend the Mode Notebooks
python data analysis tutorial.
In [9]:
# Boilerplate to make pandas charts render inline in jupyter
import matplotlib.pyplot as plt
%matplotlib inline
In [10]:
# Scan some historical Google data
ticker_history_df = client.get_dataframe("GOOGL",
startDate='2018-05-15',
endDate='2018-05-31',
frequency='daily')
In [11]:
# Check which columns you'd like to work with
ticker_history_df.columns
Out[11]:
In [12]:
# Browse the first few entries of the raw data
ticker_history_df.head(5)
Out[12]:
In [13]:
# View your columns of data on separate plots
columns_to_plot = ['adjClose', 'adjOpen']
ticker_history_df[columns_to_plot].plot.line(subplots=True)
Out[13]:
In [14]:
# Plot multiple columns of data in the same chart
ticker_history_df[columns_to_plot].plot.line(subplots=False)
Out[14]:
In [15]:
# Make a histogram to see what typical trading volumes are
ticker_history_df.volume.hist()
Out[15]:
In [ ]:
# You may also fetch data for multiple tickers at once, as long as you are only interested in 1 metric
# at a time. If you need to compare multiple metrics, you must fetch the data 1
# Here we compare Google with Apple's trading volume.
multiple_ticker_history_df = client.get_dataframe(['GOOGL', 'AAPL'],
frequency='weekly',
metric_name='volume',
startDate='2018-01-01',
endDate='2018-07-31')
In [17]:
# Compare the companies: AAPL's volume seems to be much more volatile in the first half of 2018.
multiple_ticker_history_df.plot.line()
Out[17]:
You now have all the tools you need to access the data! We look forward to seeing what you will make with Tiingo