Untitled

 avatar
unknown
plain_text
a month ago
7.8 kB
2
Indexable
// @version=5
indicator("Multi-Timeframe Two-Candle Pattern", overlay=true)

//-----------------------------------------------------------------------------// Input Parameters
//-----------------------------------------------------------------------------
tf_higher = input.timeframe("60", title="Higher Timeframe (Pattern Detection)")
tf_lower = input.timeframe("5", title="Lower Timeframe (Entry Detection)")
rr = input.float(defval=2.0, title="Risk:Reward Ratio", minval=0.1, step=0.1)
showTargets = input.bool(defval=true, title="Show Targets")
showHigherTFPattern = input.bool(defval=true, title="Show Higher Timeframe Pattern")

//-----------------------------------------------------------------------------// Higher Timeframe Pattern Detection
//-----------------------------------------------------------------------------
// Request data from higher timeframe
[high_htf1, low_htf1, open_htf1, close_htf1] = request.security(syminfo.tickerid, tf_higher, [high[1], low[1], open[1], close[1]])
[high_htf0, low_htf0, open_htf0, close_htf0] = request.security(syminfo.tickerid, tf_higher, [high, low, open, close])
[time_htf0] = request.security(syminfo.tickerid, tf_higher, [time])

// Determine candle colors
isGreen_htf1 = close_htf1 > open_htf1
isRed_htf1 = close_htf1 < open_htf1
isGreen_htf0 = close_htf0 > open_htf0
isRed_htf0 = close_htf0 < open_htf0

// Bearish Pattern Conditions
bearishPattern = isGreen_htf1 and isRed_htf0 and high_htf0 > high_htf1 and close_htf0 < open_htf1

// Bullish Pattern Conditions
bullishPattern = isRed_htf1 and isGreen_htf0 and low_htf0 < low_htf1 and close_htf0 > open_htf1

//-----------------------------------------------------------------------------// Variable Initialization
//-----------------------------------------------------------------------------
// Variables to track pattern detection state
var bool htfPatternDetected = false
var bool htfBullishDetected = false
var bool htfBearishDetected = false
var int patternTimestamp = 0

// Variables to store entry, stop, and target levels
var float bullishEntryLevel = na
var float bullishStopLevel = na
var float bullishTargetLevel = na
var float bearishEntryLevel = na
var float bearishStopLevel = na
var float bearishTargetLevel = na
var bool shouldShowBullishLevels = false
var bool shouldShowBearishLevels = false

//-----------------------------------------------------------------------------// Pattern Change Detection
//-----------------------------------------------------------------------------
// Detect market condition change
htfConditionChanged = ta.change(bearishPattern ? 1 : 0) != 0 or ta.change(bullishPattern ? 1 : 0) != 0

// Update pattern detection state
if (htfConditionChanged)
    htfPatternDetected := bearishPattern or bullishPattern
    htfBullishDetected := bullishPattern
    htfBearishDetected := bearishPattern
    patternTimestamp := time_htf0

// Check if pattern has expired (after 1 hour)
isPatternActive = htfPatternDetected and (time - patternTimestamp <= 3600000) // 1 hour in milliseconds

//-----------------------------------------------------------------------------// Higher Timeframe Pattern Visualization
//-----------------------------------------------------------------------------
// Check if we're currently on the higher timeframe chart
isHigherTimeframe = timeframe.period == tf_higher

// Draw higher timeframe confirmation
if (showHigherTFPattern and isHigherTimeframe)
    if (bearishPattern)
        label.new(bar_index, high_htf0, text="BEARISH", style=label.style_label_down, color=color.red, textcolor=color.white)
    if (bullishPattern)
        label.new(bar_index, high_htf0, text="BULLISH", style=label.style_label_down, color=color.green, textcolor=color.white)

//-----------------------------------------------------------------------------// Background Color for Active Pattern
//-----------------------------------------------------------------------------
// This must be in global scope
bgColor = color.new(color.white, 100)  // Initialize with transparent color
if (not isHigherTimeframe and isPatternActive and showHigherTFPattern)
    bgColor := htfBullishDetected ? color.new(color.green, 90) : color.new(color.red, 90)

