本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("EMA 30/50/100 Long Entries + MTF Trend", overlay=true)
// ── Inputs ──
lookback = input.int(10, "Breakout lookback (bars)", minval=1)
useHtf = input.bool(true, "Use higher timeframe filter", group="Trend filters")
useCur = input.bool(false, "Use current timeframe filter (price above rising 100 EMA)", group="Trend filters")
allowNoHist = input.bool(true, "Pass HTF filter if not enough history (new listings)", group="Trend filters")
slopeLen = input.int(5, "Slope lookback for current TF filter", minval=1, group="Trend filters")
htfInput = input.string("Auto", "Higher timeframe", options=["Auto", "5", "15", "60", "D", "W", "M"], group="Trend filters")
// ── Auto higher timeframe (tuned for Indian equities) ──
// 15m -> 1h | 1h -> Daily | Daily -> Weekly | Weekly -> Monthly
sec = timeframe.in_seconds()
autoHtf = sec <= 60 ? "5" : sec <= 300 ? "15" : sec <= 900 ? "60" : sec <= 14400 ? "D" : sec <= 86400 ? "W" : "M"
htf = htfInput == "Auto" ? autoHtf : htfInput
// ── EMAs ──
e30 = ta.ema(close, 30)
e50 = ta.ema(close, 50)
e100 = ta.ema(close, 100)
plot(e30, "EMA 30", color=color.aqua, linewidth=2)
plot(e50, "EMA 50", color=color.orange, linewidth=2)
plot(e100, "EMA 100", color=color.red, linewidth=2)
// ── Current timeframe filter (optional): price above a rising 100 EMA ──
trendCur = close > e100 and e100 > e100[slopeLen]
// ── Higher timeframe filter (fast): close > HTF 30 EMA and HTF 30 EMA > HTF 50 EMA ──
// Uses the last COMPLETED higher timeframe bar, so it never repaints
[hClose, hE30, hE50] = request.security(syminfo.tickerid, htf,
[close[1], ta.ema(close, 30)[1], ta.ema(close, 50)[1]],
lookahead=barmerge.lookahead_on)
hasHtf = not na(hE50)
trendHtf = hasHtf ? (hClose > hE30 and hE30 > hE50) : allowNoHist
trendOK = (not useHtf or trendHtf) and (not useCur or trendCur)
// ── Condition 1: 30 above both 50 & 100, with 50 above 100 ──
aligned = e30 > e50 and e50 > e100
setupTrigger = aligned and not aligned[1]
var bool setupActive = false
var bool breakoutDone = false
var bool retestDone = false
if setupTrigger
setupActive := true
breakoutDone := false
retestDone := false
if not aligned
setupActive := false
// ── Entry 2a: breakout of previous high ──
prevHigh = ta.highest(high, lookback)[1]
breakoutEntry = trendOK and setupActive and not setupTrigger and not breakoutDone and close > prevHigh
// ── Entry 2b: retest of any EMA, then break higher ──
touchedEma = (low <= e30 and close > e30) or (low <= e50 and close > e50) or (low <= e100 and close > e100)
retestBar = setupActive and touchedEma
retestEntry = trendOK and retestBar[1] and not setupTrigger[1] and setupActive and not retestDone and close > high[1]
if breakoutEntry
breakoutDone := true
if retestEntry
retestDone := true
// ── Setup booleans (for alerts / screening) ──
setupAny = setupTrigger
setupWithTrend = setupTrigger and trendOK
// ── Visuals ──
bgcolor(setupActive and trendOK ? color.new(color.green, 92) : na)
plotshape(setupTrigger, "Setup", shape.diamond, location.belowbar, color.yellow, size=size.tiny)
plotshape(breakoutEntry, "Breakout", shape.labelup, location.belowbar, color.green, text="BO", textcolor=color.white, size=size.small)
plotshape(retestEntry, "Retest", shape.labelup, location.belowbar, color.blue, text="RT", textcolor=color.white, size=size.small)
// ── Hidden plots (1 = true, 0 = false) for Pine Screener / Data Window ──
plot(setupAny ? 1 : 0, "Setup (1/0)", display=display.data_window)
plot(setupWithTrend ? 1 : 0, "Setup + Trend OK (1/0)", display=display.data_window)
plot(setupActive ? 1 : 0, "Setup Active (1/0)", display=display.data_window)
// ── Trend status table ──
var table t = table.new(position.top_right, 2, 3, border_width=1)
if barstate.islast
table.cell(t, 0, 0, "Current TF", text_color=color.white, bgcolor=color.gray)
table.cell(t, 1, 0, not useCur ? "Off" : trendCur ? "Bullish" : "Not bullish", text_color=color.white, bgcolor=not useCur ? color.gray : trendCur ? color.green : color.red)
table.cell(t, 0, 1, "HTF (" + htf + ")", text_color=color.white, bgcolor=color.gray)
table.cell(t, 1, 1, not useHtf ? "Off" : not hasHtf ? "No history" : trendHtf ? "Bullish" : "Not bullish", text_color=color.white, bgcolor=not useHtf ? color.gray : trendHtf ? color.green : color.red)
table.cell(t, 0, 2, "Entries", text_color=color.white, bgcolor=color.gray)
table.cell(t, 1, 2, trendOK ? "Enabled" : "Blocked", text_color=color.white, bgcolor=trendOK ? color.green : color.red)
// ── Alerts ──
alertcondition(setupAny, "Setup Happened", "{{ticker}}: EMA Setup formed ({{interval}})")
alertcondition(setupWithTrend, "Setup Happened + Trend OK", "{{ticker}}: EMA Setup formed with bullish higher timeframe ({{interval}})")
alertcondition(breakoutEntry, "Long Entry - Breakout", "{{ticker}}: Breakout entry ({{interval}})")
alertcondition(retestEntry, "Long Entry - Retest", "{{ticker}}: Retest entry ({{interval}})")