Volume Multiplier & Dynamic OBV Filter [EGK]

cazadorman · study · 260 行 · 点赞 15 · TradingView 原页

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

Pine Script

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
// ====================================================================================================
// 🚀Volume Multiplier & Dynamic OBV Filter [EGK] - Multi-Market Edition
// ====================================================================================================
 
//@version=6
 
indicator('Volume Multiplier & Dynamic OBV Filter [EGK]', shorttitle = 'OBV', overlay = false)
 
// ─────────────── 0. MARKET PRESET SELECTION ───────────────
groupMarket = '0. Market Selection'
 
marketPreset = input.string('Crypto', 'Select Market Type', options = ['Crypto', 'Stock (Hisse)', 'Forex', 'Commodities (Emtia)', 'Custom'], tooltip = 'Select your target market to automatically optimize liquidity and volume thresholds.\nSelect \'Custom\' to manually override all parameters below.', group = groupMarket)
 
// ─────────────── GENERAL SETTINGS ───────────────
lengthMA = input.int(20, 'MA Length', group = 'General Settings')
 
// ─────────────── VOLUME SETTINGS ───────────────
groupVolume = 'Volume Bars Settings'
 
showMA = input.bool(false, 'Show 20 MA Line?', group = groupVolume)
volScale = input.float(defval = 1.5, title = 'Volume Scale', minval = 0.1, step = 0.1, tooltip = 'Multiplies the height of volume bars. 1.0 = normal height, 2.0 = double height.', group = groupVolume)
 
// ─────────────── AUTOMATED THRESHOLD SETTINGS ───────────────
groupFilter = 'Automated Volume Filter Settings (Active in \'Custom\' Mode)'
 
usdFilterModeInput = input.string('Auto (Dynamic)', 'USD Volume Filter Mode', options = ['Auto (Dynamic)', 'Manual', 'Disabled'], tooltip = 'Auto: Dynamically adjusts threshold based on N-period average liquidity.\nManual: Uses the fixed USD volume threshold specified below.\nDisabled: Disables dollar volume filtering (recommended for Spot FX).', group = groupFilter)
 
liquidityPeriodInput = input.int(50, 'Auto: Lookback Period (N)', inline = 'auto_set', group = groupFilter)
liquidityMultInput = input.float(1.0, 'Auto: Multiplier', step = 0.1, inline = 'auto_set', group = groupFilter)
 
minUsdVolInput = input.float(1000000, 'Manual: Min USD Vol ($)', step = 100000, tooltip = 'Active only when \'Manual\' mode is selected.', group = groupFilter)
 
useRvolFilterInput = input.bool(true, 'Enable Relative Volatility Filter (RVOL)?', tooltip = 'Filters out fake volume spikes during off-peak or low-volatility trading hours.', group = groupFilter)
rvolLengthInput = input.int(50, 'RVOL Lookback Period', group = groupFilter)
minRvolRatioInput = input.float(0.8, 'Min RVOL Ratio Threshold', step = 0.1, tooltip = '1.0 = Current volume must equal or exceed 50 MA. 0.8 = Reaching 80% of 50 MA is sufficient.', group = groupFilter)
 
// Visual Warning & Dimming Options
enableDimming = input.bool(true, 'Dim Colors for Low-Liquidity Bars?', tooltip = 'Grays out/dims the volume bar colors if the USD volume threshold is not met.', group = groupFilter)
 
// ─────────────── DASHBOARD / INFO BOX SETTINGS ───────────────
groupDash = 'Info Dashboard Settings'
showDash = input.bool(true, 'Show Liquidity Info Box?', tooltip = 'Displays an on-screen HUD showing active filtering mode, real-time volume comparisons, and liquidity validation status.', group = groupDash)
dashPosition = input.string('top_right', 'Dashboard Position', options = ['top_right', 'top_left', 'bottom_right', 'bottom_left'], group = groupDash)
 
// ─────────────── TEXT SETTINGS ───────────────
groupText = 'Volume Multiplier Text Settings'
 
showVolText = input.bool(true, 'Show Volume Multiplier Text?', group = groupText)
minTextRatio = input.float(1.0, 'Minimum Multiplier to Show Text (e.g. 1.0)', minval = 0.1, step = 0.1, group = groupText)
textSize = input.string('small', 'Text Size', options = ['none', 'tiny', 'small', 'normal'], group = groupText)
 
getTextSize(sizeStr) =>
    switch sizeStr
        'tiny' => 'tiny'
        'small' => 'small'
        'normal' => 'normal'
        => 'small'
 
// ─────────────── VOLUME MULTIPLIER COLORS ───────────────
groupColors = 'Volume Multiplier Colors'
 
col_2_5_up = input.color(color.rgb(0, 255, 255), '2.5x+ (Bullish Exceptional)', group = groupColors)
col_2_0_up = input.color(color.rgb(0, 230, 118), '2.0x - 2.5x (Bullish Very High)', group = groupColors)
col_1_5_up = input.color(color.rgb(76, 175, 80), '1.5x - 2.0x (Bullish High)', group = groupColors)
col_1_0_up = input.color(color.rgb(129, 199, 132), '1.0x - 1.5x (Bullish Above Avg)', group = groupColors)
col_sub_up = input.color(color.rgb(38, 166, 154, 50), '0x - 1.0x (Bullish Low)', group = groupColors)
 
