from datetime import date # Time period of import, start and end dates start = date(2017, 10, 01) end = date(2017, 11, 06) # DataReader is a function to import, there are different sources available to import data # such as ggogle fin, yahoo fin,fred, Oanda(for exchange rates) # for eg Importing FB data from goolge stockFb = DataReader('fb', 'google', start, end) type(stockFb) # DataReader returns a pandas data frame object stockFb.head() stockFb.info() # from yahoo stockApl = DataReader('AAPL', 'yahoo', start, end) stockApl.head() stockApl.info() #plotting stockApl['Close'].plot(title='APPLE') plt.show() #sp500 from fred up to now sp500 = DataReader('SP500', 'fred', start) #note sys date is deafult for end argument sp500.tail() sp500.plot(title='SP500')
stock_data = DataReader(ticker, data_source, start, end) In the first chapter, you learned that a stock ticker is the unique symbol needed to get stock information for a certain company. In this exercise, you will practice importing the 2016 data for Apple, with ticker 'AAPL'. ''' # Import DataReader from pandas_datareader.data import DataReader # Import date from datetime import date # Set start and end dates start = date(2016, 1, 1) end = date(2016, 12, 31) # Set the ticker ticker = 'AAPL' # Set the data source data_source = 'google' # Import the stock prices stock_prices = DataReader(ticker, data_source, start, end) # Display and inspect the result print(stock_prices.head()) stock_prices.info()
import pandas as pd import matplotlib.pyplot as plt from pandas_datareader.data import DataReader from datetime import date start = date(1900,1,1) # default Jan 1, 2010 series_code = 'DGS10' # 10-year Treasury Rate data_source = 'fred' # FED Economic Data Service data = DataReader(series_code, data_source, start) data.info() pd.concat([data.head(3), data.tail(3)]) series_name = '10-year Treasury' data = data.rename(columns={series_code: series_name}) data.plot(title=series_name) plt.show()
# Set start and end dates start = date(2016, 1, 1) end = date(2016, 12, 31) # Set the ticker ticker = 'AAPL' # Set the data source data_source = 'iex' # Import the stock prices stock_prices = DataReader(ticker, data_source, start, end) # Display and inspect the result print(stock_prices.head()) stock_prices.info() ### Visualization # Import matplotlib.pyplot import matplotlib.pyplot as plt # Set start and end dates start = date(2016, 1, 1) end = date(2016, 12, 31) # Set the ticker and data_source ticker = 'FB' data_source = 'iex' # Import the data using DataReader
""" from pandas_datareader.data import DataReader from datetime import date #Date & time functionality import matplotlib.pyplot as plt import pandas as pd start = date(2015, 1, 1) end = date(2016, 12, 31) ticker = 'GOOG' data_source = 'google' stock_data = DataReader(ticker, data_source, start, end) (stock_data.info()) stock_data['Close'].plot(title=ticker) plt.show() #Economic data from the Federal Reserve, FRED series_code = 'DGS10' #10-year Treasury Rate data_source = 'fred' #FED Economic Data Service start = date(1962, 1, 1) #start date from earliest available, skip end date data = DataReader(series_code, data_source, start) data.info()
AMZN = DataReader('AMZN', 'google', start, end) MSFT = DataReader('MSFT', 'google', start, end) GE = DataReader('GE', 'google', start, end) #print(AAPL) # change the Timestamp index in the four data frames to Date tech_list = [AAPL, GOOG, AMZN, MSFT,GE] for stock in tech_list: stock["Date"] = stock.index.date for stock in tech_list: stock.set_index("Date", drop=True, inplace=True) #plotly.offline.plot(ff.create_table(GOOG.describe().round(2), index = True)) # General Info on Google data print(GOOG.info()) ### Change in Price of GOOGLE Stock over time data = [go.Scatter(x=GOOG.index, y=GOOG.High)] layout = go.Layout(title = 'GOOGLE Closing Price') fig = go.Figure(data=data,layout=layout) plotly.offline.plot(fig,filename='google-closing-price') #print(GOOG) ## Adj Close column does not exist in Google Finance datasets ### To get better overview of price variations, we plot MOVING AVERAGES # Moving Average is a constantly updated average price for a stock over a specified period # Ex. 10-day MA presents its first data point as the avg prices from Day1-10 # Next data point is avg of prices from Day2-Day11 and so on
from pandas_datareader.data import DataReader import pandas as pd from datetime import date import matplotlib.pyplot as plt import seaborn as sns series_code = 'WTISPLC' #Chicago Fed National Financial Conditions Index data_source = 'fred' #FED economic data start = date(2005, 1, 1) data = DataReader(series_code, data_source, start) print(data.info()) series_code2 = 'OIH' #S&P500 data_source2 = 'yahoo' #FED economic data data2 = DataReader(series_code2, data_source2, start) print(data2.info()) combined_df = pd.concat([data, data2], axis=1) print(type(combined_df)) print(combined_df.info()) series_name = str(series_code) + 'vs' + str(series_code2) #data = data.rename(columns={series_code: series_name}) #combined_df.plot(title=series_name) #plt.figure(figsize=(12,5)) ax1 = data.iloc[:, 0].plot(color='blue', grid=True, label=series_code) ax2 = data2['Adj Close'].plot(color='red', grid=True, secondary_y=True,
from datetime import date import pandas as pd import matplotlib.pyplot as plt # Set start date start = date(1968, 1, 1) # Set series code series = 'GOLDAMGBD228NLBM' data_source = 'fred' # Import the data gold_price = DataReader(series, data_source, start) # Inspect the price of gold gold_price.info() # Plot the price of gold gold_price.plot(title='Gold Price') # Show the plot plt.show() # Set the start date start = date(1950, 1, 1) # Define the series codes series = ['UNRATE', 'CIVPART'] data_source = 'fred' # Import the data