
Esta estrategia es un sistema de trading cuantitativo basado en dos patrones de velas clásicos: la línea del martillo y el hombre colgado. La estrategia funciona identificando estos patrones de reversión en el mercado para predecir posibles puntos de inflexión en la acción del precio. El sistema combina múltiples indicadores técnicos para confirmar la validez de la señal, incluida la relación proporcional entre el cuerpo de la línea K y la sombra, la dirección de la tendencia y otros factores, para lograr una captura precisa de los puntos de reversión del mercado.
La lógica central de la estrategia es identificar dos patrones de velas clave de manera programática:
La estrategia cuantifica estos patrones estableciendo parámetros estrictos, entre ellos:
Esta estrategia realiza la aplicación sistemática de la teoría del análisis técnico clásico a través de métodos cuantitativos y tiene un gran valor práctico. Al optimizar los parámetros y mejorar los mecanismos de control de riesgos, la estrategia puede mantener un desempeño estable en diferentes entornos de mercado. El diseño modular de la estrategia también proporciona una buena base para la optimización posterior.
/*backtest
start: 2024-12-10 00:00:00
end: 2025-01-08 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/
//@version=6
strategy("Hammer and Hanging Man Strategy", overlay=true)
// Input parameters
length = input.int(5, title="Minimum Candle Body Length (Multiplier)", minval=1)
shadowRatio = input.float(1, title="Lower Shadow to Candle Height Ratio", minval=1.0)
holdPeriods = input.int(26, title="Hold Periods (Bars)", minval=1) // Holding period in bars
// Function to calculate the absolute value
absValue(x) =>
x >= 0 ? x : -x
// Function to check if it is a Hammer
isHammer() =>
bodyLength = absValue(close - open)
candleHeight = high - low
lowerShadow = math.min(open, close) - low
upperShadow = high - math.max(open, close)
smallBody = bodyLength <= candleHeight / length
longLowerShadow = lowerShadow >= bodyLength * shadowRatio
shortUpperShadow = upperShadow <= bodyLength
smallBody and longLowerShadow and shortUpperShadow and close > open
// Function to check if it is a Hanging Man
isHangingMan() =>
bodyLength = absValue(close - open)
candleHeight = high - low
lowerShadow = math.min(open, close) - low
upperShadow = high - math.max(open, close)
smallBody = bodyLength <= candleHeight / length
longLowerShadow = lowerShadow >= bodyLength * shadowRatio
shortUpperShadow = upperShadow <= bodyLength
smallBody and longLowerShadow and shortUpperShadow and close < open
// Detect the candles
hammer = isHammer()
hangingMan = isHangingMan()
// Trading logic: Long on Hammer, Short on Hanging Man
if hammer
strategy.entry("Long", strategy.long) // Long entry on Hammer
if hangingMan
strategy.entry("Short", strategy.short) // Short entry on Hanging Man
// Exit after X bars
if strategy.position_size > 0 and bar_index - strategy.opentrades.entry_bar_index(0) >= holdPeriods
strategy.close("Long")
if strategy.position_size < 0 and bar_index - strategy.opentrades.entry_bar_index(0) >= holdPeriods
strategy.close("Short")
// Visualization of signals
plotshape(hammer, title="Hammer", location=location.belowbar, color=color.green, style=shape.labelup, text="Hammer")
plotshape(hangingMan, title="Hanging Man", location=location.abovebar, color=color.red, style=shape.labeldown, text="Hanging Man")