Estratégia de padrão ABCD de impulso

Autora:ChaoZhang, Data: 2023-09-24 13:08:28
Tags:

Resumo

Esta estratégia utiliza o indicador fractal de Williams para identificar picos e baixas de preços e combina padrões ABCD para determinar a direção da tendência.

Estratégia lógica

  1. Use o indicador fractal de Williams para identificar picos e baixas de preços. Padrões diferentes são usados para determinar padrões ABCD de alta ou baixa.

  2. Critérios de identificação do padrão ABCD:

    • A distância entre AB e CD é semelhante, e a distância entre BC e CD atende a certos requisitos proporcionais (entre 0,382-0,886 e 1,13-2,618).

    • O ponto D abaixo do ponto C é um padrão de alta, o ponto D acima do ponto C é um padrão de baixa.

  3. Use a função barssince para determinar qual direção do fractal está mais perto da corrente para julgar a direção geral da tendência.

  4. Insira longo/curto após identificar o padrão ABCD e defina stop loss e take profit para seguir as tendências de médio prazo.

Análise das vantagens

  1. O indicador fractal de Williams ajuda a identificar pontos de virada com mais precisão.

  2. Os critérios do padrão ABCD são simples e confiáveis, fáceis de automatizar.

  3. Julgar a direcção da tendência principal com o barsince evita perdas de falsas rupturas.

  4. Seguindo tendências com stop loss e take profit após entrada.

Análise de riscos

  1. O Fractal Williams pode atrasar e perder pontos de viragem causando perdas.

  2. Os padrões ABCD múltiplos que se sobrepõem podem causar uma identificação errada nos gráficos de médio prazo.

  3. A direcção errada da tendência principal aumenta o risco de ficarmos presos em transacções de médio prazo.

  4. O stop loss muito apertado pode ser facilmente interrompido.

Possíveis soluções:

  1. Teste outros indicadores para ajudar a identificar pontos de virada de forma mais eficaz.

  2. Otimizar os parâmetros do padrão ABCD para tornar a identificação mais rigorosa e confiável.

  3. Melhorar a identificação das principais tendências para evitar preconceitos direcionais errados.

  4. Teste diferentes índices de stop loss/take profit para encontrar pontos ideais.

Orientações de otimização

  1. Testar o MACD, o KDJ e outros indicadores para melhorar a precisão dos sinais de entrada.

  2. Otimizar os parâmetros com base em diferentes produtos e prazos para encontrar os níveis ideais de stop loss/take profit.

  3. Otimizar os períodos de consulta de barras para encontrar as melhores combinações de parâmetros de acordo com as condições de mercado em mudança.

  4. Adicionar médias móveis etc. para filtrar sinais e melhorar a estabilidade.

  5. Introduzir algoritmos de aprendizagem de máquina e mais dados para melhorar a precisão do reconhecimento de padrões.

Resumo

A lógica da estratégia é clara e confiável em geral, usando padrões Williams Fractal e ABCD para determinar a direção da tendência de médio prazo, combinando com filtragem de tendência, stop loss e take profit para seguir tendências para lucro.


