Getting the data

Many libraries can help download financial data; our choice though is to use the pandas library. This software Python library is well known for data manipulation and analysis. We will use the DataReader function, which is capable of connecting to a financial news server such as Yahoo, Google, and many others, and then downloading the data that you will need for the example of this book. DataReader takes four arguments in this example:

  1. The first one is the symbol (our example uses GOOG for Google) you would like to use for analysis.
  2. The second specifies the source for retrieving the data, and then you will specify the range of days to get the data.
  3. The third specifies the starting data from which to fetch historical data.
  4. The fourth and final argument specifies the end data for the historical data series:
# loading the class data from the package pandas_datareader
from pandas_datareader import data
# First day
start_date = '2014-01-01'
# Last day
end_date = '2018-01-01'
# Call the function DataReader from the class data
goog_data = data.DataReader('GOOG', 'yahoo', start_date, end_date)

The goog_data variable is the data frame containing the Google data from January 1, 2014 to January 1, 2018. If you print the goog_data variable, you will see the following:

print(goog_data)
High Low ... Volume Adj Close
Date ...
2010-01-04 312.721039 310.103088 ... 3937800.0 311.349976
2010-01-05 311.891449 308.761810 ... 6048500.0 309.978882
2010-01-06 310.907837 301.220856 ... 8009000.0 302.164703
2010-01-07 303.029083 294.410156 ... 12912000.0 295.130463

If you would like to see all the columns, you should change the option of the pandas library by allowing more than four displayed columns:

import pandas as pd
pd.set_printoptions(max_colwidth, 1000)
pd.set_option('display.width', 1000)
High Low Open Close Volume Adj Close
Date
2010-01-04 312.721039 310.103088 311.449310 311.349976 3937800.0 311.349976
2010-01-05 311.891449 308.761810 311.563568 309.978882 6048500.0 309.978882
2010-01-06 310.907837 301.220856 310.907837 302.164703 8009000.0 302.164703
2010-01-07 303.029083 294.410156 302.731018 295.130463 12912000.0 295.130463

As per the previous output, there are six columns:

  • High: The highest price of the stock on that trading day.
  • Low: The lowest price of the stock on that trading day.
  • Close: The price of the stock at closing time.
  • Open: The price of the stock at the beginning of the trading day (closing price of the previous trading day).
  • Volume: How many stocks were traded.
  • Adj Close: The closing price of the stock that adjusts the price of the stock for corporate actions. This price takes into account the stock splits and dividends.

The adjusted close is the price we will use for this example. Indeed, since it takes into account splits and dividends, we will not need to adjust the price manually.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.199.50