Live Delta Footprint v3 pranco v1

skybu78 · study · 110 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator("Live Delta Footprint v3 - Clean Layout", overlay = true, behind_chart = false, max_boxes_count = 500)
 
// Native TradingView footprints require Premium or Ultimate.
// Buy/sell classification comes from TradingView's intrabar price-action model.
int ticksPerRow = input.int(100, "Native ticks per row", minval = 1, tooltip = "Row price height = this value x the symbol minimum tick. Raise it if calculation is slow or footprints contain too many native rows.")
int barsToShow = input.int(6, "Recent bars to display", minval = 1, maxval = 40)
int targetRows = input.int(10, "Maximum displayed rows per bar", minval = 4, maxval = 40, tooltip = "Adjacent native rows are combined when necessary. Actual limit also respects the 480-cell drawing budget.")
bool highlightPOC = input.bool(true, "Outline highest-volume displayed row")
string units = input.string("Auto quote amount", "Number units", options = ["Auto quote amount", "Symbol volume", "Price x volume"], tooltip = "Auto converts base-denominated volume to approximate quote amount using row midpoint price. Quote-denominated or unspecified volume is left unchanged. For unspecified feeds select Price x volume only if volume is in base units.")
string layout = input.string("Spaced columns", "Layout", options = ["Spaced columns", "On candles"], tooltip = "Spaced columns displays recent footprints to the right of price, with wider columns and a gap. Enable sufficient right margin on the chart. On candles needs horizontal zoom for readable amounts.")
int columnSpacing = input.int(8, "Column spacing (chart bars)", minval = 3, maxval = 10)
int fontPixels = input.int(12, "Amount text size (pixels)", minval = 8, maxval = 20)
bool showStatus = input.bool(false, "Show status badge")
color buyColor = input.color(#408AC0, "Positive delta")
color sellColor = input.color(#A84850, "Negative delta")
color pocColor = input.color(#E7C65B, "POC outline")
 
 
// Explicit one-decimal compact amounts, matching the reference's K/M style.
formatAmount(float value) =>
    float magnitude = math.abs(value)
    string signText = value < 0 ? "-" : ""
    string amount = magnitude >= 1e9 ? str.tostring(magnitude / 1e9, "0.0") + "B" : magnitude >= 1e6 ? str.tostring(magnitude / 1e6, "0.0") + "M" : magnitude >= 1e3 ? str.tostring(magnitude / 1e3, "0.0") + "K" : str.tostring(magnitude, "0.0")
    signText + amount
 
bool convertToQuote = units == "Price x volume" or (units == "Auto quote amount" and syminfo.volumetype == "base")
string amountUnit = convertToQuote or (units == "Auto quote amount" and syminfo.volumetype == "quote") ? syminfo.currency : "feed volume"
 
footprint fp = request.footprint(ticksPerRow)
var array<box> cells = array.new<box>()
var array<int> sourceBars = array.new<int>()
var table status = table.new(position.top_right, 1, 1)
float barDelta = na
float pocMid = na
int rowLimit = math.min(targetRows, int(math.floor(480.0 / barsToShow)))
 
// Indicators execute on every incoming realtime update. Pine rollback removes
// the preceding unconfirmed drawings before this bar is rebuilt; no varip
// accumulation is used, so live volume is never counted twice by this script.
if not na(fp)
    barDelta := fp.delta()
    volume_row nativePOC = fp.poc()
    pocMid := math.avg(nativePOC.up_price(), nativePOC.down_price())
    if bar_index >= last_bar_index - barsToShow + 1
        array<volume_row> rows = fp.rows()
        int n = array.size(rows)
        if n > 0
            int groupSize = int(math.ceil(float(n) / rowLimit))
            array<float> lows = array.new<float>()
            array<float> highs = array.new<float>()
            array<float> deltas = array.new<float>()
            array<float> totals = array.new<float>()
            for start = 0 to n - 1 by groupSize
                float groupDelta = 0.0
                float groupTotal = 0.0
                int finish = math.min(start + groupSize - 1, n - 1)
                for j = start to finish
                    volume_row r = array.get(rows, j)
                    float multiplier = convertToQuote ? math.avg(r.up_price(), r.down_price()) : 1.0
                    groupDelta += r.delta() * multiplier
                    groupTotal += r.total_volume()
                array.push(lows, array.get(rows, start).down_price())
                array.push(highs, array.get(rows, finish).up_price())
                array.push(deltas, groupDelta)
                array.push(totals, groupTotal)
            float maxVolume = array.max(totals)
            int pocIndex = array.indexof(totals, maxVolume)
            float maxDelta = 0.0
            for d in deltas
                maxDelta := math.max(maxDelta, math.abs(d))
            for i = 0 to array.size(deltas) - 1
                // Keep all rows of the requested recent bars inside Pine's budget.
                while array.size(cells) >= 480
                    box.delete(array.shift(cells))
                    array.shift(sourceBars)
                float d = array.get(deltas, i)
                float strength = maxDelta > 0 ? math.abs(d) / maxDelta : 0.0
                color base = d >= 0 ? buyColor : sellColor
                color fill = color.new(base, int(88 - strength * 45))
                bool isPOC = highlightPOC and i == pocIndex
                string number = " " + formatAmount(d)
                int cellLeft = layout == "Spaced columns" ? last_bar_index + 3 + (bar_index - (last_bar_index - barsToShow + 1)) * columnSpacing : bar_index
                int cellRight = cellLeft + (layout == "Spaced columns" ? columnSpacing - 1 : 1)
                box cell = box.new(left = cellLeft, top = array.get(highs, i), right = cellRight, bottom = array.get(lows, i), border_color = isPOC ? pocColor : color.new(color.gray, 65), border_width = 1, bgcolor = fill, text = number, text_color = color.white, text_size = fontPixels, text_halign = text.align_left, text_valign = text.align_center, text_wrap = text.wrap_none)
                array.push(cells, cell)
                array.push(sourceBars, bar_index)
 
// Trim by age as well as count when bar ranges contain different row counts.
while array.size(cells) > 0
    box oldest = array.get(cells, 0)
    if array.get(sourceBars, 0) < bar_index - barsToShow + 1
        box.delete(array.shift(cells))
        array.shift(sourceBars)
    else
        break
 
// Re-anchor all retained columns when a new realtime bar changes last_bar_index.
if barstate.islast and layout == "Spaced columns" and array.size(cells) > 0
    for i = 0 to array.size(cells) - 1
        int cellLeft = bar_index + 3 + (array.get(sourceBars, i) - (bar_index - barsToShow + 1)) * columnSpacing
        box.set_left(array.get(cells, i), cellLeft)
        box.set_right(array.get(cells, i), cellLeft + columnSpacing - 1)
 
if barstate.islast
    string message = na(fp) ? "No footprint data for this bar" : "Delta amounts | " + amountUnit + " | " + timeframe.period + (barstate.isrealtime ? " | LIVE" : " | Historical")
    table.cell(status, 0, 0, showStatus or na(fp) ? message : "", text_color = na(fp) ? color.orange : color.silver, bgcolor = color.new(color.black, 25), text_size = size.small)
 
plot(barDelta, "Bar delta (symbol volume)", display = display.data_window)
plot(pocMid, "Native POC midpoint", display = display.data_window)

← 返回列表