/*backtest
start: 2023-09-16 00:00:00
end: 2023-09-23 00:00:00
period: 45m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// @version=4
// @author=Daveatt - BEST

// ABCD Pattern Strat

StrategyName        = "BEST ABCD Pattern Strategy"
ShortStrategyName   = "BEST ABCD Pattern Strategy" 

// strategy(title=StrategyName, shorttitle=ShortStrategyName, overlay=true, 
//  pyramiding=2, default_qty_value=100, precision=7, currency=currency.USD,
//  commission_value=0.2,commission_type=strategy.commission.percent, initial_capital=1000000,
//  default_qty_type=strategy.fixed)

filterBW = input(false, title="filter Bill Williams Fractals?")

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////// UTILITIES ///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

//  ||-----------------------------------------------------------------------------------------------------||
//  ||---   Fractal Recognition Functions:  ---------------------------------------------------------------||
isRegularFractal(mode, _high, _low) =>
    ret = mode == 1 ? _high[4] < _high[3] and _high[3] < _high[2] and _high[2] > _high[1] and _high[1] > _high[0] :
     mode == -1 ? _low[4] > _low[3] and _low[3] > _low[2] and _low[2] < _low[1] and _low[1] < _low[0] : false

isBWFractal(mode, _high, _low) =>
    ret = mode == 1 ? _high[4] < _high[2] and _high[3] <= _high[2] and _high[2] >= _high[1] and _high[2] > _high[0] :
     mode == -1 ? _low[4] > _low[2] and _low[3] >= _low[2] and _low[2] <= _low[1] and _low[2] < _low[0] : false

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
////////////////////////////// ABCD PATTERN ///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

f_abcd()=>

    _r = timeframe.period
    _g = barmerge.gaps_off
    _l = barmerge.lookahead_on

    _high = high
    _low = low

    filteredtopf = filterBW ? isRegularFractal(1, _high, _low) : isBWFractal(1, _high, _low)
    filteredbotf = filterBW ? isRegularFractal(-1, _high, _low) : isBWFractal(-1, _high, _low)

    //  ||---   ZigZag:
    istop = filteredtopf
    isbot = filteredbotf
    topcount = barssince(istop)
    botcount = barssince(isbot)

    zigzag = (istop and topcount[1] > botcount[1] ? _high[2] :
     isbot and topcount[1] < botcount[1] ? _low[2] : na)

    x = valuewhen(zigzag, zigzag, 4) 
    a = valuewhen(zigzag, zigzag, 3) 
    b = valuewhen(zigzag, zigzag, 2) 
    c = valuewhen(zigzag, zigzag, 1) 
    d = valuewhen(zigzag, zigzag, 0)

    xab = (abs(b-a)/abs(x-a))
    xad = (abs(a-d)/abs(x-a))
    abc = (abs(b-c)/abs(a-b))
    bcd = (abs(c-d)/abs(b-c))

    // ABCD Part
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    
    _bull_abcd = _abc and _bcd and d < c 
    _bear_abcd = _abc and _bcd and d > c

    _bull   = _bull_abcd and not _bull_abcd[1]
    _bear   = _bear_abcd and not _bear_abcd[1]

    [_bull, _bear, zigzag]

lapos_x = timenow + round(change(time)*12)

[isLong, isShort, zigzag]  = f_abcd()

plot(zigzag, title= 'ZigZag', color=color.black, offset=-2)
plotshape(isLong, style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), size=size.normal, text="ABCD", textcolor=color.white)
plotshape(isShort, style=shape.labeldown, location=location.abovebar, color=color.new(color.maroon, 0), size=size.normal, text="ABCD", textcolor=color.white)


long_entry_price    = valuewhen(isLong, close, 0)
short_entry_price   = valuewhen(isShort, close, 0)

sinceNUP = barssince(isLong)
sinceNDN = barssince(isShort)

buy_trend   = sinceNDN > sinceNUP
sell_trend  = sinceNDN < sinceNUP


//////////////////////////
//* Profit Component *//
//////////////////////////

//////////////////////////// MinTick ///////////////////////////
fx_pips_value = syminfo.type == "forex" ? syminfo.mintick*10 : 1

input_tp_pips = input(100, "Backtest Profit Goal (in USD)",minval=0)*fx_pips_value
input_sl_pips = input(20, "Backtest STOP Goal (in USD)",minval=0)*fx_pips_value

tp = buy_trend? long_entry_price + input_tp_pips : short_entry_price - input_tp_pips
sl = buy_trend? long_entry_price - input_sl_pips : short_entry_price + input_sl_pips

plot_tp = buy_trend and high[1] <= tp ? tp : sell_trend and low[1] <= tp ? tp : na
plot_sl = buy_trend and low[1] >= sl ? sl : sell_trend and high[1] >= sl ? sl : na

plot(plot_tp, title="TP", style=plot.style_circles, linewidth=3, color=color.blue)
plot(plot_sl, title="SL", style=plot.style_circles, linewidth=3, color=color.red)

longClose   = isShort
shortClose  = isLong


strategy.entry("Long", 1, when=isLong)
// strategy.close("Long", when=longClose )
strategy.exit("XL","Long", limit=tp,  when=buy_trend, stop=sl)


strategy.entry("Short", 0,  when=isShort)
// strategy.close("Short", when=shortClose )
strategy.exit("XS","Short", when=sell_trend, limit=tp, stop=sl)

Mais.