Simple Moving Average (SMA) Indicator Using Machine Learning

Handhika Yanuar Pratama
Data Folks Indonesia
7 min readAug 14, 2021
Photo by Maxim Hopman on Unsplash

Disclaimer: This is not an financial advice, this for educational purpose only, any loss or profit because of this is beyond my responsibility

As pandemic grows, people tend to change the way of his/her life. Working offline may not the best choices today, many of them more likely becoming an investor or trader. But, trust me, trading without knowledge will lead you into big loss. In this article, I want to tell you about using Simple Moving Average (SMA) indicator. Well, I understand that not everyone knows it. So, let’s begin exploring.

Simple Moving Average

Actually, I’m not full time trader so for this section, I will bring Charles Schwab’s explanation about SMA. SMA or Simple Moving Average is a straightforward calculation of a stock’s average price (usually using close price) over a set number of days.

Okay, for example, you are setting 10 days moving average in close price of your favorite stock, so you will adding all the 10 days number and then divide it by 10, each day passed, the moving average will moving too, like this.

There is more advanced indicator called EMA (Exponential Moving Average). In this article, I will using SMA, because it’s beginner friendly. Also Charles Schwab said that SMA is great for detecting daily pattern, the consequence is that SMA is not really good in detecting hourly pattern. In this article we will use 5 years data, so I will using SMA rather than EMA. Okay, let’s go into the next steps.

Getting the Dataset

Yahoo! Finance is media property of Yahoo! network since 2017 that provides financial news, data, and everything about stock price including financial reports. This is legit source to collect stock price data. In this example I would like to predict the PGAS.JK stock market price.

PGAS.JK owned by PT Perusahaan Gas Negara Tbk (PGAS.JK), this stock belongs to IDX (Indonesian Stock Exchange). Why I use this, because, I ever getting profit from this stock and it would be nice if I used it as example. Recently, this stock price is back just same as June-July 2020, why not test it right? Who knows if this stock will be great again in December 2021. To collect PGAS.JK data in the last 5 years you can see this GIF below

Okay, we already understand about what is SMA and had the data, let’s go into the coding section.

Coding Section

Data

Before do the code in real data, I want to explore some features from the dataset. Okay, if you open the dataset that you download before, maybe the data will look like broken, but this is CSV file. You can open it using Pandas or you can add configuration for displaying csv file into your Excel app. This picture below is displayed using Pandas.

Okay, the picture above actually is a data frame, but to make it beginner friendly let us say that the picture is a table. From the table, we had 6 columns with one index. The details for them are as follows:

  1. Open — It’s the market opening price in the current day
  2. High — It’s the highest price of the stock in the current day
  3. Low — It’s the lowest price of the stock in the current day
  4. Close — It’s the market close price in the current day
  5. Adj Close — It’s the closing price that had been amends with closing price that day and the corporation values.
  6. Volume — It’s the market volume of stock price that traded in the current day

Investopdia said that the Adj Close commonly being used to check the historical returns or analyze the past performance. So, because this is recommended, let use this as the parameter.

Code

This project is actually one piece of my 180 Days Of Data Science. All the code and the dataset can be downloaded in my GitHub here.

I started by importing the modules and setting the style of the matplotlib into seaborn-whitegrid (You can use your preferences)

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# For chart style
plt.style.use('seaborn-whitegrid')

Next, I import the dataset and set the index into ‘date’ column. After set the index into ‘date’, I turn the format into DatetimeIndex. Because, this is a time-series data. If you don’t want to turn it into the DatetimeIndex, the data can still be processed, but it takes a lot more time.

# Store the data
df = pd.read_csv("PGAS.JK (1).csv")

# Set the date columns as the index
df = df.set_index('Date')

# Set the index into datetime for efficency
df = df.set_index(pd.DatetimeIndex(df.index.values))

If you want to display the table, you can write df in your notebook, and this table will be displayed

You can check the basic information from the dataset by typing df.info() and you will get information, that all columns type are float64 and the index is DatetimeIndex which start from 2016–08–04 until 2021–08–03. The data length is 1261 and there is no null values.

Data is collected, we also know the data type. The next step is visualizing the column that we used. In this case I use the ‘Adj Close’, so the syntax will be like this.

# Visualize the adj close price
plt.figure(figsize=(16,8))
plt.plot(df['Adj Close'], label='PGAS')

# Adding text into the visualization
plt.title('Adj Close Price History', fontsize=18)
plt.ylabel('Adj Close Price', fontsize=18)
plt.xlabel('Date', fontsize=18)

# Give legend
plt.legend()

# Show the graph
plt.show()

The output will be like this

Beautiful right? But it doesn’t done yet. After visualizing the data, we will make simple function for SMA

# Create function for calculating Simple Moving Average (SMA)
def SMA(data, period=30, column='Adj Close'):
return data[column].rolling(window=period).mean()

The period=30 is just the default value for rolling function, so whenever you forget to put any value, the function will take first 30 data to build a SMA30 instead of bring error into your function.

If you don’t get what I mean. Let’s say I build SMA10 and SMA50, so I will use this syntax for saving the value.

# Create two new columns to store the 10 day and 50 day SMA
df['SMA10'] = SMA(df, 10)
df['SMA50'] = SMA(df, 50)

To understand the function, you can check the values of df[‘SMA10’] or df[‘SMA50’]. In the df[‘SMA10’] the output will be like this.

Did you get it? The first 10 of the data will be taken to build the SMA10, just like the illustrator that I used before.

Next, we build some signal, so whenever SMA10 is greater than SMA50 it will noted into the signal value as 1 and if not the signal value will be 0. After that, we build new column named ‘Position’, so whenever the pattern change the direction, it will store the data into the columns.

# Get buy and sell signals
df['Signal'] = np.where(df['SMA10'] > df['SMA50'], 1, 0)
df['Position'] = df['Signal'].diff()
df['Buy'] = np.where(df['Position'] == 1, df['Adj Close'], np.NAN)
df['Sell'] = np.where(df['Position'] == -1, df['Adj Close'], np.NAN)

Everything had been setup, let’s display it into chart.

# Visualize the close price with SMA also Buy and Sell Signals
plt.figure(figsize=(16,8))
plt.plot(df['Adj Close'], alpha=0.5, label='Actual Close Price')
plt.plot(df['SMA10'], alpha=0.5, label='SMA10')
plt.plot(df['SMA50'], alpha=0.5, label='SMA50')

# Make buy or sell signal
plt.scatter(df.index, df['Buy'], alpha=1, label='Buy Signal', marker='^', color='green')
plt.scatter(df.index, df['Sell'], alpha=1, label='Sell Signal', marker='v', color='red')

# Adding text into the visualization
plt.title('Adj Close Price w/ Buy and Sell Signals', fontsize=18)
plt.ylabel('Adj Close Price', fontsize=18)
plt.xlabel('Date', fontsize=18)

# Give legend
plt.legend()

# Show the graph
plt.show()

The output will be like this.

Remember before I built new column named ‘Position’?, the column will be displayed as mark, to display whenever SMA10 is bigger than SMA50 and otherwise. This sometimes used as indicator, we should buy stock or not (Green and Red Triangle). If you watch more detail, you will get that SMA10 will be start first.

Well that’s all about how to build SMA indicator in stock price, you can use it into your favorite stock, also you can improve it into better machine.

Conclusion

In this article, I demonstrated how to build SMA indicator in the stock price using Machine Learning. I went through three major step. First, I share about SMA. Next, we gather the data from legit source. Finally, we process the data to build SMA Indicator.

References

Source Code

Have A Nice Code ✌

--

--