M-Alpha-Pressure Profile - Buying vs Selling Volume by Price

MAlphaCore · study · 95 行 · 点赞 12 · 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/
// © MAlphaCore
 
//@version=6
// M·Alpha·Pressure Profile — a rolling volume profile that splits every price level into buying and selling pressure.
// Each bar's volume is spread evenly over the price range it traded and divided by where it closed: a close at the high
// counts as all buying, a close at the low as all selling, a close in the middle half and half (an estimate from the
// candle, not real order flow). The profile sits to the right of price; the level with the most volume (POC) and the
// levels where one side clearly dominates are marked. Built from closed bars of the chosen window.
indicator("M·Alpha·Pressure Profile", shorttitle = "M·Alpha·Pressure", overlay = true, max_boxes_count = 500, max_lines_count = 5, max_labels_count = 5)
 
// ───────────────────────────── Inputs ─────────────────────────────
grPr     = "Profile"
lookback = input.int(200, "Bars in the profile", minval = 20, maxval = 1000, group = grPr, display = display.none)
rows     = input.int(30, "Price rows", minval = 5, maxval = 80, group = grPr, display = display.none)
domShare = input.float(65, "Dominant side from (% of the row)", minval = 51, maxval = 95, group = grPr, display = display.none)
 
grShow   = "Display"
width    = input.int(40, "Profile width (bars)", minval = 5, maxval = 200, group = grShow, display = display.none)
gap      = input.int(5, "Distance from price (bars)", minval = 0, maxval = 100, group = grShow, display = display.none)
showPoc  = input.bool(true, "Show POC line", group = grShow, display = display.none)
 
grStyle  = "Style"
colBuy   = input.color(#ffeb3b, "Buying", group = grStyle, display = display.none)
colSell  = input.color(#b2b5be, "Selling", group = grStyle, display = display.none)
colPoc   = input.color(#7c4dff, "POC", group = grStyle, display = display.none)
colText  = input.color(#e8eaf0, "Text", group = grStyle, display = display.none)
 
// ───────────────────────────── Profile ─────────────────────────────
var array<box>   bxs = array.new<box>()
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
hiW = ta.highest(high, lookback)
loW = ta.lowest(low, lookback)
 
if barstate.islast
    for b in bxs
        b.delete()
    for l in lns
        l.delete()
    for lb in lbs
        lb.delete()
    bxs.clear()
    lns.clear()
    lbs.clear()
    // the window ends at the last closed bar
    off = barstate.isconfirmed ? 0 : 1
    top = off == 0 ? hiW : hiW[1]
    bot = off == 0 ? loW : loW[1]
    step = (top - bot) / rows
    buy  = array.new<float>(rows, 0.0)
    sell = array.new<float>(rows, 0.0)
    if step > 0
        for i = off to off + lookback - 1
            h = high[i]
            l = low[i]
            v = nz(volume[i])
            fb = h > l ? (close[i] - l) / (h - l) : 0.5
            r0 = math.max(0, math.min(rows - 1, int(math.floor((l - bot) / step))))
            r1 = math.max(0, math.min(rows - 1, int(math.floor((h - bot) / step))))
            for r = r0 to r1
                lo_ = bot + r * step
                ov = h > l ? (math.min(h, lo_ + step) - math.max(l, lo_)) / (h - l) : 1.0 / (r1 - r0 + 1)
                if ov > 0
                    buy.set(r, buy.get(r) + v * ov * fb)
                    sell.set(r, sell.get(r) + v * ov * (1 - fb))
        maxTot = 0.0
        poc = 0
        for r = 0 to rows - 1
            t = buy.get(r) + sell.get(r)
            if t > maxTot
                maxTot := t
                poc := r
        x0 = bar_index + gap
        for r = 0 to rows - 1
            b = buy.get(r)
            s = sell.get(r)
            t = b + s
            if t > 0 and maxTot > 0
                wTot = int(math.round(width * t / maxTot))
                wBuy = int(math.round(wTot * b / t))
                y1 = bot + (r + 1) * step
                y0 = bot + r * step
                dom = 100 * b / t >= domShare ? colBuy : 100 * s / t >= domShare ? colSell : na
                if wBuy > 0
                    bxs.push(box.new(x0, y1, x0 + wBuy, y0, border_color = na, bgcolor = color.new(colBuy, 45)))
                if wTot - wBuy > 0
                    bxs.push(box.new(x0 + wBuy, y1, x0 + wTot, y0, border_color = na, bgcolor = color.new(colSell, 45)))
                if not na(dom)
                    bxs.push(box.new(x0, y1, x0 + wTot, y0, border_color = dom, border_width = 1, bgcolor = na))
        pocPrice = bot + (poc + 0.5) * step
        if showPoc
            lns.push(line.new(bar_index - lookback + 1, pocPrice, x0 + width, pocPrice, color = colPoc, width = 2))
            pb = buy.get(poc)
            lbs.push(label.new(x0 + width, pocPrice, "POC " + str.tostring(pocPrice, format.mintick) + " · buy " + str.tostring(100 * pb / maxTot, "#") + " %", style = label.style_label_left, color = color.new(colPoc, 30), textcolor = colText, size = size.small))

← 返回列表