Now that we know how to use Quandl to get data, we want to use Quandl in conjunction with our current data building script to do some improved analysis.
The only major change you may want to make from what I make below / in the video is in the line where we identify the ticker:
ticker = each_dir.split("\\")[1]
Instead of [1], you may want to use [-1], since your directory is likely to be different than mine. [-1] is logically the superior choice to use.
import pandas as pd import os from Quandl import Quandl import time auth_tok = "yourauthtoken" path = "X:/Backups/intraQuarter" def Stock_Prices(): df = pd.DataFrame() statspath = path+'/_KeyStats' stock_list = [x[0] for x in os.walk(statspath)] print(stock_list) for each_dir in stock_list[1:]: try: ticker = each_dir.split("\\")[1] print(ticker) name = "WIKI/"+ticker.upper() data = Quandl.get(name, trim_start = "2000-12-12", trim_end = "2014-12-30", authtoken=auth_tok)Above, we're pulling the data we went, but this data comes with many columns. The column we want is the one that is for the adjusted close. "Adjusted close" is a closing price that accounts for any stock splits and adjusts historical prices for accuracy.
data[ticker.upper()] = data["Adj. Close"] df = pd.concat([df, data[ticker.upper()]], axis = 1) except Exception as e: print(str(e)) time.sleep(10)Here, we've defined that specific column we want, we're then renaming it to be the stock's ticker, then we're concatenating (adding it) it do the dataframe. Concatenation is not to be confused with appending. Appending is when we add more rows to the data frame. Concatenating is when we add more columns of data. Now, at the time of my filming, the Quandl website was acting up and going down every now and then. Because of this, we added another of the exact same loop within an exception. This way, the program will try twice before giving up. So we'll do that, and then finish up the program:
try: ticker = each_dir.split("\\")[1] print(ticker) name = "WIKI/"+ticker.upper() data = Quandl.get(name, trim_start = "2000-12-12", trim_end = "2014-12-30", authtoken=auth_tok) data[ticker.upper()] = data["Adj. Close"] df = pd.concat([df, data[ticker.upper()]], axis = 1) except Exception as e: print(str(e)) df.to_csv("stock_prices.csv") Stock_Prices()