本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Indicatr
//
// VOLUME FLOW CONFIRM
// Puts volume flow (OBV, Accumulation/Distribution or Chaikin Money Flow) and price on the same
// normalised scale and shows whether volume confirms the move: confirmed up, confirmed down,
// a rally without volume, or a decline with hidden buying. Relative volume spikes are marked.
indicator("Volume Flow Confirm [Indicatr]", "Vol Flow", overlay = false, precision = 2, max_labels_count = 200)
// ════════════════════════════════════════════════════════════════════════════════════
// INPUTS
// ════════════════════════════════════════════════════════════════════════════════════
G_F = "1 · Flow"
i_src = input.string("OBV", "Volume flow", options = ["OBV", "Accumulation/Distribution", "Chaikin Money Flow"], group = G_F, tooltip = "OBV: volume added on up closes, subtracted on down closes.\n\nAccumulation/Distribution: volume weighted by where the close sits in the bar's range.\n\nChaikin Money Flow: the same weighting summed over a window.")
i_len = input.int(50, "Normalisation length", minval = 10, maxval = 500, group = G_F, tooltip = "Flow and price are both expressed as their distance from their own moving average, in standard deviations over this window. That makes them comparable with each other and across markets.")
i_smooth = input.int(5, "Smoothing", minval = 1, maxval = 50, group = G_F)
i_thr = input.float(0.25, "Neutral zone (±)", minval = 0.0, maxval = 2.0, step = 0.05, group = G_F, tooltip = "Readings inside this band count as neutral, so small wiggles around zero do not flip the state.")
G_V = "2 · Relative volume"
i_rvLen = input.int(20, "RVOL length", minval = 5, maxval = 200, group = G_V)
i_rvThr = input.float(2.0, "Spike threshold (× average)", minval = 1.2, maxval = 10.0, step = 0.1, group = G_V)
G_S = "3 · Display"
i_showPx = input.bool(true, "Show price line", group = G_S)
i_bgDiv = input.bool(true, "Shade disagreement", group = G_S)
i_barCol = input.bool(false, "Colour main-chart candles by state", group = G_S)
i_colUp = input.color(#089981, "Up", inline = "c", group = G_S)
i_colDn = input.color(#f23645, "Down", inline = "c", group = G_S)
i_colWrn = input.color(#ff9800, "Warning", inline = "c", group = G_S)
i_colAcc = input.color(#7e57c2, "Hidden buying", inline = "c", group = G_S)
G_D = "4 · Dashboard"
i_dbShow = input.bool(true, "Show dashboard", group = G_D)
i_theme = input.string("Auto", "Theme", options = ["Auto", "Light", "Dark"], group = G_D)
i_dbPos = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = G_D)
i_dbSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = G_D)
// ════════════════════════════════════════════════════════════════════════════════════
// THEME
// ════════════════════════════════════════════════════════════════════════════════════
float bgLum = (0.299 * color.r(chart.bg_color) + 0.587 * color.g(chart.bg_color) + 0.114 * color.b(chart.bg_color)) / 255.0
bool DARK = i_theme == "Dark" or (i_theme == "Auto" and bgLum < 0.5)
color C_BG = DARK ? #1e1e28 : #ffffff
color C_HEAD = DARK ? #2a2a3c : #f0f3fa
color C_SECT = DARK ? #24243a : #f8f9fd
color C_BORDER = DARK ? #34344a : #e6e8ef
color C_FRAME = DARK ? #4a4a60 : #d1d4dc
color C_TXT = DARK ? #e9e9f0 : #131722
color C_DIM = DARK ? #9a9cab : #787b86
color C_GREY = DARK ? #5d606b : #b2b5be
f_size(string s) =>
switch s
"Tiny" => size.tiny
"Normal" => size.normal
=> size.small
// ════════════════════════════════════════════════════════════════════════════════════
// CALCULATION
// ════════════════════════════════════════════════════════════════════════════════════
float vol = nz(volume)
float hl = high - low
float mfm = hl > 0 ? ((close - low) - (high - close)) / hl : 0.0
float obv = ta.cum(math.sign(ta.change(close)) * vol)
float ad = ta.cum(mfm * vol)
float cmf = math.sum(vol, i_len) > 0 ? math.sum(mfm * vol, i_len) / math.sum(vol, i_len) : 0.0
// distance from the own moving average in standard deviations of that distance
f_norm(float x) =>
float dev = x - ta.ema(x, i_len)
float sd = ta.stdev(dev, i_len)
sd > 0 ? dev / sd : 0.0
float flowRaw = switch i_src
"Accumulation/Distribution" => f_norm(ad)
"Chaikin Money Flow" => f_norm(cmf)
=> f_norm(obv)
float flow = ta.ema(flowRaw, i_smooth)
float price = ta.ema(f_norm(close), i_smooth)
int fDir = flow > i_thr ? 1 : flow < -i_thr ? -1 : 0
int pDir = price > i_thr ? 1 : price < -i_thr ? -1 : 0
// 1 confirmed up · -1 confirmed down · 2 rally without volume · -2 decline with hidden buying · 0 neutral
int state = fDir == 1 and pDir == 1 ? 1 : fDir == -1 and pDir == -1 ? -1 : pDir == 1 and fDir <= 0 ? 2 : pDir == -1 and fDir >= 0 ? -2 : 0
float rvol = ta.sma(vol, i_rvLen) > 0 ? vol / ta.sma(vol, i_rvLen) : na
bool spike = rvol >= i_rvThr
bool noVol = ta.cum(vol) == 0
// ════════════════════════════════════════════════════════════════════════════════════
// PLOTS
// ════════════════════════════════════════════════════════════════════════════════════
color stateCol = switch state
1 => i_colUp
-1 => i_colDn
2 => i_colWrn
-2 => i_colAcc
=> C_GREY
hline(0, "Zero", color = color.new(C_DIM, 50), linestyle = hline.style_solid)
hline(2, "+2", color = color.new(C_DIM, 75), linestyle = hline.style_dotted)
hline(-2, "-2", color = color.new(C_DIM, 75), linestyle = hline.style_dotted)
pZero = plot(0, "Zero (fill)", color = na, display = display.none, editable = false)
color fCol = flow >= 0 ? i_colUp : i_colDn
// glow
plot(flow, "Flow glow wide", color = color.new(fCol, 90), linewidth = 10, editable = false, display = display.pane)
plot(flow, "Flow glow", color = color.new(fCol, 75), linewidth = 5, editable = false, display = display.pane)
pFlow = plot(flow, "Volume flow", color = fCol, linewidth = 2)
// gradient between the flow line and zero: strong at the line, transparent at zero
fill(pFlow, pZero, flow, 0, color.new(fCol, 55), color.new(fCol, 100), title = "Flow gradient")
plot(i_showPx ? price : na, "Price (normalised)", color = color.new(C_TXT, 45), linewidth = 1, style = plot.style_line, display = display.all - display.price_scale)
// disagreement between price and volume
bgcolor(i_bgDiv and state == 2 ? color.new(i_colWrn, 88) : i_bgDiv and state == -2 ? color.new(i_colAcc, 88) : na, title = "Disagreement")
// relative volume spikes on the zero line, coloured by candle direction
plotshape(spike and not noVol ? 0.0 : na, "RVOL spike", shape.circle, location.absolute, close >= open ? i_colUp : i_colDn, size = size.tiny, display = display.pane)
barcolor(i_barCol ? stateCol : na, title = "State candles")
// ════════════════════════════════════════════════════════════════════════════════════
// DASHBOARD
// ════════════════════════════════════════════════════════════════════════════════════
f_stateTxt(int s) =>
switch s
1 => "CONFIRMED UP"
-1 => "CONFIRMED DOWN"
2 => "RALLY WITHOUT VOLUME"
-2 => "HIDDEN BUYING"
=> "NEUTRAL"
f_signed(float v) => (v > 0 ? "+" : "") + str.tostring(v, "0.00")
var int lastSpikeBar = na
if spike and not noVol
lastSpikeBar := bar_index
// share of bars in each state over the normalisation window
var int[] stBuf = array.new_int()
array.push(stBuf, state)
if array.size(stBuf) > i_len
array.shift(stBuf)
var table db = table.new(i_dbPos == "Top left" ? position.top_left : i_dbPos == "Bottom right" ? position.bottom_right : i_dbPos == "Bottom left" ? position.bottom_left : position.top_right, 2, 9, bgcolor = C_BG, frame_color = C_FRAME, frame_width = 1, border_color = C_BORDER, border_width = 1)
if barstate.islast and i_dbShow
string sz = f_size(i_dbSize)
table.clear(db, 0, 0, 1, 8)
table.cell(db, 0, 0, "VOLUME FLOW CONFIRM", text_color = C_TXT, bgcolor = C_HEAD, text_size = sz)
table.merge_cells(db, 0, 0, 1, 0)
if noVol
table.cell(db, 0, 1, "No volume data for this symbol", text_color = C_DIM, bgcolor = C_BG, text_size = sz)
table.merge_cells(db, 0, 1, 1, 1)
else
table.cell(db, 0, 1, f_stateTxt(state), text_color = state == 0 ? C_DIM : color.white, bgcolor = state == 0 ? C_SECT : stateCol, text_size = sz)
table.merge_cells(db, 0, 1, 1, 1)
table.cell(db, 0, 2, "Flow (" + i_src + ")", text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.cell(db, 1, 2, f_signed(flow) + " σ", text_color = flow >= 0 ? i_colUp : i_colDn, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
table.cell(db, 0, 3, "Price", text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.cell(db, 1, 3, f_signed(price) + " σ", text_color = price >= 0 ? i_colUp : i_colDn, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
table.cell(db, 0, 4, "Relative volume", text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.cell(db, 1, 4, str.tostring(rvol, "0.0") + "×", text_color = spike ? C_TXT : C_DIM, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
table.cell(db, 0, 5, "Last spike", text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.cell(db, 1, 5, na(lastSpikeBar) ? "—" : str.tostring(bar_index - lastSpikeBar) + " bars ago", text_color = C_TXT, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
int nb = array.size(stBuf)
int nConf = 0
int nDis = 0
for s in stBuf
nConf += math.abs(s) == 1 ? 1 : 0
nDis += math.abs(s) == 2 ? 1 : 0
table.cell(db, 0, 6, "Confirmed · last " + str.tostring(nb), text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.cell(db, 1, 6, nb > 0 ? str.tostring(math.round(nConf / nb * 100)) + "%" : "—", text_color = C_TXT, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
table.cell(db, 0, 7, "Disagreement · last " + str.tostring(nb), text_color = C_DIM, bgcolor = C_BG, text_halign = text.align_left, text_size = sz)
table.cell(db, 1, 7, nb > 0 ? str.tostring(math.round(nDis / nb * 100)) + "%" : "—", text_color = nDis > nConf ? i_colWrn : C_TXT, bgcolor = C_BG, text_halign = text.align_right, text_size = sz)
// ════════════════════════════════════════════════════════════════════════════════════
// ALERTS
// ════════════════════════════════════════════════════════════════════════════════════
alertcondition(state == 1 and state[1] != 1, "Confirmed up", "Volume Flow Confirm: price and volume flow both up on {{ticker}}")
alertcondition(state == -1 and state[1] != -1, "Confirmed down", "Volume Flow Confirm: price and volume flow both down on {{ticker}}")
alertcondition(state == 2 and state[1] != 2, "Rally without volume", "Volume Flow Confirm: price rising without volume flow on {{ticker}}")
alertcondition(state == -2 and state[1] != -2, "Hidden buying", "Volume Flow Confirm: price falling while volume flow holds up on {{ticker}}")
alertcondition(ta.crossover(flow, 0), "Flow crosses above zero", "Volume Flow Confirm: volume flow turned positive on {{ticker}}")
alertcondition(ta.crossunder(flow, 0), "Flow crosses below zero", "Volume Flow Confirm: volume flow turned negative on {{ticker}}")
alertcondition(spike and not noVol, "Relative volume spike", "Volume Flow Confirm: relative volume spike on {{ticker}}")