OPEN-SOURCE SCRIPT
btall

//version=5
strategy("AR | AlphaEdge Pro (No-Repaint) v1.0",
overlay=true,
initial_capital=100000,
commission_type=strategy.commission.percent,
commission_value=0.04,
slippage=1,
pyramiding=0,
calc_on_order_fills=true,
calc_on_every_tick=false,
process_orders_on_close=true)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Inputs
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
grp1 = "Trend + Signals"
useHTF = input.bool(true, "Use Higher TF Trend Filter", group=grp1)
htf = input.timeframe("240", "Higher Timeframe", group=grp1)
htfEmaLen = input.int(200, "HTF EMA Length", minval=1, group=grp1)
emaFastLen = input.int(21, "Fast EMA", minval=1, group=grp1)
emaSlowLen = input.int(55, "Slow EMA", minval=1, group=grp1)
stAtrLen = input.int(10, "Supertrend ATR Length", minval=1, group=grp1)
stFactor = input.float(3.0, "Supertrend Factor", minval=0.1, step=0.1, group=grp1)
rsiLen = input.int(14, "RSI Length", minval=1, group=grp1)
rsiBull = input.int(52, "RSI Bull Threshold", minval=1, maxval=99, group=grp1)
rsiBear = input.int(48, "RSI Bear Threshold", minval=1, maxval=99, group=grp1)
useVolFilter = input.bool(true, "Use Volume Filter", group=grp1)
volLen = input.int(20, "Volume SMA Length", minval=1, group=grp1)
volMult = input.float(1.2, "Volume Multiplier", minval=0.1, step=0.1, group=grp1)
grp2 = "Risk Management (ATR)"
atrLen = input.int(14, "ATR Length", minval=1, group=grp2)
slAtrMult = input.float(1.6, "Stop = ATR x", minval=0.1, step=0.1, group=grp2)
rr = input.float(2.0, "Risk:Reward (TP)", minval=0.1, step=0.1, group=grp2)
useRiskPct = input.bool(true, "Dynamic Position Size by Risk %", group=grp2)
riskPct = input.float(1.0, "Risk % of Equity per Trade", minval=0.05, step=0.05, group=grp2)
maxPosPct = input.float(20.0, "Max Position % of Equity Cap", minval=1.0, step=1.0, group=grp2)
grp3 = "Breakeven + Session"
useBE = input.bool(true, "Move Stop to Breakeven", group=grp3)
beTriggerATR = input.float(1.0, "Trigger After +ATR x", minval=0.1, step=0.1, group=grp3)
beOffsetATR = input.float(0.05, "BE Offset (ATR x)", minval=0.0, step=0.01, group=grp3)
useSession = input.bool(false, "Trade Only in Session", group=grp3)
sess = input.session("1000-1500", "Session (exchange time)", group=grp3)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Helper / Filters
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
inSession = not useSession or not na(time(timeframe.period, sess))
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
rsi = ta.rsi(close, rsiLen)
atr = ta.atr(atrLen)
volOk = not useVolFilter or (volume > ta.sma(volume, volLen) * volMult)
// Supertrend
[stLine, stDir] = ta.supertrend(stFactor, stAtrLen) // stDir: 1 bullish, -1 bearish (TradingView behavior)
stBull = stDir == 1
stBear = stDir == -1
// HTF trend (lookahead_off to reduce repaint risk on historical vs realtime behavior)
htfEma = request.security(syminfo.tickerid, htf, ta.ema(close, htfEmaLen), barmerge.gaps_off, barmerge.lookahead_off)
htfBull = close >= htfEma
htfBear = close <= htfEma
trendOkLong = not useHTF or htfBull
trendOkShort = not useHTF or htfBear
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Entry Conditions (confirmed bar to avoid “intrabar” surprises)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
longSignal = barstate.isconfirmed and inSession and trendOkLong and stBull and close > emaFast and emaFast > emaSlow and rsi >= rsiBull and volOk
shortSignal = barstate.isconfirmed and inSession and trendOkShort and stBear and close < emaFast and emaFast < emaSlow and rsi <= rsiBear and volOk
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Position sizing by ATR stop distance (approx; works best on stocks/spot)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_posQty(_stopDist) =>
// Risk capital = equity * riskPct%
riskCapital = strategy.equity * (riskPct / 100.0)
// Qty to risk roughly riskCapital if stop hits
rawQty = _stopDist > 0 ? (riskCapital / _stopDist) : 0.0
// Cap position size by maxPosPct% of equity
maxPositionValue = strategy.equity * (maxPosPct / 100.0)
maxQty = close > 0 ? (maxPositionValue / close) : rawQty
qty = math.max(0.0, math.min(rawQty, maxQty))
qty
stopDist = atr * slAtrMult
qty = useRiskPct ? f_posQty(stopDist) : na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Entries
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if (longSignal and strategy.position_size <= 0)
strategy.entry("L", strategy.long, qty=useRiskPct ? qty : na)
if (shortSignal and strategy.position_size >= 0)
strategy.entry("S", strategy.short, qty=useRiskPct ? qty : na)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Exits (ATR SL/TP + optional Breakeven)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0
entry = strategy.position_avg_price
longSL = entry - stopDist
longTP = entry + stopDist * rr
shortSL = entry + stopDist
shortTP = entry - stopDist * rr
// Breakeven logic
beTrig = atr * beTriggerATR
beOff = atr * beOffsetATR
if isLong and useBE
// if price moved in our favor enough -> raise stop to entry (+offset)
if (close - entry) >= beTrig
longSL := math.max(longSL, entry + beOff)
if isShort and useBE
if (entry - close) >= beTrig
shortSL := math.min(shortSL, entry - beOff)
// Submit exits every bar so they update dynamically
strategy.exit("XL", from_entry="L", stop=longSL, limit=longTP, when=isLong)
strategy.exit("XS", from_entry="S", stop=shortSL, limit=shortTP, when=isShort)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Visuals + Alerts
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(emaFast, "EMA Fast", linewidth=2)
plot(emaSlow, "EMA Slow", linewidth=2)
plot(stLine, "Supertrend", linewidth=2)
plot(useHTF ? htfEma : na, "HTF EMA", linewidth=2)
plotshape(longSignal, title="Long Signal", style=shape.triangleup, location=location.belowbar, size=size.tiny, text="L")
plotshape(shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, size=size.tiny, text="S")
alertcondition(longSignal, title="AlphaEdge Long", message="AlphaEdge LONG on {{ticker}} @ {{close}}")
alertcondition(shortSignal, title="AlphaEdge Short", message="AlphaEdge SHORT on {{ticker}} @ {{close}}")
strategy("AR | AlphaEdge Pro (No-Repaint) v1.0",
overlay=true,
initial_capital=100000,
commission_type=strategy.commission.percent,
commission_value=0.04,
slippage=1,
pyramiding=0,
calc_on_order_fills=true,
calc_on_every_tick=false,
process_orders_on_close=true)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Inputs
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
grp1 = "Trend + Signals"
useHTF = input.bool(true, "Use Higher TF Trend Filter", group=grp1)
htf = input.timeframe("240", "Higher Timeframe", group=grp1)
htfEmaLen = input.int(200, "HTF EMA Length", minval=1, group=grp1)
emaFastLen = input.int(21, "Fast EMA", minval=1, group=grp1)
emaSlowLen = input.int(55, "Slow EMA", minval=1, group=grp1)
stAtrLen = input.int(10, "Supertrend ATR Length", minval=1, group=grp1)
stFactor = input.float(3.0, "Supertrend Factor", minval=0.1, step=0.1, group=grp1)
rsiLen = input.int(14, "RSI Length", minval=1, group=grp1)
rsiBull = input.int(52, "RSI Bull Threshold", minval=1, maxval=99, group=grp1)
rsiBear = input.int(48, "RSI Bear Threshold", minval=1, maxval=99, group=grp1)
useVolFilter = input.bool(true, "Use Volume Filter", group=grp1)
volLen = input.int(20, "Volume SMA Length", minval=1, group=grp1)
volMult = input.float(1.2, "Volume Multiplier", minval=0.1, step=0.1, group=grp1)
grp2 = "Risk Management (ATR)"
atrLen = input.int(14, "ATR Length", minval=1, group=grp2)
slAtrMult = input.float(1.6, "Stop = ATR x", minval=0.1, step=0.1, group=grp2)
rr = input.float(2.0, "Risk:Reward (TP)", minval=0.1, step=0.1, group=grp2)
useRiskPct = input.bool(true, "Dynamic Position Size by Risk %", group=grp2)
riskPct = input.float(1.0, "Risk % of Equity per Trade", minval=0.05, step=0.05, group=grp2)
maxPosPct = input.float(20.0, "Max Position % of Equity Cap", minval=1.0, step=1.0, group=grp2)
grp3 = "Breakeven + Session"
useBE = input.bool(true, "Move Stop to Breakeven", group=grp3)
beTriggerATR = input.float(1.0, "Trigger After +ATR x", minval=0.1, step=0.1, group=grp3)
beOffsetATR = input.float(0.05, "BE Offset (ATR x)", minval=0.0, step=0.01, group=grp3)
useSession = input.bool(false, "Trade Only in Session", group=grp3)
sess = input.session("1000-1500", "Session (exchange time)", group=grp3)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Helper / Filters
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
inSession = not useSession or not na(time(timeframe.period, sess))
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
rsi = ta.rsi(close, rsiLen)
atr = ta.atr(atrLen)
volOk = not useVolFilter or (volume > ta.sma(volume, volLen) * volMult)
// Supertrend
[stLine, stDir] = ta.supertrend(stFactor, stAtrLen) // stDir: 1 bullish, -1 bearish (TradingView behavior)
stBull = stDir == 1
stBear = stDir == -1
// HTF trend (lookahead_off to reduce repaint risk on historical vs realtime behavior)
htfEma = request.security(syminfo.tickerid, htf, ta.ema(close, htfEmaLen), barmerge.gaps_off, barmerge.lookahead_off)
htfBull = close >= htfEma
htfBear = close <= htfEma
trendOkLong = not useHTF or htfBull
trendOkShort = not useHTF or htfBear
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Entry Conditions (confirmed bar to avoid “intrabar” surprises)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
longSignal = barstate.isconfirmed and inSession and trendOkLong and stBull and close > emaFast and emaFast > emaSlow and rsi >= rsiBull and volOk
shortSignal = barstate.isconfirmed and inSession and trendOkShort and stBear and close < emaFast and emaFast < emaSlow and rsi <= rsiBear and volOk
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Position sizing by ATR stop distance (approx; works best on stocks/spot)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_posQty(_stopDist) =>
// Risk capital = equity * riskPct%
riskCapital = strategy.equity * (riskPct / 100.0)
// Qty to risk roughly riskCapital if stop hits
rawQty = _stopDist > 0 ? (riskCapital / _stopDist) : 0.0
// Cap position size by maxPosPct% of equity
maxPositionValue = strategy.equity * (maxPosPct / 100.0)
maxQty = close > 0 ? (maxPositionValue / close) : rawQty
qty = math.max(0.0, math.min(rawQty, maxQty))
qty
stopDist = atr * slAtrMult
qty = useRiskPct ? f_posQty(stopDist) : na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Entries
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if (longSignal and strategy.position_size <= 0)
strategy.entry("L", strategy.long, qty=useRiskPct ? qty : na)
if (shortSignal and strategy.position_size >= 0)
strategy.entry("S", strategy.short, qty=useRiskPct ? qty : na)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Exits (ATR SL/TP + optional Breakeven)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0
entry = strategy.position_avg_price
longSL = entry - stopDist
longTP = entry + stopDist * rr
shortSL = entry + stopDist
shortTP = entry - stopDist * rr
// Breakeven logic
beTrig = atr * beTriggerATR
beOff = atr * beOffsetATR
if isLong and useBE
// if price moved in our favor enough -> raise stop to entry (+offset)
if (close - entry) >= beTrig
longSL := math.max(longSL, entry + beOff)
if isShort and useBE
if (entry - close) >= beTrig
shortSL := math.min(shortSL, entry - beOff)
// Submit exits every bar so they update dynamically
strategy.exit("XL", from_entry="L", stop=longSL, limit=longTP, when=isLong)
strategy.exit("XS", from_entry="S", stop=shortSL, limit=shortTP, when=isShort)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Visuals + Alerts
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(emaFast, "EMA Fast", linewidth=2)
plot(emaSlow, "EMA Slow", linewidth=2)
plot(stLine, "Supertrend", linewidth=2)
plot(useHTF ? htfEma : na, "HTF EMA", linewidth=2)
plotshape(longSignal, title="Long Signal", style=shape.triangleup, location=location.belowbar, size=size.tiny, text="L")
plotshape(shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, size=size.tiny, text="S")
alertcondition(longSignal, title="AlphaEdge Long", message="AlphaEdge LONG on {{ticker}} @ {{close}}")
alertcondition(shortSignal, title="AlphaEdge Short", message="AlphaEdge SHORT on {{ticker}} @ {{close}}")
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.