top of page

Simple Moving Average (SMA) Crossover | Python

Simple Moving Average is a well known technical indicator used by traders as well as investors who are techno-fundamentalist for the analysis of index or stock price levels.


Please refer to Extract Historical Stock Data using API to extract the historical data of the stock for which we want to identify the support and resistance levels.


Let us understand how to identify Simple Moving Averages-

Assuming that we have already extracted the historical stock data and stored in a Python dictionary, we will now look at the calculation behind the simple moving average.


To calculate Simple Moving Average of the stock price, we simply use mean function taking the window size equal to the number of days in a period of any particular moving average. For instance, if we want to calculate 30-Days Simple Moving Average then 30 will be the window size and so on- depends on the period of any particular moving average.


To identify different SMA for all the stocks in one go, we will place our code inside a loop function which will refer to each item specified in the ticker list. An empty Python dictionary is defined so that calculated SMA points of each item can be stored classified with the respective ticker symbol and later, it can be retrieved as and when required.


To call the calculated SMA points from that dictionary, we simply specify the dictionary name followed by the ticker symbol inside the brackets with double quotes and the output of the same is shown below-


If I were to plot this, Close Price, 7-Days SMA, 30-Days SMA, 50-Days SMA, 100-Days SMA & 200-Days SMA over the period on a chart using matplotlib library, it will look like as shown below-



Finally, to capture the crossover alert, we divide the codes in two parts (one for downtrend and other for uptrend) for better understanding the code. One can also write a single piece of code using 'else' under 'if' statement.


Capturing Downtrend:

  1. Share price crossover 30-Days SMA, 50-Days SMA, 100-Days SMA & 200-Days SMA, provided with a simple conditions- If the share price is trading below the latest SMA provided that the share price was trading above the SMA on previous close of business date.

  2. 50-Days SMA crossover 100-Days SMA & 200-Days SMA, provided with a simple conditions- If the 50-Days SMA is trading below the latest 100-Days SMA or 200-Days SMA provided that the 50-Days SMA was trading above the 100-Days SMA or 200-Days SMA respectively on previous close of business date.

  3. 100-Days SMA crossover 200-Days SMA, provided with a simple conditions- If the 100-Days SMA is trading below the latest 200-Days SMA provided that the 100-Days SMA was trading above the 200-Days SMA on previous close of business date.

Capturing Uptrend:

  1. Share price crossover 30-Days SMA, 50-Days SMA, 100-Days SMA & 200-Days SMA, provided with a simple conditions- If the share price is trading above the latest SMA provided that the share price was trading below the SMA on previous close of business date.

  2. 50-Days SMA crossover 100-Days SMA & 200-Days SMA, provided with a simple conditions- If the 50-Days SMA is trading above the latest 100-Days SMA or 200-Days SMA provided that the 50-Days SMA was trading below the 100-Days SMA or 200-Days SMA respectively on previous close of business date.

  3. 100-Days SMA crossover 200-Days SMA, provided with a simple conditions- If the 100-Days SMA is trading above the latest 200-Days SMA provided that the 100-Days SMA was trading below the 200-Days SMA on previous close of business date.


 

Feel free to share your views and ask for models in the #comment section. If you like our content, please hit a #like button.

118 views0 comments
bottom of page