Fixed EMAs (Any Chart)

JovanniA · study · 162 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
indicator("Fixed 5min EMAs (Any Chart)", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ============================================================================
//  FIXED 5-MINUTE EMAs — displays your 5m EMAs on ANY timeframe
//  Companion to the Fixed 1D EMA indicator. Same request.security() approach.
//  Pulls 5-minute 10/20/50/200 EMAs and draws them as fixed levels so you can
//  read your 5m entry EMAs even when zoomed out to 15m, 1H, 1D, etc.
// ============================================================================
 
// ---------------------------------------------------------------------------
//  INPUTS
// ---------------------------------------------------------------------------
grpEMA  = "EMA Settings"
srcTF   = input.timeframe("5", "Source Timeframe", tooltip="EMAs are calculated on this timeframe and shown on your current chart. Default = 5 minutes.", group=grpEMA)
src     = input.source(close, "Source", group=grpEMA)
 
show10  = input.bool(true,  "Show 10 EMA",  inline="e10", group=grpEMA)
len10   = input.int(10,     "",             inline="e10", minval=1, group=grpEMA)
col10   = input.color(color.new(#26a69a, 0), "", inline="e10", group=grpEMA)
 
show20  = input.bool(true,  "Show 20 EMA",  inline="e20", group=grpEMA)
len20   = input.int(20,     "",             inline="e20", minval=1, group=grpEMA)
col20   = input.color(color.new(#42a5f5, 0), "", inline="e20", group=grpEMA)
 
show50  = input.bool(true,  "Show 50 EMA",  inline="e50", group=grpEMA)
len50   = input.int(50,     "",             inline="e50", minval=1, group=grpEMA)
col50   = input.color(color.new(#ffb300, 0), "", inline="e50", group=grpEMA)
 
show200 = input.bool(true,  "Show 200 EMA", inline="e200", group=grpEMA)
len200  = input.int(200,    "",             inline="e200", minval=1, group=grpEMA)
col200  = input.color(color.new(#ef5350, 0), "", inline="e200", group=grpEMA)
 
grpStyle = "Display"
lineWidth = input.int(2, "Line Width", minval=1, maxval=5, group=grpStyle)
lineStyleInput = input.string("Solid", "Line Style", options=["Solid", "Dashed", "Dotted"], group=grpStyle)
showLabels = input.bool(true, "Show Price Labels", group=grpStyle)
labelOffset = input.int(15, "Label Offset (bars)", minval=0, group=grpStyle)
extendLines = input.bool(true, "Extend Lines Left", group=grpStyle)
 
grpDash = "Dashboard"
showDash = input.bool(true, "Show EMA Dashboard", group=grpDash)
dashPos  = input.string("Top Right", "Position", options=["Top Right","Top Left","Bottom Right","Bottom Left","Middle Right"], group=grpDash)
 
// ---------------------------------------------------------------------------
//  MULTI-TIMEFRAME EMA DATA (pull 5m EMAs onto any chart)
// ---------------------------------------------------------------------------
f_ema(length) => ta.ema(src, length)
 
ema10  = request.security(syminfo.tickerid, srcTF, f_ema(len10),  barmerge.gaps_off, barmerge.lookahead_off)
ema20  = request.security(syminfo.tickerid, srcTF, f_ema(len20),  barmerge.gaps_off, barmerge.lookahead_off)
ema50  = request.security(syminfo.tickerid, srcTF, f_ema(len50),  barmerge.gaps_off, barmerge.lookahead_off)
ema200 = request.security(syminfo.tickerid, srcTF, f_ema(len200), barmerge.gaps_off, barmerge.lookahead_off)
 
// ---------------------------------------------------------------------------
//  HELPERS
// ---------------------------------------------------------------------------
lineStyle = lineStyleInput == "Dashed" ? line.style_dashed : lineStyleInput == "Dotted" ? line.style_dotted : line.style_solid
 
var line ln10  = na
var line ln20  = na
var line ln50  = na
var line ln200 = na
var label lb10  = na
var label lb20  = na
var label lb50  = na
var label lb200 = na
 
f_draw(_show, _val, _col, _txt, ln, lb) =>
    line _ln = ln
    label _lb = lb
    if _show and not na(_val)
        _left  = extendLines ? bar_index - 500 : bar_index - 1
        _right = bar_index + labelOffset
        if na(_ln)
            _ln := line.new(_left, _val, _right, _val, xloc=xloc.bar_index, color=_col, width=lineWidth, style=lineStyle)
        else
            line.set_xy1(_ln, _left, _val)
            line.set_xy2(_ln, _right, _val)
            line.set_color(_ln, _col)
            line.set_width(_ln, lineWidth)
            line.set_style(_ln, lineStyle)
        if showLabels
            _lbtxt = _txt + "  " + str.tostring(_val, format.mintick)
            if na(_lb)
                _lb := label.new(_right, _val, _lbtxt, xloc=xloc.bar_index, style=label.style_label_left, color=color.new(_col, 85), textcolor=_col, size=size.small)
            else
                label.set_xy(_lb, _right, _val)
                label.set_text(_lb, _lbtxt)
                label.set_textcolor(_lb, _col)
                label.set_color(_lb, color.new(_col, 85))
        else if not na(_lb)
            label.delete(_lb)
            _lb := na
    else
        if not na(_ln)
            line.delete(_ln)
            _ln := na
        if not na(_lb)
            label.delete(_lb)
            _lb := na
    [_ln, _lb]
 
// draw only on the last bar for performance
if barstate.islast
    [r10a, r10b]   = f_draw(show10,  ema10,  col10,  str.tostring(len10)  + " EMA", ln10,  lb10)
    [r20a, r20b]   = f_draw(show20,  ema20,  col20,  str.tostring(len20)  + " EMA", ln20,  lb20)
    [r50a, r50b]   = f_draw(show50,  ema50,  col50,  str.tostring(len50)  + " EMA", ln50,  lb50)
    [r200a, r200b] = f_draw(show200, ema200, col200, str.tostring(len200) + " EMA", ln200, lb200)
    ln10  := r10a
    lb10  := r10b
    ln20  := r20a
    lb20  := r20b
    ln50  := r50a
    lb50  := r50b
    ln200 := r200a
    lb200 := r200b
 
// ---------------------------------------------------------------------------
//  DASHBOARD (bias read at a glance — mirrors your 1D EMA dashboard)
// ---------------------------------------------------------------------------
f_pos(_p) =>
    _p == "Top Right" ? position.top_right : _p == "Top Left" ? position.top_left : _p == "Bottom Right" ? position.bottom_right : _p == "Bottom Left" ? position.bottom_left : position.middle_right
 
var table dash = na
if showDash and barstate.islast
    if not na(dash)
        table.delete(dash)
    dash := table.new(f_pos(dashPos), 2, 6, bgcolor=color.new(#1e222d, 10), border_color=color.new(#363a45, 0), border_width=1, frame_color=color.new(#363a45, 0), frame_width=1)
 
    table.cell(dash, 0, 0, "5m EMA", text_color=color.white, text_size=size.normal, bgcolor=color.new(#2962ff, 0))
    table.cell(dash, 1, 0, srcTF + "m Source", text_color=color.white, text_size=size.normal, bgcolor=color.new(#2962ff, 0))
 
    _price = close
    if show10
        table.cell(dash, 0, 1, str.tostring(len10) + " EMA", text_color=col10, text_size=size.small, text_halign=text.align_left)
        table.cell(dash, 1, 1, str.tostring(ema10, format.mintick) + (na(ema10) ? "" : (_price > ema10 ? "  ▲" : "  ▼")), text_color=col10, text_size=size.small)
    if show20
        table.cell(dash, 0, 2, str.tostring(len20) + " EMA", text_color=col20, text_size=size.small, text_halign=text.align_left)
        table.cell(dash, 1, 2, str.tostring(ema20, format.mintick) + (na(ema20) ? "" : (_price > ema20 ? "  ▲" : "  ▼")), text_color=col20, text_size=size.small)
    if show50
        table.cell(dash, 0, 3, str.tostring(len50) + " EMA", text_color=col50, text_size=size.small, text_halign=text.align_left)
        table.cell(dash, 1, 3, str.tostring(ema50, format.mintick) + (na(ema50) ? "" : (_price > ema50 ? "  ▲" : "  ▼")), text_color=col50, text_size=size.small)
    if show200
        table.cell(dash, 0, 4, str.tostring(len200) + " EMA", text_color=col200, text_size=size.small, text_halign=text.align_left)
        table.cell(dash, 1, 4, str.tostring(ema200, format.mintick) + (na(ema200) ? "" : (_price > ema200 ? "  ▲" : "  ▼")), text_color=col200, text_size=size.small)
 
    // stacked bias row: bullish when 10>20>50
    _bias = na(ema10) or na(ema20) or na(ema50) ? "—" : (ema10 > ema20 and ema20 > ema50 ? "BULL STACK" : ema10 < ema20 and ema20 < ema50 ? "BEAR STACK" : "MIXED")
    _biasCol = _bias == "BULL STACK" ? color.new(#26a69a, 0) : _bias == "BEAR STACK" ? color.new(#ef5350, 0) : color.new(#787b86, 0)
    table.cell(dash, 0, 5, "Bias", text_color=color.white, text_size=size.small, text_halign=text.align_left)
    table.cell(dash, 1, 5, _bias, text_color=color.white, bgcolor=_biasCol, text_size=size.small)
 
// ---------------------------------------------------------------------------
//  ALERTS — 5m EMA reclaim / rejection on current chart
// ---------------------------------------------------------------------------
alertcondition(ta.crossover(close, ema10),   "Close crossed above 5m 10 EMA",  "Price reclaimed the 5m 10 EMA")
alertcondition(ta.crossunder(close, ema10),  "Close crossed below 5m 10 EMA",  "Price lost the 5m 10 EMA")
alertcondition(ta.crossover(close, ema20),   "Close crossed above 5m 20 EMA",  "Price reclaimed the 5m 20 EMA")
alertcondition(ta.crossunder(close, ema20),  "Close crossed below 5m 20 EMA",  "Price lost the 5m 20 EMA")
alertcondition(ta.crossover(close, ema50),   "Close crossed above 5m 50 EMA",  "Price reclaimed the 5m 50 EMA")
alertcondition(ta.crossunder(close, ema50),  "Close crossed below 5m 50 EMA",  "Price lost the 5m 50 EMA")

← 返回列表