Helping investors beat the market.

Ivann Fok
4 months ago · 688 Views


Ivann Fok

Ivan is the founder of 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.

The Ultimate Tool for Finding Top Breakout Stocks
Ed Seykota, one of the market wizards behind computerized systems trading, turned $5,000 into $15 million in 12 years. He is a strong believer in trend following and uses a breakout trading system to enter trades when momentum is in his favour. Whether you do day trading or swing trading, using a solid breakout trading system can significantly improve the risk reward ratio of your trades and increase your trading profits in the long run. In this article, I’m going to introduce a tool that I built to help you identify top breakout stocks with explosive momentum. What is a breakout in stocks? A breakout in stocks occurs when the price of a stock breaks above its resistance level or breaks below its support level. After a breakout happens, prices tend to continue moving undeterred with strong momentum. The example below shows Tesla breaking out from it’s 140 day high in November 2019 (highlighted in yellow) when it surged from $65 to almost $400 one year later.  You can think of the stock’s price normally acting as a ping pong ball bouncing between a glass floor (support level) and a glass ceiling (resistance level).  When the ball hits the floor, it rebounds off the floor which acts as a support. When the ball hits the ceiling it bounces off the ceiling which acts as a resistance.  However, what happens if this ping pong ball suddenly transformed into a bowling ball during mid flight?  If the bowling ball is falling towards the floor, it would smash right through the glass floor which offers little support against the bowling ball’s large momentum. Similar If the bowling ball is flying towards the ceiling, it will smash right through the glass ceiling which provides negligible resistance. In the case of stocks, when investors get excited due to a positive earnings release for example, this enthusiasm provides the momentum for the stock price to break through its resistance level and surge towards new highs.  Conversely when investors start panicking due to a public health crisis such as Covid-19, the widespread fear across the market causes stock prices to crash through their support levels. As a breakout swing trader or day trader, your goal is to identify such breakouts when they occur so that you can buy stocks that break above their resistance level and short stocks that break below their support level. How do you trade breakouts? The rules of a mechanical trend following trading systems are very simple. Go long when the price breaks above the high of the past n days. Go short when the price breaks below the low of the past m days. The highest price of the past n days acts as the resistance level. If the stock’s price is able to break above this high price, it reflects strong upward momentum. This is a strong indicator that confirms a breakout into an uptrend.  Conversely the lowest price of the past m days acts as the support level. If the stock’s price is able to break below its low price, it is a strong indicator that confirms a breakout into a downtrend. Let’s take a look at Apple. The chart below shows the price of Apple (blue line) sandwiched between 2 gray lines which act as the channel.  The gray line above the blue line is the resistance level based on the high of the past 20 days. The gray line below the blue line is the support level based on the low of the past 60 days.  The green arrow is an indicator to go long when the price hits the top gray line. The red arrow is a signal to short the stock when the price hits the bottom gray line. The big question is what look back periods should we use?  The turtle traders, under the mentorship of Richard Dennis, used both a short term and a longer term lookback period for systems trading.  System 1 was a short term system based on a 20 day breakout while System 2 was a longer term system based on a 55 day breakout.  While this set of rules for systems trading were highly profitable for the turtle traders, backtest results have shown that when the same set of rules were applied many years later, the strategy was not as successful as before.  There are two possible reasons why the strategy did not perform well years many years later.  The breakout parameters for the lookback period are fixed at 20 days and 55 days. This is a problem because different instruments have different trends. Some instruments have short term trends while other instruments have long term trends. These trends could also change over time as well so it does not make sense to use a static look back period over the entire history.   The breakup parameter of 20 days is the same as the breakdown parameter of 20 days. There is no reason to assume that these parameters should be the same because in one case we are looking to enter a long trade while in the other we are trying to short the market.  Why is it important to pick the right indicator to confirm a breakout? Picking the wrong stock breakout signal that fails to properly capture the trend can be detrimental to your strategy’s performance.  This is because if a stock is in a long term uptrend and we use a short look back period, there is a high chance that we might get whipsawed due to the volatility of the stock’s price.  For example, if we use a 120 day break up and 80 day break down window for Tesla, the plot below shows that a bad trade would be made in the part highlighted in yellow below. The system generated a sell signal in March 2020 anticipating that the stock will enter a down trend. However, the stock rebounded shortly after that, causing the trading system to miss out on the recovery after the Covid crash. If a longer term break down parameter was used, the sell signal would not have triggered because the gray line would have been lower, hence allowing more room for volatility. What traders typically do to account for the volatility of the stock’s price when deciding their breakout parameters is to use the average true range (ATR) which is a measure of the market volatility over a given period of time.  However, this approach is subjective as well. There are different multiples of ATR which you could choose to set your stop loss. For short term trends, setting your stop loss at 1 X ATR might be more appropriate while for longer term trends, setting your stop loss further away at 2 X ATR might be better to avoid being whipsawed due to volatility.  How do we remove this subjective approach towards trading and instead rely on data analysis to select the optimal breakout parameters? Finding the best stock breakout signals The best approach is to backtest your trading strategy using different breakout parameters. This allows us to figure out which breakout parameters are the most profitable and are able to generate trades with the highest risk reward ratio.  To help you out, I’ve created a tool that runs 100 backtests using different breakup and breakdown parameters. This is how you use it. First head over to Fill in the form below specifying the stock you are interested in. In this example we are going to look at Apple. Next select your start and end date for the optimization. The start date by default is chosen to be 5 years ago and the end date is set as today’s date.  Select the performance metric that you would like to use to determine which is the best breakout trading parameter. By default, the Sharpe Ratio is used which allows us to find out which breakout parameters lead to the highest risk adjusted returns of our trading strategy.  Finally hit the blue button to start trading breakouts. The results will show a heat map where each square represents the results of running a backtest with a specific breakout parameter. The greener the square, the better the performance would be based on our selected performance metric (Sharpe Ratio). The results show that the Sharpe Ratio generally improves as the breakup lookback period gets shorter. On the other hand, there is no clear pattern for the breakdown parameter in affecting the trading strategy’s performance. If you click on any of the squares, it will show you the backtest results using the respective parameters. For example, here are the results from the 20 day breakup and 60 day breakdown window. Based on these parameters, the latest signal generated by the trading system was a buy signal in April when the markets rebounded from the Covid crash and broke into an uptrend. Scrolling down we can see the profit and loss (P&L) of trading Apple using the 20 day breakup and 60 day breakdown parameters along with performance numbers such as total returns, Sharpe Ratio and max drawdown.  Conclusion Breakout trading can be highly profitable if we are able to catch onto a major trend. However, finding the best stock breakout signals is crucial as it prevents us from being whipsawed due to price volatility. We discussed how we can improve our risk reward ratio by running multiple backtests with different parameters to find the optimal parameters for our breakout trading strategy.  Now head over to and find out the best trading system for your favourite stocks today.  Happy investing and may the odds be in your favour.
Ivann Fok · 2 months, 2 weeks ago
How to Backtest a Trading Strategy Even if You Can't Code
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 software 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.
Ivann Fok · 3 months, 2 weeks ago
Ivann Fok · 4 months, 1 week ago
Here's Why Every Investor Should Backtest Their Investment Strategy
What is Backtesting? Backtesting is the process of simulating an investment strategy using historical prices to test how well the strategy would have done in the past. Running a simulation over a large number of stocks over the past decades is a computationally intensive process. Fortunately, with the help of technology, investors can rely on backtesting software to run these calculations in a matter of seconds. Some of the most popular backtesting frameworks used to backtest trading strategies are created using Python code.     Why is Backtesting Important? Investment backtesting allows investors to analyze the historical behaviour of an investment strategy and determine how profitable the strategy is. If the backtest results show that a strategy has high returns and low risk, investors will have greater confidence of going live with the strategy. The main idea is that any investment strategy that has performed well in the past is likely to perform well going forward. Conversely if a backtest on a particular investment strategy shows poor performance, the strategy should be rejected because if it performs poorly in the past, it is unlikely to perform well in the future.  Backtesting investment strategies also helps investors understand their strategy’s behaviour during different key periods in history such as the global financial crisis and the Covid-19 public health crisis. They will know how much money they can expect to lose during these black swan events. For older investors, taking on too much risk could mean significantly delaying your retirement. For younger investors who are investing on margin (borrowed money), a large drawdown could mean receiving the dreaded margin call from your stock broker. By backtesting our investment strategy, we are able to know exactly how much money we will lose during these difficult periods so we are able to determine how much risk we can afford to take before we actually start investing. What kind of investment strategies can be backtested? Any strategy that can be expressed as a set of rules can be backtested. For example, a value investor might be interested in a strategy that selects 10 stocks with the lowest price to earnings ratio every year. Similarly an investor that relies on technical analysis might look at buying a stock when its fast moving average crosses above its slow moving average.These strategies all follow a consistent set of rules that can be simulated using historical data. The advantage of rules based investing is that it removes emotions from the picture. Every decision to buy or sell a stock is driven completely based on logic. This prevents investors from making behavioural mistakes such as panic selling or buying due to the fear of missing out. How do we create a realistic backtest? As the purpose of investment strategy backtesting is to find out how effective an investment strategy is, it is important to ensure that our backtest is as realistic as possible. This prevents us from creating backtests that look very attractive on paper but yet perform poorly during live trading. There are a few ways to achieve a more realistic backtest. 1. Choose a large investment universe of at least 100 stocks  A large universe will allow your strategy to select from a wide variety of stocks. This is important because it allows us to test whether the strategy is able to pick winning stocks from a large basket of stocks. A great strategy will be able to differentiate between winners and losers. However, if we restrict our investment universe to a small number of stocks, there are not many stocks for the strategy to select from. This makes it difficult for us to confirm whether a strategy works because both a great and a poor strategy are likely to pick the same stocks when there are so few to choose from. 2. Include at least 20 stocks in your portfolio Having 20 stocks equally weighted in your portfolio will reduce your portfolio’s concentration risk. This means that no single stock will have a huge impact on your portfolio’s performance which allows us to reduce the chance that your strategy just happened to be lucky and picked a winning stock. For example, if our strategy only picked Tesla and was 100% invested, it would have made huge returns over the last few months. However I would not be convinced that the strategy works because it could have picked another stock out of sheer randomness. It is much harder for a strategy to pick 20 winning stocks than a single winning stock. By ensuring that our strategy holds a diversified portfolio, we can confirm that the strategy is able to consistently pick multiple winning stocks and include them in a high sharpe ratio basket of stocks. 3. Choose a sufficiently long backtest period Ideally, your backtest period should be at least 15 years. This allows us to observe how the strategy performs over multiple economic regimes. For example, by including the 2008 financial crisis which happened 12 years ago, we can understand how much money our strategy would lose during a crisis. This allows us to manage the risk of our portfolio. For older investors that are retiring soon, this means deciding how much money they should invest in the market such that even if a crisis were to happen, they would have enough cash savings to retire as planned. For younger investors investing with leverage, it could mean understanding how much money they can afford to lose during a crisis without being hit by a margin call.  4. Include transaction cost The backtest should include your broker’s transaction cost. Whenever an investor buys or sells stocks, they would incur transaction cost that is paid to the broker. Over time as the number of transactions increase, your transaction cost will increase which would have a significant impact on your strategy’s returns. Moreover, strategies that have high turnovers and make a lot of trades will incur higher transaction costs than strategies that have low turnovers. Hence it is important to include transaction cost so we get a realistic idea of how much money our strategy will make during live trading.  How do we create a backtest? While backtesting requires a great deal of calculations, there are multiple websites we can use to create a backtest easily without writing a single line of code. These platforms include,, and In the table below, I compare the different backtesting platforms and weigh the pros and cons. To be able to backtest a portfolio of different stocks, all platforms except TradingView provide this functionality. TradingView is used more for backtesting a single stock and does not allow users to analyze the performance of a portfolio with multiple stocks. In terms of allowing users to backtest different kinds of strategies, Portfolio Visualizer is the only platform that lacks options. They only allow users to run an allocation backtest where the user has to specify the weights of each stock in the portfolio. All other platforms allow users to choose from multiple types of backtests such as relative strength and moving average backtests.  Regarding stock coverage, all platforms have extensive coverage across multiple exchanges except ETF Replay which is limited to ETFs and a handful of US stocks. If you invest in the Singapore market, unfortunately this platform will not be very helpful as it does not include Singapore stocks.  Among the different platforms, only PyInvesting provides fundamental data such as price to earnings ratio, free cash flow, return on equity, profit growth and more. These fundamental data are obtained from the company’s financial statements going as far back as 15 years. For investors that rely on fundamental data, this is currently the only platform that allows you to create a backtest using fundamental data without writing a single line of code. All other backtesting platforms above only provide technical indicators based on price or volume data which is not suitable for most long term investors. Finally, only PyInvesting allows users to go live with their investment strategy on the cloud. The website will send users an email to update them on their live positions and orders which they can trade on their personal account.  Here is a tutorial on how you can use PyInvesting to create a moving average backtest where the algorithm would select stocks trading above their moving average and sell stocks that are trading below their moving average.   Final Thoughts Every investor should backtest their investment strategy because it allows them to analyze the historical behaviour of their investment strategy and determine how effective it is. Backtesting also helps investors understand their strategy’s behaviour during different key periods in history which allows them to manage their risk. Finally backtesting allows investors to follow a logical set of rules which removes emotion from the picture and helps them make data driven decisions.
Ivann Fok · 4 months, 4 weeks ago
External Mentions of PyInvesting
SG Stock Market Investor   Datascience Investor   Dr Wealth   Tree of Prosperity   The Finance.SG   SG Invest Bloggers   Lady, you can be FREE   Feedspot Top 75 Singapore Investment Blogs   Singaporean Talks Money   StocksCafe   A Path to Forever Financial Freedom (3Fs)   My Sweet Retirement   QuantPedia   Money Maverick   The Babylonians   Financial Horse   Re-ThinkWealth   Jayron Ong   Investment Cache   Financially Independent Pharmacist   Scrappy Finance Investment Moats  
Ivann Fok · 5 months ago
Beating the S&P 500 by selecting US stocks with strong fundamentals Most long term investors rely on fundamental data to decide which stocks they should include in their portfolios. Fundamental data can be obtained from financial statements such as the company’s balance sheet, income statement and cash flow statement. These reports provide valuable insights into the company’s financial health, profitability and growth. These are key fundamental factors that investors should look out for as they are highly correlated to the stocks performance.  However, measuring these fundamental factors is a tedious process. Investors need to comb through hundreds of financial reports to search for these factors and then combine these different factors together to rank the available stocks. Fortunately with the help of technology, this whole process has been simplified for us. Here is how you can go live and profit from a fundamental strategy for US stocks.  Introducing PyInvesting is a website that provides financial data and backtesting tools to help you go live with your own investment strategies. Our backtesting software helps you comb through hundreds of financial reports and tells you which stocks you should trade based on your personal investment strategy. We are going to use PyInvesting to create our fundamental strategy for US stocks. Let’s go! First go to where we will be creating a fundamental backtest. A backtest is a simulation of our investment strategy using historical data. If the simulation results are poor where the strategy significantly underperformed its benchmark, it confirms that the strategy does not work. In contrast, if the strategy significantly outperforms its benchmark, it gives us confidence that it’s likely to perform well going forward. The backtest also tells us what stocks we should be holding in our portfolio so we know which stocks to buy or sell when we go live with our strategy.  Click on the “Select your stocks” button which opens a window to select stocks for your fundamentals backtest. Under template portfolios, click on “S&P 500” to select s&p 500 stocks from the index. The default benchmark is the SPDR S&P 500 Index (which passive investors can use to invest in the s&p 500) where we will compare the historical performance of our fundamentals investment strategy against the s&p 500 historical prices. Financial Health We want to select companies with a healthy balance sheet and a low debt to equity ratio because these companies have strong abilities to repay their creditors. Because these companies have relatively low leverage, these companies are usually quite resilient and are able to survive through tough periods without going bankrupt. As a result, investors holding these stocks tend to lose less money during a crisis because these stocks are well positioned to weather a storm.  Profitability Next, we want to select companies that are highly profitable, and have a high return on equity. A high return on equity implies that the company’s management is efficient in using investment financing to grow their business and is hence able to provide better returns to investors. A low return on equity implies that the company could be mismanaged where the management is investing in unproductive assets. Return on equity is also a measure of how efficient the company is using its resources. A high return on equity could mean that the company is increasing its profits with a relatively low amount of capital.  Growth Finally, we want to select growth companies whose earnings increase at a much faster rate than the overall economy. These companies have high profit growth and tend to reinvest their earnings into profitable areas of their business. They usually do not pay dividends and choose to reinvest profits to further grow their business. Most growth companies such as Tesla, Google, and Amazon are in the technology sector where they are constantly investing in innovative ideas and expanding into new businesses. Growth companies are able to provide huge returns to investors by focusing on revenue growth and maintaining leadership in their industry. Constructing our portfolio strategy To select stocks with strong financial health, profitability and growth, we are going to select 3 factors shown below. Stocks with the highest return on equity, highest profit growth and lowest debt to equity. These 3 factors are equally weighted at 33% each. All other factors in the table are allocated a weight of 0%. The website will automatically combine these 3 factors for you using a z-scoring approach where the factors are normalized using the mean and volatility. Subsequently, we need to decide how many stocks to hold in the portfolio. I decided to hold 20 equally weighted stocks in my portfolio to avoid concentration risks. Every month, the stocks from the S&P 500 are ranked based on these 3 fundamental factors and the top 20 stocks are selected to form an equal weighted portfolio.  Performance The results show that our fundamental strategy for US stocks significantly outperforms the S&P 500 benchmark with an annualized return of 17.8% vs 9.0% since 2006. The strategy also has a higher Sharpe Ratio of 0.73 vs the S&P 500 which has a Sharpe Ratio of 0.37 implying that the strategy has higher risk adjusted returns. While the strategy has a higher volatility than the S&P 500, it has a lower semi deviation than the S&P 500. This means that the strategy has lower downside volatility and higher upside volatility. The max drawdown of the strategy is also significantly lower than the S&P 500 where it lost 46% during the 2008 crisis vs the S&P 500 which lost 55.2%.  Overall, this backtest confirms that the fundamental strategy for US stocks works where if we select stocks with the highest return on equity, highest profit growth and lowest debt to equity ratio, we can expect to outperform the S&P 500 by 8.8% every year. This strategy is suitable for long term fundamental investors who can afford to take on some market risk in exchange for significantly higher returns.   The website also shows the current positions in the portfolio which users can trade on their own personal account. Subscribers have the option of “going live” with their portfolios where they will receive daily email updates to notify them if their strategy decides to make a trade. I’ve added a link below for users to check out the backtest results from this strategy. If you want a full video demonstration of how to create a fundamentals backtest, check out this tutorial video below.   Do consider signing up for a free account with PyInvesting to receive future updates. In my next article, I will be explaining how to use the relative strength backtest. Stay tuned and I’ll see you guys next time!
Ivann Fok · 5 months, 1 week ago