Files
trading/strategies/Hammer_Candle_Strategy.pine

83 lines
3.7 KiB
Plaintext
Executable File

//@version=5
strategy("Hammer Candle Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Inputs for customization
hammertBodyRatio = input.float(0.3, title="Max Body Size Ratio", minval=0.1, maxval=0.5, step=0.1)
hammerWickRatio = input.float(2.0, title="Min Wick to Body Ratio", minval=1.0, maxval=5.0, step=0.1)
// Function to determine if a candle is bullish or bearish
is_candle_bullish(index) =>
close[index] > open[index]
is_candle_bearish(index) =>
close[index] < open[index]
// Hammer Candle Detection Function
//isHammer() =>
string dbg = ""
bodySize = math.abs(close - open)
totalRange = high - low
// Calculate upper and lower wicks
upperWick = high - math.max(close, open)
lowerWick = math.min(close, open) - low
// Check if body is small (less than 30% of total candle range)
isSmallBody = bodySize <= (totalRange * hammertBodyRatio)
//plotchar(isSmallBody, title="lw", char='-', location=location.abovebar, color=#40fbf5, size=size.small)
// Lower/Upper wick should be at least 2x the body size and significantly long
bool isLongUpperWick = upperWick >= (totalRange * 0.5) and upperWick >= (bodySize * hammerWickRatio)
bool isLongLowerWick = lowerWick >= (totalRange * 0.5) and lowerWick >= (bodySize * hammerWickRatio)
//plotchar(isLongUpperWick, title="LW", char='L', location=location.abovebar, color=color.fuchsia, size=size.small)
//plotchar(isLongLowerWick, title="lw", char='l', location=location.belowbar, color=color.fuchsia, size=size.small)
// Minimal lower/upper wick
bool isShortUpperWick = upperWick <= (bodySize * 0.5) or upperWick <= (totalRange * 0.2)
bool isShortLowerWick = lowerWick <= (bodySize * 0.5) or lowerWick <= (totalRange * 0.2)
//plotchar(isShortUpperWick, title="Bullish Candle", char='S', location=location.abovebar, color=color.fuchsia, size=size.small)
//plotchar(isShortLowerWick, title="Bullish Candle", char='s', location=location.belowbar, color=color.fuchsia, size=size.small)
// Bullish Hammer (Green/White)
isBullishHammer = isSmallBody and isShortUpperWick and isLongLowerWick //and is_candle_bearish(1) and is_candle_bearish(2) // and close > open
// Bearish Hammer (Red/Black)
isBearishHammer = isSmallBody and isLongUpperWick and isShortLowerWick // and is_candle_bullish(1) and is_candle_bullish(2) // and close < open
if (isBearishHammer)
dbg := "TEST: " + str.tostring(isShortUpperWick) + " | "+ str.tostring(upperWick) + " | "+ str.tostring(bodySize) + " | "+ str.tostring(totalRange) + " | "
// [isBullishHammer, isBearishHammer, dbg]
//[bullish, bearish, deb] = isHammer()
bullish = isBullishHammer
bearish = isBearishHammer
// Plot a '*' when close is above open (bullish candle)
plotchar(bullish, title="Bullish Candle", char='*', location=location.belowbar, color=color.green, size=size.small)
// Plot an '*' when close is below open (bearish candle)
plotchar(bearish, title="Bearish Candle", char='*', location=location.abovebar, color=color.red, size=size.small)
bullish := isBullishHammer and is_candle_bullish(1) and is_candle_bullish(2)
bearish := isBearishHammer and is_candle_bullish(1) and is_candle_bullish(2)
// Plot hammer candle markers
plotshape(bullish,
title="Bullish Hammer",
location=location.belowbar,
style=shape.triangleup,
size=size.small,
color=color.green)
plotshape(bearish,
title="Bearish Hammer",
location=location.abovebar,
style=shape.triangledown,
size=size.small,
color=color.red)
// Trading logic
if (bullish)
strategy.entry("Long", strategy.long)
if (bearish)
strategy.entry("Short", strategy.short)