Intraday Stock Analysis With Python Part 1 - Google Finance Mining and Visualization

Daily stock quotes are commonly used by investors to track historic trends in finance. These daily quotes give highs, lows, opening, and closing prices as well as volume movement for particular stocks during exchange hours. I wanted to take this a step further by employing Google's Finance API and download 60-second intraday information. This intraday analysis will expand the common practice and introduce more in-depth study of financial trends. For the intraday analysis, I'll be using the following tools (click to visit sites): 


Python and Plotly

Python will be doing the heavy lifting, while Google Finance acts as the data repository and Plotly will help us visualize the data. First, it's likely that Plotly needs to be installed (or upgraded), which can be done as follows:

pi@raspberrypi:~$sudo pip3 install plotly --upgrade

Once Plotly is installed or updated, be sure to open Python and setup (or verify) your credentials:

>>>import plotly
>>>plotly.tools.set_credentials_file(username='username123', api_key='abcdef12345')

If you're unsure what your username or API key are, go to your settings in Plotly:

plotly_api_key_location.png

The key should be located at the following:

api_key_example.png

Copy and paste your API key into the 'set_credentials' command above, and Plotly should be ready to go!



Python and Google Finance

Google Finance requires the following convention when requesting historic stock data:

https://finance.google.com/finance/getprices?q=GOOG&i=60&p=10d&x=NASD&f=d,o,h,l,c,v
 

KEY:
q=GOOG - The 'q' denotes the selection of the stock ticker (GOOGLE in this case)
i=60 - The 'i' denotes the period (in seconds) between data
p=10d - The 'p' denotes amount of days
x=NASD - The 'x' denotes selection of stock exchange
f=d,o,h,l,c,v - The 'f' denotes the desired data (d ≡ date, o ≡ open price, h ≡ high, l ≡ low, c ≡ close, v ≡ volume)

 

In the Python routine, we need to use this convention to request historic intraday stock data from Google Finance. Below is the routine for creating a vector with dates and data outlined above:


Visualization with Plotly

Plotly is an incredible tool that bridges the gap between interactive visualization and programmatic data analysis. Below is an example of an OHLC (Open-High-Low-Close) plot for the Netflix stock ticker [all code on GitHub here]. I also added volume to the hover text to view all five parameters available for each stock pull.

The above data still does not take advantage of the high frequency capabilities of Google Finance's API. In order to acquire high frequency data, we need to change the 'period' variable to 60. This will allow us to investigate stock price changes every 60 seconds. Below is an example plot of 60-second stock closing price and volume for five days in July 2018 for Microsoft (MSFT). Notice the log axis for volume (right y-axis) - this simplifies the visual representation to account for large fluctuations in volume. 

The visualizations above are just two ways to visualize the stock data. There are also candlestick plots (sometimes called forex charts), bar charts, and point and figure charts used to inspect behaviors and characteristics of stock trends.

All code used to produce the plots above can be found on the project's GitHub page:

https://github.com/engineersportal/intraday_stock_python


Conclusion

This concludes part 1 of the Intraday Stock Analysis With Python. In part 2, I will cover research trends and statistical analysis of high-frequency data, specifically with heavier mathematic and programmatic tools used to analyze trends and identify buy and sell points in historic data. Ultimately, the algorithm will be developed to predict future trends and maximize investment profit.