Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Stock Market Analysis Project Overview

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

import matplotlib.pyplot as plt

%matplotlib inline

In [4]:
pip install pandas_datareader

Requirement already satisfied: pandas_datareader in /srv/conda/envs/notebook/lib/python3.6/site-packages (0.10.0)

Requirement already satisfied: pandas>=0.23 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from pandas_datareader) (1.1.5)

Requirement already satisfied: requests>=2.19.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from pandas_datareader) (2.26.0)

Requirement already satisfied: lxml in /srv/conda/envs/notebook/lib/python3.6/site-packages (from pandas_datareader) (4.7.1)

Requirement already satisfied: python-dateutil>=2.7.3 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from pandas>=0.23->pandas_datareader) (2.8.2)

Requirement already satisfied: pytz>=2017.2 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from pandas>=0.23->pandas_datareader) (2021.3)

Requirement already satisfied: numpy>=1.15.4 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from pandas>=0.23->pandas_datareader) (1.19.5)

Requirement already satisfied: charset-normalizer~=2.0.0 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from requests>=2.19.0->pandas_datareader)


(2.0.0)

Requirement already satisfied: certifi>=2017.4.17 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from requests>=2.19.0->pandas_datareader) (2021.5.3


0)

Requirement already satisfied: idna<4,>=2.5 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from requests>=2.19.0->pandas_datareader) (3.1)

Requirement already satisfied: urllib3<1.27,>=1.21.1 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from requests>=2.19.0->pandas_datareader) (1.26.


7)

Requirement already satisfied: six>=1.5 in /srv/conda/envs/notebook/lib/python3.6/site-packages (from python-dateutil>=2.7.3->pandas>=0.23->pandas_datareader)


(1.16.0)

Note: you may need to restart the kernel to use updated packages.

Part 1: Getting the Data


Tesla Stock (Ticker: TSLA on the NASDAQ)
Use pandas_datareader to obtain the historical stock information for Tesla from Jan 1, 2012 to Jan 1, 2017.

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)

tesla = web.DataReader("TSLA", 'yahoo', start, end)

In [7]:
tesla.head()

Out[7]: High Low Open Close Volume Adj Close

Date

2012-01-03 5.900 5.530 5.788 5.616 4640500 5.616

2012-01-04 5.734 5.500 5.642 5.542 3150500 5.542

2012-01-05 5.586 5.370 5.552 5.424 5027500 5.424

2012-01-06 5.558 5.282 5.440 5.382 4931500 5.382

2012-01-09 5.498 5.224 5.400 5.450 4485000 5.450

In [8]:
tesla.to_csv('Tesla_Stock.csv')

Other Car Companies


Repeat the same steps to grab data for Ford and General Motors (GM).

In [9]:
ford = web.DataReader("F", 'yahoo' , start, end)

gm = web.DataReader("GM", 'yahoo' , start, end)

In [10]:
ford.head()

Out[10]: High Low Open Close Volume Adj Close

Date

2012-01-03 11.25 10.99 11.00 11.13 45709900.0 7.506939

2012-01-04 11.53 11.07 11.15 11.30 79725200.0 7.621601

2012-01-05 11.63 11.24 11.33 11.59 67877500.0 7.817202

2012-01-06 11.80 11.52 11.74 11.71 59840700.0 7.898139

2012-01-09 11.95 11.70 11.83 11.80 53981500.0 7.958842

In [11]:
ford.to_csv('Ford_Stock.csv')

In [12]:
gm.head()

Out[12]: High Low Open Close Volume Adj Close

Date

2012-01-03 21.180000 20.750000 20.830000 21.049999 9321300.0 16.269413

2012-01-04 21.370001 20.750000 21.049999 21.150000 7856700.0 16.346706

2012-01-05 22.290001 20.959999 21.100000 22.170000 17880600.0 17.135052

2012-01-06 23.030001 22.240000 22.260000 22.920000 18234500.0 17.714722

2012-01-09 23.430000 22.700001 23.200001 22.840000 12084500.0 17.652893

In [ ]:
gm.to_csv('GM_Stock.csv')

Part 2: Visualizing the Data


Time to visualize the data. Follow along and recreate the plots below according to the instructions and explanations.

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()

Out[14]: <matplotlib.legend.Legend at 0x7f2285a8bbe0>

Plot the Volume of stock traded each day.

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()

Out[15]: <matplotlib.legend.Legend at 0x7f2285ab20b8>

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']

ford['Total Traded'] = ford['Open']*ford['Volume']

gm['Total Traded'] = gm['Open']*gm['Volume']

Plot the "Total Traded" against the time index.

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')

Out[20]: Text(0, 0.5, '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'>

You might also like