Lesson 01-01 - Reading and Plotting Stock Data
● Why use python for financial applications?
○ Allows you to quickly prototype algorithms
○ Provides computational speed
○ Features:
■ Strong scientific libraries
■ Strongly maintained
■ Fast if you can stick to metrics notation because lower levels are written
in C
● What a CSV file looks like
○ CSV - Comma Separated Values (plain text files)
○ Most CSV files have
■ Header Line
■ Rows of Data
● In a CSV of stock data, you would see
○ Date/Time
○ Open Price
○ High Price
○ Low Price
○ Closing Price
■ Actual price that was reported at the exchange when the stock closed for
that day
○ Volume
○ Adjusted Closing Price
■ Number that the data provider generates for us (adjusted for stocks,
splits, and dividend payments)
● Pandas - python library used to read, manipulate, and plot data
○ Dataframes are tables in pandas and a series is a column
○ Import statement to import pandas = import pandas as pd)
■ Use pd so that you don’t have to keep writing out pandas
○ Reading CSV files = pd.read_csv(“<name of csv>”)
○ Print out first n rows in a data frame called df = df.head(n)
○ Print out last n rows in a data frame called df = df.tail(n)
○ Print rows between index x and y = df[x:y+1]
○ Compute max value in column ‘Close’ = df[‘Close’].max()
○ Compute mean value in column ‘Value’ = df[‘Value’].mean()
● Matplotlib - python library used to plot data
○ Import statement to import matplotlib = import matplotlib.pyplot as plt
■ Use plt so that you don’t have to keep writing out matplotlib.pyplot
○ Plot column ‘Adjusted Close’ from dataframe df = df[‘Adjusted Close’].plot()
○ Plot columns ‘Adjusted Close’ and ‘Close’ from dataframe df = df[[‘Adjusted
Close’, ‘Close’]].plot()
○ Show a plot = plt.show()
● The best way to normalize price data so that all prices start at 1.0 (normalize to 1) is df1
= df1/df1[0]
● Set a title and font size in a plot = df.plot(title = “title”, fontsize = 2)
● Add x and y axis (need a handler for the plot and the output of df.plot is the handler)
○ ax = df.plot(title = “title”, fontsize = 2)
■ Create object ax for axis
○ ax.set_xlabel(“Date”) and ax.set_ylabel(“Price”) will set x and y labels
■ set_xlabel and set_ylabel are functions of the object retrieved from df.plot