HMA y CCI Combo Tendencia siguiendo la estrategia

El autor:¿ Qué pasa?, Fecha: 2023-09-11 15:02:37
Las etiquetas:

Esta estrategia combina HMA y CCI para identificar y intercambiar tendencias. Específicamente, es larga cuando HMA rompe hacia arriba y CCI cruza por encima de la banda inferior, y es corta cuando HMA rompe hacia abajo y CCI cruza por debajo de la banda superior. Las salidas ocurren cuando HMA rompe en dirección opuesta, o CCI vuelve a entrar en el rango de umbral.

La ventaja de esta estrategia es el uso de HMA para determinar la dirección de la tendencia, y CCI para confirmar el inicio de la tendencia, reduciendo efectivamente los errores de retroceso y errores de retroceso.

En resumen, la estrategia de seguimiento de tendencia combinada de HMA y CCI puede producir resultados decentes durante las fases de tendencia fuertes. Pero en el comercio en vivo, todavía se necesita atención en el stop loss para reducir las pérdidas de los eventos LIQR. Solo con la optimización adecuada de los parámetros se puede aplicar con éxito esta estrategia a largo plazo.


/*backtest
start: 2023-08-11 00:00:00
end: 2023-09-10 00:00:00
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
strategy("HMA+CCI strategy", overlay=true)

src = input(close)
hmaLen = input(21)
cciLen = input(10)
cciLower = input(-50)
cciUpper = input(50)
cciLowerExit = input(-100)
cciUpperExit = input(100)
hmaExit = input(false)
cciExit = input(true)
//rciLower = input(-60)
//rciUpper = input(60)

// Backtest
fromyear = input(2017, defval = 2018, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(21, defval = 01, minval = 01, maxval = 31, title = "From day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day")

leverage = input(100)

term = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59))

//itvs = input(9, "short interval")
//itvm = input(36, "middle interval")
//itvl = input(52, "long interval")
//src = input(close, "source")
//upperband=input(title="High line[%]",defval=80,type=integer)
//lowerband=input(title="Low line[%]",defval=-80,type=integer)

ord(seq, idx, itv) =>
    p = seq[idx]
    o = 1
    for i = 0 to itv - 1
        if p < seq[i]
            o := o + 1
    o

d(itv) =>
    sum = 0.0
    for i = 0 to itv - 1
        sum := sum + pow((i + 1) - ord(src, i, itv), 2)
    sum

rci(itv) => (1.0 - 6.0 * d(itv) / (itv * (itv * itv - 1.0))) * 100.0

hullma = wma(2*wma(src, hmaLen/2)-wma(src, hmaLen), round(sqrt(hmaLen)))
cci = cci(close, cciLen)
plot(hullma, color=hullma[1]>hullma?red:green, linewidth=4)
longCondition = hullma[1] < hullma and crossover(cci, cciLower) //rci < -60 // crossover(cci, cciLower)
shortCondition = hullma[1] > hullma and crossunder(cci, cciUpper) //rci > 60
exitLong1 = hmaExit ? hullma[1] > hullma : false
exitLong2 = cciExit ? cci > cciUpperExit : false
exitShort1 = hmaExit ? hullma[1] < hullma : false
exitShort2 = cciExit ? cci < cciLowerExit : false

if (longCondition and term)
    strategy.entry("Long",  strategy.long )
if (shortCondition and term)
    strategy.entry("Short",  strategy.short)
        
if strategy.position_size > 0 and term
    if (exitLong1 or exitLong2)
        strategy.close_all()
if strategy.position_size < 0 and term
    if (exitShort1 or exitShort2)
        strategy.close_all()

Más.