Welcome! In today's lesson, we will calculate the Exponential Moving Average (EMA) for Tesla ($TSLA
) stock using Pandas. Understanding EMA will allow you to give more weight to recent stock prices, which can help you make smarter trading decisions compared to using a Simple Moving Average (SMA).
The goal of this lesson is to help you understand how to:
The Exponential Moving Average (EMA) is a type of moving average that places a greater weight and significance on the most recent data points. This makes it more responsive to new information.
In many trading strategies, the EMA is preferred over the SMA because it reacts more quickly to recent price changes, making it a reliable tool for identifying trends.
The formula for EMA involves:
Current Price ($P$): The price at the current time.
Previous EMA ($EMA_{previous}$): The EMA calculated at the previous time.
Smoothing Constant ($\alpha$): A constant that is derived from the number of periods. It is calculated as: $\alpha = \frac{2}{N + 1}$
where $N$ is the number of periods.
The EMA formula is:
$EMA_{current} = (P \sdot \alpha) + (EMA_{previous} \sdot (1 - \alpha))$
Before we calculate the EMA, we need to load and preprocess the dataset to make it suitable for time series analysis. We will use the load_dataset
function from the datasets
library to fetch historical Tesla stock prices.
Here is how you load and preprocess the dataset:
Python1import pandas as pd 2import matplotlib.pyplot as plt 3from datasets import load_dataset 4 5# Load Tesla dataset 6dataset = load_dataset('codesignal/tsla-historic-prices') 7tesla_df = pd.DataFrame(dataset['train']) 8 9# Convert the 'Date' column to datetime 10tesla_df['Date'] = pd.to_datetime(tesla_df['Date']) 11 12# Set 'Date' as index 13tesla_df.set_index('Date', inplace=True) 14 15# Sort the data by date 16tesla_df.sort_index(inplace=True)
To get a sense of the data we're working with, let's display the first few rows:
Python1print(tesla_df.head())
Running this, we should see the first few rows of the Tesla stock historical data, which typically includes columns like Date, Open, High, Low, Close, Volume, etc. This preprocessing ensures our data is in chronological order, essential for time series manipulation.
With our data preprocessed, the next step is to calculate the EMA. Pandas makes this task straightforward with its ewm()
function, which calculates the Exponential Weighted Moving Average.
The ewm()
function takes parameters like span
(the number of periods) and adjust
.
Here, we compute the 20-day EMA for the closing price:
Python1# Calculate the 20-day Exponential Moving Average for the Close Price 2tesla_df['EMA_20'] = tesla_df['Close'].ewm(span=20, adjust=False).mean()
The ewm(span=20, adjust=False).mean()
calculates the 20-day EMA. The adjust=False
parameter ensures that the smoothing factor is applied only once to the series.
To verify that the EMA calculation is added to our DataFrame:
Python1print(tesla_df[['Close', 'EMA_20']].head())
The output of the above code will be:
Plain text1 Close EMA_20 2Date 32010-06-29 1.592667 1.592667 42010-06-30 1.588667 1.592286 52010-07-01 1.464000 1.580068 62010-07-02 1.280000 1.551490 72010-07-06 1.074000 1.506015
This output demonstrates the initial calculation of the 20-day Exponential Moving Average (EMA) alongside the actual closing prices for the first few days in the dataset. The EMA gives more weight to recent prices, which is evident from how it starts to adjust as new price data comes in.
Visualizing the EMA alongside the closing prices helps in understanding the trend and the impact of recent prices. We will use Matplotlib to plot the data. To make the plot clearer, we will consider data from the year 2018.
We narrow our dataset to a smaller date range, such as one year, to improve visualization clarity.
Python1import matplotlib.pyplot as plt 2 3# Using a smaller date range for better visualization 4tesla_df_small = tesla_df.loc['2018']
We plot the closing prices and the 20-day EMA using Matplotlib:
Python1# Plotting 2tesla_df_small[['Close', 'EMA_20']].plot(figsize=(12, 6), title="TSLA Close Price and 20-day EMA (2018)") 3plt.show()
This will generate a plot, making it easier to visually compare the 20-day EMA with the actual closing prices. The EMA line will generally follow the trend of the closing prices but will be smoother.
Great job! In this lesson, you learned how to:
Understanding EMA helps in making informed trading decisions by providing insights into recent price trends while smoothing out noise.
Now, it's time to practice these tasks to solidify your understanding and improve your ability to analyze financial data using technical indicators. Dive in, and soon, you'll be able to implement and interpret various financial indicators for better trading strategies!