Professional Documents
Culture Documents
Stock Market Analysis Project Overview: Part 1: Getting The Data
Stock Market Analysis Project Overview: Part 1: Getting The Data
Welcome to your first Python Project! This project introduces the basics of Financial Modeling Using Python.
You will be analyzing stock data related to a few car companies, from January 1, 2012, to Jan 1, 2017. Keep in mind that this project is mainly just to practice your skills with matplotlib, pandas, and
numpy. Do not infer financial trading advice from it.
Part 0: Import
Import the various libraries you will need. You can always just come back up here or import as you go along.
In [1]:
import numpy as np
import pandas as pd
%matplotlib inline
In [4]:
pip install pandas_datareader
Note: you may need to restart the kernel to use updated packages.
In [3]:
import pandas_datareader
import datetime
In [5]:
import pandas_datareader.data as web
In [6]:
start = datetime.datetime(2012, 1, 1)
end = datetime.datetime(2017, 1, 1)
In [7]:
tesla.head()
Date
In [8]:
tesla.to_csv('Tesla_Stock.csv')
In [9]:
ford = web.DataReader("F", 'yahoo' , start, end)
In [10]:
ford.head()
Date
In [11]:
ford.to_csv('Ford_Stock.csv')
In [12]:
gm.head()
Date
In [ ]:
gm.to_csv('GM_Stock.csv')
Recreate the linear plot of all the stocks' Open price! Hint: For the legend, use label parameter and plt.legend().
In [14]:
from matplotlib import pyplot as plt
tesla['Open'].plot(label='Tesla',figsize=(16,8),title='Open Price')
gm['Open'].plot(label='GM')
ford['Open'].plot(label='Ford')
plt.legend()
In [15]:
tesla['Volume'].plot(label='Tesla',figsize=(16,8),title='Volume Traded')
gm['Volume'].plot(label='gm')
ford['Volume'].plot(label='ford')
plt.legend()
As can be observed, Ford had a really big spike somewhere in late 2013. What was the date of this maximum trading volume for Ford?
In [17]:
ford['Volume'].argmax()
Out[17]: 493
The Open Price Time Series Visualization makes Tesla look like its always been much more valuable as a company than GM and Ford. But to understand this, one would need to look at the total
market cap of the company, not just the stock price. Unfortunately, the current data does not have that information of total units of the stock present. But a simple calculation can be done to represent
the total money traded. It is by multiplying the Volume column with the Open price. Remember that this still is not the actual Market Cap, it is just a visual representation of the total amount of money
being traded around using the time series.
To do the above, create a new column for each dataframe called "Total Traded" which is the Open Price multiplied by the Volume Traded.
In [19]:
tesla['Total Traded'] = tesla['Open']*tesla['Volume']
In [20]:
tesla['Total Traded'].plot(label='Tesla',figsize=(16,8))
gm['Total Traded'].plot(label='GM')
ford['Total Traded'].plot(label='Ford')
plt.legend()
plt.ylabel('Total Traded')
As can be observed, there was a huge amount of money traded for Tesla somewhere in early 2014. What date was that and what happened?
In [21]:
tesla['Total Traded'].argmax()
Out[21]: 538
Let us practice plotting a sample moving average (MA) of one of the cars. Plot out the MA50 and MA200 for GM.
In [22]:
gm['MA50'] = gm['Open'].rolling(50).mean()
gm['MA200'] = gm['Open'].rolling(200).mean()
gm[['Open','MA50','MA200']].plot(label='gm' ,figsize=(16,8))
Out[22]: <AxesSubplot:xlabel='Date'>