How to Quickly Construct and Backtest a Simple Moving Average Crossover Strategy with Python

Photo by m. on Unsplash

Getting Started

There are three components needed to construct and backtest our SMA Cross Strategy:

  • Historical EOD data
  • A Long Moving Average
  • A Short Moving Average

What are Moving Averages?

The Moving Average (MA) of a stock is simply the average price of a security over a specific period.

How to Use a Moving Average to Generate a Trade Signal?

The Simple Moving Averages (SMAs) that construct our strategy are known as lagging indicators, as they are made up of historical data.

  • Price Crossover
  • Moving Average Crossover

Price Crossover

Investors typically view longer moving averages, 50-day, 100-day, and 200-day, as either support or resistance benchmarks to a stock price’s current trend.

Moving Average Crossover

The moving average crossover is a similar concept to the price crossover discussed above; however, it relies on two moving averages, one longer and one shorter, crossing over each other — instead of the price crossing over a single moving average.

What are the Drawbacks of a Moving Average Crossover Strategy?

One drawback occurs when investors use stop-loss orders with their trend-following strategies. A stop-loss order automatically closes a position when a particular trailing threshold is hit. For example, if you bought a stock at $100 a share and set a 10% trailing stop-loss order, you would automatically close this trade at $90 per share if the stock price directly fell from your initial entry.

How to develop a Moving Average Crossover Strategy with Python?

For its simplicity of creating a coding environment, we will be using Google Colab to construct and backtest our strategy; more information on Google Colab can be found here.

SMA Crossover Strategy Results

Putting all of this into practice we will analyze our SMA Crossover’s returns for the following three stocks: PLTR | AAPL | GME.

Where to access clean market data

At Intrinio, we’re recognized for being one of the top data providers that specializes in high-quality data, immediate customer service, and modern toolsfor a variety of fintech platforms and businesses. Our dedicated team wants to help you find the best data package that fits your unique needs. Reach out to one of our data specialists today to find out more about our equities market data packages. And to learn more about all our financial services and data please visit

Full Code Walkthrough



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


High-quality financial data. Tools built for developers. First-class, US-based support. Data doesn’t have to be hard or expensive.