Eye EMA Pulse Histogram [EyeAlgo]

eyealgo · study · 303 行 · 点赞 18 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EyeAlgo
//@version=6
indicator("Eye EMA Pulse Histogram [EyeAlgo]", "Eye EMA Pulse", overlay = false)
 
// ───────────────────────── Inputs ─────────────────────────
G1 = "5-EMA stack"
src    = input.source(close, "Source", group = G1)
len1   = input.int(7, "EMA 1 (fastest)", minval = 1, group = G1)
len2   = input.int(9, "EMA 2", minval = 1, group = G1)
len3   = input.int(12, "EMA 3", minval = 1, group = G1)
len4   = input.int(15, "EMA 4", minval = 1, group = G1)
len5   = input.int(21, "EMA 5 (slowest)", minval = 1, group = G1)
minGap = input.float(0.0, "Min gap between EMAs (ATR)", minval = 0.0, step = 0.01, group = G1, tooltip = "0 = histogram shows only when all 5 EMAs are in full order with no touch or cross. Raise it to demand a clear gap between every pair of neighbour EMAs.")
atrLen = input.int(50, "ATR length (normaliser)", minval = 5, group = G1, tooltip = "EMA 1 - EMA 5 gap is divided by this ATR, so the scale is the same on gold, forex, crypto and indices.")
 
G2 = "Strength zones (ATR units)"
lvlNormal  = input.float(0.25, "Normal from", minval = 0.0, step = 0.05, group = G2, tooltip = "Keep Normal < Strong < Extreme.")
lvlStrong  = input.float(1.0, "Strong from", minval = 0.1, step = 0.1, group = G2)
lvlExtreme = input.float(2.0, "Extreme from", minval = 0.2, step = 0.1, group = G2)
showZones  = input.bool(true, "Shade strong / extreme zones", group = G2)
 
G3 = "Higher-timeframe bias"
useHtf  = input.bool(true, "Use HTF EMA bias filter", group = G3)
htfTf   = input.timeframe("240", "HTF timeframe", group = G3)
htfLen  = input.int(50, "HTF EMA length", minval = 2, group = G3)
 
G4 = "Signals"
showStart = input.bool(true, "Stack start marks", group = G4)
showPB    = input.bool(true, "Pullback-continue signals", group = G4)
pbDepth   = input.float(0.5, "Pullback depth (part of leg peak)", minval = 0.1, maxval = 0.95, step = 0.05, group = G4, tooltip = "Gap must fall to this part of the leg's peak before a continue signal can fire. The signal fires only when all 5 EMAs are stacked again.")
cooldown  = input.int(5, "Cooldown bars", minval = 0, group = G4)
showExh   = input.bool(true, "Exhaustion dots (caution, not reversal)", group = G4)
 
G5 = "Signal stats"
statTgt  = input.float(1.0, "Target (ATR)", minval = 0.1, step = 0.1, group = G5)
statStop = input.float(1.0, "Stop (ATR)", minval = 0.1, step = 0.1, group = G5)
statBars = input.int(20, "Max bars to resolve", minval = 2, group = G5)
 
G6 = "Display"
showEma   = input.bool(true, "Show 5 EMAs on price chart", group = G6)
showCloud = input.bool(true, "GMMA-style ribbon fill between EMAs (only when stacked)", group = G6)
showHtfL  = input.bool(false, "Show HTF EMA on price chart", group = G6)
colorBars = input.bool(true, "Colour chart candles (only when stacked)", group = G6)
ribbonTr  = input.int(55, "Ribbon fill transparency", minval = 0, maxval = 95, group = G6)
histStyle = input.string("Neon Pulse", "Histogram style", options = ["Neon Pulse", "Classic"], group = G6, tooltip = "Neon Pulse = strength gradient + soft glow + edge line. Classic = plain 4-colour columns.")
showBeat  = input.bool(true, "Live heartbeat dot on last bar", group = G6, tooltip = "Blinks on every live tick while the market is open.")
showDash  = input.bool(true, "Show dashboard", group = G6)
dashPos   = input.string("Top Right", "Dashboard position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = G6)
dashSize  = input.string("Small", "Dashboard size", options = ["Tiny", "Small", "Normal"], group = G6)
cBullS = input.color(#089981, "Bull rising", group = G6)
cBullW = input.color(#8FD9CB, "Bull fading", group = G6)
cBearS = input.color(#F23645, "Bear falling", group = G6)
cBearW = input.color(#F7A6AE, "Bear fading", group = G6)
 
// ───────────────────────── 5-EMA stack ─────────────────────────
e1 = ta.ema(src, len1)
e2 = ta.ema(src, len2)
e3 = ta.ema(src, len3)
e4 = ta.ema(src, len4)
e5 = ta.ema(src, len5)
atrV = ta.atr(atrLen)
g    = minGap * atrV
 
bullStack = e1 - e2 > g and e2 - e3 > g and e3 - e4 > g and e4 - e5 > g
bearStack = e2 - e1 > g and e3 - e2 > g and e4 - e3 > g and e5 - e4 > g
stacked   = bullStack or bearStack
stackDir  = bullStack ? 1 : bearStack ? -1 : 0
 
// raw gap (used for leg tracking); histogram shows it only when stacked
raw   = atrV > 0 ? (e1 - e5) / atrV : 0.0
pulse = stacked ? raw : na
up    = raw > nz(raw[1])
col   = raw >= 0 ? (up ? cBullS : cBullW) : (up ? cBearW : cBearS)
absP  = math.abs(raw)
neon  = histStyle == "Neon Pulse"
tStr  = math.min(absP / lvlExtreme, 1.0)
nBase = raw >= 0 ? color.from_gradient(tStr, 0.0, 1.0, cBullW, cBullS) : color.from_gradient(tStr, 0.0, 1.0, cBearW, cBearS)
histCol = neon ? (up == (raw >= 0) ? nBase : color.new(nBase, 45)) : col
sideCol = raw >= 0 ? cBullS : cBearS
 
stateTxt = absP >= lvlExtreme ? "Extreme" : absP >= lvlStrong ? "Strong" : absP >= lvlNormal ? "Normal" : "Weak"
 
// ───────────────────────── HTF bias (no repaint) ─────────────────────────
htfValid = timeframe.in_seconds(htfTf) > timeframe.in_seconds()
htfEma   = request.security(syminfo.tickerid, htfTf, ta.ema(close, htfLen)[1], lookahead = barmerge.lookahead_on)
htfCls   = request.security(syminfo.tickerid, htfTf, close[1], lookahead = barmerge.lookahead_on)
htfBias  = not useHtf or not htfValid ? 0 : htfCls > htfEma ? 1 : htfCls < htfEma ? -1 : 0
okBull   = not useHtf or not htfValid or htfBias == 1
okBear   = not useHtf or not htfValid or htfBias == -1
 
// ───────────────────────── Leg / pullback logic ─────────────────────────
var float legPeak  = 0.0
var bool  legArmed = false
var bool  inPull   = false
var int   lastSig  = -100000
 
if math.sign(raw) != math.sign(nz(raw[1]))
    legPeak  := 0.0
    legArmed := false
    inPull   := false
 
legPeak := math.max(legPeak, absP)
if legPeak >= lvlStrong and stacked
    legArmed := true
if legArmed and absP <= legPeak * pbDepth
    inPull := true
 
turnUp   = raw > nz(raw[1]) and nz(raw[1]) <= nz(raw[2])
turnDown = raw < nz(raw[1]) and nz(raw[1]) >= nz(raw[2])
coolOk   = bar_index - lastSig > cooldown
 
// pullback-continue: deep pullback seen, momentum turns back AND all 5 EMAs stacked again
pbBull = showPB and barstate.isconfirmed and inPull and bullStack and raw > nz(raw[1]) and okBull and coolOk
pbBear = showPB and barstate.isconfirmed and inPull and bearStack and raw < nz(raw[1]) and okBear and coolOk
 
if pbBull or pbBear
    lastSig  := bar_index
    inPull   := false
    legArmed := false
    legPeak  := absP
 
startBull = showStart and barstate.isconfirmed and bullStack and nz(stackDir[1]) != 1
startBear = showStart and barstate.isconfirmed and bearStack and nz(stackDir[1]) != -1
exhBull   = showExh and barstate.isconfirmed and bullStack[1] and nz(raw[1]) >= lvlExtreme and turnDown
exhBear   = showExh and barstate.isconfirmed and bearStack[1] and nz(raw[1]) <= -lvlExtreme and turnUp
 
// stack age + last signal (for dashboard)
var int stackStartBar = na
if stacked and stackDir != nz(stackDir[1])
    stackStartBar := bar_index
stackAge = stacked and not na(stackStartBar) ? bar_index - stackStartBar + 1 : 0
 
var string lastSigTxt = "—"
var int    lastSigBar = na
var color  lastSigCol = na
if startBull
    lastSigTxt := "Stack ▲"
    lastSigBar := bar_index
    lastSigCol := cBullS
if startBear
    lastSigTxt := "Stack ▼"
    lastSigBar := bar_index
    lastSigCol := cBearS
if exhBull or exhBear
    lastSigTxt := "Exhaust ●"
    lastSigBar := bar_index
    lastSigCol := color.orange
if pbBull
    lastSigTxt := "PB ▲"
    lastSigBar := bar_index
    lastSigCol := cBullS
if pbBear
    lastSigTxt := "PB ▼"
    lastSigBar := bar_index
    lastSigCol := cBearS
 
// ───────────────────────── Signal stats (in-sample) ─────────────────────────
var array<int>   sDir = array.new<int>()
var array<float> sEnt = array.new<float>()
var array<float> sAtr = array.new<float>()
var array<int>   sBar = array.new<int>()
var int bWin = 0
var int bLoss = 0
var int sWin = 0
var int sLoss = 0
 
if barstate.isconfirmed and array.size(sDir) > 0
    for i = array.size(sDir) - 1 to 0
        d  = array.get(sDir, i)
        e  = array.get(sEnt, i)
        a  = array.get(sAtr, i)
        b0 = array.get(sBar, i)
        if bar_index > b0
            tp    = e + d * statTgt * a
            sl    = e - d * statStop * a
            hitTP = d == 1 ? high >= tp : low <= tp
            hitSL = d == 1 ? low <= sl : high >= sl
            done  = false
            if hitSL
                if d == 1
                    bLoss += 1
                else
                    sLoss += 1
                done := true
            else if hitTP
                if d == 1
                    bWin += 1
                else
                    sWin += 1
                done := true
            else if bar_index - b0 >= statBars
                done := true
            if done
                array.remove(sDir, i)
                array.remove(sEnt, i)
                array.remove(sAtr, i)
                array.remove(sBar, i)
 
if (pbBull or pbBear) and atrV > 0
    array.push(sDir, pbBull ? 1 : -1)
    array.push(sEnt, close)
    array.push(sAtr, atrV)
    array.push(sBar, bar_index)
 
// ───────────────────────── Plots (lower pane) ─────────────────────────
plot(neon ? pulse : na, "Glow", color.new(sideCol, 84), style = plot.style_areabr, display = display.pane)
plot(pulse, "Pulse", histCol, style = plot.style_columns)
plot(neon ? pulse : na, "Edge", color.new(sideCol, 15), 1, plot.style_linebr, display = display.pane)
hline(0, "Zero", color.new(color.gray, 30), hline.style_solid)
hSu   = hline(lvlStrong, "Strong +", color.new(color.gray, 60), hline.style_dotted)
hSd   = hline(-lvlStrong, "Strong -", color.new(color.gray, 60), hline.style_dotted)
hEu   = hline(lvlExtreme, "Extreme +", color.new(color.gray, 60), hline.style_dashed)
hEd   = hline(-lvlExtreme, "Extreme -", color.new(color.gray, 60), hline.style_dashed)
fill(hSu, hEu, showZones ? color.new(cBullS, 92) : na, "Bull strong zone")
fill(hSd, hEd, showZones ? color.new(cBearS, 92) : na, "Bear strong zone")
 
plotshape(pbBull, "Pullback Continue Bull", shape.triangleup, location.bottom, cBullS, text = "PB", textcolor = cBullS, size = size.tiny)
plotshape(pbBear, "Pullback Continue Bear", shape.triangledown, location.top, cBearS, text = "PB", textcolor = cBearS, size = size.tiny)
plotshape(exhBull ? raw : na, "Exhaustion Bull", shape.circle, location.absolute, color.orange, size = size.tiny)
plotshape(exhBear ? raw : na, "Exhaustion Bear", shape.circle, location.absolute, color.orange, size = size.tiny)
plotshape(startBull ? 0.0 : na, "Stack Start Bull", shape.diamond, location.absolute, cBullS, size = size.tiny)
plotshape(startBear ? 0.0 : na, "Stack Start Bear", shape.diamond, location.absolute, cBearS, size = size.tiny)
 
// ───────────────────────── Price chart ─────────────────────────
barcolor(colorBars and stacked ? col : na)
 
 
emaOn = showEma or showCloud
p1 = plot(emaOn ? e1 : na, "EMA 1", showEma ? #2962FF : na, 1, force_overlay = true)
p2 = plot(emaOn ? e2 : na, "EMA 2", showEma ? #00BCD4 : na, 1, force_overlay = true)
p3 = plot(emaOn ? e3 : na, "EMA 3", showEma ? #9C27B0 : na, 1, force_overlay = true)
p4 = plot(emaOn ? e4 : na, "EMA 4", showEma ? #FF9800 : na, 1, force_overlay = true)
p5 = plot(emaOn ? e5 : na, "EMA 5", showEma ? #F44336 : na, 2, force_overlay = true)
 
// GMMA-style ribbon: each band between neighbour EMAs filled, fast band strongest
rib = showCloud and stacked
fill(p1, p2, rib ? color.new(sideCol, ribbonTr) : na, "Ribbon 1-2")
fill(p2, p3, rib ? color.new(sideCol, math.min(ribbonTr + 8, 100)) : na, "Ribbon 2-3")
fill(p3, p4, rib ? color.new(sideCol, math.min(ribbonTr + 16, 100)) : na, "Ribbon 3-4")
fill(p4, p5, rib ? color.new(sideCol, math.min(ribbonTr + 24, 100)) : na, "Ribbon 4-5")
plot(showHtfL and useHtf and htfValid ? htfEma : na, "HTF EMA", #795548, 2, plot.style_stepline, force_overlay = true)
 
// ───────────────────────── Live heartbeat ─────────────────────────
varip int tick = 0
if barstate.isrealtime
    tick += 1
var label beat = label.new(bar_index, 0.0, "", style = label.style_label_center, color = color.new(color.black, 100))
if barstate.islast
    if showBeat
        on = tick % 2 == 0
        bc = stacked ? sideCol : color.gray
        label.set_xy(beat, bar_index, stacked ? raw : 0.0)
        label.set_text(beat, "●")
        label.set_textcolor(beat, on ? bc : color.new(bc, 65))
        label.set_size(beat, on ? size.normal : size.small)
    else
        label.set_text(beat, "")
 
// ───────────────────────── Dashboard ─────────────────────────
pos = dashPos == "Top Left" ? position.top_left : dashPos == "Bottom Right" ? position.bottom_right : dashPos == "Bottom Left" ? position.bottom_left : position.top_right
ts  = dashSize == "Tiny" ? size.tiny : dashSize == "Normal" ? size.normal : size.small
var table dash = table.new(pos, 2, 10, bgcolor = color.new(chart.bg_color, 4), border_color = color.new(chart.fg_color, 90), border_width = 1, frame_color = color.new(chart.fg_color, 75), frame_width = 1)
 
rateTxt(w, l) =>
    w + l > 0 ? str.tostring(100.0 * w / (w + l), "0") + "%" : "—"
 
dRow(int r, string k, string v, color vc, color rowBg) =>
    table.cell(dash, 0, r, k, text_color = color.new(chart.fg_color, 35), text_size = ts, text_halign = text.align_left, bgcolor = rowBg)
    table.cell(dash, 1, r, v, text_color = vc, text_size = ts, text_halign = text.align_right, bgcolor = rowBg)
 
if showDash and barstate.islast
    fg    = chart.fg_color
    zebra = color.new(fg, 96)
    stCol = bullStack ? cBullS : bearStack ? cBearS : color.new(fg, 30)
    pairs = raw >= 0 ? (e1 > e2 ? 1 : 0) + (e2 > e3 ? 1 : 0) + (e3 > e4 ? 1 : 0) + (e4 > e5 ? 1 : 0) : (e1 < e2 ? 1 : 0) + (e2 < e3 ? 1 : 0) + (e3 < e4 ? 1 : 0) + (e4 < e5 ? 1 : 0)
    order = str.repeat("●", pairs) + str.repeat("○", 4 - pairs) + "  " + str.tostring(pairs) + "/4"
    nBar  = math.round(tStr * 10)
    meter = str.repeat("▰", nBar) + str.repeat("▱", 10 - nBar)
    hdrBg = stacked ? color.new(stCol, 15) : color.new(fg, 85)
    hdrTx = stacked ? color.white : fg
    table.cell(dash, 0, 0, "◉ EYE EMA PULSE", text_color = hdrTx, text_size = ts, text_halign = text.align_left, bgcolor = hdrBg)
    table.cell(dash, 1, 0, syminfo.ticker + " · " + timeframe.period, text_color = hdrTx, text_size = ts, text_halign = text.align_right, bgcolor = hdrBg)
    dRow(1, "Stack", bullStack ? "▲ BULL STACKED" : bearStack ? "▼ BEAR STACKED" : "◇ NOT STACKED", stCol, na)
    dRow(2, "EMA order", order, stCol, zebra)
    dRow(3, "Strength", meter + "  " + str.tostring(raw, "0.00"), stacked ? histCol : color.new(fg, 40), na)
    dRow(4, "Level", stacked ? stateTxt : "—", stacked ? sideCol : color.new(fg, 40), zebra)
    dRow(5, "Momentum", not stacked ? "—" : up == (raw >= 0) ? "↑ Building" : "↓ Fading", not stacked ? color.new(fg, 40) : up == (raw >= 0) ? sideCol : color.new(fg, 25), na)
    dRow(6, "Stack age", stacked ? str.tostring(stackAge) + " bars" : "—", fg, zebra)
    dRow(7, "HTF bias (" + htfTf + ")", not useHtf ? "Off" : not htfValid ? "TF too low" : htfBias == 1 ? "▲ Bull" : htfBias == -1 ? "▼ Bear" : "Flat", htfBias == 1 ? cBullS : htfBias == -1 ? cBearS : fg, na)
    dRow(8, "Last signal", na(lastSigBar) ? "—" : lastSigTxt + " · " + str.tostring(bar_index - lastSigBar) + " bars ago", na(lastSigBar) ? fg : lastSigCol, zebra)
    dRow(9, "PB hit " + str.tostring(statTgt, "0.0") + ":" + str.tostring(statStop, "0.0") + " ATR", "▲ " + rateTxt(bWin, bLoss) + " (" + str.tostring(bWin + bLoss) + ")  ▼ " + rateTxt(sWin, sLoss) + " (" + str.tostring(sWin + sLoss) + ")", fg, na)
 
// ───────────────────────── Alerts ─────────────────────────
alertcondition(startBull, "Stack Start Bull", "Eye EMA Pulse: all 5 EMAs stacked bullish on {{ticker}} {{interval}}")
alertcondition(startBear, "Stack Start Bear", "Eye EMA Pulse: all 5 EMAs stacked bearish on {{ticker}} {{interval}}")
alertcondition(pbBull, "Pullback Continue Bull", "Eye EMA Pulse: bull pullback-continue on {{ticker}} {{interval}}")
alertcondition(pbBear, "Pullback Continue Bear", "Eye EMA Pulse: bear pullback-continue on {{ticker}} {{interval}}")
alertcondition(exhBull or exhBear, "Exhaustion", "Eye EMA Pulse: exhaustion (caution) on {{ticker}} {{interval}}")
 
if pbBull or pbBear or exhBull or exhBear or startBull or startBear
    msg = (pbBull ? "PB Continue BULL" : pbBear ? "PB Continue BEAR" : startBull ? "Stack start BULL" : startBear ? "Stack start BEAR" : exhBull ? "Bull exhaustion" : "Bear exhaustion") + " | " + syminfo.ticker + " " + timeframe.period + " | Pulse " + str.tostring(raw, "0.00") + " ATR | Close " + str.tostring(close, format.mintick)
    alert(msg, alert.freq_once_per_bar_close)

← 返回列表