本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// [DAOL] Trend Participation v1.0 - original implementation for this request.
// No Community script source copied. Underlying methods are established concepts.
// Linear regression reference:
// https://www.tradingview.com/support/solutions/43000599877-least-squares-moving-average/
// HHI reference (sum of squared shares; adapted here to PRICE increments):
// https://www.justice.gov/atr/herfindahl-hirschman-index
// We use fractional shares (0..1), not the DOJ's 0..100 percentage convention.
// Effective bars = 1/HHI = sum(weights)^2 / sum(weights^2).
// Participation concerns price bars, NOT traders, volume, or market breadth.
indicator("[DAOL] Trend Participation", shorttitle="DAOL Part", overlay=false, max_bars_back=1000)
int length = input.int(20, "Close changes in window", minval=5, maxval=250, group="Calculation")
int atrLength = input.int(14, "ATR length", minval=2, maxval=250, group="Calculation")
float minSpan = input.float(0.5, "Minimum fitted span / prior ATR", minval=0.01, maxval=10, step=0.05, group="State thresholds")
float minSupport = input.float(60, "Minimum supporting bars (%)", minval=0, maxval=100, step=5, group="State thresholds")
float minEffective = input.float(30, "Minimum effective bars / window (%)", minval=0, maxval=100, step=5, group="State thresholds")
int confirmation = input.int(2, "Consecutive closed bars", minval=1, maxval=10, group="Confirmation")
bool showDashboard = input.bool(true, "Show dashboard", group="Display")
bool showBackground = input.bool(false, "Shade confirmed state", group="Display")
if barstate.isfirst and not chart.is_standard
runtime.error("Use a standard OHLC/candlestick chart.")
// N increments require N+1 closes. Both regression offsets use the SAME window.
float regressionEnd = ta.linreg(close, length + 1, 0)
float regressionPreviousPoint = ta.linreg(close, length + 1, 1)
float slope = regressionEnd - regressionPreviousPoint
float priorATR = ta.atr(atrLength)[1]
bool ready = bar_index >= length and not na(slope) and not na(priorATR) and priorATR > 0
float spanATR = ready ? slope * length / priorATR : na
int direction = ready ? (spanATR >= minSpan ? 1 : spanATR <= -minSpan ? -1 : 0) : 0
int supportingCount = 0
float weightSum = 0.0
float squareSum = 0.0
if ready and direction != 0
for i = 0 to length - 1
float weight = math.max(direction * (close[i] - close[i + 1]), 0.0)
if weight > 0
supportingCount += 1
weightSum += weight
squareSum += weight * weight
float supportPct = ready ? 100.0 * supportingCount / length : na
float hhi = squareSum > 0 ? squareSum / (weightSum * weightSum) : na
float effectiveBars = squareSum > 0 ? math.min(float(supportingCount), weightSum * weightSum / squareSum) : ready ? 0.0 : na
float effectivePct = ready ? 100.0 * effectiveBars / length : na
// Score: signed equivalent-bar share. No slope magnitude multiplier.
// Directionless windows get zero; unavailable data remain na.
float score = ready ? direction * effectivePct : na
// 1 distributed up, -1 distributed down, 2 concentrated up,
// -2 concentrated down, 0 unclear/low support.
int rawState = 0
if ready and direction != 0 and supportPct >= minSupport and weightSum > 0
rawState := effectivePct >= minEffective ? direction : 2 * direction
var int candidate = 0
var int streak = 0
var int confirmedState = 0
var bool initialized = false
var float closedScore = na
var float closedSupport = na
var float closedEffective = na
var float closedCount = na
var float closedHHI = na
var float closedSpan = na
var int closedRaw = 0
bool changed = false
if barstate.isconfirmed
closedScore := score
closedSupport := supportPct
closedEffective := effectiveBars
closedCount := supportingCount
closedHHI := hhi
closedSpan := spanATR
closedRaw := rawState
if not ready
streak := 0
initialized := false
confirmedState := 0
else
if streak == 0 or candidate != rawState
candidate := rawState
streak := 1
else
streak := math.min(streak + 1, confirmation)
if streak >= confirmation
if not initialized
confirmedState := candidate
initialized := true
else if candidate != confirmedState
confirmedState := candidate
changed := true
f_color(int state) =>
state == 1 ? color.lime : state == -1 ? color.red : math.abs(state) == 2 ? color.rgb(212, 175, 55) : color.gray
f_state(int state) =>
state == 1 ? "Distributed up" : state == -1 ? "Distributed down" : state == 2 ? "Concentrated up" : state == -2 ? "Concentrated down" : "Unclear / low support"
f_number(float value) =>
na(value) ? "--" : str.tostring(value, "#.00")
color stateColor = initialized ? f_color(confirmedState) : color.gray
plot(closedScore, "Signed effective-bar share (-100 to 100)", color=stateColor, style=plot.style_columns, linewidth=2)
hline(0, "Zero", color=color.gray)
hline(100, "Upper bound", color=color.new(color.gray, 80))
hline(-100, "Lower bound", color=color.new(color.gray, 80))
plot(closedSupport, "Supporting bars (%)", display=display.data_window)
plot(closedEffective, "Effective number of bars", display=display.data_window)
plot(closedHHI, "HHI (fractional shares)", display=display.data_window)
plot(closedSpan, "Signed fitted span / prior ATR", display=display.data_window)
bgcolor(showBackground and initialized ? color.new(stateColor, 90) : na)
var table dashboard = table.new(position.top_right, 2, 8, border_width=1)
if barstate.islast and showDashboard
table.cell(dashboard, 0, 0, "DAOL Participation", bgcolor=color.rgb(19, 62, 103), text_color=color.white)
table.cell(dashboard, 1, 0, "CLOSED-BAR VALUES", bgcolor=color.rgb(19, 62, 103), text_color=color.white)
table.cell(dashboard, 0, 1, "Confirmed state")
table.cell(dashboard, 1, 1, initialized ? f_state(confirmedState) : "Building / confirming", bgcolor=color.new(stateColor, 75))
table.cell(dashboard, 0, 2, "Latest raw state")
table.cell(dashboard, 1, 2, na(closedScore) ? "--" : f_state(closedRaw))
table.cell(dashboard, 0, 3, "Fitted span / prior ATR")
table.cell(dashboard, 1, 3, f_number(closedSpan))
table.cell(dashboard, 0, 4, "Supporting bars")
table.cell(dashboard, 1, 4, f_number(closedCount) + " / " + str.tostring(length) + " (" + f_number(closedSupport) + "%)")
table.cell(dashboard, 0, 5, "Effective bars")
table.cell(dashboard, 1, 5, f_number(closedEffective))
table.cell(dashboard, 0, 6, "HHI (fractional)")
table.cell(dashboard, 1, 6, f_number(closedHHI))
table.cell(dashboard, 0, 7, "Signed score")
table.cell(dashboard, 1, 7, f_number(closedScore))
alertcondition(changed and confirmedState == 1, "DAOL: Distributed up", "[DAOL] Trend Participation: confirmed distributed-up state. {{ticker}} {{interval}}")
alertcondition(changed and confirmedState == -1, "DAOL: Distributed down", "[DAOL] Trend Participation: confirmed distributed-down state. {{ticker}} {{interval}}")
alertcondition(changed and math.abs(confirmedState) == 2, "DAOL: Concentrated movement", "[DAOL] Trend Participation: confirmed concentrated movement. {{ticker}} {{interval}}")
alertcondition(changed and confirmedState == 0, "DAOL: Unclear or low support", "[DAOL] Trend Participation: confirmed unclear direction or low support. {{ticker}} {{interval}}")
alertcondition(changed, "DAOL: Any confirmed state change", "[DAOL] Trend Participation: confirmed state changed. {{ticker}} {{interval}}")
// Defaults: 20 changes / 21 closes; span >=0.5 prior ATR; support >=60%;
// effective bars >=30% of N for distributed state; confirm 2 consecutive closes.
// If direction is unclear, weights are not assigned and score/support are zero;
// HHI remains undefined. Zero changes count in N but are not supporting bars.
// Opposite changes affect slope/support, but do not enter the HHI weights.
// HHI measures concentration of supporting increments, not trend certainty.
// Score magnitude =100*effectiveBars/N; sign follows regression direction.
// Historical bars show THEIR confirmed state; first state initializes silently.
// Metrics update each close while color may retain the preceding confirmed state.
// Gold requires sufficient supporting-bar count and low effective-bar share.
// No volume, participant identification, breadth, strategy or future data used.
// Rolling-window exits, gaps and continuous-futures rolls can change the metrics.
// Set alerts Once Per Bar Close; recreate after changing code/inputs.
// Numerical checks passed; TradingView compilation/market validation pending.
// Educational use; no buy/sell recommendation or return guarantee.