Simple Moving Average (SMA) is a highly used indicator in Technical Analysis for financial assets. I explained how to calculate in my previous post.
In this post, I will show you how you can calculate SMA using actual world data, Tesla's stock price data, and Python.

Start with importing required libraries:
#import required libraries:
import pandas as pd
import pandas_datareader.data as web
import datetime as dt
import matplotlib.pyplot as plt
import yfinance as yf
import finplot as fplt
import pandas_ta as ta
Tesla's Initial Public Offering was made on 29 June 2010. The first price in the stock exchange market emerged on 02 July 2010. Let's get the price data for 02 July 2010 and 04 June 2023:
#query Tesla's data from yf :
tesla = yf.download('TSLA', start='2010-07-02', end='2023-06-04')
Now we can create the candle stick chart for Tesla:

As you can see from the above chart, between 02 July 2010 and 31 December 2019, the price of Tesla's shares was $1.66 and $28.40, which indicates a 17X return.
And the second big run happened between 31 December 2019 and 04 November 2021, from $28.40 to $415.07, which indicates almost a 15X return:

I will focus on how to calculate the returns in anothoer
I will focus on how to calculate the returns in another post. Now let's figure 50-days-SMA:
#Calculate 50-day-SMA and 200-day-SMA:
#First take the average of Open, High, Low and Close prices:
tesla['OHLC_avg'] = (tesla['Open'] + tesla['High'] + tesla['Low'] + tesla['Close']) / 4
#50-day-SMA:
tesla['SMA_50'] = tesla['OHLC_avg'].rolling(window=50).mean()
#200-day-SMA:
tesla['SMA_200'] = tesla['OHLC_avg'].rolling(window=200).mean()
Create the chart:
#create candle stick chart:
fplt.candlestick_ochl(tesla[['Open','Close','High','Low']])
fplt.plot(tesla[['SMA_50']])
fplt.plot(tesla[['SMA_200']])
fplt.show()
The stratecy is simple buy whenever 50-day-SMA crosses above 200-day-SMA (the golden cross: green arrows) and sell whenever 50-day-SMA crosses below 200-day-SMA (the death cross: red arrows):

Buy this legendary book if you would like to master on the Technical Analysis:
I hope you enjoyed reading this post. I will see you in another post!
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.