CodeX
Published in

CodeX

Momentum Based Portfolio Rebalancing in Python

Assuming people have a portfolio of stocks, the best way to optimize gains is to adjust which stocks are being held & replace poor performing stocks. One strategy when rebalancing a portfolio is a momentum based approach. To test this approach we picked the 30 stocks in the Dow Jones Industrial Average & created a portfolio to measure various key performance indicators. Based on these metrics we picked stocks to replace in the portfolio and visualized the results. To start this analysis we collected & processed the data from a yahoo API using R. We cleaned the data & exported a csv to develop our portfolio in python. Lets start with the need packages and global variables we used for replacement.

We start by importing various packages including LinearRegression from sklearn to calculate the capm or capital asset pricing model used to predict returns. From there we set some global variables most of which are focused on trading fees. These were sourced from sites like Vanguard, Fidelity & other brokers and we often used the average values. Next are two arrays, N & R. The number of stocks picked for selection in the portfolio and number of stocks to be replaced in each rebalancing cycle respectively. Next we create the portfolio of stocks to analyze the KPIs

To analyze the changing portfolio, we developed a series of KPIs when rebalancing the portfolio either monthly or quarterly. An explanation of each KPI can be seen in the image below. In addition, the definition of how capm is calculated and its meaning are shown.

Using these metrics we developed a way to select the optimal portfolio for two rebalancing periods, monthly & quarterly. We then printed these value to a dataframe for visualization.

The following results show our models performance over a three year period from September 2017 — September 2020.

Our model achieved great results, outperforming the market by 7.76% when rebalancing quarterly & 8.52% when rebalancing monthly. However, there are some limitations to this model. First, we assumed the trading fees and they could be not completely accurate or too lenient. In addition, this was only tested on a small period in history during a largely bull run and on a small amount of stocks with low exposure to the broad market. Part of the reason for such a high alpha value is also due to the COVID-19 drop in prices. During this time our model reduced exposure to the broad market.

Overall, we proved that a momentum based trading strategy can be successful in rebalancing your portfolio. In addition, trading quarterly provided better downside protection while posting comparable returns.

Full code can be found here: https://github.com/colaso96/Back_Testing_Momentum_based_trading_strategy

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

Simplified Saving

Credit should be your secret weapon

No Money Manager Wants This Deal

photo via Dziana Hasanbekava

A Guide to Construction Loans & Financing

Insightful newsletter of Replete Equities — Issue #2

Chatting Mortgage-Tech with Orion Parrott, Co-Founder & CEO of Lendsnap

Understanding Forward Contracts..

How Does Securitisation Work?

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
Cole Crescas

Cole Crescas

https://www.linkedin.com/in/cole-crescas/

More from Medium

Inheritance with Types in Python:

Running X server on UBUNTU– 18.04

Projected Scores: Reliving childhood memories through Data Science

Covid-19 cheast x_ray_image Classification(CNN)end_to_end with streamlit .Deploy