TRADING STRATEGY
TRADING STRATEGY
// 🔹 Input Parameters
riskPerTrade = input.float(1.0, "Risk Per Trade (%)", minval=0.1, maxval=5.0,
step=0.1)
useEma = input.bool(true, "Use EMA Filter")
emaLength = input.int(20, "EMA Length", minval=5, maxval=200)
atrLength = input.int(14, "ATR Length", minval=1)
volumeMultiplier = input.float(1.5, "Volume Multiplier", minval=1.0, maxval=3.0,
step=0.1)
takeProfitR = input.float(2.0, "Take Profit (× Risk)", minval=0.5,
maxval=10.0, step=0.5)
atrMultiplier = input.float(1.0, "ATR Stop Loss Buffer", minval=0.1, maxval=2.0,
step=0.1)
useHigherTimeframe = input.bool(true, "Use Higher Timeframe EMA")
higherTimeframe = input.timeframe("D", "Higher Timeframe")
// 🔹 Technical Indicators
ema = ta.ema(close, emaLength)
atr = ta.atr(atrLength)
htfEma = request.security(syminfo.tickerid, higherTimeframe, ta.ema(close,
emaLength))
// 🔹 Volume Analysis
avgVolume = ta.sma(volume, 20)
highVolume = volume > (avgVolume * volumeMultiplier)
bearishEngulfing = (
close < open and
close < close[1] and
open > open[1] and
(open - close) > (close[1] - open[1])
)
bullishPinBar = (
low < low[1] and
close > open and
(close - open) / (high - low) > 0.6
)
bearishPinBar = (
high > high[1] and
close < open and
(open - close) / (high - low) > 0.6
)
// 🔹 RSI Divergence
rsi = ta.rsi(close, 14)
bullishDivergence = (low < low[2]) and (rsi > rsi[2])
bearishDivergence = (high > high[2]) and (rsi < rsi[2])
// 🔹 Entry Conditions
longCondition = (
((useHigherTimeframe and close > htfEma) or
(not useHigherTimeframe and close > ema)) and
(bullishEngulfing or bullishPinBar) and
highVolume and
bullishDivergence
)
shortCondition = (
((useHigherTimeframe and close < htfEma) or
(not useHigherTimeframe and close < ema)) and
(bearishEngulfing or bearishPinBar) and
highVolume and
bearishDivergence
)
strategy.entry("Long", strategy.long)
strategy.exit("Take Profit", from_entry="Long", limit=takeProfit,
stop=stopLoss)
label.new(
bar_index, low, "LONG\nSL: " + str.tostring(stopLoss) + "\nTP: " +
str.tostring(takeProfit),
color=color.green, textcolor=color.white, style=label.style_label_down,
size=size.small
)
if shortCondition
stopLoss = high + (atr * atrMultiplier)
riskAmount = stopLoss - close
takeProfit = close - (riskAmount * takeProfitR)
strategy.entry("Short", strategy.short)
strategy.exit("Take Profit", from_entry="Short", limit=takeProfit,
stop=stopLoss)
label.new(
bar_index, high, "SHORT\nSL: " + str.tostring(stopLoss) + "\nTP: " +
str.tostring(takeProfit),
color=color.red, textcolor=color.white, style=label.style_label_up,
size=size.small
)
// 🔹 Visual Signals
plot(ema, "EMA", color=color.blue)
plot(useHigherTimeframe ? htfEma : na, "HTF EMA", color=color.yellow)
plotshape(
series=longCondition, location=location.belowbar,
color=color.green, style=shape.triangleup, size=size.small, title="Long Signal"
)
plotshape(
series=shortCondition, location=location.abovebar,
color=color.red, style=shape.triangledown, size=size.small, title="Short
Signal"
)