Estratégia de inversão de tendência baseada em médias móveis múltiplas

Autora:ChaoZhang, Data: 2023-11-21 14:53:48
Tags:

img

Resumo

Esta estratégia gera sinais de compra e venda com base em inversões de múltiplos indicadores de tendência, incluindo TDI, TCF, TTF e TII. A estratégia permite selecionar qual sinal de indicador utilizar para entradas e saídas.

Estratégia lógica

  • Indicador TDI

    O indicador TDI é construído usando a dinâmica do preço com técnicas de soma e suavização.

  • Indicador TCF

    O indicador TCF mede mudanças positivas e negativas nos preços para avaliar as forças de alta e baixa.

  • Indicador TTF

    O indicador TTF compara o poder dos preços mais altos e mais baixos para determinar a tendência.

  • Indicador TII

    O TII combina a média móvel e as faixas de preços para identificar inversões de tendência.

A lógica de entrada longa e próxima seleciona os sinais adequados com base no indicador configurado.

Vantagens

A estratégia incorpora vários indicadores de negociação de tendências comumente utilizados, o que permite flexibilidade para se adaptar às condições de mercado em evolução.

  1. Captura oportunamente as oportunidades de reversão da tendência utilizando sinais de reversão
  2. Otimizável através da configuração de diferentes indicadores
  3. Combinações de indicadores ricos podem ser usadas para confirmar sinais

Riscos

Os principais riscos que esta estratégia enfrenta são:

  1. Os sinais do indicador podem ter sinais falsos que resultam em perdas.
  2. Indicadores individuais não podem avaliar plenamente as tendências e são suscetíveis ao ruído do mercado
  3. Configurações incorretas de indicadores e parâmetros de negociação podem falsear a interpretação do mercado e gerar operações errôneas

Os riscos podem ser reduzidos:

  1. Optimização dos parâmetros do indicador para encontrar as melhores combinações
  2. Exigir confirmações de sinais de indicadores múltiplos para melhorar a qualidade
  3. Ajuste do tamanho das posições para controlar a perda de uma única transação

Oportunidades de melhoria

A estratégia pode ser reforçada em vários domínios:

  1. Teste de indicadores e parâmetros ideais em diferentes ciclos de mercado
  2. Adicionar ou reduzir indicadores para encontrar as melhores combinações
  3. Filtrar sinais falsos
  4. Otimizar estratégias de dimensionamento de posições, por exemplo, tamanho variável, stop loss de trailing
  5. Incorporar pontuação de aprendizado de máquina para ajudar na qualidade do sinal

Conclusão

A combinação de múltiplos indicadores de reversão de tendência e de configurações de otimização permite adaptar esta estratégia a diferentes ambientes de mercado para operar em pontos de virada de tendência.A chave é encontrar os parâmetros e indicadores ótimos enquanto se controla o risco.Otimizações e validações contínuas podem construir uma estratégia alfa estável.


