Advanced EG Code For Tradingview

//@version=5 strategy("FINAL SMC MTF Engulfing Strategy", overlay=true, pyramiding=0, initial_capital=50, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=2.0) // Default 2% Risk for $50 Capital // --- 1. USER INPUTS (Risk Management & Optimization) --- riskPercent = input.float(2.0, title="Risk Per Trade (%) - Max $1", minval=0.1, maxval=5.0, step=0.1) // 2% of $50 = $1 Risk rrRatio = input.float(2.0, title="Risk-Reward Ratio (Min 1:2)", minval=1.5, step=0.1) // Faster TP hit for small capital slBufferPips = input.float(3.0, title="SL Buffer (Pips/Points) - For SL Hunting", minval=0.0, step=0.5) // 3 Pips for EURUSD M5/M15 tradeSession = input.session("0800-1700", "Trading Hours (London-NY Active)", session.extended) isSession = time(timeframe.period, tradeSession) // --- 2. ENGULFING DETECTION --- isBullEngulfing(index) => isEngulfing = close[index] > open[index] and open[index] < close[index+1] and close[index] > open[index+1] and close[index+1] < open[index+1] isOpposite = close[index+1] < open[index+1] isEngulfing and isOpposite isBearEngulfing(index) => isEngulfing = close[index] < open[index] and open[index] > close[index+1] and close[index] < open[index+1] and close[index+1] > open[index+1] isOpposite = close[index+1] > open[index+1] isEngulfing and isOpposite // --- 3. SIMPLIFIED SUPPLY/DEMAND (SMC) ZONE DETECTION --- // Simple logic: Engulfing followed by a large opposing move (Order Block) isDemandZone(index) => isEngulfing(index) and low[index+1] > low[index] and high[index+1] < high[index] and close[index+1] < open[index+1] // Simplistic Bullish OB isSupplyZone(index) => isEngulfing(index) and high[index+1] < high[index] and low[index+1] > low[index] and close[index+1] > open[index+1] // Simplistic Bearish OB // --- 4. RISK, POSITION, and SL CALCULATION --- pipsToPoints = syminfo.mintick * 10 bufferTicks = slBufferPips * pipsToPoints calcPositionSize(stopLossPrice) => slDistance = math.abs(close - stopLossPrice) riskAmount = strategy.equity * (riskPercent / 100) // Micro Lot (0.01) based calculation riskPerTick = riskAmount / slDistance math.floor(riskPerTick * syminfo.mintick) // Position Size (Qty) // --- 5. BREAK-EVEN MANAGEMENT (LOSS PROTECTION) --- var bool BE_Active = false manageBreakEven() => if strategy.position_size > 0 // Long Trade // BE Level = Entry Price + SL Distance (1R Profit) breakEvenLevel = strategy.position_avg_price + (strategy.position_avg_price - strategy.position_stop) if not BE_Active and close > breakEvenLevel strategy.exit("BE_Long_EXIT", from_entry=strategy.last_entry_id, stop=strategy.position_avg_price, comment="BE") BE_Active := true else if strategy.position_size < 0 // Short Trade // BE Level = Entry Price - SL Distance (1R Profit) breakEvenLevel = strategy.position_avg_price - (strategy.position_stop - strategy.position_avg_price) if not BE_Active and close < breakEvenLevel strategy.exit("BE_Short_EXIT", from_entry=strategy.last_entry_id, stop=strategy.position_avg_price, comment="BE") BE_Active := true if barstate.isconfirmed BE_Active := false // --- 6. TRADE EXECUTION LOGIC (LONG/SHORT) --- if isSession // A. Bullish Engulfing (LONG Trade) - Buy from Demand Zone if isBullEngulfing(1) and isDemandZone(2) and strategy.position_size <= 0 slPrice = low[1] - bufferTicks // SL (Stop Loss) qty = calcPositionSize(slPrice) // Position (Lot Size) slDistance = close - slPrice tpPrice = close + (slDistance * rrRatio) // TP (Take Profit) strategy.entry("Long_Buy", strategy.long, qty=qty, comment="DEMAND_LONG") strategy.exit("Exit_Long", from_entry="Long_Buy", stop=slPrice, limit=tpPrice) // B. Bearish Engulfing (SHORT Trade) - Sell from Supply Zone if isBearEngulfing(1) and isSupplyZone(2) and strategy.position_size >= 0 slPrice = high[1] + bufferTicks // SL (Stop Loss) qty = calcPositionSize(slPrice) // Position (Lot Size) slDistance = slPrice - close tpPrice = close - (slDistance * rrRatio) // TP (Take Profit) strategy.entry("Short_Sell", strategy.short, qty=qty, comment="SUPPLY_SHORT") strategy.exit("Exit_Short", from_entry="Short_Sell", stop=slPrice, limit=tpPrice) // C. Failed Engulfing (Reverse Trades) - High Probability Continuation if isBullEngulfing(2) and close < low[2] // Bull EG Failed -> Short strategy.close_all("Failed_Reverse") slPrice = high[2] + bufferTicks qty = calcPositionSize(slPrice) slDistance = slPrice - close tpPrice = close - (slDistance * rrRatio) strategy.entry("Short_Failed", strategy.short, qty=qty, comment="EG_FAIL_SELL") strategy.exit("Exit_Short_Failed", from_entry="Short_Failed", stop=slPrice, limit=tpPrice) if isBearEngulfing(2) and close > high[2] // Bear EG Failed -> Long strategy.close_all("Failed_Reverse") slPrice = low[2] - bufferTicks qty = calcPositionSize(slPrice) slDistance = close - slPrice tpPrice = close + (slDistance * rrRatio) strategy.entry("Long_Failed", strategy.long, qty=qty, comment="EG_FAIL_BUY") strategy.exit("Exit_Long_Failed", from_entry="Long_Failed", stop=slPrice, limit=tpPrice) // --- 7. Apply Break-Even Logic --- manageBreakEven()

Post a Comment

Previous Post Next Post