Alfa Volume Grid

DimmDim · study · 400 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
indicator("Alfa Volume Grid — Volume Profile Levels & Market State", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
 
lookback = input.int(200, "Lookback", minval=1)
bins = input.int(48, "Bins", minval=1)
vaPct = input.float(70.0, "Value Area %", minval=0.0, maxval=100.0)
bufferPct = input.float(5.0, "Buffer %", minval=0.0, step=0.1)
widthThreshold = input.float(12.0, "Width Threshold %", minval=0.0, step=0.1)
fastCalc = input.bool(false, "Fast calculation", tooltip="Computes the volume profile only when the grid needs to be rebuilt, instead of on every bar. Much faster on long histories; the State/Bias/Width readings are then those of the last rebuild rather than of the current bar.")
showBackground = input.bool(true, "Show Background")
displayMode = input.string("Both", "Display", options=["Lines", "Zones", "Both"])
showL7 = input.bool(true, "Show L7 level", tooltip="Toggles level 7 — the intermediate level between L6 (C + 2u·kC) and L8 (C). Turn off if you want the original 15-level grid.")
tfInput = input.timeframe("", "Timeframe", tooltip="Empty = chart timeframe. Set 60 / 240 / 1D / 1W to show higher-timeframe levels on lower timeframes")
effTf = tfInput == "" ? timeframe.period : tfInput
 
fmtPrice(x) => na(x) ? "na" : str.tostring(x, format.mintick)
 
levelColor(idx) =>
    idx == 0 or idx == 1 or idx == 14 or idx == 15 ? color.rgb(239, 83, 80) : idx == 2 or idx == 3 or idx == 12 or idx == 13 ? color.rgb(255, 152, 0) : idx == 4 or idx == 5 or idx == 10 or idx == 11 ? color.rgb(255, 213, 79) : color.rgb(176, 190, 197)
 
levelWidth(idx) =>
    idx == 0 or idx == 2 or idx == 4 or idx == 8 or idx == 10 or idx == 12 or idx == 14 ? 2 : 1
 
calcEngine(_lookback, _bins, _vaPct, _widthThreshold, _bufferPct, _fastCalc) =>
    hasHistory = bar_index >= _lookback and not na(volume)
    pHi = ta.highest(high, _lookback)
    pLo = ta.lowest(low, _lookback)
    stp = hasHistory and not na(pHi) and not na(pLo) ? (pHi - pLo) / _bins : na
    ok = hasHistory and not na(stp) and stp > 0 and not na(volume)
 
    float profilePOC = na
    float profileVAL = na
    float profileVAH = na
    float curC = na
    float curU = na
    float curKC = na
    var float vWidthPct = na
    var string vState = "NA"
    var string vBias = "NA"
 
    var float vPH = na
    var float vPL = na
    var float vC = na
    var float vU = na
    var float vKC = na
    var float vPOC = na
    var float vVAL = na
    var float vVAH = na
    var float vL0 = na
    var float vL1 = na
    var float vL2 = na
    var float vL3 = na
    var float vL4 = na
    var float vL5 = na
    var float vL6 = na
    var float vL7 = na
    var float vL8 = na
    var float vL9 = na
    var float vL10 = na
    var float vL11 = na
    var float vL12 = na
    var float vL13 = na
    var float vL14 = na
    var float vL15 = na
    var int vCnt = 0
 
    bool initN = na(vC) or na(vU) or na(vKC) or na(vPH) or na(vPL)
    bool bufBr = barstate.isconfirmed and not na(vPH) and not na(vPL) and (close > vPH * (1.0 + _bufferPct / 100.0) or close < vPL * (1.0 - _bufferPct / 100.0))
    bool runProfile = ok and (not _fastCalc or initN or bufBr)
 
    if runProfile
        float totalVol = 0.0
        float[] volBins = array.new_float(_bins, 0.0)
 
        for i = 0 to _lookback - 1
            float barLo = low[i]
            float barHi = high[i]
            float barVol = volume[i]
            if not na(barLo) and not na(barHi) and not na(barVol)
                int fB = int(math.floor((barLo - pLo) / stp))
                int lB = int(math.floor((barHi - pLo) / stp))
                fB := math.max(0, math.min(_bins - 1, fB))
                lB := math.max(0, math.min(_bins - 1, lB))
                int cov = math.max(1, lB - fB + 1)
                float sh = barVol / cov
                for b = fB to lB
                    array.set(volBins, b, array.get(volBins, b) + sh)
                totalVol += barVol
 
        int pocIdx = 0
        float pocVol = array.get(volBins, 0)
        for b = 1 to _bins - 1
            float v = array.get(volBins, b)
            if v > pocVol
                pocVol := v
                pocIdx := b
 
        int lowIdx = pocIdx
        int highIdx = pocIdx
        float cumVol = array.get(volBins, pocIdx)
        float targetVol = totalVol * _vaPct / 100.0
        while cumVol < targetVol and (lowIdx > 0 or highIdx < _bins - 1)
            float lv = lowIdx > 0 ? array.get(volBins, lowIdx - 1) : -1.0
            float rv = highIdx < _bins - 1 ? array.get(volBins, highIdx + 1) : -1.0
            if rv > lv
                highIdx += 1
                cumVol += rv
            else if lowIdx > 0
                lowIdx -= 1
                cumVol += lv
            else
                highIdx += 1
                cumVol += rv
 
        profilePOC := pLo + stp * (pocIdx + 0.5)
        profileVAL := pLo + stp * lowIdx
        profileVAH := pLo + stp * (highIdx + 1)
 
        curC := (profileVAH + profileVAL + profilePOC) / 3.0
        curU := (profileVAH - profileVAL) / 2.0
        curKC := curU > 0 ? (profilePOC - (profileVAH + profileVAL) / 2.0) / (3.0 * curU) : na
        vWidthPct := close != 0 ? (pHi - pLo) / close * 100.0 : na
 
        bool belowPOC = close >= profileVAL and close <= profilePOC and not na(profileVAL) and not na(profilePOC)
        bool abovePOC = close > profilePOC and close <= profileVAH and not na(profileVAH) and not na(profilePOC)
        bool tr = (close > profileVAH or close < profileVAL) or (not na(vWidthPct) and vWidthPct >= _widthThreshold)
 
        if belowPOC and not tr and not na(vWidthPct) and vWidthPct < _widthThreshold
            vState := "Accumulation"
            vBias := "RANGE"
        else if abovePOC and not tr and not na(vWidthPct) and vWidthPct < _widthThreshold
            vState := "Distribution"
            vBias := close > profileVAH ? "UP" : "RANGE"
        else if tr
            vState := "Trend"
            vBias := close > profileVAH ? "UP" : close < profileVAL ? "DOWN" : "RANGE"
        else
            vState := "Range"
            vBias := close > profileVAH ? "UP" : close < profileVAL ? "DOWN" : "RANGE"
 
    bool doRebuild = runProfile and not na(curC) and not na(curU) and curU > 0 and not na(curKC) and (initN or bufBr)
 
    if doRebuild
        float totalVol = 0.0
        float[] volBins = array.new_float(_bins, 0.0)
 
        for i = 0 to _lookback - 1
            float barLo = low[i]
            float barHi = high[i]
            float barVol = volume[i]
            if not na(barLo) and not na(barHi) and not na(barVol)
                int fB = int(math.floor((barLo - pLo) / stp))
                int lB = int(math.floor((barHi - pLo) / stp))
                fB := math.max(0, math.min(_bins - 1, fB))
                lB := math.max(0, math.min(_bins - 1, lB))
                int cov = math.max(1, lB - fB + 1)
                float sh = barVol / cov
                for b = fB to lB
                    array.set(volBins, b, array.get(volBins, b) + sh)
                totalVol += barVol
 
        int pocIdx = 0
        float pocVol = array.get(volBins, 0)
        for b = 1 to _bins - 1
            float v = array.get(volBins, b)
            if v > pocVol
                pocVol := v
                pocIdx := b
 
        int lowIdx = pocIdx
        int highIdx = pocIdx
        float cumVol = array.get(volBins, pocIdx)
        float targetVol = totalVol * _vaPct / 100.0
        while cumVol < targetVol and (lowIdx > 0 or highIdx < _bins - 1)
            float lv = lowIdx > 0 ? array.get(volBins, lowIdx - 1) : -1.0
            float rv = highIdx < _bins - 1 ? array.get(volBins, highIdx + 1) : -1.0
            if rv > lv
                highIdx += 1
                cumVol += rv
            else if lowIdx > 0
                lowIdx -= 1
                cumVol += lv
            else
                highIdx += 1
                cumVol += rv
 
        profilePOC := pLo + stp * (pocIdx + 0.5)
        profileVAL := pLo + stp * lowIdx
        profileVAH := pLo + stp * (highIdx + 1)
 
        curC := (profileVAH + profileVAL + profilePOC) / 3.0
        curU := (profileVAH - profileVAL) / 2.0
        curKC := curU > 0 ? (profilePOC - (profileVAH + profileVAL) / 2.0) / (3.0 * curU) : na
        vWidthPct := close != 0 ? (pHi - pLo) / close * 100.0 : na
 
        bool belowPOC = close >= profileVAL and close <= profilePOC and not na(profileVAL) and not na(profilePOC)
        bool abovePOC = close > profilePOC and close <= profileVAH and not na(profileVAH) and not na(profilePOC)
        bool tr = (close > profileVAH or close < profileVAL) or (not na(vWidthPct) and vWidthPct >= _widthThreshold)
 
        if belowPOC and not tr and not na(vWidthPct) and vWidthPct < _widthThreshold
            vState := "Accumulation"
            vBias := "RANGE"
        else if abovePOC and not tr and not na(vWidthPct) and vWidthPct < _widthThreshold
            vState := "Distribution"
            vBias := close > profileVAH ? "UP" : "RANGE"
        else if tr
            vState := "Trend"
            vBias := close > profileVAH ? "UP" : close < profileVAL ? "DOWN" : "RANGE"
        else
            vState := "Range"
            vBias := close > profileVAH ? "UP" : close < profileVAL ? "DOWN" : "RANGE"
 
    if doRebuild
        vPH := pHi
        vPL := pLo
        vC := curC
        vU := curU
        vKC := curKC
        vPOC := profilePOC
        vVAL := profileVAL
        vVAH := profileVAH
        vL0 := vC + vU * 4
        vL1 := vC + vU * (4 + vKC / 4)
        vL2 := vC + vU * 2
        vL3 := vC + vU * (2 + vKC / 4)
        vL4 := vC + vU * (1 + vKC)
        vL5 := vC + vU * (1 + 1.5 * vKC)
        vL6 := vC + vU * (2 * vKC)
        vL7 := vC + vU * vKC
        vL8 := vC
        vL9 := vC + vU * (vKC / 4)
        vL10 := vC + vU * (vKC - 1)
        vL11 := vC + vU * (1.5 * vKC - 1)
        vL12 := vC + vU * -2
        vL13 := vC + vU * (-2 + vKC / 4)
        vL14 := vC + vU * -4
        vL15 := vC + vU * (-4 + vKC / 4)
        vCnt += 1
    else if _fastCalc and not initN and not bufBr
        profilePOC := vPOC
        profileVAL := vVAL
        profileVAH := vVAH
        curC := vC
        curU := vU
        curKC := vKC
        vWidthPct := vWidthPct
        vState := vState
        vBias := vBias
 
    [vL0, vL1, vL2, vL3, vL4, vL5, vL6, vL7, vL8, vL9, vL10, vL11, vL12, vL13, vL14, vL15, vPOC, vVAH, vVAL, vC, vU, vKC, vCnt, vWidthPct, vState, vBias]
 
float cL0 = na
float cL1 = na
float cL2 = na
float cL3 = na
float cL4 = na
float cL5 = na
float cL6 = na
float cL7 = na
float cL8 = na
float cL9 = na
float cL10 = na
float cL11 = na
float cL12 = na
float cL13 = na
float cL14 = na
float cL15 = na
float cPOC = na
float cVAH = na
float cVAL = na
float cC = na
float cU = na
float cKC = na
int cCnt = 0
float cWidth = na
string cState = "NA"
string cBias = "NA"
 
if effTf == timeframe.period
    [eL0, eL1, eL2, eL3, eL4, eL5, eL6, eL7, eL8, eL9, eL10, eL11, eL12, eL13, eL14, eL15, ePOC, eVAH, eVAL, eC, eU, eKC, eCnt, eW, eS, eB] = calcEngine(lookback, bins, vaPct, widthThreshold, bufferPct, fastCalc)
    cL0 := eL0
    cL1 := eL1
    cL2 := eL2
    cL3 := eL3
    cL4 := eL4
    cL5 := eL5
    cL6 := eL6
    cL7 := eL7
    cL8 := eL8
    cL9 := eL9
    cL10 := eL10
    cL11 := eL11
    cL12 := eL12
    cL13 := eL13
    cL14 := eL14
    cL15 := eL15
    cPOC := ePOC
    cVAH := eVAH
    cVAL := eVAL
    cC := eC
    cU := eU
    cKC := eKC
    cCnt := eCnt
    cWidth := eW
    cState := eS
    cBias := eB
else
    [hL0, hL1, hL2, hL3, hL4, hL5, hL6, hL7, hL8, hL9, hL10, hL11, hL12, hL13, hL14, hL15, hPOC, hVAH, hVAL, hC, hU, hKC, hCnt, hW, hS, hB] = request.security(syminfo.tickerid, effTf, calcEngine(lookback, bins, vaPct, widthThreshold, bufferPct, fastCalc), lookahead=barmerge.lookahead_off)
    cL0 := hL0
    cL1 := hL1
    cL2 := hL2
    cL3 := hL3
    cL4 := hL4
    cL5 := hL5
    cL6 := hL6
    cL7 := hL7
    cL8 := hL8
    cL9 := hL9
    cL10 := hL10
    cL11 := hL11
    cL12 := hL12
    cL13 := hL13
    cL14 := hL14
    cL15 := hL15
    cPOC := hPOC
    cVAH := hVAH
    cVAL := hVAL
    cC := hC
    cU := hU
    cKC := hKC
    cCnt := hCnt
    cWidth := hW
    cState := hS
    cBias := hB
 
showLines = displayMode == "Lines" or displayMode == "Both"
showZones = displayMode == "Zones" or displayMode == "Both"
 
stateBg = cState == "Accumulation" ? color.new(color.rgb(38, 166, 154), 88) : cState == "Distribution" ? color.new(color.rgb(239, 83, 80), 88) : cState == "Trend" ? color.new(color.rgb(255, 183, 77), 90) : na
bgcolor(showBackground ? stateBg : na)
 
p0 = plot(showLines ? cL0 : na, "lvl0", color=levelColor(0), linewidth=levelWidth(0))
p1 = plot(showLines ? cL1 : na, "lvl1", color=levelColor(1), linewidth=levelWidth(1))
p2 = plot(showLines ? cL2 : na, "lvl2", color=levelColor(2), linewidth=levelWidth(2))
p3 = plot(showLines ? cL3 : na, "lvl3", color=levelColor(3), linewidth=levelWidth(3))
p4 = plot(showLines ? cL4 : na, "lvl4", color=levelColor(4), linewidth=levelWidth(4))
p5 = plot(showLines ? cL5 : na, "lvl5", color=levelColor(5), linewidth=levelWidth(5))
p6 = plot(showLines ? cL6 : na, "lvl6", color=levelColor(6), linewidth=levelWidth(6))
p7 = plot(showLines and showL7 ? cL7 : na, "lvl7", color=levelColor(7), linewidth=levelWidth(7))
p8 = plot(showLines ? cL8 : na, "lvl8", color=levelColor(8), linewidth=levelWidth(8))
p9 = plot(showLines ? cL9 : na, "lvl9", color=levelColor(9), linewidth=levelWidth(9))
p10 = plot(showLines ? cL10 : na, "lvl10", color=levelColor(10), linewidth=levelWidth(10))
p11 = plot(showLines ? cL11 : na, "lvl11", color=levelColor(11), linewidth=levelWidth(11))
p12 = plot(showLines ? cL12 : na, "lvl12", color=levelColor(12), linewidth=levelWidth(12))
p13 = plot(showLines ? cL13 : na, "lvl13", color=levelColor(13), linewidth=levelWidth(13))
p14 = plot(showLines ? cL14 : na, "lvl14", color=levelColor(14), linewidth=levelWidth(14))
p15 = plot(showLines ? cL15 : na, "lvl15", color=levelColor(15), linewidth=levelWidth(15))
 
fillColor = showZones ? color.new(color.silver, 88) : na
fill(p0, p1, color=fillColor)
fill(p1, p2, color=fillColor)
fill(p2, p3, color=fillColor)
fill(p3, p4, color=fillColor)
fill(p4, p5, color=fillColor)
fill(p5, p6, color=fillColor)
fill(p6, p8, color=showL7 ? na : fillColor)
fill(p6, p7, color=showL7 ? fillColor : na)
fill(p7, p8, color=showL7 ? fillColor : na)
fill(p8, p9, color=fillColor)
fill(p9, p10, color=fillColor)
fill(p10, p11, color=fillColor)
fill(p11, p12, color=fillColor)
fill(p12, p13, color=fillColor)
fill(p13, p14, color=fillColor)
fill(p14, p15, color=fillColor)
 
var table t = table.new(position.top_right, 2, 11, border_width=1)
if barstate.islast
    table.cell(t, 0, 0, "TF", text_color=color.white)
    table.cell(t, 1, 0, effTf, text_color=color.white)
    table.cell(t, 0, 1, "State", text_color=color.white)
    table.cell(t, 1, 1, cState, text_color=color.white)
    table.cell(t, 0, 2, "Bias", text_color=color.white)
    table.cell(t, 1, 2, cBias, text_color=color.white)
    table.cell(t, 0, 3, "POC", text_color=color.white)
    table.cell(t, 1, 3, fmtPrice(cPOC), text_color=color.white)
    table.cell(t, 0, 4, "VAH", text_color=color.white)
    table.cell(t, 1, 4, fmtPrice(cVAH), text_color=color.white)
    table.cell(t, 0, 5, "VAL", text_color=color.white)
    table.cell(t, 1, 5, fmtPrice(cVAL), text_color=color.white)
    table.cell(t, 0, 6, "C / u / kC", text_color=color.white)
    table.cell(t, 1, 6, fmtPrice(cC) + " / " + fmtPrice(cU) + " / " + str.tostring(cKC, "#.####"), text_color=color.white)
    table.cell(t, 0, 7, "Width %", text_color=color.white)
    table.cell(t, 1, 7, na(cWidth) ? "na" : str.tostring(cWidth, "#.##"), text_color=color.white)
    table.cell(t, 0, 8, "Blocks", text_color=color.white)
    table.cell(t, 1, 8, str.tostring(cCnt), text_color=color.white)
    table.cell(t, 0, 9, "Long zone", text_color=color.white)
    table.cell(t, 1, 9, fmtPrice(cL10) + " - " + fmtPrice(cL12), text_color=color.white)
    table.cell(t, 0, 10, "Short zone", text_color=color.white)
    table.cell(t, 1, 10, fmtPrice(cL4) + " - " + fmtPrice(cL2), text_color=color.white)

← 返回列表