Prev Article: 1.2 - Searching for Strategies

Backtesting

Having chosen some candidate strategies, you then need to check whether the strategy is actually profitable.

We do this through backtesting, which is testing our strategy on data. The data itself can be either historical or simulated.

In this section we will look into a few considerations when backtesting:

Example implemented backtests can be seen here:
https://github.com/Aldo-Aditiya/algo_trading/blob/master/lib/backtest.py

Basic

A backtest is basically this: Using some price data, we run our selected strategy, and then generate metrics to evaluate said strategy.

Also, backtests are more conveniently done with periodic (e.g. daily) returns rather than prices:

For Example:

Once we run our backtest, we calculate the relevant metrics.

Metrics

Cumulative Return
Pasted image 20221206153248.png
Cumulative Return is just the total amount of return generated across some period of time:

This shows the strategy's performance on the data. Note that it is usually useful to compare the results with some benchmark. For instance, an american equity strategy should have a benchmark of S&P500.

Monthly Return
Pasted image 20221206154426.png
Like Cumulative return, but for monthly period. Very useful to get a feel of the strategy's performance in different market conditions.

Compound Annual Growth Rate (CAGR)
If cumulative return is the return over some period of time, CAGR is the return every year, assuming the final value.

Volatility
Standard deviation of a stock price over some period of time. Usually measured daily, but if we want an annualized volatility, we just need to multiply by for 252 number of trading days.

As a side note, the term "Risk" in quantitative finance is synonymous with Volatility. So a historically volatile asset is thus risky.

Also, be careful that standard deviation are usually calculated asssuming the data is distributed normally. Though a useful approximation, sometimes it does not reflect the reality.

Sharpe Ratio
Risk adjusted returns. Or, returns that have been adjusted by the volatility and some risk-free return rate.

The risk free return rate is the return rate of some instrument that provides a "risk free return". This is usually synonymous with the treasury interest rate, i.e. 3.6%.

Useful to assess whether the startegy has a good tradeoff between returns and volatility.

Maximum Drawdown
Pasted image 20221206155444.png
The worst % downturn that ever happened when running the strategy on the data. Calculated from the local peak, starting from the point where the returns started going down, and ending when the returns goes back to that local peak.

Very useful to assess the risk of the strategy.

Pasted image 20221206155617.png
Another useful visualization is the underwater plot, which visualizes how much was lost in every downturn period.

Longest Drawdown Period
The longest period where the returns remain underwater.

Other Metrics
There are other useful metrics such as, among others:

You can pick and choose based on your needs.

Methods

Walk Forward Backtest
The simplest form of backtesting. We simply set some historical period of backtesting, and then run our backtest within those periods.

The problem with this is that:

Historical Event Backtest
It might be useful to test the performance of our strategy at particular times in history.

For example, it might be useful to see how our strategy would have performed in the 2008 stock market crash to understand how much we would stand to lose if such an event happens again.

Some interesting periods filtered based on the LQ45 Index are as such:

Bootstrapped Backtest
By running our backtest with our base data, we would have generated a returns probability distribution. illustrated below.

Pasted image 20221206162623.png

The idea of bootstrapping is basically to randomly sample from the returns distribution, and generate multiple different possible return curves.

Pasted image 20221206161724.png
By doing this, we are essentially simulating possible return curves based on the strategy's characteristic that is implicit from the returns probability distribution.

This is useful to estimate the probability distribution of our strategy's metrics.

For example, by having 500 returns curve we would have 500 of the same metric. Thus, we can model the probability distribution of the 500 metrics, and use it to make a conclusion about our model characteristics.

Model Backtest
Model backtest extends the bootstrap idea by asking: instead of just the returns distribution, can we directly model the underlying process that generates the price / returns?

Modelling a process is (slightly) more complicated, and outside the scope of this discussion, but to give some examples of stochastic process models:

Once we have made the model, we can use the model the same way as we did in the bootstrap method.

You can read more here: http://www.turingfinance.com/random-walks-down-wall-street-stochastic-processes-in-python/

Considerations in Backtesting

There are a number of considerations when doing backtesting and evaluating its results.

Biases
Market Model
Statistical Significance
Regime Change
Backtest Method

Sources


Next Article: 1.4 - Fitting and Quantifying Forecasts