Ichimoku Cloud Indicator [Dots3Red]

Dots3Red · study · 205 行 · 点赞 279 · TradingView 原页

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

Pine Script

//@version=6
indicator("Ichimoku Cloud - Signal Statistics [Dots3Red]",
          shorttitle = "Ichimoku Stats [D3R]",
          overlay    = true,
          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_IC  = "☁️ Ichimoku"
GRP_ENG = "🎯 Signal Grading"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
 
tk_len = input.int(9, "Tenkan-sen Length", minval=1, group=GRP_IC)
kj_len = input.int(26, "Kijun-sen Length", minval=1, group=GRP_IC)
sb_len = input.int(52, "Senkou Span B Length", minval=1, group=GRP_IC)
disp   = input.int(26, "Displacement", minval=1, group=GRP_IC)
 
atr_len = input.int(14, "ATR Length", minval=2, group=GRP_ENG)
tgt_atr = input.float(2.0, "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(30, "Max Bars to Resolve", minval=3, maxval=300, 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_tk    = input.color(#00f0ff, "Tenkan-sen", group=GRP_VIS)
col_kj    = input.color(#ff00aa, "Kijun-sen", group=GRP_VIS)
w_line    = input.int(1, "Line Thickness", minval=1, maxval=6, group=GRP_VIS)
col_cup   = input.color(#00c896, "Bullish Cloud", group=GRP_VIS)
col_cdn   = input.color(#ff4466, "Bearish Cloud", group=GRP_VIS)
cloud_tr  = input.int(82, "Cloud Transparency", minval=40, maxval=98, group=GRP_VIS)
show_chk  = input.bool(true, "Show Chikou Span", group=GRP_VIS)
col_chk   = input.color(#94a3b8, "Chikou Span", group=GRP_VIS)
lbl_mode  = input.string("Strong TK + Kumo Breaks", "Labels", options=["Strong TK + Kumo Breaks", "All Signals", "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)
lbl_gap   = input.int(8, "Min Bars Between Labels", minval=1, maxval=100, group=GRP_VIS,
          tooltip="Skips a new label if another one was drawn within this many bars. Signals are still graded and counted.")
use_bars  = input.bool(false, "Use Gray Bars", group=GRP_VIS)
 
 
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos  = input.string("Top Right", "Position",
           options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
 
f_mid(int len) => math.avg(ta.lowest(len), ta.highest(len))
 
float tenkan = f_mid(tk_len)
float kijun  = f_mid(kj_len)
float span_a = math.avg(tenkan, kijun)
float span_b = f_mid(sb_len)
float atr    = ta.atr(atr_len)
 
float a_now = span_a[disp - 1]
float b_now = span_b[disp - 1]
float c_top = math.max(a_now, b_now)
float c_bot = math.min(a_now, b_now)
 
bool tk_up = ta.crossover(tenkan, kijun)
bool tk_dn = ta.crossunder(tenkan, kijun)
bool kb_up = not na(c_top) and close > c_top and close[1] <= c_top[1]
bool kb_dn = not na(c_bot) and close < c_bot and close[1] >= c_bot[1]
 
int tk_slot = tk_up ? (close > c_top ? 0 : 1) : tk_dn ? (close < c_bot ? 2 : 3) : -1
int kb_slot = kb_up ? 4 : kb_dn ? 5 : -1
 
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("▲ TK Cross Above Cloud", "▲ TK Cross Inside/Below", "▼ TK Cross Below Cloud", "▼ TK Cross Inside/Above", "▲ Kumo Breakout", "▼ Kumo Breakdown")
 
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
 
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) and not na(c_top)
    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 tk_slot >= 0
        f_register(tk_slot)
    if kb_slot >= 0
        f_register(kb_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)
 
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, "ICHIMOKU 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)
 
    string pos = na(c_top) ? "—" : close > c_top ? "Above cloud" : close < c_bot ? "Below cloud" : "Inside cloud"
    table.cell(hud, 0, 8, "Price Now", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
    table.cell(hud, 1, 8, pos + " · " + (tenkan > kijun ? "TK bullish" : "TK bearish"),
               text_color = close > c_top ? C_UP : close < c_bot ? C_DN : C_TXT, 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
plot(tenkan, "Tenkan-sen", color=col_tk, linewidth=w_line)
plot(kijun,  "Kijun-sen",  color=col_kj, linewidth=w_line)
plot(show_chk ? close : na, "Chikou Span", color=col_chk, linewidth=1, offset = -(disp - 1))
 
p_a = plot(span_a, "Senkou Span A", color=color.new(col_cup, 50), offset = disp - 1)
p_b = plot(span_b, "Senkou Span B", color=color.new(col_cdn, 50), offset = disp - 1)
fill(p_a, p_b, color = span_a > span_b ? color.new(col_cup, cloud_tr) : color.new(col_cdn, cloud_tr), title="Kumo")
 
bool lbl_all = lbl_mode == "All Signals"
bool lbl_key = lbl_mode == "Strong TK + Kumo Breaks"
 
f_lbl(int i) =>
    bool  bull = f_bull(i)
    float r    = f_rate(i)
    string tag = i >= 4 ? (bull ? "Kumo ▲" : "Kumo ▼") : (bull ? "TK ▲" : "TK ▼")
    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)
 
var int last_lbl = -1000000
 
int lbl_slot = kb_slot >= 0 and (lbl_all or lbl_key) ? kb_slot : tk_slot >= 0 and (lbl_all or (lbl_key and (tk_slot == 0 or tk_slot == 2))) ? tk_slot : -1
 
if barstate.isconfirmed and not na(c_top) and lbl_slot >= 0 and bar_index - last_lbl >= lbl_gap
    f_lbl(lbl_slot)
    last_lbl := bar_index
 
barcolor( use_bars ? C_BAR: na)
 
 
// Alert
alertcondition(tk_up and close > c_top, "Strong Bullish TK Cross", "D3R Ichimoku Stats: bullish TK cross above the cloud on {{ticker}}")
alertcondition(tk_dn and close < c_bot, "Strong Bearish TK Cross", "D3R Ichimoku Stats: bearish TK cross below the cloud on {{ticker}}")
alertcondition(kb_up, "Kumo Breakout",   "D3R Ichimoku Stats: close broke above the cloud on {{ticker}}")
alertcondition(kb_dn, "Kumo Breakdown",  "D3R Ichimoku Stats: close broke below the cloud on {{ticker}}")

← 返回列表