//@version=5 strategy("Trade with Pat - Liquidity Sweep Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10) // --- Inputs --- lookbackInput = input.int(20, title="S/R Lookback Period", minval=5) emaPeriodInput = input.int(200, title="Trend Filter EMA Period", minval=10) rrRatioInput = input.float(1.5, title="Risk-to-Reward Ratio", minval=0.5, step=0.1) bufferTicksInput = input.int(10, title="Stop Loss Buffer (Ticks/Pipettes)", minval=0) // --- Indicator Calculations --- emaVal = ta.ema(close, emaPeriodInput) // Support & Resistance levels based on historical lookback (excluding current candle) resistanceLevel = ta.highest(high, lookbackInput)[1] supportLevel = ta.lowest(low, lookbackInput)[1] // --- Strategy Rules --- // 1. Long Sweep: Low goes below the support level, but the candle closes back above the support level. // To align with the trend, the close must also be above the EMA. longSweep = (low < supportLevel) and (close > supportLevel) and (close > emaVal) // 2. Short Sweep: High goes above the resistance level, but the candle closes back below the resistance level. // To align with the trend, the close must also be below the EMA. shortSweep = (high > resistanceLevel) and (close < resistanceLevel) and (close < emaVal) // --- Plotting --- plot(emaVal, title="EMA Trend Filter", color=color.blue, linewidth=2) plot(resistanceLevel, title="Resistance Level", color=color.red, style=plot.style_circles, offset=-1) plot(supportLevel, title="Support Level", color=color.green, style=plot.style_circles, offset=-1) // --- Execution & Risk Management --- var float longStopLoss = na var float longTakeProfit = na var float shortStopLoss = na var float shortTakeProfit = na tickValue = syminfo.mintick // Handle Buy (Long) Setup if (longSweep and strategy.position_size == 0) longStopLoss := low - (bufferTicksInput * tickValue) riskDistance = close - longStopLoss longTakeProfit := close + (riskDistance * rrRatioInput) strategy.entry("Long Sweep", strategy.long) strategy.exit("Exit Long", "Long Sweep", stop=longStopLoss, limit=longTakeProfit) // Handle Sell (Short) Setup if (shortSweep and strategy.position_size == 0) shortStopLoss := high + (bufferTicksInput * tickValue) riskDistance = shortStopLoss - close shortTakeProfit := close - (riskDistance * rrRatioInput) strategy.entry("Short Sweep", strategy.short) strategy.exit("Exit Short", "Short Sweep", stop=shortStopLoss, limit=shortTakeProfit) // Clean up plotting values plotshape(longSweep and strategy.position_size == 0, title="Long Entry Signal", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) plotshape(shortSweep and strategy.position_size == 0, title="Short Entry Signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)