//@version=5
strategy("Filtered VWAP Trend Strategy + RSI + EMA50 + SL/TP", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// Inputs
slopeLength = input.int(5, "VWAP Slope Period", minval=1)
slopeThreshold = input.float(0.0002, "Minimum VWAP slope for trend")  // increased threshold
cooldown = input.int(5, "Cooldown Bars")
rsiLength = input.int(14, "RSI Length")
rsiThreshold = input.float(50, "RSI Threshold")
emaLength = input.int(50, "EMA Length")

// Stop-loss and take-profit inputs as percentage of entry price
stopLossPerc = input.float(1.0, "Stop Loss (%)", minval=0.1, step=0.1)  // e.g. 1% SL
takeProfitPerc = input.float(2.0, "Take Profit (%)", minval=0.1, step=0.1)  // e.g. 2% TP

// VWAP and slope
vwap = ta.vwap
vwap_slope = vwap - vwap[slopeLength]
trendUp = vwap_slope > slopeThreshold
trendDown = vwap_slope < -slopeThreshold

// Price crossing VWAP
priceCrossUp = ta.crossover(close, vwap)
priceCrossDown = ta.crossunder(close, vwap)

// RSI and EMA
rsi = ta.rsi(close, rsiLength)
ema50 = ta.ema(close, emaLength)

// Define buy and sell signals with added filters
buySignal = priceCrossUp and trendUp and (rsi > rsiThreshold) and (close > ema50)
sellSignal = priceCrossDown and trendDown and (rsi < rsiThreshold) and (close < ema50)

// Cooldown logic
var int lastBuyBar = na
var int lastSellBar = na

buySignalFiltered = false
sellSignalFiltered = false

if buySignal
    if na(lastBuyBar) or (bar_index - lastBuyBar) > cooldown
        buySignalFiltered := true
        lastBuyBar := bar_index
    else
        buySignalFiltered := false

if sellSignal
    if na(lastSellBar) or (bar_index - lastSellBar) > cooldown
        sellSignalFiltered := true
        lastSellBar := bar_index
    else
        sellSignalFiltered := false

// Plot VWAP and EMA50
plot(vwap, title="VWAP", color=color.orange)
plot(ema50, title="EMA 50", color=color.blue)

// Strategy entries and exits with SL/TP

// Calculate stop-loss and take-profit prices relative to entry price
long_stop_price = strategy.position_avg_price * (1 - stopLossPerc / 100)
long_take_price = strategy.position_avg_price * (1 + takeProfitPerc / 100)
short_stop_price = strategy.position_avg_price * (1 + stopLossPerc / 100)
short_take_price = strategy.position_avg_price * (1 - takeProfitPerc / 100)

// Enter long
if (buySignalFiltered)
    if (strategy.position_size <= 0)
        strategy.entry("Long", strategy.long)

// Close long and enter short
if (sellSignalFiltered)
    if (strategy.position_size > 0)
        strategy.close("Long")
    if (strategy.position_size >= 0)
        strategy.entry("Short", strategy.short)

// Close short and enter long
if (buySignalFiltered)
    if (strategy.position_size < 0)
        strategy.close("Short")

// Exit long with SL/TP
if (strategy.position_size > 0)
    strategy.exit("Long Exit", from_entry="Long", stop=long_stop_price, limit=long_take_price)

// Exit short with SL/TP
if (strategy.position_size < 0)
    strategy.exit("Short Exit", from_entry="Short", stop=short_stop_price, limit=short_take_price)

// Plot signals for visual
plotshape(buySignalFiltered, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, size=size.small, text="BUY")
plotshape(sellSignalFiltered, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, size=size.small, text="SELL")