Beating the Market with a Momentum Trading Strategy using Python: How You Can Too

Zach English
Geek Culture
Published in
9 min readMar 7, 2021

--

Last month I wrote about automating gathering financial data with Python, today I am going to walk through creating a momentum trading strategy using financial data scraped from Yahoo Finance. The goal of a momentum trading strategy is to capitalize on run-ups and minimize downside risk by exiting the position before a sell off. Put simply, repeating buy low and sell high to maximize returns. For this strategy I am going to create buy and sell indicators using the price and volume moving averages for securities.

To get started we will pull price data from Yahoo Finance using Pandas DataReader. I am going to choose Facebook ($FB) as an example stock. First, we need to import the necessary modules: pandas, pandas_datareader, datetime and timedelta. How each module is used will be explained as we go along. The code to import the modules is shown below.

Choosing the Strategy’s Time Frame:

The first step when setting up a momentum trading strategy is deciding on the time frame. This can make or break how well your approach to quant trading works. For my momentum strategy I am going to focus on a medium sized window of a week to a couple months for buying and selling securities. I believe that this window will produce the best results. However, back-testing, previous trading and industry knowledge will be the best indicators for the length of time frame you should choose.

--

--

Zach English
Geek Culture

Data engineering consultant and aspiring data scientist, writing about tech and finance