Volatility Filtered Market Timing Strategy

Author: ChaoZhang, Date: 2024-01-15 12:27:47
Tags:

img

Overview

This strategy implements an enhanced buy and hold strategy by adding a filter based on historical volatility. The filter closes long positions during high volatility market regimes and re-enters long positions when volatility is low, in order to reduce maximum drawdown.

Strategy Logic

  1. Calculate the historical volatility of SPY over the past 100 days
  2. If current volatility is higher than the 95th percentile of past 100 day’s volatility, filter out that trading day, close long position
  3. If volatility is below 95th percentile, enter long position

Advantage Analysis

Compared to simple buy and hold without filter, this strategy improved annualized returns over the 28 year backtest period (7.95% vs 9.92%) and significantly reduced max drawdown (50.79% vs 31.57%). This shows adding a volatility filter can improve returns and lower risk to some extent.

Risk Analysis

Main risks come from accuracy of volatility calculation methodology and filter parameter tuning. If volatility calc is inaccurate, filter will fail. If filter parameters are tuned poorly (too conservative or aggressive), it can adversely impact strategy returns. Also, past performance does not guarantee future results.

Optimization Directions

Consider adding other confirming indicators as additional filters, like long term moving average, ADX index etc. Parameter tuning is also critical, like testing different lookback periods, filtering thresholds etc. Machine learning & time series analysis techniques can also be used to build and optimize the volatility prediction model.

Summary

This strategy greatly improved returns and reduced max drawdown of a SPY buy & hold strategy via a simple volatility filter. It shows the importance of market regime identification and asset allocation. We can further refine it by optimizing the volatility model and adding confirming signals.


/*backtest
start: 2023-01-08 00:00:00
end: 2024-01-14 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// 
// @author Sunil Halai
//
// This script has been created to demonstrate the effectiveness of using market regime filters in your trading strategy, and how they can improve your returns and lower your drawdowns
//
// This strategy adds a simple filter (The historical volatility filter, which can be found on my trading profile) to a traditional buy and hold strategy of the index SPY. There are other filters
// that could also be added included a long term moving average / percentile rank filter / ADX filter etc, to improve the returns further.
//
// The filter added closes our long position during periods of volatility that exceed the 95th percentile (or in the top 5% of volatile days)
//
// Have included the back test results since 1993 which is 28 years of data at the time of writing,  Comparing  buy and hold of the SPY (S&P 500), to improved by and hold offered here.
//
// Traditional buy and hold:
//
// Return per year:     7.95   % (ex Dividends)
// Total return :       851.1  %
// Max drawdown:        50.79  %
//
// 'Modified' buy and hold (this script):
//
// Return per year:     9.92    % (ex Dividends)
// Total return:        1412.16 %
// Max drawdown:        31.57   %
//
// Feel free to use some of the market filters in my trading profile to improve and refine your strategies further, or make a copy and play around with the code yourself. This is just 
// a simple example for demo purposes.
//

//@version=4
strategy(title = "Simple way to beat the market [STRATEGY]", shorttitle = "Beat The Market [STRATEGY]", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, currency="USD", default_qty_value=100)


upperExtreme = input(title = "Upper percentile filter (Do not trade above this number)", type = input.integer, defval = 95)
lookbackPeriod = input(title = "Lookback period", type = input.integer, defval = 100)

annual = 365
per = timeframe.isintraday or timeframe.isdaily and timeframe.multiplier == 1 ? 1 : 7
hv = lookbackPeriod * stdev(log(close / close[1]), 10) * sqrt(annual / per)

filtered = hv >= percentile_nearest_rank(hv, 100, upperExtreme)

if(not(filtered))
    strategy.entry("LONG", strategy.long)
else
    strategy.close("LONG")

More