//@version=5 strategy("XAUUSD Liquidity Grab + 30 EMA Strategy", overlay=true, default_qty_type=strategy.fixed, default_qty_value=2, initial_capital=10000) // ------------------ INPUTS ------------------ emaLen = input.int(30, "EMA Length") leftBars = input.int(1, "Left Bars (S/R)") rightBars = input.int(1, "Right Bars (S/R)") rrRatio = input.float(2.5, "Risk : Reward Ratio (Final Target)", step=0.1) partialRR = input.float(1.0, "Partial Book RR", step=0.1) partialPct = input.float(50, "Partial Book Quantity %", minval=1, maxval=99, step=1) lotSize = input.float(2.0, "Lot Size (Qty)", step=0.01) enableBE = input.bool(false, "🔘 Move SL to Breakeven after Partial Book") // ------------------ SL BUFFER ------------------ // Initial SL buffer only. // 0.30 = $0.30 = 3 pip-units when XAUUSD pip size is $0.10. slBuffer = input.float(0.30, "Initial SL Buffer ($)", minval=0.00, step=0.01) // ------------------ EMA ------------------ ema30 = ta.ema(close, emaLen) plot(ema30, "30 EMA", color=color.blue, linewidth=2) // ------------------ SUPPORT / RESISTANCE ------------------ pivotHigh = ta.pivothigh(high, leftBars, rightBars) pivotLow = ta.pivotlow(low, leftBars, rightBars) var float resistance = na var float support = na if not na(pivotHigh) resistance := pivotHigh if not na(pivotLow) support := pivotLow plot(resistance, "Resistance", color=color.red, style=plot.style_stepline, linewidth=1) plot(support, "Support", color=color.green, style=plot.style_stepline, linewidth=1) // ------------------ LIQUIDITY GRAB CONDITIONS ------------------ longCondition = not na(support) and low < support and close > support and close > ema30 shortCondition = not na(resistance) and high > resistance and close < resistance and close < ema30 // ------------------ TRADE STATE ------------------ var float entryPrice = na var float slPrice = na var float tp1Price = na var float tp2Price = na var float origQty = na var bool partialDone = false // ------------------ LONG ENTRY ------------------ if longCondition and strategy.position_size == 0 entryPrice := high // Initial SL = sweep low - $0.30 buffer slPrice := low - slBuffer riskPtsL = entryPrice - slPrice if riskPtsL > 0 tp1Price := entryPrice + riskPtsL * partialRR tp2Price := entryPrice + riskPtsL * rrRatio origQty := lotSize partialDone := false strategy.entry( "Long", strategy.long, qty=lotSize, stop=entryPrice) // ------------------ SHORT ENTRY ------------------ if shortCondition and strategy.position_size == 0 entryPrice := low // Initial SL = sweep high + $0.30 buffer slPrice := high + slBuffer riskPtsS = slPrice - entryPrice if riskPtsS > 0 tp1Price := entryPrice - riskPtsS * partialRR tp2Price := entryPrice - riskPtsS * rrRatio origQty := lotSize partialDone := false strategy.entry( "Short", strategy.short, qty=lotSize, stop=entryPrice) // ------------------ PARTIAL BOOK DETECTION ------------------ if strategy.position_size != 0 and not partialDone if math.abs(strategy.position_size) <= origQty * (1 - partialPct / 100) * 1.001 partialDone := true // ------------------ SL LOGIC ------------------ // Before TP1: // Original sweep SL with $0.30 buffer. // // After TP1 + BE ON: // SL = EXACT entry price. // NO buffer is applied here. float currentSL = slPrice if partialDone and enableBE currentSL := entryPrice // ------------------ LONG EXITS ------------------ if strategy.position_size > 0 strategy.exit( "Long TP1", "Long", qty_percent=partialPct, limit=tp1Price, stop=currentSL) strategy.exit( "Long TP2", "Long", qty_percent=100, limit=tp2Price, stop=currentSL) // ------------------ SHORT EXITS ------------------ if strategy.position_size < 0 strategy.exit( "Short TP1", "Short", qty_percent=partialPct, limit=tp1Price, stop=currentSL) strategy.exit( "Short TP2", "Short", qty_percent=100, limit=tp2Price, stop=currentSL) // ------------------ RESET WHEN FLAT ------------------ if strategy.position_size == 0 partialDone := false // ------------------ VISUAL SIGNALS ------------------ plotshape( longCondition, title="Buy Signal", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) plotshape( shortCondition, title="Sell Signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small) // ------------------ ACTIVE LEVELS ------------------ plot( strategy.position_size != 0 ? currentSL : na, title="Active SL", color=color.orange, style=plot.style_circles) plot( strategy.position_size != 0 ? tp1Price : na, title="TP1 (Partial)", color=color.aqua, style=plot.style_circles) plot( strategy.position_size != 0 ? tp2Price : na, title="TP2 (Final)", color=color.purple, style=plot.style_circles)