Objective of stock backtesting:

Backtesting in stock algo trading is to test a trading strategy or predictive model with historical data to determine its performance. This process is important and it can let investor to find out an optimal trading strategy. Here is a sample of backtest on stock HK0388 on my own algo trading system:

This is HK0388, total trading days = 5472
HK0388, parameters = 2, 4, 2, 5
-4.8161, -13.7103, -13.5331, 8.3339, 20.2138, -3.8829, 11.6122, 6.0678, -9.4557, 6.7058, 0.5790, 2.8548, 20.4323, -16.3260, -12.7435, 1.3719, 36.7052, 2.6215, 4.5382, 10.0345, -5.4869, 41.8896, 23.1570, -12.9089, 9.1296, 2.1442, -15.6910, -22.3721, -13.8829, 33.1733, -4.0782, 2.7715, -3.0572, -0.0898, 11.7850, -12.5571, 32.3933, 44.1903, -17.7951, -16.2971, -19.6498, 4.8608, 11.8734, -11.9400, -7.7838, -10.3083, 196.2293, -1.0429, -1.4139, 32.5750, 205.5250, 12.2433, 6.0711, -5.2982, -14.5095, 19.5167, -1.9226, 33.5607, 2.5945, 29.4525, -7.2467, -5.0261, -7.3288, 4.4833, -14.4691, 5.4733, 2.1917, 6.3505, 14.0477, -14.1704, -2.5907, -20.1280, 11.2891, -8.7847, 22.6951, 
2022-07-26, 2022-03-25, 2022-03-16, 2022-02-25, 2021-12-03, 2021-11-30, 2021-08-24, 2021-07-26, 2021-05-18, 2021-05-07, 2020-11-20, 2020-11-11, 2020-07-21, 2020-06-15, 2020-05-18, 2020-05-07, 2020-04-02, 2020-03-09, 2020-02-17, 2020-01-21, 2019-12-12, 2019-11-11, 2019-08-16, 2019-07-22, 2019-06-06, 2019-05-06, 2018-11-01, 2018-10-11, 2018-07-20, 2018-07-05, 2018-05-02, 2018-03-05, 2018-02-14, 2018-01-25, 2017-08-25, 2017-08-10, 2017-06-19, 2017-06-12, 2016-08-30, 2016-08-17, 2014-12-03, 2014-10-27, 2014-10-22, 2014-09-29, 2013-07-10, 2013-05-23, 2012-07-30, 2012-05-14, 2012-04-13, 2012-03-20, 2011-11-28, 2011-11-16, 2011-10-24, 2011-09-22, 2011-08-23, 2011-08-08, 2011-02-18, 2011-02-17, 2011-01-12, 2010-10-27, 2010-02-23, 2010-02-05, 2010-02-03, 2010-01-13, 2009-11-16, 2009-10-28, 2009-10-07, 2009-09-24, 2009-09-04, 2009-08-17, 2009-06-25, 2009-06-23, 2009-06-11, 2009-06-08, 2009-04-30, 2009-04-22, 2009-03-11, 2009-01-20, 2008-12-16, 2008-10-08, 2008-09-19, 2008-08-19, 2008-07-21, 2008-05-08, 2008-04-21, 2008-04-14, 2008-03-26, 2008-03-20, 2008-03-12, 2008-02-06, 2008-02-04, 2007-11-22, 2007-11-15, 2007-10-04, 2007-05-04, 2007-04-19, 2007-04-03, 2007-01-25, 2007-01-15, 2006-11-28, 2006-06-29, 2006-05-16, 2005-06-16, 2005-04-04, 2004-09-01, 2004-07-26, 2004-06-24, 2004-05-10, 2004-05-04, 2004-03-25, 2004-02-06, 2004-01-29, 2003-12-22, 2003-10-23, 2003-10-16, 2003-10-08, 2003-07-29, 2003-07-23, 2003-07-07, 2003-06-24, 2003-04-28, 2003-03-24, 2003-03-03, 2003-02-14, 2003-02-12, 2003-01-17, 2002-11-25, 2002-11-11, 2002-09-25, 2002-09-16, 2002-08-20, 2002-07-11, 2002-03-28, 2002-01-15, 2001-12-05, 2001-11-29, 2001-10-24, 2001-10-16, 2001-09-19, 2001-08-28, 2001-08-02, 2001-07-05, 2001-04-11, 2001-03-12, 2000-11-28, 2000-11-13, 2000-11-01, 2000-09-18, 2000-09-14, 2000-08-09, 2000-07-12, 

