High Volume Bubbles [NJ]

NJ_Algo · study · 107 行 · 点赞 69 · 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/
// © NJ_Algo
 
//@version=6
indicator("High Volume Bubbles [NJ]", "HVB", overlay = true, behind_chart = false, max_labels_count = 500)
 
//  ███╗   ██╗     ██╗      █████╗ ██╗      ██████╗  ██████╗ 
//  ████╗  ██║     ██║     ██╔══██╗██║     ██╔════╝ ██╔═══██╗
//  ██╔██╗ ██║     ██║     ███████║██║     ██║  ███╗██║   ██║
//  ██║╚██╗██║     ██║     ██╔══██║██║     ██║   ██║██║   ██║
//  ██║ ╚████║ ██╗ ██║     ██║  ██║██║     ██║   ██║██║   ██║
//  ██║  ╚███║ ╚████╔╝     ██║  ██║███████╗╚██████╔╝╚██████╔╝
//  ╚═╝   ╚══╝  ╚═══╝      ╚═╝  ╚═╝╚══════╝ ╚═════╝  ╚═════╝
 
// ✧═════════════════✧
// ║      INPUTS      ║
// ✧═════════════════✧
 
const string g1 = "✧ DISPLAY ✧"
colorMode   = input.string("Colored", "Color mode", options = ["Colored", "Neutral"], group = g1)
dispCandles = input.bool(false, "Color candles", group = g1)
transp      = input.int(75, "Bubbles transparency %", 0, 100, group = g1)
 
const string g2 = "✧ CALCULATION ✧"
rowTicks = input.int(100, "Price slice size (ticks)", minval = 1, group = g2, tooltip = "Smaller = more precise bubbles. Bigger = fewer, rougher bubbles.")
mult     = input.float(5.0, "Show when volume is X times normal", minval = 0.1, step = 0.5, group = g2)
avgLen   = input.int(120, "Candles used to measure normal volume", minval = 1, group = g2)
minVol   = input.float(1, "Ignore volume smaller than", minval = 0, group = g2, tooltip = "Hides tiny bubbles in quiet markets. In the symbol's volume units (e.g. BTC).")
 
// ✧══════════════════════✧
// ║      CALCULATION      ║
// ✧══════════════════════✧
 
// Shows decimals for small volumes instead of rounding them to 0
fmt(float v) =>
    float a = math.abs(v)
    a >= 1000 ? str.tostring(v, format.volume) : a >= 1 ? str.tostring(v, "#,##0.##") : str.tostring(v, "0.####")
 
bubbleSize(float ratio) =>
    ratio >= 5 ? size.huge : ratio >= 3 ? size.large : ratio >= 2 ? size.normal : ratio >= 1.5 ? size.small : size.tiny
 
bubble(float price, float ratio, color col, string tip) =>
    label.new(bar_index, price, "", style = label.style_circle, color = col, size = bubbleSize(ratio), tooltip = tip)
 
// Footprint rows of this candle (empty if there is no data)
footprint fp = request.footprint(rowTicks)
array<volume_row> rows = array.new<volume_row>()
if not na(fp)
    rows := footprint.rows(fp)
 
// Normal volume = average of the last closed candles only, so live and past
// candles are judged the same way
var array<float> history = array.new<float>()
float benchmark = history.size() > 0 ? math.max(history.avg() * mult, minVol) : na
 
// Add this candle's average volume per side per row once it closes
float total = 0.0
for r in rows
    total += volume_row.buy_volume(r) + volume_row.sell_volume(r)
if rows.size() > 0 and barstate.isconfirmed
    history.push(total / (2 * rows.size()))
    if history.size() > avgLen
        history.shift()
 
// ✧════════════════✧
// ║      PLOTS      ║
// ✧════════════════✧
 
longCol  = colorMode == "Colored" ? color.rgb(0, 136, 100) : color.rgb(179, 179, 179)
shortCol = colorMode == "Colored" ? color.rgb(120, 0, 32) : color.rgb(60, 60, 60)
sigCol = close >= open ? longCol : shortCol
 
plotcandle(
     open, high, low, close,
     "Colored candles",
     dispCandles ? sigCol : na,
     dispCandles ? sigCol : na,
     bordercolor = dispCandles ? sigCol : na,
     display = display.all - display.status_line - display.price_scale,
     force_overlay = true)
 
if not na(benchmark) and benchmark > 0
    for r in rows
        float rowUp = volume_row.up_price(r)
        float rowDn = volume_row.down_price(r)
        float buyV  = volume_row.buy_volume(r)
        float sellV = volume_row.sell_volume(r)
        float buyR  = buyV / benchmark
        float sellR = sellV / benchmark
 
        if buyR >= 1 or sellR >= 1
            // Keep the bubble inside the candle: use the part of the row the candle covers
            float price = (math.min(rowUp, high) + math.max(rowDn, low)) / 2
            string tip = "Buy: " + fmt(buyV) + "  (" + str.tostring(buyR, "#.#") + "×)" +
                 "\nSell: " + fmt(sellV) + "  (" + str.tostring(sellR, "#.#") + "×)" +
                 "\nBenchmark: " + fmt(benchmark) +
                 "\nRow: " + str.tostring(rowDn, format.mintick) + " – " + str.tostring(rowUp, format.mintick)
 
            // Draw the bigger bubble first so the smaller one sits on top and stays visible
            if buyR >= sellR
                bubble(price, buyR, color.new(longCol, transp), tip)
                if sellR >= 1
                    bubble(price, sellR, color.new(shortCol, transp), tip)
            else
                bubble(price, sellR, color.new(shortCol, transp), tip)
                if buyR >= 1
                    bubble(price, buyR, color.new(longCol, transp), tip)

← 返回列表