Equity market Technical analysis in Python (series 1 —SMA)

Eric Chen, CFA, FRM
5 min readJun 16, 2024

I know stat arb and alt data have been the hot ones over the most recent decade, but something before machine learning came into picture, the OG of quants, technical indicators are still worth discussing and implementing as a piece of the quant puzzle. As I’m receiving some great news last week from one of my colleagues on personal trading accounts restrictions I’m writing this article to show you how to implement these technical analysis and back test them to fit into your overall strategies. The code may be A LOT EASIER than you think, but to back test your thoery into a profitable strategy without overfitting on the data mining side, it may need more contemplation.

1. SMA

Simple moving average, the number one technical analysis because of its simplicity. We usually construct two moving averages of closing prices: one for a shorter period to show latest momentum, and another for longer to demonstrate the general trend. The theory is when the near term momentum breaks through the long term general trend, it shows stronger upward strength, or more buying power in the recent period which serves as a buy signal, and vise versa.

I believe you are familiar with the concept in general, so here’s how you construct it in Python.

import warnings
warnings.filterwarnings('ignore')

import pandas as pd
import numpy as np
import yfinance as yf
from datetime import date

idx = pd.IndexSlice

# set the time period of which you want to retrieve…

--

--