<jupyter_start><jupyter_text>**Financial Data Tools: Tiingo and Pandas Data Reader**This notebook contains a short demonstration of some of the features of the Tiingo API and the Pandas Data Reader library.Tiingo documentation:Tiingo API: https://api.tiingo.comTiingo on Pypi.org: https://pypi.org/project/tiingo/descriptionTiingo on readthedocs: https://tiingo-python.readthedocs.io/en/latest/readme.htmlusagePandas Data Reader documentation:GitHub: https://github.com/pydata/pandas-datareaderReadthedocs (Tiingo section): https://pandas-datareader.readthedocs.io/en/latest/readers/tiingo.htmlmodule-pandas_datareader.tiingo First, install tiingo. You can do this from the command line if you prefer.<jupyter_code>!pip install tiingo
import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>For some reason, I need to point the code to where tiingo is installed on my machine. YMMV.<jupyter_code>import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages")<jupyter_output><empty_output><jupyter_text>Import TiingoClient and initialize.<jupyter_code>from tiingo import TiingoClient
# 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.
config = {}
# To reuse the same HTTP Session across API calls (and have better performance), include a session key.
config['session'] = True

### You will need to get an API key from  https://api.tiingo.com
### The API key is free and includes a max number of requests per hour and day, and data transfer.
### Paid account will give more requests and data transfer.

# If you don't have your API key as an environment variable,
# pass it in via a configuration dictionary.
config['api_key'] = "Your API key here"
#Initialize
client = TiingoClient(config)<jupyter_output><empty_output><jupyter_text>Now install Pandas Data Reader (here, or from the command line).<jupyter_code>!pip install pandas-datareader
import os
import pandas_datareader as pdr<jupyter_output><empty_output><jupyter_text>You can pass individual ticker symbols or a list of ticker symbols into the functions. Let's get the metadata for Gamestop (ticker symbol GME).<jupyter_code>gamestop_metadata = client.get_ticker_metadata("GME")
print(gamestop_metadata)
##Here is the metadata about Google in a dataframe format (a bit easier to read!):
df_gamestop=pd.DataFrame.from_dict(gamestop_metadata, orient='index') 
df_gamestop<jupyter_output><empty_output><jupyter_text>Instead of single ticker symbols, you can pass a list of ticker symbols into client.get_ticker_metadata. Note that ticker symbols do not have to be uppercase.<jupyter_code>company_symbols = ['EXEL', 'MSFT', 'gme','AMGN', 'DNA']
dict_list=[]
for symbol in company_symbols:
    dict_companies=client.get_ticker_metadata(symbol)
    dict_list.append(dict_companies)
df__company_symbols=pd.DataFrame(dict_list) 
df__company_symbols.head()<jupyter_output><empty_output><jupyter_text>What if we want to get historical price data for our list of stocks? Use pdr.get_data(), pass in the list of symbols, date range and API key. The last few days in January 2021 were interesting for Gamestop...![Screen%20Shot%202021-03-09%20at%207.59.18%20PM.png](attachment:Screen%20Shot%202021-03-09%20at%207.59.18%20PM.png)<jupyter_code>#Pass in the list of ticker symbols, and pandas datareader will download 
#the stock data for the specified time period.
#Need to add error handling for KeyErrors, because some dates are out of range.

test_hist_data_list=[]

for symbol in company_symbols:
    try:
        df_test_prices = pdr.get_data_tiingo(symbol, start='2021-01-25', end='2021-01-29', 
                                           pause=0.2, api_key='Your API Key here') 
        test_hist_data_list.append(df_test_prices)
       
    except KeyError as ke:
        print('KeyError ', ke)<jupyter_output>KeyError  'date'<jupyter_text>One of our stocks had a very interesting couple of days. Let's take a look.<jupyter_code>print(test_hist_data_list[2])
df_test_hist_data=pd.concat(test_hist_data_list, axis=0)
df_test_hist_data<jupyter_output><empty_output><jupyter_text>What if we want to read news about specific stocks, topics, or just news in general? https://tiingo-python.readthedocs.io/en/latest/readme.htmlusage<jupyter_code>##Use the Tiingo client to get the news. You can pass in tickers, tags, sources, and start
##end dates. See info on the readthedocs website above.
gme_news = client.get_news(tickers=['gme'],
                            #tags=['Laptops'],
                            sources=['washingtonpost.com'],
                            startDate='2021-01-15',
                            endDate='2021-01-29')
df_gme_news=pd.DataFrame(gme_news)
df_gme_news.head(10)<jupyter_output><empty_output><jupyter_text>To get a list of symbols for which Tiingo can access data, and save it to a csv file, use:<jupyter_code>df_symbols=pdr.tiingo.get_tiingo_symbols()
df_symbols.to_csv('tiingo_symbols.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09153679226671928, 'fraction_numerical': 0.01814953639771158, 'mean_word_length': 5.035714285714286, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '551492', 'license': 'MIT', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1613, 'n_words': 475, 'path': 'Investigation-financial-data-tools.ipynb', 'repo_name': 'sutrofog/Metis-Investigation', 'size': '5069'}