MACD - Signal Statistics [Dots3Red]

Dots3Red · study · 188 行 · 点赞 340 · TradingView 原页

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

Pine Script

//@version=6
indicator("MACD - Signal Statistics [Dots3Red]",
          shorttitle = "MACD Stats [D3R]",
          overlay    = false,
          max_labels_count = 500)
 
// Inputs
C_TXT  = color.new(#f8fafc, 0)
C_DIM  = color.new(#94a3b8, 0)
C_BG   = color.new(#131722, 5)
C_BORD = color.new(#334155, 0)
C_GOOD = color.new(#00c896, 0)
C_BAD  = color.new(#ff4466, 0)
C_UP   = color.new(#00f0ff, 0)
C_DN   = color.new(#ff00aa, 0)
C_BAR     = color.new(#b2b5be,0)
 
 
GRP_MD  = "📊 MACD"
GRP_ENG = "🎯 Signal Grading"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
 
fast_len = input.int(12, "Fast Length", minval=1, group=GRP_MD)
slow_len = input.int(26, "Slow Length", minval=2, group=GRP_MD)
sig_len  = input.int(9, "Signal Length", minval=1, group=GRP_MD)
src      = input.source(close, "Source", group=GRP_MD)
 
atr_len = input.int(14, "ATR Length", minval=2, group=GRP_ENG)
tgt_atr = input.float(1.5, "Target (× ATR)", minval=0.2, step=0.1, group=GRP_ENG,
          tooltip="Worked = price moves this far from the signal bar's close in the signal direction before the stop.")
stp_atr = input.float(1.0, "Stop (× ATR)", minval=0.2, step=0.1, group=GRP_ENG,
          tooltip="Failed = price moves this far against the signal first.")
horizon = input.int(20, "Max Bars to Resolve", minval=3, maxval=200, group=GRP_ENG,
          tooltip="A signal that reaches neither level within this many bars counts as failed.")
min_n   = input.int(10, "Min Samples to Show Rate", minval=3, group=GRP_ENG)
 
col_macd  = input.color(#00f0ff, "MACD Line", group=GRP_VIS)
col_sig   = input.color(#ff00aa, "Signal Line", group=GRP_VIS)
w_line    = input.int(2, "Line Thickness", minval=1, maxval=6, group=GRP_VIS)
col_h_up1 = input.color(#00c896, "Histogram: Rising Above Zero", group=GRP_VIS)
col_h_up2 = input.color(#0b6b55, "Histogram: Falling Above Zero", group=GRP_VIS)
col_h_dn1 = input.color(#ff4466, "Histogram: Falling Below Zero", group=GRP_VIS)
col_h_dn2 = input.color(#7a2233, "Histogram: Rising Below Zero", group=GRP_VIS)
show_hist = input.bool(true, "Show Histogram", group=GRP_VIS)
lbl_mode  = input.string("Signal Crosses", "Price Chart Labels", options=["Signal Crosses", "Zero Crosses", "All", "Off"], group=GRP_VIS)
col_bull  = input.color(#00f0ff, "Bullish Label Color", group=GRP_VIS)
col_bear  = input.color(#ff00aa, "Bearish Label Color", group=GRP_VIS)
use_bars  = input.bool(false, "Use Gray Bars", group=GRP_VIS)
 
 
show_hud = input.bool(true, "Show Dashboard (on price chart)", group=GRP_HUD)
hud_pos  = input.string("Top Right", "Position",
           options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
 
[macd, signal, hist] = ta.macd(src, fast_len, slow_len, sig_len)
float atr = ta.atr(atr_len)
 
bool sx_up = ta.crossover(macd, signal)
bool sx_dn = ta.crossunder(macd, signal)
bool zx_up = ta.crossover(macd, 0)
bool zx_dn = ta.crossunder(macd, 0)
 
var int[]   p_bar = array.new<int>(6, -1)
var float[] p_tgt = array.new<float>(6, na)
var float[] p_stp = array.new<float>(6, na)
var float[] st_n  = array.new<float>(6, 0.0)
var float[] st_h  = array.new<float>(6, 0.0)
 
string[] names = array.from("▲ Cross Below Zero", "▲ Cross Above Zero", "▼ Cross Above Zero", "▼ Cross Below Zero", "▲ Zero Line Cross", "▼ Zero Line Cross")
 
f_bull(int i) => i == 0 or i == 1 or i == 4
 
f_rate(int i) =>
    float n = array.get(st_n, i)
    n >= min_n ? array.get(st_h, i) / n * 100.0 : na
 
int s_slot = sx_up ? (macd < 0 ? 0 : 1) : sx_dn ? (macd > 0 ? 2 : 3) : -1
int z_slot = zx_up ? 4 : zx_dn ? 5 : -1
 
f_register(int i) =>
    if array.get(p_bar, i) < 0
        bool bull = f_bull(i)
        array.set(p_bar, i, bar_index)
        array.set(p_tgt, i, bull ? close + tgt_atr * atr : close - tgt_atr * atr)
        array.set(p_stp, i, bull ? close - stp_atr * atr : close + stp_atr * atr)
 
if barstate.isconfirmed and not na(atr)
    for i = 0 to 5
        int pb = array.get(p_bar, i)
        if pb >= 0 and bar_index > pb
            bool  bull  = f_bull(i)
            float tg    = array.get(p_tgt, i)
            float sp    = array.get(p_stp, i)
            bool  hit_t = bull ? high >= tg : low <= tg
            bool  hit_s = bull ? low <= sp  : high >= sp
            bool  timed = bar_index - pb >= horizon
            if hit_t or hit_s or timed
                array.set(st_n, i, array.get(st_n, i) + 1.0)
                if hit_t and not hit_s
                    array.set(st_h, i, array.get(st_h, i) + 1.0)
                array.set(p_bar, i, -1)
    if s_slot >= 0
        f_register(s_slot)
    if z_slot >= 0
        f_register(z_slot)
 
// Dashboard
f_hud_pos(string s) =>
    switch s
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Bottom Right" => position.bottom_right
        =>                position.bottom_left
 
var table hud = table.new(f_hud_pos(hud_pos), 3, 10,
                          bgcolor=C_BG, border_color=C_BORD,
                          border_width=1, frame_color=C_BORD, frame_width=2,
                          force_overlay=true)
 
f_row(int row, int i, color bg) =>
    float n = array.get(st_n, i)
    float r = f_rate(i)
    table.cell(hud, 0, row, array.get(names, i), text_color = f_bull(i) ? C_UP : C_DN, bgcolor=bg, text_size=size.small)
    table.cell(hud, 1, row, str.tostring(int(n)), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, row, na(r) ? "Collecting…" : str.tostring(math.round(r)) + "%",
               text_color = na(r) ? C_DIM : r >= 55 ? C_GOOD : r <= 45 ? C_BAD : C_TXT,
               bgcolor=bg, text_size=size.small, text_halign=text.align_center)
 
if show_hud and barstate.islast
    color bgA = color.new(#1e293b, 40)
    color bgB = color.new(#0f172a, 40)
 
    table.cell(hud, 0, 0, "MACD STATS [D3R]", text_color=C_TXT,
               bgcolor=color.new(#1e293b, 0), text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 0, 0, 2, 0)
 
    table.cell(hud, 0, 1, "Signal", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 1, "Graded", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.cell(hud, 2, 1, "Worked", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
               tooltip="Share of graded signals where price moved " + str.tostring(tgt_atr) + "× ATR in the signal direction before moving " + str.tostring(stp_atr) + "× ATR against it, within " + str.tostring(horizon) + " bars.")
 
    for i = 0 to 5
        f_row(i + 2, i, i % 2 == 0 ? bgA : bgB)
 
    table.cell(hud, 0, 8, "MACD Now", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 8, (macd > signal ? "Above signal" : "Below signal") + " · " + (macd > 0 ? "above zero" : "below zero"),
               text_color = macd > signal ? C_UP : C_DN, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 8, 2, 8)
 
    table.cell(hud, 0, 9, "Target / Stop / Window", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 9, str.tostring(tgt_atr) + " / " + str.tostring(stp_atr) + " ATR · " + str.tostring(horizon) + " bars",
               text_color=C_TXT, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 9, 2, 9)
 
// Visual
color h_col = hist >= 0 ? (hist >= nz(hist[1]) ? col_h_up1 : col_h_up2) : (hist <= nz(hist[1]) ? col_h_dn1 : col_h_dn2)
plot(hist, "Histogram", style=plot.style_columns, color=h_col, display = show_hist ? display.all : display.none)
plot(macd, "MACD", color=col_macd, linewidth=w_line)
plot(signal, "Signal", color=col_sig, linewidth=w_line)
hline(0, "Zero", color=color.new(C_DIM, 50), linestyle=hline.style_dashed)
 
bool lbl_s = (lbl_mode == "Signal Crosses" or lbl_mode == "All") and s_slot >= 0
bool lbl_z = (lbl_mode == "Zero Crosses" or lbl_mode == "All") and z_slot >= 0
 
f_lbl(int i) =>
    bool  bull = f_bull(i)
    float r    = f_rate(i)
    string tag = i >= 4 ? (bull ? "Zero ▲" : "Zero ▼") : (bull ? "MACD ▲" : "MACD ▼")
    label.new(bar_index, bull ? low : high, tag + (na(r) ? "" : " " + str.tostring(math.round(r)) + "%"),
              style     = bull ? label.style_label_up : label.style_label_down,
              color     = color.new(#000000, 100),
              textcolor = bull ? col_bull : col_bear,
              size      = size.small,
              force_overlay = true)
 
if barstate.isconfirmed and lbl_s
    f_lbl(s_slot)
if barstate.isconfirmed and lbl_z and not lbl_s
    f_lbl(z_slot)
 
barcolor( use_bars ? C_BAR: na)
 
// Alert
alertcondition(sx_up, "MACD Bullish Cross", "D3R MACD Stats: MACD crossed above signal on {{ticker}}")
alertcondition(sx_dn, "MACD Bearish Cross", "D3R MACD Stats: MACD crossed below signal on {{ticker}}")
alertcondition(zx_up, "MACD Above Zero",    "D3R MACD Stats: MACD crossed above zero on {{ticker}}")
alertcondition(zx_dn, "MACD Below Zero",    "D3R MACD Stats: MACD crossed below zero on {{ticker}}")

← 返回列表