本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("EMA Cross · Locked TF", shorttitle = "EMA Zone", overlay = true)
// ───────── Inputs ─────────
tf = input.timeframe("1", "Lock to timeframe", tooltip = "The lines are calculated on this timeframe no matter which chart you're on. Leave blank to follow the chart.")
len1 = input.int(34, "EMA 1 length (fast)", minval = 1)
len2 = input.int(233, "EMA 2 length (slow)", minval = 1)
noRepaint = input.bool(true, "Higher timeframe: wait for its bar to close (no repaint)", tooltip = "Only matters when the locked timeframe is HIGHER than your chart (e.g. locked 5m on a 1m chart). On: lines only update when the 5m bar closes, so they never move after the fact.")
grpC = "Colors"
cLong = input.color(color.lime, "Aligned LONG (fast above slow)", group = grpC)
cShort = input.color(color.red, "Aligned SHORT (fast below slow)", group = grpC)
w1 = input.int(2, "Fast line width", minval = 1, maxval = 5, group = grpC)
w2 = input.int(3, "Slow line width", minval = 1, maxval = 5, group = grpC)
showFill = input.bool(true, "Fill between the lines", group = grpC)
fillTr = input.int(80, "Fill transparency (0 solid · 100 clear)", minval = 0, maxval = 100, group = grpC)
showX = input.bool(true, "Mark crosses", group = grpC)
// ───────── EMAs on the locked timeframe ─────────
tfUse = tf == "" ? timeframe.period : tf
higher = timeframe.in_seconds(tfUse) > timeframe.in_seconds(timeframe.period)
// live value (same or lower timeframe, or repaint allowed)
[aLive, bLive] = request.security(syminfo.tickerid, tfUse, [ta.ema(close, len1), ta.ema(close, len2)])
// last CLOSED higher-timeframe bar (no repaint)
[aHtf, bHtf] = request.security(syminfo.tickerid, tfUse, [ta.ema(close, len1)[1], ta.ema(close, len2)[1]], lookahead = barmerge.lookahead_on)
e1 = higher and noRepaint ? aHtf : aLive
e2 = higher and noRepaint ? bHtf : bLive
isLong = e1 > e2
cNow = isLong ? cLong : cShort
// ───────── Plots ─────────
p1 = plot(e1, "Fast EMA", color = cNow, linewidth = w1)
p2 = plot(e2, "Slow EMA", color = cNow, linewidth = w2)
fill(p1, p2, color = showFill ? color.new(cNow, fillTr) : na, title = "Fill")
crossUp = ta.crossover(e1, e2)
crossDn = ta.crossunder(e1, e2)
plotshape(showX and crossUp, "Cross up", style = shape.xcross, location = location.belowbar, color = cLong, size = size.small)
plotshape(showX and crossDn, "Cross down", style = shape.xcross, location = location.abovebar, color = cShort, size = size.small)
// ───────── Alerts ─────────
alertcondition(crossUp, "Aligned LONG (fast crosses above slow)", "EMA cross UP on {{ticker}} @ {{close}}")
alertcondition(crossDn, "Aligned SHORT (fast crosses below slow)", "EMA cross DOWN on {{ticker}} @ {{close}}")