
Cette stratégie est un système de trading quantitatif basé sur deux modèles de chandeliers classiques : la ligne du marteau et le pendu. La stratégie consiste à identifier ces modèles d’inversion sur le marché pour prédire les points de retournement potentiels dans l’action des prix. Le système combine plusieurs indicateurs techniques pour confirmer la validité du signal, y compris la relation proportionnelle entre le corps de la ligne K et l’ombre, la direction de la tendance et d’autres facteurs, pour obtenir une capture précise des points de retournement du marché.
La logique principale de la stratégie consiste à identifier deux modèles de chandeliers clés de manière programmatique :
La stratégie quantifie ces modèles en définissant des paramètres stricts, notamment :
Cette stratégie réalise l’application systématique de la théorie de l’analyse technique classique au moyen de méthodes quantitatives et présente une forte valeur pratique. En optimisant les paramètres et en améliorant les mécanismes de contrôle des risques, la stratégie peut maintenir des performances stables dans différents environnements de marché. La conception modulaire de la stratégie fournit également une bonne base pour l’optimisation ultérieure.
/*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")