A Simple Guide to a Moving Average Crossover Strategy in Python.

Creating & Evaluating a Simple Crossover Strategy Step-by-Step in Python.

Sofien Kaabar, CFA
10 min readFeb 10, 2022


Moving average crossovers are one of the simplest trading techniques and even though they have their weaknesses, they have stood the test of time with regards to usage. This article presents a moving average crossover research strategy from A to Z in Python.

I have just released a new book after the success of the previous book. It features advanced trend following indicators and strategies with a GitHub page dedicated to the continuously updated code. Also, this book features the original colors after having optimized for printing costs. If you feel that this interests you, feel free to visit the below Amazon link, or if you prefer to buy the PDF version, you could contact me on LinkedIn.

Fetching Historical OHLC Data

One of the most famous trading platforms in the retail community is the MetaTrader5 software. It is a powerful tool that comes with its own programming language and its huge online community support. It also offers the possibility to export its historical short-term and long-term FX data.

The first thing we need to do is to simply download the platform from the official website. Then, after creating the demo account, we are ready to import the library in Python that allows to import the OHLC data from MetaTrader5.

A library is a group of structured functions that can be imported into our Python interpreter from where we can call and use the ones we want.

The easiest way to install the MetaTrader5 library is to go to the Python prompt on our computer and type:

pip install MetaTrader5

This should install the library in our local Python. Now, we want to import it to the Python interpreter (such as Pycharm or SPYDER) so that we can use it. Let us actually import all the libraries we will be using for…



Sofien Kaabar, CFA

Top writer in Finance, Investing, Business | Trader & Author

Recommended from Medium


See more recommendations