//@version=5
indicator("Shooting Star Indicator with Conditional Trade Setup", overlay=true)

// User-defined input settings
shootingStarBodyRatio = input.float(0.35, title="Shooting Star Body Ratio")
shootingStarWickMultiplier = input.float(3, title="Shooting Star Wick Multiplier")
volumeCheckPeriod = input.int(20, title="Number of Candles to Check for Max Volume", minval=1)
atrPeriod = input.int(14, title="ATR Period") // ATR period input

// Calculations for body and wick sizes
bodySize = math.abs(open - close)
upperWick = high - math.max(open, close)
lowerWick = math.min(open, close) - low
maxVolume = ta.highest(volume, volumeCheckPeriod)
volumeCheck = volume == maxVolume

// Calculate ATR (Average True Range)
atrValue = ta.atr(atrPeriod)

// Condition: High above the last 10 candles
highAboveLast10Candles = high > ta.highest(high[1], 10)

// Condition: High is at least 2% above the daily ATR from the most recent candle
highAboveATR = high > close[1] + atrValue * 0.02

// Shooting Star condition
isShootingStar = bodySize < (high - low) * shootingStarBodyRatio and upperWick > lowerWick * shootingStarWickMultiplier and volumeCheck and highAboveLast10Candles and highAboveATR

// Variables for tracking trade state and price levels
var float entryPrice = na
var float stopLossPrice = na
var float takeProfitPrice = na
var bool tradeActive = false

// Plot and alert conditions
plotshape(isShootingStar, title="Shooting Star", location=location.abovebar, color=color.red, style=shape.labelup, text="SS")
alertcondition(isShootingStar, title="Shooting Star Alert", message="Shooting Star pattern detected")

// Set up the trade when a Shooting Star is detected
if isShootingStar
    entryPrice := low  // Entry at the low of the pattern (Shooting Star)
    stopLossPrice := high + (atrValue * 0.02)  // Stop loss at the high of the pattern + 2% of the daily ATR
    risk = stopLossPrice - entryPrice
    takeProfitPrice := entryPrice - (2 * risk)  // 1:2 risk-to-reward ratio
    tradeActive := true

// Check trade execution on the next candle if the trade is active
if tradeActive and bar_index == bar_index[1] + 1
    if low[0] <= entryPrice
        // Trigger sell order at the low if it is crossed first
        label.new(bar_index, entryPrice, text="Sell Entry", color=color.red, textcolor=color.white, style=label.style_label_down, size=size.small)
        line.new(bar_index, entryPrice, bar_index + 1, entryPrice, color=color.red, width=2, style=line.style_dotted)
        line.new(bar_index, stopLossPrice, bar_index + 1, stopLossPrice, color=color.blue, width=2, style=line.style_dotted)
        line.new(bar_index, takeProfitPrice, bar_index + 1, takeProfitPrice, color=color.green, width=2, style=line.style_dotted)
        label.new(bar_index, stopLossPrice, text="Stop Loss", color=color.blue, textcolor=color.white, style=label.style_label_up, size=size.small)
        label.new(bar_index, takeProfitPrice, text="Take Profit", color=color.green, textcolor=color.white, style=label.style_label_down, size=size.small)
        tradeActive := false  // Deactivate trade after triggering
    else if high[0] >= stopLossPrice
        // Invalidate the trade if the high is broken first
        label.new(bar_index, high, text="Trade Invalidated", color=color.gray, textcolor=color.black, style=label.style_label_down, size=size.small)
        tradeActive := false  // Cancel the trade