EDGE Volume Flow Footprint

tradingqueen18 · study · 181 行 · 点赞 39 · TradingView 原页

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

Pine Script

//@version=6
indicator("EDGE Volume Flow Footprint v2.1", shorttitle="EDGE VFF 2.1", overlay=true, behind_chart=false, max_lines_count=30, max_labels_count=30)
// Independent of EDGE GLI. Native TradingView footprint values are ESTIMATES,
// NOT consolidated trade-by-trade buyer/seller classifications.
// v2.1 uses a fixed-position ladder to prevent price-row text collisions.
 
// -------------------------- Settings ----------------------------------------
gData = "Footprint calculation"
ticksPerRow = input.int(100, "Ticks per price row", minval=1, group=gData)
valueArea = input.float(70, "Value area %", minval=1, maxval=100, group=gData)
imbalance = input.float(300, "Imbalance threshold %", minval=100, group=gData)
 
gLayout = "Table appearance"
tablePositionChoice = input.string("Top Right", "Table position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group=gLayout)
tableTextChoice = input.string("Small", "Table text size", options=["Tiny", "Small", "Normal", "Large"], group=gLayout)
 
tablePosition = switch tablePositionChoice
    "Top Left" => position.top_left
    "Top Center" => position.top_center
    "Middle Left" => position.middle_left
    "Middle Center" => position.middle_center
    "Middle Right" => position.middle_right
    "Bottom Left" => position.bottom_left
    "Bottom Center" => position.bottom_center
    "Bottom Right" => position.bottom_right
    => position.top_right
 
string chosenFontSize = switch tableTextChoice
    "Tiny" => size.tiny
    "Normal" => size.normal
    "Large" => size.large
    => size.small
 
gView = "Historical inspection"
selectedBack = input.int(0, "Inspect visible candle (0 = last visible; 1 = prior)", minval=0, maxval=4, group=gView)
rowsShown = input.int(12, "Maximum price rows in ladder", minval=4, maxval=15, group=gView)
showPOC = input.bool(true, "Mark inspected candle's POC on chart", group=gView)
showVA = input.bool(true, "Highlight value-area rows", group=gView)
showHistory = input.bool(true, "Show last five visible candle totals", group=gView)
warningAt = input.float(80, "Warn below coverage %", minval=0, maxval=100, group=gView)
 
// -------------------------- Data --------------------------------------------
footprint fp = request.footprint(ticksPerRow, valueArea, imbalance)
bool hasFootprint = not na(fp)
float fpBuy = hasFootprint ? fp.buy_volume() : na
float fpSell = hasFootprint ? fp.sell_volume() : na
float fpTotal = hasFootprint ? fp.total_volume() : na
float fpDelta = hasFootprint ? fp.delta() : na
float coverage = hasFootprint and volume > 0 ? fpTotal / volume * 100 : na
// Hidden numerical series remain accessible in TradingView's Data Window.
plot(volume, "Reported chart volume", display=display.data_window)
plot(fpTotal, "Footprint represented volume", display=display.data_window)
plot(fpBuy, "Estimated buy volume", display=display.data_window)
plot(fpSell, "Estimated sell volume", display=display.data_window)
plot(fpDelta, "Estimated delta", display=display.data_window)
plot(coverage, "Footprint coverage percent", display=display.data_window)
 
// -------------------------- Visible window ----------------------------------
// Scrolling/zooming changes these built-ins and forces Pine to recalculate.
var array<footprint> visibleFP = array.new<footprint>()
var array<int> visibleIndex = array.new<int>()
var array<int> visibleTime = array.new<int>()
var array<float> visibleVol = array.new<float>()
if barstate.isfirst
    array.clear(visibleFP)
    array.clear(visibleIndex)
    array.clear(visibleTime)
    array.clear(visibleVol)
 
bool onScreen = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
if onScreen and hasFootprint
    array.push(visibleFP, fp)
    array.push(visibleIndex, bar_index)
    array.push(visibleTime, time)
    array.push(visibleVol, volume)
    if array.size(visibleFP) > 5
        array.shift(visibleFP)
        array.shift(visibleIndex)
        array.shift(visibleTime)
        array.shift(visibleVol)
 
// A single stable table instead of hundreds of boxes; it is a footprint LADDER,
// not a separate dashboard or panel. Chart candles remain untouched.
var table ladder = table.new(tablePosition, 3, 27, bgcolor=color.rgb(18, 24, 34), frame_color=color.rgb(91, 101, 120), frame_width=1, border_color=color.rgb(41, 49, 61), border_width=1)
var line pocLine = na
 
fCell(int c, int r, string value, color background, color fontColor) =>
    table.cell(ladder, c, r, value, bgcolor=background, text_color=fontColor, text_size=chosenFontSize, text_halign=text.align_center, text_font_family=font.family_monospace)
 
fFmt(float value) =>
    na(value) ? "n/a" : str.tostring(value, format.volume)
 
if barstate.islast
    table.clear(ladder, 0, 0, 2, 26)
    if not na(pocLine)
        line.delete(pocLine)
        pocLine := na
    int n = array.size(visibleFP)
    if n == 0
        fCell(0, 0, "No footprint", color.rgb(32, 39, 52), color.white)
        fCell(1, 0, "data for", color.rgb(32, 39, 52), color.white)
        fCell(2, 0, "visible bars", color.rgb(32, 39, 52), color.white)
    else
        int chosen = math.max(0, n - 1 - selectedBack)
        footprint chosenFP = array.get(visibleFP, chosen)
        int chosenIdx = array.get(visibleIndex, chosen)
        int chosenTime = array.get(visibleTime, chosen)
        float chartVol = array.get(visibleVol, chosen)
        float total = chosenFP.total_volume()
        float buy = chosenFP.buy_volume()
        float sell = chosenFP.sell_volume()
        float delta = chosenFP.delta()
        float cov = chartVol > 0 ? 100 * total / chartVol : na
        string whenText = str.format_time(chosenTime, "MMM dd HH:mm", "America/New_York")
        fCell(0, 0, "EDGE VFF", color.rgb(29, 38, 56), color.white)
        fCell(1, 0, "v2.1", color.rgb(29, 38, 56), color.yellow)
        fCell(2, 0, "ESTIMATE", color.rgb(29, 38, 56), color.rgb(253, 195, 86))
        fCell(0, 1, "SELL", color.rgb(135, 39, 52), color.white)
        fCell(1, 1, whenText, color.rgb(34, 43, 56), color.white)
        fCell(2, 1, "BUY", color.rgb(5, 111, 78), color.white)
        fCell(0, 2, "Volume", color.rgb(73, 31, 39), color.rgb(255, 182, 185))
        fCell(1, 2, "Price", color.rgb(39, 49, 64), color.white)
        fCell(2, 2, "Volume", color.rgb(14, 59, 50), color.rgb(161, 244, 197))
        array<volume_row> allRows = chosenFP.rows()
        int count = array.size(allRows)
        if count > 0
            volume_row poc = chosenFP.poc()
            volume_row vaHi = chosenFP.vah()
            volume_row vaLo = chosenFP.val()
            float pocMid = (poc.up_price() + poc.down_price()) / 2
            if showPOC
                pocLine := line.new(chosenIdx, pocMid, chosenIdx + 1, pocMid, xloc=xloc.bar_index, color=color.yellow, width=3)
            // A contiguous subset around POC. Other rows are excluded from DISPLAY
            // only; all candle totals use the complete native footprint object.
            int pocIndex = 0
            float minDistance = 1e20
            for i = 0 to count - 1
                volume_row r = array.get(allRows, i)
                float mid = (r.up_price() + r.down_price()) / 2
                float dist = math.abs(mid - pocMid)
                if dist < minDistance
                    minDistance := dist
                    pocIndex := i
            int shown = math.min(count, rowsShown)
            int start = math.max(0, math.min(count - shown, pocIndex - int(math.floor(shown / 2))))
            // Volume rows from HIGH to LOW like a conventional footprint ladder.
            for k = 0 to 14
                int displayRow = 3 + k
                if k < shown
                    int rowIdx = start + shown - 1 - k
                    volume_row r = array.get(allRows, rowIdx)
                    float midpoint = (r.up_price() + r.down_price()) / 2
                    bool isPoc = math.abs(midpoint - pocMid) <= syminfo.mintick * 0.5
                    bool inValueArea = showVA and r.up_price() >= vaLo.down_price() and r.down_price() <= vaHi.up_price()
                    color sBg = inValueArea ? color.rgb(165, 54, 68) : color.rgb(97, 35, 46)
                    color bBg = inValueArea ? color.rgb(13, 124, 89) : color.rgb(13, 72, 57)
                    fCell(0, displayRow, fFmt(r.sell_volume()), sBg, color.white)
                    fCell(1, displayRow, str.tostring(midpoint, format.mintick), isPoc ? color.rgb(145, 116, 29) : color.rgb(35, 44, 58), isPoc ? color.yellow : color.white)
                    fCell(2, displayRow, fFmt(r.buy_volume()), bBg, color.white)
        fCell(0, 18, "SELL " + fFmt(sell), color.rgb(103, 38, 48), color.white)
        fCell(1, 18, "TOTAL " + fFmt(total), color.rgb(40, 50, 62), color.white)
        fCell(2, 18, "BUY " + fFmt(buy), color.rgb(10, 87, 66), color.white)
        fCell(0, 19, "Delta", color.rgb(31, 39, 52), color.white)
        fCell(1, 19, fFmt(delta), delta >= 0 ? color.rgb(12, 87, 66) : color.rgb(124, 40, 53), color.white)
        fCell(2, 19, "Coverage " + (na(cov) ? "n/a" : str.tostring(cov, "#.0") + "%"), not na(cov) and cov < warningAt ? color.rgb(123, 69, 24) : color.rgb(40, 50, 62), color.white)
        fCell(0, 20, "Chart " + fFmt(chartVol), color.rgb(31, 39, 52), color.white)
        fCell(1, 20, "Rows " + str.tostring(count), color.rgb(31, 39, 52), color.white)
        fCell(2, 20, "Shown " + str.tostring(math.min(count, rowsShown)), color.rgb(31, 39, 52), color.white)
        if showHistory
            fCell(0, 21, "HISTORY", color.rgb(33, 43, 59), color.white)
            fCell(1, 21, "NET DELTA", color.rgb(33, 43, 59), color.white)
            fCell(2, 21, "COVERAGE", color.rgb(33, 43, 59), color.white)
            for j = 0 to n - 1
                int ix = n - 1 - j
                footprint past = array.get(visibleFP, ix)
                float cv = array.get(visibleVol, ix)
                float pc = cv > 0 ? 100 * past.total_volume() / cv : na
                int t = array.get(visibleTime, ix)
                fCell(0, 22 + j, str.format_time(t, "MMM dd", "America/New_York"), color.rgb(32, 41, 53), color.white)
                fCell(1, 22 + j, fFmt(past.delta()), past.delta() >= 0 ? color.rgb(12, 76, 57) : color.rgb(105, 39, 51), color.white)
                fCell(2, 22 + j, na(pc) ? "n/a" : str.tostring(pc, "#.0") + "%", not na(pc) and pc < warningAt ? color.rgb(119, 70, 25) : color.rgb(32, 41, 53), color.white)

← 返回列表