Prior Session Volume Profile [Badr Edge]

kh_badrr · study · 141 行 · 点赞 26 · TradingView 原页

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

Pine Script

//@version=6
// Prior Session Volume Profile [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("Prior Session Volume Profile [Badr Edge]", shorttitle = "BE Profile", overlay = true, max_boxes_count = 200, max_labels_count = 50)
const color GOLD = #D9B46C
const color TEAL = #4FB8A0
const color ROSE = #E07A6C
const color GREY = #8F939B
const string TZ = "America/New_York"
const string G_P = "Profile"
sess    = input.session("1800-1700", "Session (New York time)", group = G_P, tooltip = "The profile is built from every bar of the last completed session and shown on the next one. The default is the full futures trading day. Use 0930-1600 for the cash session only.")
rowsN   = input.int(40, "Rows", minval = 10, maxval = 100, group = G_P)
vaPct   = input.float(70, "Value area (%)", minval = 40, maxval = 95, step = 5, group = G_P)
const string G_V = "Visuals"
showHist  = input.bool(true, "Volume histogram", group = G_V)
histW     = input.int(40, "Histogram width (bars)", minval = 5, maxval = 300, group = G_V)
showMarks = input.bool(true, "Rejection markers", group = G_V, tooltip = "A small triangle when a candle trades through the value area high or low and closes back inside the value area. A marker shows what happened. It is not a trade signal.")
firstOnly = input.bool(true, "First marker per level each session", group = G_V)
showLabels = input.bool(true, "Level labels", group = G_V)
const string G_Y = "Style"
colProf = input.color(GOLD, "Profile", group = G_Y)
showWm  = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert on a rejection marker", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
 
tfOK  = timeframe.isintraday and timeframe.in_seconds() <= 1800
inS   = tfOK and not na(time(timeframe.period, sess, TZ))
newTD = ta.change(time_tradingday) != 0
start = inS and (not inS[1] or newTD)
ended = not inS and inS[1]
 
var array<float> hs = array.new<float>()
var array<float> ls = array.new<float>()
var array<float> vs = array.new<float>()
var array<float> bins = array.new<float>()
var array<box>   hist = array.new<box>()
var float poc = na
var float vah = na
var float val = na
var float pLo = na
var float pStep = na
var bool  upDone = false
var bool  dnDone = false
 
if (start or ended) and vs.size() > 1
    float lo = ls.min()
    float hi = hs.max()
    float step = (hi - lo) / rowsN
    if step > 0
        bins := array.new<float>(rowsN, 0.0)
        for i = 0 to vs.size() - 1
            int i0 = math.min(rowsN - 1, math.max(0, int((ls.get(i) - lo) / step)))
            int i1 = math.min(rowsN - 1, math.max(0, int((hs.get(i) - lo) / step)))
            float share = vs.get(i) / (i1 - i0 + 1)
            for k = i0 to i1
                bins.set(k, bins.get(k) + share)
        int pk = bins.indexof(bins.max())
        float need = bins.sum() * vaPct / 100
        float got = bins.get(pk)
        int a = pk
        int b = pk
        while got < need and (a > 0 or b < rowsN - 1)
            float dnV = a > 0 ? bins.get(a - 1) : -1.0
            float upV = b < rowsN - 1 ? bins.get(b + 1) : -1.0
            if upV >= dnV
                b += 1
                got += upV
            else
                a -= 1
                got += dnV
        poc := math.round_to_mintick(lo + (pk + 0.5) * step)
        vah := math.round_to_mintick(lo + (b + 1) * step)
        val := math.round_to_mintick(lo + a * step)
        pLo := lo
        pStep := step
    hs.clear()
    ls.clear()
    vs.clear()
 
if start
    upDone := false
    dnDone := false
    for bx in hist
        bx.delete()
    hist.clear()
    if showHist and not na(pStep) and bins.size() == rowsN
        float mx = bins.max()
        for k = 0 to rowsN - 1
            float bot = pLo + k * pStep
            float mid = bot + pStep / 2
            int w = int(math.round(histW * bins.get(k) / mx))
            bool inVA = mid >= val and mid <= vah
            bool isPoc = math.abs(mid - poc) < pStep / 2
            color c = isPoc ? color.new(colProf, 35) : inVA ? color.new(colProf, 78) : color.new(GREY, 86)
            if w > 0
                hist.push(box.new(bar_index, bot + pStep * 0.92, bar_index + w, bot + pStep * 0.08, border_color = na, bgcolor = c))
if inS
    hs.push(high)
    ls.push(low)
    vs.push(volume)
 
bool up = false
bool dn = false
if inS and not start and barstate.isconfirmed and not na(vah)
    dn := high >= vah and close < vah and open < vah and (firstOnly ? not dnDone : true)
    up := low <= val and close > val and open > val and (firstOnly ? not upDone : true)
    if dn
        dnDone := true
    if up
        upDone := true
 
lv(float v) => inS and not start ? v : na
plot(lv(poc), "Prior session POC", color.new(colProf, 5), 1, plot.style_linebr)
plot(lv(vah), "Prior session value area high", color.new(GREY, 25), 1, plot.style_linebr)
plot(lv(val), "Prior session value area low", color.new(GREY, 25), 1, plot.style_linebr)
plotshape(showMarks and up, "Rejection at the value area low", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and dn, "Rejection at the value area high", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
 
var array<label> tags = array.new<label>()
tag(float v, string txt, color c) =>
    if not na(v)
        tags.push(label.new(bar_index + 3, v, txt, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = size.small))
if barstate.islast and showLabels and tfOK
    for t in tags
        t.delete()
    tags.clear()
    tag(poc, "POC", colProf)
    tag(vah, "VAH", GREY)
    tag(val, "VAL", GREY)
 
if useAlerts and up
    alert(syminfo.ticker + " traded through the prior value area low " + str.tostring(val, format.mintick) + " and closed back above it", alert.freq_once_per_bar_close)
if useAlerts and dn
    alert(syminfo.ticker + " traded through the prior value area high " + str.tostring(vah, format.mintick) + " and closed back below it", alert.freq_once_per_bar_close)
 
var table wm = table.new(position.bottom_right, 1, 1)
if barstate.islast and showWm
    wm.cell(0, 0, "BADR EDGE", text_color = color.new(GREY, 72), text_size = size.small)
var table note = table.new(position.top_right, 1, 1)
if barstate.islast and not tfOK
    note.cell(0, 0, "Prior Session Volume Profile works on 1m to 30m charts.", text_color = GOLD, bgcolor = color.new(color.black, 20), text_size = size.small)

← 返回列表