//@version=5
strategy("Optimized BTCUSDT Strategy", overlay=true, 
         default_qty_type=strategy.percent_of_equity, 
         default_qty_value=5, 
         initial_capital=10000, 
         currency=currency.USDT)

// ------------------- Parameters -------------------
shortEmaLength = input.int(9, title="Short EMA Length")
longEmaLength = input.int(21, title="Long EMA Length")
volumeEmaLength = input.int(20, title="Volume EMA Length")

atrLength = input.int(14, title="ATR Length")
stopAtrMultiplier = input.float(1.5, title="ATR Stop Multiplier", step=0.1)
takeAtrMultiplier = input.float(3.0, title="ATR Take Profit Multiplier", step=0.1)

enableCandlestickFilter = input.bool(true, title="Use Candlestick Pattern Filter")

// ------------------- Indicators -------------------
// Price Action: Moving Averages
shortEma = ta.ema(close, shortEmaLength)
longEma = ta.ema(close, longEmaLength)

// Volume Indicators
volumeEma = ta.ema(volume, volumeEmaLength)
volumeOsc = volume / volumeEma

// ATR for Stop Loss and Take Profit
atr = ta.atr(atrLength)

// ------------------- Candlestick Patterns -------------------
// Define bullish engulfing pattern
bullishEngulfing = (close > open) and (close[1] < open[1]) and (close > open[1]) and (open < close[1])

// Define bearish engulfing pattern
bearishEngulfing = (close < open) and (close[1] > open[1]) and (close < open[1]) and (open > close[1])

// ------------------- Entry Conditions -------------------
// Logical condition: If candlestick filter is disabled, ignore engulfing pattern
longCondition = ta.crossover(shortEma, longEma) and (volume > volumeEma) and (not enableCandlestickFilter or bullishEngulfing)
shortCondition = ta.crossunder(shortEma, longEma) and (volume > volumeEma) and (not enableCandlestickFilter or bearishEngulfing)

// ------------------- Exit Conditions -------------------
// Define Stop Loss and Take Profit levels
longStop = close - (atr * stopAtrMultiplier)
longTake = close + (atr * takeAtrMultiplier)

shortStop = close + (atr * stopAtrMultiplier)
shortTake = close - (atr * takeAtrMultiplier)

// ------------------- Enter and Exit Positions -------------------
if (longCondition) {
    strategy.entry("Long", strategy.long)
    strategy.exit("Long Exit", from_entry="Long", stop=longStop, limit=longTake)
}

if (shortCondition) {
    strategy.entry("Short", strategy.short)
    strategy.exit("Short Exit", from_entry="Short", stop=shortStop, limit=shortTake)
}

// ------------------- Plotting -------------------
plot(shortEma, color=color.blue, title="Short EMA")
plot(longEma, color=color.orange, title="Long EMA")
plot(volumeEma, title="Volume EMA", color=color.gray, style=plot.style_dotted)

// Optional: Plot Volume Oscillator with single color argument
plot(volumeOsc, title="Volume Oscillator", 
     color=color.new(color.purple, 60), // Single color argument with transparency
     style=plot.style_histogram, 
     linewidth=1)

// ------------------- Strategy Performance Metrics -------------------
// Note: Profit Factor, Max Drawdown, etc., are available in the Strategy Tester tab.
// For Sharpe and Sortino ratios, export the trade data and perform calculations externally.

// ------------------- Alerts -------------------
alertcondition(longCondition, title="Long Entry", message="Potential Long Entry Signal")
alertcondition(shortCondition, title="Short Entry", message="Potential Short Entry Signal")