Untitled

 avatar
unknown
plain_text
a month ago
6.4 kB
3
Indexable
// @version=5
indicator("Two-Candle Price Action Pattern", overlay=true)

//-----------------------------------------------------------------------------
// Input Parameters
//-----------------------------------------------------------------------------
timeframe = input.string(defval="", title="Timeframe Override", tooltip="Leave empty to use chart timeframe")
stopMultiplier = input.float(defval=1.0, title="Stop Loss Multiplier", minval=0.1, step=0.1, tooltip="Multiplier relative to candle 2 high/low")
rr = input.float(defval=2.0, title="Risk:Reward Ratio", minval=0.1, step=0.1)
showLabels = input.bool(defval=true, title="Show Signal Labels")
showTargets = input.bool(defval=true, title="Show Targets")

//-----------------------------------------------------------------------------
// Price Data
//-----------------------------------------------------------------------------
// Previous candle (Candle 1)
[high1, low1, open1, close1] = request.ohlc(syminfo.tickerid, timeframe)

// Current candle (Candle 2)
[high0, low0, open0, close0] = request.ohlc(syminfo.tickerid, timeframe)

//-----------------------------------------------------------------------------
// Pattern Detection
//-----------------------------------------------------------------------------
// Determine candle colors
isGreen1 = close1 > open1
isRed1 = close1 < open1
isGreen0 = close0 > open0
isRed0 = close0 < open0

// Bearish Pattern Conditions
bearishPattern = isGreen1 and isRed0 and high0 > high1 and close0 < open1

// Bullish Pattern Conditions (reverse of bearish)
bullishPattern = isRed1 and isGreen0 and low0 < low1 and close0 > open1

//-----------------------------------------------------------------------------
// Trade Levels Calculation
//-----------------------------------------------------------------------------
// Bearish Pattern Levels
bearishEntryLevel = close0
bearishStopLevel = high0 * stopMultiplier
bearishRisk = bearishStopLevel - bearishEntryLevel
bearishTargetLevel = bearishEntryLevel - (bearishRisk * rr)

// Bullish Pattern Levels
bullishEntryLevel = close0
bullishStopLevel = low0 * (2 - stopMultiplier) // Inverse relationship for the multiplier
bullishRisk = bullishEntryLevel - bullishStopLevel
bullishTargetLevel = bullishEntryLevel + (bullishRisk * rr)

//-----------------------------------------------------------------------------
// Visual Signals
//-----------------------------------------------------------------------------
// Signal markers
plotshape(bearishPattern and showLabels, title="Bearish Signal", style=shape.labeldown, 
    location=location.abovebar, color=color.red, size=size.small, text="SELL", textcolor=color.white)

plotshape(bullishPattern and showLabels, title="Bullish Signal", style=shape.labelup, 
    location=location.belowbar, color=color.green, size=size.small, text="BUY", textcolor=color.white)

//-----------------------------------------------------------------------------
// Trade Levels Visualization
//-----------------------------------------------------------------------------
// Draw bearish pattern levels
if bearishPattern and showTargets
    line.new(
        x1=bar_index, 
        y1=bearishStopLevel, 
        x2=bar_index + 10, 
        y2=bearishStopLevel, 
        color=color.red, 
        width=2, 
        style=line.style_solid
    )
    label.new(
        x=bar_index + 11, 
        y=bearishStopLevel, 
        text="SL", 
        color=color.red, 
        style=label.style_label_left, 
        textcolor=color.white, 
        size=size.small
    )
    
    line.new(
        x1=bar_index, 
        y1=bearishEntryLevel, 
        x2=bar_index + 10, 
        y2=bearishEntryLevel, 
        color=color.yellow, 
        width=2, 
        style=line.style_solid
    )
    label.new(
        x=bar_index + 11, 
        y=bearishEntryLevel, 
        text="Entry", 
        color=color.yellow, 
        style=label.style_label_left, 
        textcolor=color.black, 
        size=size.small
    )
    
    line.new(
        x1=bar_index, 
        y1=bearishTargetLevel, 
        x2=bar_index + 10, 
        y2=bearishTargetLevel, 
        color=color.green, 
        width=2, 
        style=line.style_solid
    )
    label.new(
        x=bar_index + 11, 
        y=bearishTargetLevel, 
        text="TP", 
        color=color.green, 
        style=label.style_label_left, 
        textcolor=color.white, 
        size=size.small
    )

// Draw bullish pattern levels
if bullishPattern and showTargets
    line.new(
        x1=bar_index, 
        y1=bullishStopLevel, 
        x2=bar_index + 10, 
        y2=bullishStopLevel, 
        color=color.red, 
        width=2, 
        style=line.style_solid
    )
    label.new(
        x=bar_index + 11, 
        y=bullishStopLevel, 
        text="SL", 
        color=color.red, 
        style=label.style_label_left, 
        textcolor=color.white, 
        size=size.small
    )
    
    line.new(
        x1=bar_index, 
        y1=bullishEntryLevel, 
        x2=bar_index + 10, 
        y2=bullishEntryLevel, 
        color=color.yellow, 
        width=2, 
        style=line.style_solid
    )
    label.new(
        x=bar_index + 11, 
        y=bullishEntryLevel, 
        text="Entry", 
        color=color.yellow, 
        style=label.style_label_left, 
        textcolor=color.black, 
        size=size.small
    )
    
    line.new(
        x1=bar_index, 
        y1=bullishTargetLevel, 
        x2=bar_index + 10, 
        y2=bullishTargetLevel, 
        color=color.green, 
        width=2, 
        style=line.style_solid
    )
    label.new(
        x=bar_index + 11, 
        y=bullishTargetLevel, 
        text="TP", 
        color=color.green, 
        style=label.style_label_left, 
        textcolor=color.white, 
        size=size.small
    )

//-----------------------------------------------------------------------------
// Alerts
//-----------------------------------------------------------------------------
alertcondition(bearishPattern, title="Bearish Pattern Detected", message="Bearish Two-Candle Pattern: SELL")
alertcondition(bullishPattern, title="Bullish Pattern Detected", message="Bullish Two-Candle Pattern: BUY")
Editor is loading...
Leave a Comment