Distribution Day Counter

vishalreddy247 · study · 86 行 · 点赞 6 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
 
//@version=6
indicator("Distribution Day Counter", shorttitle = "DD Count", overlay = true, max_labels_count = 500)
 
// ── Inputs ─────────────────────────────────────────────────────────────
string GRP_RULE = "Rules"
float minDrop  = input.float(0.2, "Minimum decline (%)", minval = 0.0, step = 0.05, group = GRP_RULE,
     tooltip = "A distribution day is a close at least this much below the prior close, on higher volume than the prior session.")
int   window   = input.int(25, "Count window (sessions)", minval = 5, group = GRP_RULE)
float rallyPct = input.float(5.0, "Drop after a rally of (%)", minval = 0.0, step = 0.5, group = GRP_RULE,
     tooltip = "A distribution day stops counting once price closes this far above that day's close. 0 disables this rule.")
int   warnAt   = input.int(5, "Warning level (count)", minval = 1, group = GRP_RULE)
 
string GRP_VOL = "Volume"
string volInput = input.symbol("", "Volume source (blank = auto)", group = GRP_VOL,
     tooltip = "Blank uses the chart's own volume. If the symbol reports no volume (some indexes), it falls back to SPY for the S&P 500, QQQ for the Nasdaq 100 and Composite, and NIFTYBEES for the Nifty 50.")
 
string GRP_SHOW = "Display"
bool showLabels = input.bool(true, "Mark distribution days", group = GRP_SHOW)
bool showTbl    = input.bool(true, "Show count table", group = GRP_SHOW)
 
// ── Volume source ──────────────────────────────────────────────────────
string tk       = syminfo.ticker
string proxySym = tk == "SPX" ? "AMEX:SPY" : tk == "NDX" or tk == "IXIC" ? "NASDAQ:QQQ" : tk == "NIFTY" ? "NSE:NIFTYBEES" : syminfo.tickerid
float  ownVol   = volume
float  proxyVol = request.security(proxySym, timeframe.period, volume, ignore_invalid_symbol = true)
float  custVol  = volInput != "" ? request.security(volInput, timeframe.period, volume, ignore_invalid_symbol = true) : na
bool   ownOk    = nz(ownVol) > 0
float  vol      = volInput != "" ? custVol : ownOk ? ownVol : proxyVol
string volSym   = volInput != "" ? volInput : ownOk ? syminfo.tickerid : proxySym
 
// ── Distribution days ──────────────────────────────────────────────────
bool isDaily = timeframe.isdaily
bool hasVol  = not na(vol) and not na(vol[1]) and vol[1] > 0
bool isDD    = isDaily and hasVol and close < close[1] * (1 - minDrop / 100) and vol > vol[1]
 
color ACTIVE  = #E53935
color EXPIRED = color.new(#9E9E9E, 40)
 
var array<int>   ddBars   = array.new<int>()
var array<float> ddCloses = array.new<float>()
var array<label> ddLabels = array.new<label>()
var int lastDDTime = na
 
int n = array.size(ddBars)
if n > 0
    for i = n - 1 to 0
        bool aged    = bar_index - array.get(ddBars, i) >= window
        bool rallied = rallyPct > 0 and close >= array.get(ddCloses, i) * (1 + rallyPct / 100)
        if aged or rallied
            label old = array.get(ddLabels, i)
            if not na(old)
                label.set_color(old, EXPIRED)
            array.remove(ddBars, i)
            array.remove(ddCloses, i)
            array.remove(ddLabels, i)
 
if isDD
    label lb = showLabels ? label.new(bar_index, na, "D", yloc = yloc.abovebar, style = label.style_label_down, color = ACTIVE, textcolor = color.white, size = size.tiny) : na
    array.push(ddBars, bar_index)
    array.push(ddCloses, close)
    array.push(ddLabels, lb)
    lastDDTime := time
 
int ddCount = array.size(ddBars)
 
// ── Outputs ────────────────────────────────────────────────────────────
plot(ddCount, "Distribution days", display = display.data_window)
plot(isDD ? 1 : 0, "Distribution day today (1/0)", display = display.data_window)
 
alertcondition(isDD, "New distribution day", "{{ticker}}: new distribution day")
alertcondition(ddCount >= warnAt and ddCount[1] < warnAt, "Warning level reached", "{{ticker}}: distribution-day count reached the warning level")
 
// ── Table ──────────────────────────────────────────────────────────────
var table tbl = table.new(position.top_right, 2, 3, bgcolor = color.new(color.gray, 85), border_width = 1)
 
if showTbl and barstate.islast
    color countColor = ddCount >= warnAt ? color.red : ddCount >= warnAt - 1 ? color.orange : color.teal
    string countText = not isDaily ? "Daily chart only" : na(vol) ? "No volume data" : str.tostring(ddCount)
    table.cell(tbl, 0, 0, "Distribution days (" + str.tostring(window) + ")", text_color = color.gray, text_size = size.small)
    table.cell(tbl, 1, 0, countText, text_color = isDaily and not na(vol) ? countColor : color.gray, text_size = size.small)
    table.cell(tbl, 0, 1, "Last", text_color = color.gray, text_size = size.small)
    table.cell(tbl, 1, 1, na(lastDDTime) ? "–" : str.format_time(lastDDTime, "dd MMM yyyy", syminfo.timezone), text_color = color.gray, text_size = size.small)
    table.cell(tbl, 0, 2, "Volume from", text_color = color.gray, text_size = size.small)
    table.cell(tbl, 1, 2, volSym, text_color = color.gray, text_size = size.small)

← 返回列表