Estratégia de calendário do mercado filtrada pela volatilidade

Autora:ChaoZhang, Data: 2024-01-15 12:27:47
Tags:

img

Resumo

Esta estratégia implementa uma estratégia aprimorada de compra e retenção adicionando um filtro baseado na volatilidade histórica.

Estratégia lógica

  1. Calcular a volatilidade histórica do SPY nos últimos 100 dias
  2. Se a volatilidade atual for superior ao 95o percentil da volatilidade dos últimos 100 dias, filtrar para fora desse dia de negociação, fechar posição longa
  3. Se a volatilidade for inferior ao 95o percentil, introduzir posição longa

Análise das vantagens

Em comparação com a simples compra e retenção sem filtro, esta estratégia melhorou os retornos anualizados ao longo do período de backtest de 28 anos (7,95% vs 9,92%) e reduziu significativamente a retirada máxima (50,79% vs 31,57%).

Análise de riscos

Os principais riscos vêm da precisão da metodologia de cálculo da volatilidade e do ajuste do parâmetro do filtro. Se o cálculo da volatilidade for impreciso, o filtro falhará. Se os parâmetros do filtro forem mal ajustados (muito conservadores ou agressivos), isso pode afetar negativamente os retornos da estratégia. Além disso, o desempenho passado não garante resultados futuros.

Orientações de otimização

Considere a adição de outros indicadores de confirmação como filtros adicionais, como média móvel de longo prazo, índice ADX, etc. O ajuste de parâmetros também é crítico, como testar diferentes períodos de retorno, filtragem de limiares, etc. As técnicas de aprendizado de máquina e análise de séries temporais também podem ser usadas para construir e otimizar o modelo de previsão de volatilidade.

Resumo

Esta estratégia melhorou muito os retornos e reduziu a retirada máxima de uma estratégia de compra e retenção SPY através de um simples filtro de volatilidade.


/*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")

Mais.