bgcolor(bgColor)

//-----------------------------------------------------------------------------// Lower Timeframe Entry Setup
//-----------------------------------------------------------------------------
// Detect candle patterns on lower timeframe
[close_ltf2, close_ltf1, close_ltf0] = request.security(syminfo.tickerid, tf_lower, [close[2], close[1], close])
[open_ltf2, open_ltf1, open_ltf0] = request.security(syminfo.tickerid, tf_lower, [open[2], open[1], open])
[high_ltf2, high_ltf1, high_ltf0] = request.security(syminfo.tickerid, tf_lower, [high[2], high[1], high])
[low_ltf2, low_ltf1, low_ltf0] = request.security(syminfo.tickerid, tf_lower, [low[2], low[1], low])

// Detect two consecutive green candles (for bullish setup)
twoConsecutiveGreen = close_ltf1 > open_ltf1 and close_ltf2 > open_ltf2

// Detect two consecutive red candles (for bearish setup)
twoConsecutiveRed = close_ltf1 < open_ltf1 and close_ltf2 < open_ltf2

// Set bullish entry logic
if (isPatternActive and htfBullishDetected and twoConsecutiveGreen and not shouldShowBullishLevels)
    bullishEntryLevel := close_ltf1
    bullishStopLevel := low_ltf2
    bullishTargetLevel := bullishEntryLevel + ((bullishEntryLevel - bullishStopLevel) * rr)
    shouldShowBullishLevels := true

// Set bearish entry logic
if (isPatternActive and htfBearishDetected and twoConsecutiveRed and not shouldShowBearishLevels)
    bearishEntryLevel := close_ltf1
    bearishStopLevel := high_ltf2
    bearishTargetLevel := bearishEntryLevel - ((bearishStopLevel - bearishEntryLevel) * rr)
    shouldShowBearishLevels := true

// Reset levels when pattern expires
if (not isPatternActive)
    shouldShowBullishLevels := false
    shouldShowBearishLevels := false

//-----------------------------------------------------------------------------// Trade Levels Visualization
//-----------------------------------------------------------------------------
// Draw bullish trade levels when conditions met
if (shouldShowBullishLevels and showTargets)
    line.new(x1=bar_index, y1=bullishStopLevel, x2=bar_index + 2, y2=bullishStopLevel, color=color.red, width=2)
    line.new(x1=bar_index, y1=bullishEntryLevel, x2=bar_index + 2, y2=bullishEntryLevel, color=color.blue, width=2)
    line.new(x1=bar_index, y1=bullishTargetLevel, x2=bar_index + 2, y2=bullishTargetLevel, color=color.green, width=2)

// Draw bearish trade levels when conditions met
if (shouldShowBearishLevels and showTargets)
    line.new(x1=bar_index, y1=bearishStopLevel, x2=bar_index + 2, y2=bearishStopLevel, color=color.red, width=2)
    line.new(x1=bar_index, y1=bearishEntryLevel, x2=bar_index + 2, y2=bearishEntryLevel, color=color.blue, width=2)
    line.new(x1=bar_index, y1=bearishTargetLevel, x2=bar_index + 2, y2=bearishTargetLevel, color=color.green, width=2)

//-----------------------------------------------------------------------------// Alerts
//-----------------------------------------------------------------------------
// Entry alerts
bullishEntryCondition = isPatternActive and htfBullishDetected and twoConsecutiveGreen and not shouldShowBullishLevels
bearishEntryCondition = isPatternActive and htfBearishDetected and twoConsecutiveRed and not shouldShowBearishLevels

alertcondition(bullishEntryCondition, title="Bullish Entry Signal", message="Bullish Entry Signal Detected")
alertcondition(bearishEntryCondition, title="Bearish Entry Signal", message="Bearish Entry Signal Detected")
Editor is loading...
Leave a Comment