/*backtest
start: 2023-11-13 00:00:00
end: 2023-11-15 03:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
//
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kruskakli
//
// Here is a collection of Trend Indicators as defined by M.H Pee and presented
// in various articles of the "STOCKS & COMMODITIES Magazine"
//
// The actual implementation of the indicators here are made by: everget
//
// I have gather them here so that they easily can be tested.
//
// My own test was made using 15 companies from the OMXS30 list
// during the time period of 2016-2018, and I only went LONG.
//
// The result was as follows:
//
//        Average    Std.Dev
//        profit
//  TDI    3.04%      5.97
//  TTF    1.22%.     5.73
//  TII    1.07%      6.2
//  TCF    0.32%      2.68
//
strategy("M.H Pee indicators", overlay=true)


use = input(defval="TDI", title="Use Indicator", type=input.string,
             options=["TDI","TCF","TTF","TII"])

src = close


//
// TDI
//
length = input(title="Length", type=input.integer, defval=20)
mom = change(close, length)
tdi = abs(sum(mom, length)) - sum(abs(mom), length * 2) + sum(abs(mom), length)
// Direction Indicator
tdiDirection = sum(mom, length)
tdiLong = crossover(tdiDirection, tdi)
tdiXLong = crossunder(tdiDirection, tdi)

//
// TCF
//
tcflength = input(title="Length", type=input.integer, defval=35)

plusChange(src) =>
    change_1 = change(src)
    change(src) > 0 ? change_1 : 0.0
minusChange(src) =>
    change_1 = change(src)
    change(src) > 0 ? 0.0 : -change_1

plusCF = 0.0
plusChange__1 = plusChange(src)
plusCF := plusChange(src) == 0 ? 0.0 : plusChange__1 + nz(plusCF[1])

minusCF = 0.0
minusChange__1 = minusChange(src)
minusCF := minusChange(src) == 0 ? 0.0 : minusChange__1 + nz(minusCF[1])

plusTCF = sum(plusChange(src) - minusCF, tcflength)
minusTCF = sum(minusChange(src) - plusCF, tcflength)

tcfLong = plusTCF > 0 
tcfXLong = plusTCF < 0

//
// TTF
//
ttflength = input(title="Lookback Length", type=input.integer, defval=15)

hh = highest(length)
ll = lowest(length)

buyPower = hh - nz(ll[length])
sellPower = nz(hh[length]) - ll

ttf = 200 * (buyPower - sellPower) / (buyPower + sellPower)

ttfLong = crossover(ttf, 100)
ttfXLong = crossunder(ttf, -100)

//
// TII
//
majorLength = input(title="Major Length", type=input.integer, defval=60)
minorLength = input(title="Minor Length", type=input.integer, defval=30)
upperLevel = input(title="Upper Level", type=input.integer, defval=80)
lowerLevel = input(title="Lower Level", type=input.integer, defval=20)

sma = sma(src, majorLength)

positiveSum = 0.0
negativeSum = 0.0

for i = 0 to minorLength - 1 by 1
    price = nz(src[i])
    avg = nz(sma[i])
    positiveSum := positiveSum + (price > avg ? price - avg : 0)
    negativeSum := negativeSum + (price > avg ? 0 : avg - price)
    negativeSum

tii = 100 * positiveSum / (positiveSum + negativeSum)

tiiLong = crossover(tii, 80)
tiiXLong = crossunder(tii,80)

//
// LOGIC 
//
enterLong = (use == "TDI" and tdiLong) or (use == "TCF" and tcfLong) or (use == "TTF" and ttfLong) or (use == "TII" and tiiLong)
exitLong = (use == "TDI" and tdiXLong) or (use == "TCF" and tcfXLong) or (use == "TTF" and ttfXLong) or (use == "TII" and tiiXLong)


// Time range for Back Testing
btStartYear  = input(title="Back Testing Start Year",  type=input.integer, defval=2016)
btStartMonth = input(title="Back Testing Start Month", type=input.integer, defval=1)
btStartDay   = input(title="Back Testing Start Day",   type=input.integer, defval=1)
startTime = timestamp(btStartYear, btStartMonth, btStartDay, 0, 0)

btStopYear  = input(title="Back Testing Stop Year",  type=input.integer, defval=2028)
btStopMonth = input(title="Back Testing Stop Month", type=input.integer, defval=12)
btStopDay   = input(title="Back Testing Stop Day",   type=input.integer, defval=31)
stopTime  = timestamp(btStopYear, btStopMonth, btStopDay, 0, 0)

window() => time >= startTime and time <= stopTime ? true : false


riskPerc     = input(title="Max Position  %", type=input.float, defval=20, step=0.5)
maxLossPerc  = input(title="Max Loss Risk %", type=input.float, defval=5, step=0.25)

// Average True Range (ATR) measures market volatility.
// We use it for calculating position sizes.
atrLen   = input(title="ATR Length", type=input.integer, defval=14)
stopOffset = input(title="Stop Offset", type=input.float, defval=1.5, step=0.25)
limitOffset = input(title="Limit Offset", type=input.float, defval=1.0, step=0.25)
atrValue = atr(atrLen)


// Calculate position size
maxPos = floor((strategy.equity * (riskPerc/100)) / src)
// The position sizing algorithm is based on two parts:
// a certain percentage of the strategy's equity and
// the ATR in currency value.
riskEquity  = (riskPerc / 100) * strategy.equity
// Translate the ATR into the instrument's currency value.
atrCurrency = (atrValue * syminfo.pointvalue)
posSize0    = min(floor(riskEquity / atrCurrency), maxPos)
posSize     = posSize0 < 1 ? 1 : posSize0

if (window())
    strategy.entry("Long", long=true, qty=posSize0, when=enterLong)
    strategy.close_all(when=exitLong)

Mais.