This strategy is a quantitative trading system based on two classic candlestick patterns: Hammer and Hanging Man. It predicts potential market turning points by identifying these reversal patterns. The system combines multiple technical indicators to confirm signal validity, including the relationship between candlestick body and shadows, trend direction, and other elements, achieving precise capture of market reversal points.
The core logic of the strategy is to identify two key candlestick patterns programmatically: 1. Hammer: Appears in downtrends, suggesting potential upward reversal. Characterized by a small body, long lower shadow (at least twice the body length), and minimal or no upper shadow. 2. Hanging Man: Appears in uptrends, suggesting potential downward reversal. Similar characteristics to Hammer but appears in different locations with opposite implications.
The strategy quantifies these patterns through strict parameters, including: - Minimum candle body length multiplier - Lower shadow to candle height ratio - Holding periods
This strategy implements classical technical analysis theory systematically through quantification, demonstrating strong practical value. Through parameter optimization and risk control mechanism refinement, the strategy can maintain stable performance in different market environments. The modular design also provides a solid foundation for subsequent optimization.
/*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")