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
>>>'username123', api_key='abcdef12345')

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


The key should be located at the following:


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:,o,h,l,c,v

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:

import csv,datetime,requests,time
import numpy as np

def numpy_stock(exchange,ticker,interval,days):
    url = ',o,h,l,c,v' %\
    with requests.Session() as ses:
        file = ses.get(url)
        csv_reader = csv.reader(file.text.splitlines(),delimiter=',')
        header_pass = 0
        for row in csv_reader:

            if row[0].split('=')[0]=='COLUMNS':
                column_names = [row[0].split('=')[1]]+row[1:]
                data = [[] for ii in range(0,len(column_names))]
            if row[0].split('=')[0]=='TIMEZONE_OFFSET':
                t_offset = float(row[0].split('=')[1])
                header_pass = 1
            if header_pass==1:
                if row[0][0]=='a':
                    new_day = datetime.datetime.fromtimestamp(int(row[0][1:]))
                    curr_datetime = new_day
                    curr_datetime = new_day+datetime.timedelta(seconds=period*int(row[0]))
                for ii in range(0,len(column_names)):
                    if ii==0:
    return url,data,column_names

ticker = 'NFLX' # example ticker (Netflix)
period = 60 # data every 60 seconds
days = 10 # 10 days worth of data
exchange = 'NASD' # NASDAQ exchange

url,data,names = numpy_stock(exchange,ticker,period,days)

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:


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. 


See More in Programming: