Footprint Buy/Sell Pressure

ueuito · study · 260 行 · 点赞 52 · TradingView 原页

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

Pine Script

//@version=6
indicator("Footprint Buy/Sell Pressure — 6 Tiers + POC", overlay = true, max_labels_count = 500, max_lines_count = 500)
 
// ── Footprint and filters ────────────────────────────────────────────────────
ticksPerRow = input.int(10, "Ticks per footprint row", minval = 1, group = "Footprint")
minimumRowPercentOfPOC = input.float(10.0, "Minimum row volume (% of POC)", minval = 0, group = "Filters")
volumeAverageLength = input.int(20, "Volume average length", minval = 1, group = "Filters")
minimumRelativeVolume = input.float(0.0, "Minimum candle volume (% of average; 0 = disabled)", minval = 0, group = "Filters")
maxDotsPerBar = input.int(12, "Maximum dots per candle", minval = 1, maxval = 50, group = "Filters")
 
// ── Signal display ───────────────────────────────────────────────────────────
showBarDeltaInTooltip = input.bool(true, "Include whole-candle delta in dot tooltip", group = "Signal display")
showDotsAfterCloseOnly = input.bool(false, "Show dots only after candle closes", group = "Signal display")
 
// ── POC display ──────────────────────────────────────────────────────────────
showPoc = input.bool(true, "Show candle POC", group = "POC")
pocColor = input.color(color.yellow, "POC color", group = "POC")
pocWidth = input.int(2, "POC line width", minval = 1, maxval = 4, group = "POC")
 
// ── Candle body tint ─────────────────────────────────────────────────────────
showCandleTint = input.bool(true, "Tint candle bodies", group = "Candle appearance")
bullishCandleColor = input.color(color.lime, "Bullish candle tint", group = "Candle appearance")
bearishCandleColor = input.color(color.red, "Bearish candle tint", group = "Candle appearance")
candleTransparency = input.int(82, "Body tint transparency", minval = 0, maxval = 100, group = "Candle appearance")
 
color candleTint = close >= open ? bullishCandleColor : bearishCandleColor
color transparentCandlePart = color.new(candleTint, 100)
 
plotcandle(
     showCandleTint ? open : na,
     showCandleTint ? high : na,
     showCandleTint ? low : na,
     showCandleTint ? close : na,
     title = "Soft candle body tint",
     color = color.new(candleTint, candleTransparency),
     wickcolor = transparentCandlePart,
     bordercolor = transparentCandlePart
)
 
// ── Tier 1 ───────────────────────────────────────────────────────────────────
enableTier1 = input.bool(true, "Enable Tier 1", group = "Tier 1")
threshold1 = input.float(150, "Imbalance threshold (%)", minval = 1, group = "Tier 1")
size1 = input.string("Tiny", "Dot size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Tier 1")
buyColor1 = input.color(color.rgb(125, 255, 150), "Buy dot color", group = "Tier 1")
sellColor1 = input.color(color.rgb(255, 150, 150), "Sell dot color", group = "Tier 1")
 
// ── Tier 2 ───────────────────────────────────────────────────────────────────
enableTier2 = input.bool(true, "Enable Tier 2", group = "Tier 2")
threshold2 = input.float(200, "Imbalance threshold (%)", minval = 1, group = "Tier 2")
size2 = input.string("Small", "Dot size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Tier 2")
buyColor2 = input.color(color.rgb(80, 255, 110), "Buy dot color", group = "Tier 2")
sellColor2 = input.color(color.rgb(255, 100, 100), "Sell dot color", group = "Tier 2")
 
// ── Tier 3 ───────────────────────────────────────────────────────────────────
enableTier3 = input.bool(true, "Enable Tier 3", group = "Tier 3")
threshold3 = input.float(250, "Imbalance threshold (%)", minval = 1, group = "Tier 3")
size3 = input.string("Normal", "Dot size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Tier 3")
buyColor3 = input.color(color.rgb(40, 235, 80), "Buy dot color", group = "Tier 3")
sellColor3 = input.color(color.rgb(235, 55, 55), "Sell dot color", group = "Tier 3")
 
// ── Tier 4 ───────────────────────────────────────────────────────────────────
enableTier4 = input.bool(true, "Enable Tier 4", group = "Tier 4")
threshold4 = input.float(300, "Imbalance threshold (%)", minval = 1, group = "Tier 4")
size4 = input.string("Large", "Dot size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Tier 4")
buyColor4 = input.color(color.rgb(20, 210, 65), "Buy dot color", group = "Tier 4")
sellColor4 = input.color(color.rgb(210, 35, 35), "Sell dot color", group = "Tier 4")
 
// ── Tier 5 ───────────────────────────────────────────────────────────────────
enableTier5 = input.bool(true, "Enable Tier 5", group = "Tier 5")
threshold5 = input.float(400, "Imbalance threshold (%)", minval = 1, group = "Tier 5")
size5 = input.string("Huge", "Dot size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Tier 5")
buyColor5 = input.color(color.rgb(0, 180, 55), "Buy dot color", group = "Tier 5")
sellColor5 = input.color(color.rgb(180, 20, 20), "Sell dot color", group = "Tier 5")
 
// ── Tier 6 ───────────────────────────────────────────────────────────────────
enableTier6 = input.bool(true, "Enable Tier 6", group = "Tier 6")
threshold6 = input.float(500, "Imbalance threshold (%)", minval = 1, group = "Tier 6")
size6 = input.string("Huge", "Dot size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = "Tier 6")
buyColor6 = input.color(color.rgb(0, 120, 40), "Buy dot color", group = "Tier 6")
sellColor6 = input.color(color.rgb(130, 0, 0), "Sell dot color", group = "Tier 6")
 
// Converts a size selection into a Pine marker size.
f_markerSize(string choice) =>
    switch choice
        "Tiny" => size.tiny
        "Small" => size.small
        "Normal" => size.normal
        "Large" => size.large
        => size.huge
 
// Footprint data requires a TradingView Premium or Ultimate plan.
footprint fp = request.footprint(ticksPerRow, imbalance_percent = 300)
 
float averageVolume = ta.sma(volume, volumeAverageLength)
bool buyPressureOnBar = false
bool sellPressureOnBar = false
 
if not na(fp)
    volume_row pocRow = fp.poc()
    array<volume_row> rows = fp.rows()
 
    float pocVolume = na(pocRow) ? na : pocRow.total_volume()
    float wholeCandleDelta = fp.delta()
 
    string deltaTooltip = showBarDeltaInTooltip and not na(wholeCandleDelta)
         ? "\nWhole-candle delta (buy - sell): " + str.tostring(wholeCandleDelta, format.volume)
         : ""
 
    // Draw a short horizontal segment at this candle's POC price.
    if showPoc and not na(pocRow)
        float pocPrice = math.avg(pocRow.up_price(), pocRow.down_price())
        line.new(
             bar_index,
             pocPrice,
             bar_index + 1,
             pocPrice,
             xloc = xloc.bar_index,
             extend = extend.none,
             color = pocColor,
             width = pocWidth
         )
 
    bool candleVolumePasses = minimumRelativeVolume == 0 or (not na(averageVolume) and volume >= averageVolume * minimumRelativeVolume / 100)
    bool candleIsEligible = candleVolumePasses and (not showDotsAfterCloseOnly or barstate.isconfirmed)
 
    int dotsDrawn = 0
    int rowCount = array.size(rows)
 
    if rowCount > 0 and not na(pocVolume) and candleIsEligible
        for i = 0 to rowCount - 1
            if dotsDrawn >= maxDotsPerBar
                break
 
            volume_row currentRow = array.get(rows, i)
            volume_row lowerRow = i > 0 ? array.get(rows, i - 1) : na
            volume_row upperRow = i < rowCount - 1 ? array.get(rows, i + 1) : na
 
            float rowVolume = currentRow.total_volume()
            float rowBuyVolume = currentRow.buy_volume()
            float rowSellVolume = currentRow.sell_volume()
            float rowPrice = math.avg(currentRow.up_price(), currentRow.down_price())
 
            float sellBelow = na(lowerRow) ? na : lowerRow.sell_volume()
            float buyAbove = na(upperRow) ? na : upperRow.buy_volume()
 
            bool rowVolumePasses = rowVolume >= pocVolume * minimumRowPercentOfPOC / 100
 
            float buyRatio = not na(sellBelow) and sellBelow > 0 ? rowBuyVolume / sellBelow * 100 : na
            float sellRatio = not na(buyAbove) and buyAbove > 0 ? rowSellVolume / buyAbove * 100 : na
 
            int buyTier = 0
            int sellTier = 0
 
            if rowVolumePasses
                if enableTier1 and not na(buyRatio) and buyRatio >= threshold1
                    buyTier := 1
                if enableTier2 and not na(buyRatio) and buyRatio >= threshold2
                    buyTier := 2
                if enableTier3 and not na(buyRatio) and buyRatio >= threshold3
                    buyTier := 3
                if enableTier4 and not na(buyRatio) and buyRatio >= threshold4
                    buyTier := 4
                if enableTier5 and not na(buyRatio) and buyRatio >= threshold5
                    buyTier := 5
                if enableTier6 and not na(buyRatio) and buyRatio >= threshold6
                    buyTier := 6
 
                if enableTier1 and not na(sellRatio) and sellRatio >= threshold1
                    sellTier := 1
                if enableTier2 and not na(sellRatio) and sellRatio >= threshold2
                    sellTier := 2
                if enableTier3 and not na(sellRatio) and sellRatio >= threshold3
                    sellTier := 3
                if enableTier4 and not na(sellRatio) and sellRatio >= threshold4
                    sellTier := 4
                if enableTier5 and not na(sellRatio) and sellRatio >= threshold5
                    sellTier := 5
                if enableTier6 and not na(sellRatio) and sellRatio >= threshold6
                    sellTier := 6
 
            bool showBuy = buyTier > 0
            bool showSell = sellTier > 0
 
            // If both directions qualify at the same price row, show the stronger one.
            if showBuy and showSell
                showBuy := buyRatio >= sellRatio
                showSell := not showBuy
 
            if showBuy and dotsDrawn < maxDotsPerBar
                string chosenSize = switch buyTier
                    1 => size1
                    2 => size2
                    3 => size3
                    4 => size4
                    5 => size5
                    => size6
 
                color chosenColor = switch buyTier
                    1 => buyColor1
                    2 => buyColor2
                    3 => buyColor3
                    4 => buyColor4
                    5 => buyColor5
                    => buyColor6
 
                label.new(
                     bar_index,
                     rowPrice,
                     "",
                     yloc = yloc.price,
                     style = label.style_circle,
                     color = chosenColor,
                     size = f_markerSize(chosenSize),
                     tooltip = "Buy pressure — Tier " + str.tostring(buyTier) +
                         "\nBuy/sell imbalance: " + str.tostring(buyRatio, "#.0") + "%" +
                         "\nBuy volume at row: " + str.tostring(rowBuyVolume, format.volume) +
                         "\nSell volume below: " + str.tostring(sellBelow, format.volume) +
                         deltaTooltip
                 )
 
                dotsDrawn += 1
                buyPressureOnBar := true
 
            if showSell and dotsDrawn < maxDotsPerBar
                string chosenSize = switch sellTier
                    1 => size1
                    2 => size2
                    3 => size3
                    4 => size4
                    5 => size5
                    => size6
 
                color chosenColor = switch sellTier
                    1 => sellColor1
                    2 => sellColor2
                    3 => sellColor3
                    4 => sellColor4
                    5 => sellColor5
                    => sellColor6
 
                label.new(
                     bar_index,
                     rowPrice,
                     "",
                     yloc = yloc.price,
                     style = label.style_circle,
                     color = chosenColor,
                     size = f_markerSize(chosenSize),
                     tooltip = "Sell pressure — Tier " + str.tostring(sellTier) +
                         "\nSell/buy imbalance: " + str.tostring(sellRatio, "#.0") + "%" +
                         "\nSell volume at row: " + str.tostring(rowSellVolume, format.volume) +
                         "\nBuy volume above: " + str.tostring(buyAbove, format.volume) +
                         deltaTooltip
                 )
 
                dotsDrawn += 1
                sellPressureOnBar := true
 
alertcondition(buyPressureOnBar, "Buy pressure detected", "Footprint buy pressure detected.")
alertcondition(sellPressureOnBar, "Sell pressure detected", "Footprint sell pressure detected.")

← 返回列表