This statistics is from 2000-06-27 to 2022-08-10, with 75 trades:
No. of wins = 40, total profit = $953,738.  No. of loss = 35, total loss = $-352,297
Total % return on 75 trades: 2752.07%, with cut loss at -0.12. Commission fee = 0.725%
Actual profit or loss of $100,000 rollover = $2,752,065
Actual profit or loss of fixed $100,000, non-rollover = $601,440
Avg trade separate = 20.39 days. Avg holding days per trade = 52.53
Profit between 0 - 12.0% of all trades = 30.67% (23)
Profit between 12% - 24.0% of all trades = 13.33% (10), profit above 24% = 9.33% (7)
Day 1 close = $8.15, last close = $344.2, change % = 4,123.31%
Max Profit %: 205.52
Max Loss %: -22.37
Average Profit: 19.1849%, without max profit trade
Average Loss: -9.7037%, without max loss trade
Profit/loss ratio: 1.9771, without max profit or loss trades
Win Rate: 53.3333%
  ====> Status: on hold, last buy at $370.0000(next D.O.), signal date = 2022-07-26. Today=$344.2000, chg=-6.97% <====
Buy or sell next day: NO
***** End of HK0388, strategy=1. Parameter up: type_a, parameter down: type_c *****

 

  • Line 3, it shows all the past trading result (in descending order) in percentage.
  • Line 4, it shows all the past buy and sell trading signals that match the above trading. That is, the first one (2000-07-12) is always a buy signal, then the next one is sell signal, then repeat the cycle.
  • Line 3 and 4, are simple reference for software developer only. Trader usually uses these buy sell signals and result to plot on candlestick chart and excel for further investigation.

  • Line 6 to the end, the core of the stock trading backtest result. Profit or loss is assume if there is $100,000 invest on each transaction.

  • Line 8, system cut loss is to 12% (-0.12). However, due to bear market in global, all transactions should cut off earlier. e.g 7.5%. This is subject to change at anytime. Stock transaction fee is set to 0.725%. That is, profit or loss transaction is added -0.725% on each.

  • Line 9 and 10, there are 2 profit amounts: rollover and non-rollover. Rollover is to put all remaining amount in next transaction. E.g. if transaction 1 get 10% profit, next transaction amount is $110,000. Normally, I will use non-rollover profit or loss for reference.

  • Line 11, average holding days for transaction is also an important factor for algo trading strategy, because we will assume investor has limited amount of money. Later, I will improve the system to find out average holding days for winning transactions, and average holding days for losing transactions. I will explain it in later demo.

  • Line 14, the trading strategy return percentage should compare with the organic growth of the stock in the same period.

  • Line 18, max loss can be more than cut loss percentage, due to stop loss is execute on next trading day day-open price.

  • Line 19, profit / loss ratio, often called reward-risk ratio. This is calculated from previous average profit / average loss. This is another important factor to consider if this trading strategy is good or not. The higher the value, the better performance. It is recommended that this figure should be greater than 2.0.

  • Line 20, win rate is calculated from total number of winning trades / total number of trades. In this example, it is 40 / (40 + 35)  (figures shown on line 7). In most algo trading strategies, this figure usually range in 40% – 60%. This figure should analysis together with profit/loss ratio, to give more accurate result.

  • Line 21-22, system should suggest whether this stock is to buy, sell, hold or wait.

  • Line 23, system shows which algo strategy using. My program trading platform has several different trading strategies for each stock. Each strategy can use different parameters to optimize the trading result. The optimize result is not necessary the maximum profit. Factors like reward-risk ratio, win rate and average trading days are consider.

Many algo trading platform such as (MetaTrader) includes a factor called “maximum drawdown”. However, if user strictly apply stop loss on each transaction, it should reduce risk and avoid maximum drawdown occurs. Factors that to be consider included in algo trading system can be found in previous article.