col_2_5_dn = input.color(color.rgb(255, 0, 128), '2.5x+ (Bearish Exceptional)', group = groupColors)
col_2_0_dn = input.color(color.rgb(255, 23, 68), '2.0x - 2.5x (Bearish Very High)', group = groupColors)
col_1_5_dn = input.color(color.rgb(229, 57, 53), '1.5x - 2.0x (Bearish High)', group = groupColors)
col_1_0_dn = input.color(color.rgb(229, 115, 115), '1.0x - 1.5x (Bearish Above Avg)', group = groupColors)
col_sub_dn = input.color(color.rgb(239, 83, 80, 50), '0x - 1.0x (Bearish Low)', group = groupColors)
 
// ─────────────── OBV SETTINGS ───────────────
groupOBV = 'On Balance Volume Settings'
 
showOBVMa = input.bool(true, title = 'Show OBV MA?', group = groupOBV)
showZeroLine = input.bool(false, title = 'Show OBV Zero Line', group = groupOBV)
 
// ─────────────── DYNAMIC MARKET PRESET LOGIC ───────────────
string usdFilterMode = na
int liquidityPeriod = na
float liquidityMult = na
float minUsdVol = na
bool useRvolFilter = false
int rvolLength = na
float minRvolRatio = na
 
if marketPreset == 'Crypto'
    usdFilterMode := 'Auto (Dynamic)'
    liquidityPeriod := 50
    liquidityMult := 1.2
    minUsdVol := 1000000.0
    useRvolFilter := true
    rvolLength := 50
    minRvolRatio := 1.0
    minRvolRatio
else if marketPreset == 'Stock (Hisse)'
    usdFilterMode := 'Auto (Dynamic)'
    liquidityPeriod := 30
    liquidityMult := 1.0
    minUsdVol := 500000.0
    useRvolFilter := true
    rvolLength := 30
    minRvolRatio := 0.8
    minRvolRatio
else if marketPreset == 'Forex'
    usdFilterMode := 'Disabled'
    liquidityPeriod := 50
    liquidityMult := 1.0
    minUsdVol := 0.0
    useRvolFilter := true
    rvolLength := 50
    minRvolRatio := 0.9
    minRvolRatio
else if marketPreset == 'Commodities (Emtia)'
    usdFilterMode := 'Auto (Dynamic)'
    liquidityPeriod := 40
    liquidityMult := 1.1
    minUsdVol := 750000.0
    useRvolFilter := true
    rvolLength := 40
    minRvolRatio := 0.8
    minRvolRatio
else // Custom Mode
    usdFilterMode := usdFilterModeInput
    liquidityPeriod := liquidityPeriodInput
    liquidityMult := liquidityMultInput
    minUsdVol := minUsdVolInput
    useRvolFilter := useRvolFilterInput
    rvolLength := rvolLengthInput
    minRvolRatio := minRvolRatioInput
    minRvolRatio
 
// ─────────────── CALCULATIONS & FILTERS ───────────────
ma20 = ta.sma(volume, lengthMA)
volRatio = volume / ma20
 
// USD Volume Calculation ($)
usdVolume = volume * close
 
// Dynamic Liquidity Threshold Calculation
avgUsdVolume = ta.sma(usdVolume, liquidityPeriod)
calculatedThreshold = avgUsdVolume * liquidityMult
 
// Active Threshold Determination Based on Mode
effectiveUsdThreshold = usdFilterMode == 'Auto (Dynamic)' ? calculatedThreshold : usdFilterMode == 'Manual' ? minUsdVol : 0.0
 
// Flexible RVOL Calculation
longTermVolMa = ta.sma(volume, rvolLength)
rvolValue = longTermVolMa > 0 ? volume / longTermVolMa : 1.0
 
isRvolValid = not useRvolFilter or rvolValue >= minRvolRatio
isUsdValid = usdFilterMode == 'Disabled' or usdVolume >= effectiveUsdThreshold
 
// Automated Liquidity Filter Validation Check
isVolumeValid = isUsdValid and isRvolValid
 
// Fill Ratio Percentage (%)
usdFillRatio = effectiveUsdThreshold > 0 ? usdVolume / effectiveUsdThreshold * 100 : 100.0
 
// ─────────────── OBV CALCULATIONS ───────────────
obv(src) =>
    var float obv_val = na
    change_src = ta.change(src)
    obv_val := na(obv_val) ? 0 : obv_val + (change_src > 0 ? volume : change_src < 0 ? -volume : 0)
    obv_val
 
src = close
os = obv(src)
obv_osc = os - ta.ema(os, lengthMA)
obc_color = obv_osc > 0 ? color.new(color.green, 0) : color.new(color.red, 0)
 
