
La estrategia de compra y venta de seguimiento de tendencias es una estrategia simple de seguimiento de tendencias durante el día. La idea básica de la estrategia es determinar la dirección de la tendencia según los promedios móviles y comprar y vender en las oscilaciones de la tendencia.
La estrategia utiliza el SMA para determinar la dirección de la tendencia. En una tendencia alcista, cuando la línea K tiene un punto bajo, la estrategia hace más al romper el punto más alto de la línea K anterior; en una tendencia descendente, cuando la línea K tiene un punto alto, la estrategia hace vacío al romper el punto más bajo de la línea K anterior.
La estrategia también utiliza los indicadores de tendencia Blanchflower %K y %D para determinar la tendencia. Se realiza una operación en sentido contrario cuando el%K se cierra en el%D. Además, la estrategia utiliza la curva MACD y la curva de señal como condiciones de filtración y solo ejecuta operaciones cuando la MACD y la señal coinciden con la dirección de la tendencia.
La estrategia puede hacer solo más, solo vacío o hacer más vacío al mismo tiempo. La fecha de inicio puede establecer el mes y el año de inicio de la medición. Todos los parámetros, como el ciclo de media móvil, el ciclo K, el ciclo D, el parámetro MACD, etc., pueden ser personalizados.
El principal riesgo de esta estrategia es:
La estrategia puede ser optimizada en los siguientes aspectos:
La estrategia de seguimiento de tendencias es sencilla y clara, determina la dirección de la tendencia a través de las medias móviles y utiliza filtros de indicadores para bloquear las oportunidades de negociación en la tendencia. La estrategia puede obtener buenos resultados mediante la optimización de los parámetros, pero aún requiere el envase de código Combine para reducir el riesgo de optimización y mejorar la estabilidad.
/*backtest
start: 2022-10-10 00:00:00
end: 2023-10-16 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Higher High / Lower Low Strategy", overlay=true)
// Getting inputs
longOnly = input(true, title="Long or Short Only")
useMACD = input(true, title="Use MACD Filter")
useSignal = input(true, title="Use Signal Filter")
//Filter backtest month and year
startMonth = input(10, minval=1, maxval=12, title="Month")
startYear = input(2020, minval=2000, maxval=2100, title="Year")
//Filter funtion inputs
periodA = input(20, minval=1, title="Period SMA")
periodK = input(5, minval=1, title="Period %K")
fast_length = input(title="Period Fast", type=input.integer, defval=5)
slow_length = input(title="Period Slow", type=input.integer, defval=20)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 30)
//Calculations
smoothD = 3 //input(3, minval=1, title="Smooth %D")
smoothK = 2 //input(2, minval=1, title="Smooth %K")
ma50 = sma(close, periodA)
k = sma(stoch(close, high, low, periodK), smoothK) - 50
d = sma(k, smoothD)
macd = ema(close,fast_length) - ema(close,slow_length)
signal = ema(macd,signal_length)
hist = macd - signal
if (not na(k) and not na(d) and not na(macd) and not na(signal) and longOnly and month>=startMonth and year>=startYear)// if(k > k[1] and k[2] >= k[1] and (ma50 > ma50[1]) and (not useK or k[1] <= -threshold_k) and (not useMACD or macd > macd[1]) and (not useSignal or signal > signal[1]) and (not useHHLL or close >= high[1]) and (not useD or d <= -threshold_d))
if(high[2] >= high[1] and high > high[1] and (ma50 > ma50[1]) and (not useMACD or macd > macd[1]) and (not useSignal or signal > signal[1]))
strategy.order("HH_LE", strategy.long, when=strategy.position_size == 0, comment="HH_LE")
if (k < k[1])
strategy.order("HH_SX", strategy.short, when=strategy.position_size != 0, comment="HH_SX")
if (not na(k) and not na(d) and not na(macd) and not na(signal) and not longOnly and month>=startMonth and year>=startYear)
if(low[2] <= low[1] and low < low[1] and (ma50 < ma50[1]) and (not useMACD or macd < macd[1]) and (not useSignal or signal < signal[1]))
strategy.order("HH_SE", strategy.short, when=strategy.position_size == 0, comment="HH_SE")
if (k > k[1])
strategy.order("HH_LX", strategy.long, when=strategy.position_size != 0, comment="HH_LX")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)