Have you ever tried an investment strategy that was highly recommended, yet decided to quit once you started losing money?
I know I have.
A buddy of mine who used to work at a hedge fund was preaching to me about his insane portfolio with super star stocks. For some reason I thought it would be cool to go along and invest in the same stocks as he did. And so I did.
When I bought in, it happened to be a good entry point as the market was going up almost everyday. With stocks like Sea and Tesla, there were days when the portfolio surged 4% in a single day!
However as markets became highly overbought, a huge red day soon followed. I panicked and cashed out shortly after that. Overall, I stuck with the portfolio for three short weeks. While it was an exciting experience, never again would I invest in a strategy that I had no confidence in.
How do I find a strategy that I can stick to, even when markets are volatile?
Backtest your portfolio
Backtesting is the process of simulating an investment strategy using historical prices to test how well the strategy would have done in the past. You need to backtest your investment strategy because it allows you to confirm whether you have an edge in the market without risking any of your own money.
It’s kind of like how pilots have to train using a flight simulator before they are allowed to fly a plane. In the flight simulator, the pilot will be tested on whether he is able to safely navigate the plane through different situations. If a pilot crashes a plane during a flight simulation, there is no way he will be allowed to fly a real plane with hundreds of passenger lives at risk.
Similarly, if an investment strategy has performed poorly during a backtest, why should you risk your hard earned life savings on this strategy?
In addition, backtesting your trading strategy does not put any of your money at risk. It is a way to research your trading ideas and test whether they are profitable before your money is on the line.
Now that we have discussed the importance of backtesting, the next question is how do we backtest a trading strategy?
Decide on your trading rules
Every backtest relies on a logical set of trading rules that gets applied consistently during each day’s simulation. This set of trading rules is also commonly referred to as your trading plan.
As an example, we are going to backtest a moving average crossover strategy. Moving averages are used to estimate the momentum of a stock. Stocks trading above their moving average have upward trending prices (uptrend stocks) while stocks trading below their moving average have downward trending prices (downtrend stocks). We want stocks that are in a strong uptrend as they are likely to continue going up.
This strategy’s trading rules are as follows:
Filter for stocks trading above their 200 day simple moving average (200SMA).
Rank the filtered stocks based on the following signals:
Lowest price to earnings ratio (PE Ratio)
Highest return on equity (ROE)
Highest profit growth
Select the top 20 stocks with the highest ranking to form an equally weighted portfolio.
Our investment universe will be stocks from the S&P 500, we will be observing the portfolio weekly to check whether every stock in the portfolio is above its 200SMA. If a stock falls below its 200SMA it will be replaced with another stock above its 200SMA and with the highest ranking.
Backtesting a trading strategy is highly computationally intensive. Fortunately, we can rely on the power of technology to simplify this process.
Head over to PyInvesting’s moving average backtest where we will backtest our moving average crossover strategy. PyInvesting is a backtesting software that I built for users to go live with their investment strategies on the cloud.
Select stocks for your investment universe
Click on the blue button to select your stocks and select S&P 500 under the template portfolio. This will select stocks from the S&P 500 that will form our investment universe.
Create your signals to rank your stocks
Next we are going to select the smart beta factors used to rank our stocks. Based on our trading rules, we are going to select stocks with the lowest PE ratio, highest ROE and highest profit growth. These signals are equally weighted at 33% each and combined to form an overall signal.
Decide on your moving average parameters
Following our trading plan, we are going to use the 200 day simple moving average (200SMA) to determine whether a stock is in a long term uptrend or downtrend. By default, the moving average parameters are set to the 200SMA so we do not need to make further changes.
Number of stocks and portfolio rebalancing frequency
Set the number of stocks to 20 and a weekly rebalancing frequency where our trading algorithm will be checking that each of the 20 stocks in our portfolio is above its 200 day moving average. Hit the run backtest button to simulate your trading strategy!
Portfolio performance analysis
The backtest results show that our strategy has an annualized return of 16.7% vs the S&P 500 which did 9.1% from March 2016 to October 2020. The volatility of our strategy is also lower at 17.2% vs the S&P 500’s volatility of 19.9%. Consequently, our strategy has a higher Sharpe ratio of 0.78 vs the benchmark’s Sharpe of 0.37. In addition, our strategy has a max drawdown of 39.9% during the 2008 global financial crisis while the S&P 500 was down 55.2%.
The annual returns plot also shows that our strategy beats the S&P 500 in 12 out of the last 14 years. It underperformed in 2016 and 2019. This means that there is a 85.7% (12/14) chance of beating the benchmark in any year going forward. Not too shabby.
Here is a training video I created with a live demo of me running a moving average backtest.
Profiting from your investment strategy
After analyzing our backtest results, we are happy with our strategy’s performance. So how do we turn our backtest into a live actionable trading strategy that we can profit from? Simply click on the “Go Live” button. Once your strategy is live, PyInvesting’s backtester will run your strategy daily with live prices and send you daily email updates with any buy or sell orders from your strategy. You can then make the trades on your own personal account to profit from your strategy. FYI Interactive Brokers IBKR is great as transaction costs are extremely low.
Happy investing, and may the odds be in your favour.
If you want to develop an effective investment strategy, learning how to utilize the results of backtesting can be one of the best decisions you ever make since backtesting can help you identify an incorrect or correct investment before your money is on the line. PyInvesting is a backtesting software written in Python that helps investors go live with their investment strategies on the cloud without writing a single line of code.
Ivan is the founder of PyInvesting.com. He is passionate about technology and finance and has worked as a software developer at a hedge fund where he was responsible for building the fund's trading system. He hopes that PyInvesting will help investors adopt a data driven approach to investing and support them in their journey towards financial freedom.