TS 0.2

Wait 5 sec.

TS 0.2Crude Oil FuturesMCX:CRUDEOIL1!srm76999//@version=6 strategy( "BPTS 0.2 PRO - Crude Oil", overlay = true, pyramiding = 2, initial_capital = 100000, default_qty_type = strategy.fixed, default_qty_value = 1, commission_type = strategy.commission.cash_per_order, commission_value = 0, calc_on_order_fills = true, process_orders_on_close = true) //==================================================== // INPUTS //==================================================== groupTrend = "1. Trend / Double Band" fastLen = input.int(20, "Fast EMA", minval = 1, group = groupTrend) slowLen = input.int(50, "Slow EMA", minval = 1, group = groupTrend) groupRSI = "2. RSI Filter" rsiLen = input.int(14, "RSI Length", minval = 2, group = groupRSI) rsiBuyLevel = input.float(55, "BUY RSI Level", group = groupRSI) rsiSellLevel = input.float(45, "SELL RSI Level", group = groupRSI) rsiOB = input.float(70, "RSI Overbought", group = groupRSI) rsiOS = input.float(30, "RSI Oversold", group = groupRSI) groupRisk = "3. Risk Management" target1 = input.float(100, "Target 1 - Points", minval = 1, group = groupRisk) target2 = input.float(200, "Target 2 - Points", minval = 1, group = groupRisk) stopPts = input.float(70, "Initial Stop - Points", minval = 1, group = groupRisk) useBE = input.bool(true, "Move SL to Break-even", group = groupRisk) beTrigger = input.float(70, "Break-even Trigger", minval = 1, group = groupRisk) useTrail = input.bool(true, "Use Trailing Stop", group = groupRisk) trailPts = input.float(60, "Trailing Stop - Points", minval = 1, group = groupRisk) groupEntry = "4. Entry Control" useSecondEntry = input.bool(true, "Allow 2nd Entry", group = groupEntry) pullbackTolerance = input.float(10, "EMA Pullback Tolerance", minval = 0, group = groupEntry) groupSession = "5. Session Filter" useSession = input.bool(false, "Use Session Filter", group = groupSession) tradeSession = input.session("0900-2330", "Trading Session", group = groupSession) //==================================================== // INDICATORS //==================================================== emaFast = ta.ema(close, fastLen) emaSlow = ta.ema(close, slowLen) rsi = ta.rsi(close, rsiLen) //==================================================== // TREND //==================================================== bullTrend = emaFast > emaSlow bearTrend = emaFast < emaSlow priceAboveBand = close > emaFast priceBelowBand = close < emaFast //==================================================== // SESSION //==================================================== inSession = not useSession or not na(time(timeframe.period, tradeSession)) //==================================================== // MOMENTUM //==================================================== rsiBull = rsi > rsiBuyLevel rsiBear = rsi < rsiSellLevel rsiRising = rsi > rsi rsiFalling = rsi < rsi //==================================================== // FIRST ENTRY //==================================================== buySetup = bullTrend and priceAboveBand and rsiBull and rsiRising and inSession sellSetup = bearTrend and priceBelowBand and rsiBear and rsiFalling and inSession // Trigger only when setup becomes active buySignal = buySetup and not buySetup and barstate.isconfirmed sellSignal = sellSetup and not sellSetup and barstate.isconfirmed //==================================================== // ENTRY 1 //==================================================== if buySignal and strategy.position_size == 0 strategy.entry("BUY-1", strategy.long) if sellSignal and strategy.position_size == 0 strategy.entry("SELL-1", strategy.short) //==================================================== // SECOND ENTRY LOGIC //==================================================== // Pullback toward EMA 20 buyPullback = bullTrend and low emaFast and rsi > 50 and rsiRising and inSession sellPullback = bearTrend and high >= emaFast - pullbackTolerance and close < emaFast and rsi < 50 and rsiFalling and inSession // Track whether second entry has been used var bool secondBuyUsed = false var bool secondSellUsed = false // Reset when flat if strategy.position_size == 0 secondBuyUsed := false secondSellUsed := false // Second BUY if useSecondEntry and strategy.position_size > 0 and not secondBuyUsed and buyPullback and barstate.isconfirmed strategy.entry("BUY-2", strategy.long) secondBuyUsed := true // Second SELL if useSecondEntry and strategy.position_size < 0 and not secondSellUsed and sellPullback and barstate.isconfirmed strategy.entry("SELL-2", strategy.short) secondSellUsed := true //==================================================== // POSITION MANAGEMENT //==================================================== var float highestSinceEntry = na var float lowestSinceEntry = na if strategy.position_size > 0 highestSinceEntry := na(highestSinceEntry) ? high : math.max(highestSinceEntry, high) lowestSinceEntry := na else if strategy.position_size < 0 lowestSinceEntry := na(lowestSinceEntry) ? low : math.min(lowestSinceEntry, low) highestSinceEntry := na else highestSinceEntry := na lowestSinceEntry := na //==================================================== // LONG EXIT //==================================================== if strategy.position_size > 0 avgPrice = strategy.position_avg_price initialSL = avgPrice - stopPts target1Price = avgPrice + target1 target2Price = avgPrice + target2 // Break-even beSL = useBE and highestSinceEntry >= avgPrice + beTrigger ? avgPrice : initialSL // Trailing trailSL = useTrail ? highestSinceEntry - trailPts : initialSL finalSL = math.max(beSL, trailSL) strategy.exit( "TP1 LONG", from_entry = "BUY-1", stop = finalSL, limit = target1Price, qty_percent = 50) strategy.exit( "TP2 LONG", from_entry = "BUY-1", stop = finalSL, limit = target2Price, qty_percent = 50) strategy.exit( "TP2 LONG-2", from_entry = "BUY-2", stop = finalSL, limit = target2Price) //==================================================== // SHORT EXIT //==================================================== if strategy.position_size < 0 avgPrice = strategy.position_avg_price initialSL = avgPrice + stopPts target1Price = avgPrice - target1 target2Price = avgPrice - target2 // Break-even beSL = useBE and lowestSinceEntry 0 and buyPullback and not secondBuyUsed, title = "2nd BUY", style = shape.labelup, location = location.belowbar, color = color.lime, text = "BUY 2", textcolor = color.black) plotshape( useSecondEntry and strategy.position_size < 0 and sellPullback and not secondSellUsed, title = "2nd SELL", style = shape.labeldown, location = location.abovebar, color = color.maroon, text = "SELL 2", textcolor = color.white) //==================================================== // 100 POINT / 200 POINT VISUAL //==================================================== long100 = strategy.position_size > 0 and high >= strategy.position_avg_price + target1 short100 = strategy.position_size < 0 and low 0, title = "BPTS PRO 2nd BUY", message = "BPTS 0.2 PRO 2nd BUY / Pullback") alertcondition( useSecondEntry and sellPullback and strategy.position_size < 0, title = "BPTS PRO 2nd SELL", message = "BPTS 0.2 PRO 2nd SELL / Pullback")