// ─────────────── VOLUME COLOR FUNCTION ───────────────
volumeColorMultiplied(vol, ma, op, cl, valid) =>
    isBull = cl >= op
    ratio = vol / ma
 
    // Dim the color if volume is unvalidated and dimming option is enabled
    if not valid and enableDimming
        isBull ? col_sub_up : col_sub_dn
    else // Apply normal multiplier color scale
        if ratio >= 2.5
            isBull ? col_2_5_up : col_2_5_dn
        else if ratio >= 2.0
            isBull ? col_2_0_up : col_2_0_dn
        else if ratio >= 1.5
            isBull ? col_1_5_up : col_1_5_dn
        else if ratio >= 1.0
            isBull ? col_1_0_up : col_1_0_dn
        else
            isBull ? col_sub_up : col_sub_dn
 
volColor = volumeColorMultiplied(volume, ma20, open, close, isVolumeValid)
 
// ─────────────── PLOTS ───────────────
scaledVol = volume * volScale
plot(scaledVol, color = volColor, style = plot.style_columns, title = 'Volume Bars')
plot(showMA ? ma20 * volScale : na, color = color.white, title = '20 MA', linewidth = 1)
 
// ─────────────── LABELS & WARNINGS ───────────────
// Standard Volume Multiplier Text
if showVolText and volRatio >= minTextRatio and (isVolumeValid or not enableDimming) and textSize != 'none'
    lblText = str.tostring(volRatio, '#.#')
    label.new(x = bar_index, y = scaledVol, text = lblText, xloc = xloc.bar_index, yloc = yloc.price, color = color.new(color.white, 100), textcolor = color.white, style = label.style_label_down, size = getTextSize(textSize))
 
// ─────────────── OBV PLOTS ───────────────
plot(obv_osc, color = obc_color, style = plot.style_line, title = 'OBV Points', linewidth = 2)
 
LineColor = showZeroLine ? color.gray : na
hline(0, 'OBV Zero Line', color = LineColor)
 
// ─────────────── OBV MA ───────────────
o_ma = ta.rma(obv_osc, lengthMA)
o_ma_s = ta.sma(o_ma, math.round(math.sqrt(lengthMA)))
ma_col = o_ma > o_ma_s ? color.new(color.blue, 0) : color.new(color.orange, 0)
o_ma := o_ma * math.pow(lengthMA, 0.15)
 
plot(showOBVMa ? o_ma : na, color = ma_col, linewidth = 2, style = plot.style_line, title = 'OBV MA')
 
// ─────────────── COMPARATIVE DASHBOARD / INFO BOX ───────────────
formatUsd(val) =>
    if val >= 1000000
        str.tostring(val / 1000000, '#.##') + 'M $'
    else if val >= 1000
        str.tostring(val / 1000, '#.#') + 'K $'
    else
        str.tostring(val, '#') + ' $'
 
getPos(posStr) =>
    switch posStr
        'top_right' => position.top_right
        'top_left' => position.top_left
        'bottom_right' => position.bottom_right
        'bottom_left' => position.bottom_left
        => position.top_right
 
if showDash and barstate.islast
    var table dash = table.new(getPos(dashPosition), 2, 5, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.gray)
 
    // Row 2: Filter Mode Status
    table.cell(dash, 0, 1, 'USD Filter Mode:', text_color = color.white, text_size = 'small')
    table.cell(dash, 1, 1, usdFilterMode, text_color = usdFilterMode == 'Auto (Dynamic)' ? color.green : usdFilterMode == 'Manual' ? color.orange : color.gray, text_size = 'small')
 
    // Row 3: Current Volume vs Required Threshold (Comparison)
    table.cell(dash, 0, 2, 'Current / Required:', text_color = color.white, text_size = 'small')
    reqText = usdFilterMode == 'Disabled' ? formatUsd(usdVolume) + ' / OFF' : formatUsd(usdVolume) + ' / ' + formatUsd(effectiveUsdThreshold)
    table.cell(dash, 1, 2, reqText, text_color = color.yellow, text_size = 'small')
 
    // Row 4: Liquidity Fill Ratio (%)
    table.cell(dash, 0, 3, 'Fill Ratio (%):', text_color = color.white, text_size = 'small')
    fillStr = usdFilterMode == 'Disabled' ? '100%' : str.tostring(usdFillRatio, '#.#') + '%'
    table.cell(dash, 1, 3, fillStr, text_color = usdFillRatio >= 100 or usdFilterMode == 'Disabled' ? color.green : color.orange, text_size = 'small')
 
    // Row 5: Validation Status
    statusText = usdFilterMode == 'Disabled' ? 'OFF' : isVolumeValid ? '✅ VALID' : '⚠️ INVALID'
    statusColor = usdFilterMode == 'Disabled' ? color.gray : isVolumeValid ? color.green : color.red
    table.cell(dash, 0, 4, 'Liquidity Status:', text_color = color.white, text_size = 'small')
    table.cell(dash, 1, 4, statusText, text_color = statusColor, text_size = 'small')

← 返回列表