Stock backtest

Share on facebook
Share on twitter

Trading strategy and Backtest in Python [Momentum of ALL S&P 500 stocks]

703 views | 2 Jan. 2021

Video is for educational

Video is for educational and entertainment purposes only. This is not an investment advice!

Prior video on Momentum on the Dow Jones:


In this video I am building a trading strategy in Python from scratch. The strategy used is the Momentum strategy. You should have at least basic knowledge of Pandas and maybe have gone through some videos on my Python for Finance playlist to better follow along.

The momentum strategy is based on the findings of Jegadeesh/Titman and the conventional momentum strategy taking the past 12 month skipping the most recent month is just an application of Ken Frenchs approach.

Wikipedia reference:


Further reading:



Momentum is a hot topic in academics as there is empirical evidence that this strategy is working but there is a discussion about WHY this strategy is working.

The most recent month is skipped due to the 1-month reversal effect.

If you have any questions please don't hesitate to drop me a comment.

Want more trading strategies? Please let me know but liking this video and subscribing to the channel.

0:00 - 01:42 Introduction

01:42 - 03:45 Getting stock tickers from the S&P 500

03:45 - 04:27 Requesting the data

04:27 - 05:17 New year wishes while data download (skippable)

05:17 - 06:39 Necessary data manipulations

06:39 - 07:47 Calculating monthly returns

07:57 - 09:06 Calculating past 11 months returns

09:06 - 16:22 Step by step calculation of a 1-month Momentumprofit

16:22 - 19:22 Wrapping all code into a function to prepare for Backtest

19:22 - 21:42 Backtesting the strategy

21:42 - 27:24 Benchmarking and final thoughts

Richard Pitt

Great job

M Schuer

Hi again, just going through your code again for my own education and cleaned it up a little bit. I think you would want the corresponding date to the return and then put that into a dataframe. I did this when replicating this strategy and first created a dictionary of the date and profits lists ==> ex. results = dict(zip(date, profits)) and then df = pd.Dataframe.from_dict(results, orient = 'index')

worked perfectly for me. Hope this helps. And please keep the videos coming, they are great

Sujal Shukla

Bro, can you help me for creating a program for intraday trading live chart plz bro

Michael T

Hey Algovibes,

Once again great work mate, thanks for taking a look at the code - I took on board the feedback you gave me and I'm glad to see some of it made it into this video. I've learned a lot from your channel, keep up the good work bro.

Sujal Shukla

Bro can u make a video on autorelod program

P. M. Hijes

Hi :) thanks a lot for a great video. Regarding the 2 failed downloads, it seems to be a problem with how yfinance deals with the tickers BRK.B and BF.B. It seems like yfinance understands BRK-B and BF-B, hence, you can manually change "." by "-":. Otherwise you get NaN in those two columns.

for n,ticker in enumerate(tickers):
if ticker == 'BRK.B' or 'BF.B':
tickers[n] = ticker.replace(".", "-")

Cheers, keep it up!

Rafael Farias

Happy New Year!

Wouldn't it be more correct to evaluate by the sum of the returns?

For this example, momentum performs 203% and SP 135% -- frame[0].sum() and frame['SP500'].sum()

Søren Godbersen

How could the code be modified to handle several months of holding periods instead of just the one you used? Would that be difficult?


Thanks from Brazil. I Will use this for local stocks market


Hello Algovibes!

Happy New Year!

Do you think it is worth using this strategy by listing the values ​​of the stock betas in the same way used in the momentum strategy?

Greetings from Brazil!

Ayon Rabbani

Thank you! My new years plan is to learn and build off your videos! Thanks for the info and inspiration :)

M Schuer

Awesome video! Thanks for sharing this. Great follow up to the Dow momentum strategy. Just as an aside, i would include BRK.B and BF.B just to round out your data set. If you are not aware, in Yahoo Finance, those stocks with a period (.) are replaced with a dash (-) so BRK.B becomes BRK-B. Here is how i took care of it ::

symbols = [ ]
for i in tickers:

Keep the videos coming


Good vid thanks

Stock backtest

Share on facebook
Share on twitter

IWM Weekly Iron Condor Backtest

4 847 views | 28 Jul. 2020

Click here to Subscribe -

Click here to Subscribe - https://www.youtube.com/OptionAlpha?sub_confirmation=1

Are you familiar with stock trading and the stock market but want to learn how to trade options? Check out our Options Trading For Beginners Playlist here - https://www.youtube.com/playlist?list=PLhKnvfWKsu42LtgQmXvuFIf7wveXup1Fm

Looking for more? Check out our Top 10 Most Watched Videos! Whether you are only familiar with stock trading and the stock market and want to learn how to trade options, or are already an advanced trader, there is something in this list for you - https://www.youtube.com/playlist?list=PLhKnvfWKsu42bE1u3wj6zZphZWbPB8DUI

Welcome to the Option Alpha YouTube Channel! Our mission is to provide traders like you with the most comprehensive options trading and investing education available anywhere, free of charge. We're here to help you take your trading and financial education to the next level! For more, visit our website at http://www.optionalpha.com