Supar tendNifty 50 IndexNSE:NIFTYthummaambadas123//@version=5 strategy("Nifty50 15m Supertrend Backtest (ATR=21, Mult=1) – 6M Stats", overlay=true, initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.0, pyramiding=0, calc_on_every_tick=false, process_orders_on_close=true) // ---------- Fixed Inputs (as requested) ---------- atrPeriod = input.int(21, "ATR Period", minval=1) multiplier = input.float(1.0, "Multiplier", step=0.1) // ---------- Date Range: last N months ---------- monthsBack = input.int(6, "Backtest Window (months)", minval=1, maxval=36) msPerDay = 24 * 60 * 60 * 1000 fromTime = timenow - monthsBack * 30 * msPerDay inRange = time >= fromTime // ---------- Supertrend Calculation ---------- atr = ta.atr(atrPeriod) hl2 = ta.hl2 basicUpper = hl2 + multiplier * atr basicLower = hl2 - multiplier * atr var float finalUpper = na var float finalLower = na var int trend = 1 // 1 = up, -1 = down finalUpper := na(finalUpper) ? basicUpper : (basicUpper < finalUpper or hl2 > finalUpper) ? basicUpper : finalUpper finalLower := na(finalLower) ? basicLower : (basicLower > finalLower or hl2 < finalLower) ? basicLower : finalLower trend := na(trend) ? 1 : trend == -1 and hl2 > finalUpper ? 1 : trend == 1 and hl2 < finalLower ? -1 : trend supertrend = trend == 1 ? finalLower : finalUpper // ---------- Signals ---------- longSignal = ta.crossover(close, supertrend) or (trend == 1 and trend == -1) shortSignal = ta.crossunder(close, supertrend) or (trend == -1 and trend == 1) // ---------- Strategy Entries (for visual) ---------- if inRange and longSignal strategy.entry("Long", strategy.long) if inRange and shortSignal strategy.entry("Short", strategy.short) // ---------- Manual P&L Tracking (Points) ---------- var bool inLong = false var float longEntry = na var float longSum = 0.0 var int longCount = 0 var bool inShort = false var float shortEntry = na var float shortSum = 0.0 var int shortCount = 0 // Enter Long if inRange and not inLong and longSignal inLong := true longEntry := close // Exit Long on shortSignal if inRange and inLong and shortSignal longSum += (close - longEntry) longCount += 1 inLong := false // Enter Short if inRange and not inShort and shortSignal inShort := true shortEntry := close // Exit Short on longSignal if inRange and inShort and longSignal shortSum += (shortEntry - close) shortCount += 1 inShort := false // Close open positions when leaving range (end-of-window safety) justLeftRange = not inRange and inRange if justLeftRange if inLong longSum += (close - longEntry) longCount += 1 inLong := false if inShort shortSum += (shortEntry - close) shortCount += 1 inShort := false longAvg = longCount > 0 ? longSum / longCount : na shortAvg = shortCount > 0 ? shortSum / shortCount : na totalPts = longSum + shortSum // ---------- Plots ---------- plot(supertrend, title="Supertrend (ATR=21, Mult=1)", linewidth=2, color = trend == 1 ? color.new(color.green, 0) : color.new(color.red, 0)) plotshape(longSignal, title="BUY", style=shape.triangleup, location=location.belowbar, size=size.tiny, text="Buy") plotshape(shortSignal, title="SELL", style=shape.triangledown, location=location.abovebar, size=size.tiny, text="Sell") // ---------- On-chart Stats Table ---------- var table t = table.new(position.top_right, 2, 6, border_width=1) if barstate.islast table.cell(t, 0, 0, "Window", text_size=size.small, text_color=color.white, bgcolor=color.new(color.blue, 35)) table.cell(t, 1, 0, str.tostring(monthsBack) + " mo", text_size=size.small) table.cell(t, 0, 1, "BUY Trades", text_size=size.small) table.cell(t, 1, 1, str.tostring(longCount), text_size=size.small) table.cell(t, 0, 2, "BUY Avg pts", text_size=size.small) table.cell(t, 1, 2, na(longAvg) ? "—" : str.tostring(longAvg, format.mintick), text_size=size.small) table.cell(t, 0, 3, "SELL Trades", text_size=size.small) table.cell(t, 1, 3, str.tostring(shortCount), text_size=size.small) table.cell(t, 0, 4, "SELL Avg pts", text_size=size.small) table.cell(t, 1, 4, na(shortAvg) ? "—" : str.tostring(shortAvg, format.mintick), text_size=size.small) table.cell(t, 0, 5, "Total Points", text_size=size.small, text_color=color.white, bgcolor=color.new(color.teal, 35)) table.cell(t, 1, 5, str.tostring(totalPts, format.mintick), text_size=size.small)