//@version=5
strategy("Enhanced SuperTrend 315 Strategy", overlay=true,
margin_long=100, margin_short=100, pyramiding=1,
default_qty_type=strategy.percent_of_equity, default_qty_value=100,
commission_type=[Link], commission_value=0.05)
// Input Parameters
factor = [Link](2.5, "SuperTrend Factor", minval=1, maxval=5,
step=0.1)
atrPeriod = [Link](7, "ATR Period", minval=3, maxval=20)
emaShort = [Link](3, "Fast EMA", minval=2, maxval=10)
emaLong = [Link](9, "Slow EMA", minval=5, maxval=20)
rsiPeriod = [Link](14, "RSI Period", minval=7, maxval=21)
volPeriod = [Link](21, "Volume SMA Period", minval=10, maxval=50)
riskPct = [Link](2, "Risk %", minval=1, maxval=5, step=0.5)
maxLoss = [Link](5, "Max Loss %", minval=2, maxval=10, step=0.5)
// SuperTrend Calculation
atr = [Link](atrPeriod)
upperBand = (high + low)/2 + factor * atr
lowerBand = (high + low)/2 - factor * atr
var float superTrend = na
var float trend = na
var float prevUp = na
var float prevDn = na
prevUp := upperBand
prevDn := lowerBand
if close > prevUp[1]
superTrend := lowerBand
trend := 1
else if close < prevDn[1]
superTrend := upperBand
trend := -1
else
superTrend := superTrend[1]
trend := trend[1]
// Indicator Calculations
emaFast = [Link](close, emaShort)
emaSlowHigh = [Link](high, emaLong)
emaSlowLow = [Link](low, emaLong)
ema200 = [Link](close, 200)
rsi = [Link](close, rsiPeriod)
[macdLine, signalLine, _] = [Link](close, 12, 26, 9)
volSMA = [Link](volume, volPeriod)
// Entry Conditions
bullishCrossover = [Link](emaFast, emaSlowHigh)
bearishCrossover = [Link](emaFast, emaSlowLow)
validVolume = volume > volSma * 1.5
uptrend = trend == 1
downtrend = trend == -1
// Strategy Logic
longCondition = bullishCrossover and uptrend and
close > ema200 and
rsi > 45 and rsi < 70 and
validVolume and
macdLine > signalLine
shortCondition = bearishCrossover or downtrend or
close < ema200 or
rsi > 70
// Risk Management
trailOffset = factor * atr
var float stopPrice = na
if longCondition
[Link]("Long", [Link])
stopPrice := high - trailOffset
if strategy.position_size > 0
stopPrice := [Link](stopPrice, high - trailOffset)
[Link]("Exit", "Long", stop=stopPrice, loss=close * (1 -
maxLoss/100))
if shortCondition or time >= timestamp(year, month, dayofmonth, 15, 15)
[Link]("Long")
// Plotting
plot(superTrend, color=trend == 1 ? [Link] : [Link], linewidth=2,
title="SuperTrend")
plot(ema200, color=[Link], title="200 EMA")
plot(emaFast, color=[Link], title="Fast EMA")
// Alerts
alertcondition(longCondition, title="Buy Signal", message="Long Entry
Signal")
alertcondition(shortCondition, title="Sell Signal", message="Exit Position")
// Table Display
var table positionTable = [Link](position.bottom_right, 2, 2)
if [Link]
[Link](positionTable, 0, 0, "Current Trend", bgcolor=[Link])
[Link](positionTable, 1, 0, trend == 1 ? "Bullish" : "Bearish",
bgcolor=trend == 1 ? [Link] : [Link])
[Link](positionTable, 0, 1, "Next ATR", bgcolor=[Link])
[Link](positionTable, 1, 1, [Link](atr, "#.00"),
bgcolor=[Link])