Update Candle_Bear-Bull_v6.pine

This commit is contained in:
2024-12-13 15:10:02 -08:00
parent 7d8c93d91c
commit e3de2d521c

View File

@@ -1,9 +1,9 @@
//@version=5 //@version=6
strategy("Hammer Candle Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) strategy('Hammer Candle Strategy', overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
// Inputs for customization // Inputs for customization
hammertBodyRatio = input.float(0.3, title="Max Body Size Ratio", minval=0.1, maxval=0.5, step=0.1) 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) 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 // Function to determine if a candle is bullish or bearish
is_candle_bullish(index) => is_candle_bullish(index) =>
@@ -14,7 +14,7 @@ is_candle_bearish(index) =>
// Hammer Candle Detection Function // Hammer Candle Detection Function
//isHammer() => //isHammer() =>
string dbg = "" string dbg = ''
bodySize = math.abs(close - open) bodySize = math.abs(close - open)
totalRange = high - low totalRange = high - low
@@ -23,18 +23,18 @@ upperWick = high - math.max(close, open)
lowerWick = math.min(close, open) - low lowerWick = math.min(close, open) - low
// Check if body is small (less than 30% of total candle range) // Check if body is small (less than 30% of total candle range)
isSmallBody = bodySize <= (totalRange * hammertBodyRatio) isSmallBody = bodySize <= totalRange * hammertBodyRatio
//plotchar(isSmallBody, title="lw", char='-', location=location.abovebar, color=#40fbf5, size=size.small) //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 // 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 isLongUpperWick = upperWick >= totalRange * 0.5 and upperWick >= bodySize * hammerWickRatio
bool isLongLowerWick = lowerWick >= (totalRange * 0.5) and lowerWick >= (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(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) //plotchar(isLongLowerWick, title="lw", char='l', location=location.belowbar, color=color.fuchsia, size=size.small)
// Minimal lower/upper wick // Minimal lower/upper wick
bool isShortUpperWick = upperWick <= (bodySize * 0.5) or upperWick <= (totalRange * 0.2) bool isShortUpperWick = upperWick <= bodySize * 0.5 or upperWick <= totalRange * 0.2
bool isShortLowerWick = lowerWick <= (bodySize * 0.5) or lowerWick <= (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(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) //plotchar(isShortLowerWick, title="Bullish Candle", char='s', location=location.belowbar, color=color.fuchsia, size=size.small)
@@ -43,8 +43,9 @@ isBullishHammer = isSmallBody and isShortUpperWick and isLongLowerWick //and is_
// Bearish Hammer (Red/Black) // Bearish Hammer (Red/Black)
isBearishHammer = isSmallBody and isLongUpperWick and isShortLowerWick // and is_candle_bullish(1) and is_candle_bullish(2) // and close < open isBearishHammer = isSmallBody and isLongUpperWick and isShortLowerWick // and is_candle_bullish(1) and is_candle_bullish(2) // and close < open
if (isBearishHammer) if isBearishHammer
dbg := "TEST: " + str.tostring(isShortUpperWick) + " | "+ str.tostring(upperWick) + " | "+ str.tostring(bodySize) + " | "+ str.tostring(totalRange) + " | " dbg := 'TEST: ' + str.tostring(isShortUpperWick) + ' | ' + str.tostring(upperWick) + ' | ' + str.tostring(bodySize) + ' | ' + str.tostring(totalRange) + ' | '
dbg
// [isBullishHammer, isBearishHammer, dbg] // [isBullishHammer, isBearishHammer, dbg]
@@ -52,32 +53,22 @@ if (isBearishHammer)
bullish = isBullishHammer bullish = isBullishHammer
bearish = isBearishHammer bearish = isBearishHammer
// Plot a '*' when close is above open (bullish candle) // Plot a '*' when close is above open (bullish candle)
plotchar(bullish, title="Bullish Candle", char='*', location=location.belowbar, color=color.green, size=size.small) plotchar(bullish, title = 'Bullish Candle', char = '*', location = location.belowbar, color = color.green, size = size.small)
// Plot an '*' when close is below open (bearish candle) // Plot an '*' when close is below open (bearish candle)
plotchar(bearish, title="Bearish Candle", char='*', location=location.abovebar, color=color.red, size=size.small) 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) bullish := isBullishHammer and is_candle_bullish(1) and is_candle_bullish(2)
bearish := isBearishHammer 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 // Plot hammer candle markers
plotshape(bullish, plotshape(bullish, title = 'Bullish Hammer', location = location.belowbar, style = shape.triangleup, size = size.small, color = color.green)
title="Bullish Hammer",
location=location.belowbar,
style=shape.triangleup,
size=size.small,
color=color.green)
plotshape(bearish, plotshape(bearish, title = 'Bearish Hammer', location = location.abovebar, style = shape.triangledown, size = size.small, color = color.red)
title="Bearish Hammer",
location=location.abovebar,
style=shape.triangledown,
size=size.small,
color=color.red)
// Trading logic // Trading logic
if (bullish) if bullish
strategy.entry("Long", strategy.long) strategy.entry('Long', strategy.long)
if (bearish) if bearish
strategy.entry("Short", strategy.short) strategy.entry('Short', strategy.short)