Stock Market Analysis Project via Python on Tesla, Ford and GM

Gayan Samarakoon
5 min readMar 10, 2019

This a basic stock market analysis project to understand some of the basics of Python programming in financial markets.

Part 0: Import the necessary modules

Part 1: Get the data from yahoo finance on the daily closing prices of Tesla, GM and Ford.

Tesla
Ford and GM

Part 2: Visualise the data, and visually inspect interesting events. In this section, we also create a candlestick chart from scratch.

What are the dates of maximum trading volumes for each?

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 really understand this we would need to look at the total market cap of the company, not just the stock price. Unfortunately, our current data doesn’t have that information about total units of the stock present. But what we can do as a simple calculation to try to represent total money traded would be to multiply the Volume column by the Open price. Remember that this still isn’t the actual Market Cap, it's just a visual representation of the total amount of money being traded around using the time series. (e.g. 100 units of stock at $10 each versus 100000 units of stock at $1 each)

Create a new column for each dataframe called “Total Traded” which is the Open Price multiplied by the Volume Traded.

looks like there was a huge amount of money traded for Tesla somewhere in early 2018. What date was that and what happened?

Tesla shares surge 10% after Elon Musk shocks market with a tweet about going private Tesla shares surged more than 10 per cent after being halted for more than an hour. Musk issued a string of tweets about possibly taking the company private at $420 a share.

Let’s plot out some MA (Moving Averages). Plot out the MA50 and MA200 for GM.

Check for a relationship between these three equities. Import scatter_matrix from pandas.plotting and use it to create a scatter matrix plot of all the stocks closing price. Rearrange the columns into a new single dataframe. Info can be found here: https://pandas.pydata.org/pandas-docs/stable/visualization.html#scatter-matrix-plot

Creating a Candlestick Chart for Ford

Part 3: Basic financial analysis, by calculating and comparing daily returns, and plotting it to identify relationships.

Daily Percentage Change

Calculate the return from the Close price column.

Some box plots comparing the returns.

Comparing Daily Returns between Stocks

Scatter matrix plot to see the correlation between each of the stocks daily returns.

It looks like Tesla and GM do have some sort of possible relationship, so plot just these two against each other in scatter plot

Cumulative Daily Returns

Daily Return: Daily return is the profit/loss made by the stock compared to the previous day.

Cumulative Return: Cumulative return is computed relative to the day investment is made. If the cumulative return is above one, you are making profits else you are in loss.

The formula for a cumulative daily return is:

df[daily_cumulative_return] = ( 1 + df[pct_daily_return] ).cumprod()

--

--

Gayan Samarakoon

Emerging Technology Enthusiast | Digital Asset Management | Quantitative Strategy | Financial Markets