WaveTrend Oscillator - Cross Statistics [Dots3Red]

Dots3Red · study · 192 行 · 点赞 384 · TradingView 原页

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

Pine Script

//@version=6
indicator("WaveTrend Oscillator - Cross Statistics [Dots3Red]",
          shorttitle = "WaveTrend 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)
 
GRP_WT  = "🌊 WaveTrend"
GRP_ENG = "🎯 Cross Grading"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
 
n1   = input.int(10, "Channel Length", minval=2, group=GRP_WT)
n2   = input.int(21, "Average Length", minval=2, group=GRP_WT)
ob1  = input.int(60, "Overbought Level 1", group=GRP_WT)
ob2  = input.int(53, "Overbought Level 2", group=GRP_WT,
       tooltip="A bearish cross with the signal line at or above this level counts as an overbought cross.")
os1  = input.int(-60, "Oversold Level 1", group=GRP_WT)
os2  = input.int(-53, "Oversold Level 2", group=GRP_WT,
       tooltip="A bullish cross with the signal line at or below this level counts as an oversold cross.")
 
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 cross bar's close in the cross 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 cross direction first.")
horizon = input.int(20, "Max Bars to Resolve", minval=3, maxval=200, group=GRP_ENG,
          tooltip="A cross 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_wt1   = input.color(#00f0ff, "WT Line Color", group=GRP_VIS)
col_wt2   = input.color(#ff00aa, "Signal Line Color", group=GRP_VIS)
w_wt      = input.int(2, "Line Thickness", minval=1, maxval=6, group=GRP_VIS)
show_hist = input.bool(true, "Show Histogram (WT − Signal)", group=GRP_VIS)
hist_tr   = input.int(60, "Histogram Transparency", minval=0, maxval=95, group=GRP_VIS)
col_lvl   = input.color(#64748b, "Level Lines Color", group=GRP_VIS)
show_dots = input.bool(true, "Cross Dots", group=GRP_VIS)
lbl_mode  = input.string("Zone Crosses", "Price Chart Labels", options=["Zone Crosses", "All Crosses", "Off"], group=GRP_VIS,
            tooltip="Zone Crosses: label only oversold bullish and overbought bearish crosses on the price chart.")
col_bull  = input.color(#00f0ff, "Bullish Label Color", group=GRP_VIS)
col_bear  = input.color(#ff00aa, "Bearish Label Color", 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)
 
float ap  = hlc3
float esa = ta.ema(ap, n1)
float d   = ta.ema(math.abs(ap - esa), n1)
float ci  = d != 0 ? (ap - esa) / (0.015 * d) : 0.0
float wt1 = ta.ema(ci, n2)
float wt2 = ta.sma(wt1, 4)
float atr = ta.atr(atr_len)
 
bool x_up = ta.crossover(wt1, wt2)
bool x_dn = ta.crossunder(wt1, wt2)
 
int slot = x_up ? (wt2 <= os2 ? 0 : 2) : x_dn ? (wt2 >= ob2 ? 1 : 3) : -1
 
var int[]   p_bar = array.new<int>(4, -1)
var float[] p_tgt = array.new<float>(4, na)
var float[] p_stp = array.new<float>(4, na)
var float[] st_n  = array.new<float>(4, 0.0)
var float[] st_h  = array.new<float>(4, 0.0)
 
string[] names = array.from("▲ Oversold Cross", "▼ Overbought Cross", "▲ Other Bull Cross", "▼ Other Bear Cross")
 
f_rate(int i) =>
    float n = array.get(st_n, i)
    n >= min_n ? array.get(st_h, i) / n * 100.0 : na
 
bool new_sig = false
 
if barstate.isconfirmed and not na(atr)
    for i = 0 to 3
        int pb = array.get(p_bar, i)
        if pb >= 0 and bar_index > pb
            bool  bull  = i == 0 or i == 2
            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 slot >= 0
        new_sig := true
        bool bull = slot == 0 or slot == 2
        if array.get(p_bar, slot) < 0
            array.set(p_bar, slot, bar_index)
            array.set(p_tgt, slot, bull ? close + tgt_atr * atr : close - tgt_atr * atr)
            array.set(p_stp, slot, bull ? close - stp_atr * atr : close + stp_atr * atr)
 
// 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, 8,
                          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 = (i == 0 or i == 2) ? 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, "WAVETREND 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 crosses where price moved " + str.tostring(tgt_atr) + "× ATR in the cross direction before moving " + str.tostring(stp_atr) + "× ATR against it, within " + str.tostring(horizon) + " bars.")
 
    f_row(2, 0, bgA)
    f_row(3, 1, bgB)
    f_row(4, 2, bgA)
    f_row(5, 3, bgB)
 
    string zone = wt2 >= ob2 ? "Overbought" : wt2 <= os2 ? "Oversold" : "Neutral"
    table.cell(hud, 0, 6, "WaveTrend Now", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 6, str.tostring(wt1, "#.#") + " · " + zone,
               text_color = wt2 >= ob2 ? C_DN : wt2 <= os2 ? C_UP : C_TXT, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
    table.merge_cells(hud, 1, 6, 2, 6)
 
    table.cell(hud, 0, 7, "Target / Stop / Window", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
    table.cell(hud, 1, 7, 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, 7, 2, 7)
 
// Visual
hline(ob1, "Overbought 1", color=col_lvl, linestyle=hline.style_solid)
hline(ob2, "Overbought 2", color=col_lvl, linestyle=hline.style_dotted)
hline(0,   "Zero",         color=col_lvl, linestyle=hline.style_dashed)
hline(os2, "Oversold 2",   color=col_lvl, linestyle=hline.style_dotted)
hline(os1, "Oversold 1",   color=col_lvl, linestyle=hline.style_solid)
 
float hist = wt1 - wt2
plot(hist, "Histogram", style=plot.style_area,
     color = hist >= 0 ? color.new(col_wt1, hist_tr) : color.new(col_wt2, hist_tr),
     display = show_hist ? display.all : display.none)
plot(wt1, "WT", color=col_wt1, linewidth=w_wt)
plot(wt2, "Signal", color=col_wt2, linewidth=w_wt)
 
plot(show_dots and x_up ? wt2 : na, "Bull Cross", color=col_bull, style=plot.style_circles, linewidth=4)
plot(show_dots and x_dn ? wt2 : na, "Bear Cross", color=col_bear, style=plot.style_circles, linewidth=4)
 
bool lbl_on = lbl_mode == "All Crosses" or (lbl_mode == "Zone Crosses" and (slot == 0 or slot == 1))
 
if new_sig and lbl_on
    bool  bull = slot == 0 or slot == 2
    float r    = f_rate(slot)
    string t   = (slot == 0 ? "OS ▲" : slot == 1 ? "OB ▼" : bull ? "▲" : "▼") + (na(r) ? "" : " " + str.tostring(math.round(r)) + "%")
    label.new(bar_index, bull ? low : high, t,
              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)
 
// Alert
alertcondition(x_up and wt2 <= os2, "Oversold Bullish Cross",   "D3R WaveTrend: bullish cross in oversold on {{ticker}}")
alertcondition(x_dn and wt2 >= ob2, "Overbought Bearish Cross", "D3R WaveTrend: bearish cross in overbought on {{ticker}}")
alertcondition(x_up, "Any Bullish Cross", "D3R WaveTrend: bullish cross on {{ticker}}")
alertcondition(x_dn, "Any Bearish Cross", "D3R WaveTrend: bearish cross on {{ticker}}")

← 返回列表