Developing Value Area [BOSWaves]

BOSWaves · study · 475 行 · 点赞 140 · 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/
// © BOSWaves
 
//@version=6
indicator("Developing Value Area [BOSWaves]", shorttitle="Developing VA [BOSWaves]", overlay=true,
     max_lines_count=500, max_labels_count=150, max_bars_back=1000)
 
// ┌────────────────────────────── BOSWaves ─ Tooltips ───────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
const string tt_seed       = "Bars used to initialise a new developing auction."
const string tt_maxBars    = "Maximum number of recent bars included in the developing auction. Once reached, the oldest bars drop out as new ones are added."
const string tt_rows       = "Number of price rows the auction's high-low span is divided into when building the volume profile. Higher values give a more precise POC, VAH, and VAL."
const string tt_va         = "Percentage of auction volume contained inside the developing value area."
const string tt_expand     = "Expands the displayed VAH and VAL slightly away from POC without changing the underlying value-area calculation."
const string tt_minBars    = "Minimum number of bars the current auction must contain before a value shift can start a new auction."
const string tt_accept     = "Confirmed closes outside raw value required before the current auction shifts."
const string tt_shiftBuf   = "Distance beyond raw VAH or VAL, as a multiple of ATR, that a close must reach to count toward acceptance. The POC must also be moving in the shift direction."
const string tt_volLen     = "Number of bars used to calculate average volume. Relative volume against this baseline drives volume bar size and the reversal volume filter."
const string tt_volBars    = "Plots volume participation inward from VAH and VAL on each historical bar."
const string tt_barWidth   = "Line width of volume bars at average volume. Bars widen as relative volume rises above average, up to a maximum width of 5."
const string tt_volHistory = "Number of recent bars that keep their volume bars on the chart. The oldest bars are removed first."
const string tt_lowVol     = "Shows below-average volume bars in the Low Volume color. When disabled, only above-average volume bars are plotted."
const string tt_reversal   = "Requires an excursion outside value, sufficient relative volume, and a confirmed return inside before marking a diamond."
const string tt_revBuffer  = "Distance beyond raw VAH or VAL, as a multiple of ATR, that the bar's high or low must reach to start an excursion."
const string tt_revVolume  = "Minimum peak relative volume reached during the excursion for a reversal to qualify."
const string tt_revBars    = "Minimum number of bars the excursion must last, including the bar that closes back inside value."
const string tt_revSpacing = "Minimum number of bars between reversal diamonds."
const string tt_gradient   = "Toggles the gradient fill between VAH, POC, and VAL."
const string tt_poc        = "Toggles the developing point of control line, the price row with the highest auction volume."
const string tt_candles    = "Colors candles by position relative to value. Inside value, candles shift from neutral at the POC toward the Sell color near VAH and the Buy color near VAL. Closes above VAH use the Buy color and closes below VAL use the Sell color."
const string tt_colors     = "Buy colors VAL, the lower gradient, lower volume bars, lower reversal diamonds, and closes above VAH. Sell colors VAH, the upper gradient, upper volume bars, upper reversal diamonds, and closes below VAL."
const string tt_neutral    = "Color used for below-average volume bars."
 
// ┌────────────────────────────── BOSWaves ─ Groups ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
const string g1 = "Developing Auction"
const string g2 = "Value Shift"
const string g3 = "Edge Volume"
const string g4 = "VA Reversals"
const string g5 = "Display"
 
// ┌────────────────────────────── BOSWaves ─ Inputs ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
 
// Developing Auction
int seedBars       = input.int(35, "Initial Auction Bars", minval=10, maxval=100, group=g1, tooltip=tt_seed)
int maxAuctionBars = input.int(220, "Maximum Auction Bars", minval=50, maxval=500, group=g1, tooltip=tt_maxBars)
int profileRows    = input.int(32, "Profile Rows", minval=16, maxval=50, group=g1, tooltip=tt_rows)
float valuePct     = input.float(80.0, "Value Area %", minval=55.0, maxval=90.0, step=1.0, group=g1, tooltip=tt_va)
float vaExpansion  = input.float(0.12, "Display Expansion", minval=0.0, maxval=0.40, step=0.02, group=g1, tooltip=tt_expand)
int minAuctionBars = input.int(20, "Minimum Auction Bars", minval=5, maxval=100, group=g1, tooltip=tt_minBars)
 
// Value Shift
int acceptCloses  = input.int(3, "Acceptance Closes", minval=2, maxval=6, group=g2, tooltip=tt_accept)
float shiftBuffer = input.float(0.10, "Acceptance Buffer ATR", minval=0.0, maxval=1.0, step=0.02, group=g2, tooltip=tt_shiftBuf)
 
// Edge Volume
int volLen           = input.int(20, "Volume Baseline", minval=5, maxval=100, group=g3, tooltip=tt_volLen)
float volumeScale    = input.float(0.92, "Maximum Bar Height", minval=0.25, maxval=1.20, step=0.02, group=g3, tooltip=tt_volBars)
int baseBarWidth     = input.int(2, "Base Bar Width", minval=1, maxval=5, group=g3, tooltip=tt_barWidth)
int volumeHistory    = input.int(220, "Volume Bar History", minval=50, maxval=240, group=g3, tooltip=tt_volHistory)
bool lowVolumeBars   = input.bool(true, "Show Low Volume Bars", group=g3, tooltip=tt_lowVol)
 
// VA Reversals
bool showReversals     = input.bool(true, "Reversal Diamonds", group=g4, tooltip=tt_reversal)
float reversalBuffer   = input.float(0.12, "Minimum Excursion ATR", minval=0.0, maxval=1.0, step=0.02, group=g4, tooltip=tt_revBuffer)
float reversalVolume   = input.float(1.20, "Minimum Relative Volume", minval=0.5, maxval=4.0, step=0.05, group=g4, tooltip=tt_revVolume)
int reversalBars       = input.int(5, "Minimum Excursion Bars", minval=1, maxval=8, group=g4, tooltip=tt_revBars)
int reversalSpacing    = input.int(10, "Minimum Signal Spacing", minval=1, maxval=50, group=g4, tooltip=tt_revSpacing)
 
// Display
bool showGradient = input.bool(true, "Value Area Gradient", group=g5, tooltip=tt_gradient)
bool showPOC      = input.bool(true, "Developing POC", group=g5, tooltip=tt_poc)
bool paintCandles = input.bool(true, "Color Candles", group=g5, tooltip=tt_candles)
 
color buyCol     = input.color(#00FF66, "Buy", inline="COL", group=g5, tooltip=tt_colors)
color sellCol    = input.color(#FF0066, "Sell", inline="COL", group=g5, tooltip=tt_colors)
color neutralCol = input.color(#666666, "Low Volume", group=g5, tooltip=tt_neutral)
 
// ┌────────────────────────────── BOSWaves ─ Core Values ────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float atr = ta.atr(14)
float volBase = ta.sma(volume, volLen)
float relVol = volBase > 0 ? nz(volume) / volBase : 1.0
 
var int auctionStart = na
 
if na(auctionStart) and bar_index >= seedBars
    auctionStart := bar_index - seedBars + 1
 
// ┌────────────────────────────── BOSWaves ─ Value Area Engine ──────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
f_value_area(int bars) =>
    float profileHigh = na
    float profileLow = na
 
    for off = 0 to bars - 1
        profileHigh := na(profileHigh) ? high[off] : math.max(profileHigh, high[off])
        profileLow := na(profileLow) ? low[off] : math.min(profileLow, low[off])
 
    float profileSpan = profileHigh - profileLow
    array<float> bins = array.new_float(profileRows, 0.0)
 
    if profileSpan > syminfo.mintick
        for off = 0 to bars - 1
            float bHigh = high[off]
            float bLow = low[off]
            float bVol = nz(volume[off], 0.0)
            float bSpan = math.max(bHigh - bLow, syminfo.mintick)
 
            for row = 0 to profileRows - 1
                float rowLow = profileLow + profileSpan * row / profileRows
                float rowHigh = profileLow + profileSpan * (row + 1) / profileRows
                float overlap = math.max(0.0, math.min(bHigh, rowHigh) - math.max(bLow, rowLow))
 
                if overlap > 0
                    bins.set(row, bins.get(row) + bVol * overlap / bSpan)
 
    float totalVol = 0.0
    float maxVol = 0.0
    int pocRow = 0
 
    for row = 0 to profileRows - 1
        float rowVol = bins.get(row)
        totalVol += rowVol
 
        if rowVol > maxVol
            maxVol := rowVol
            pocRow := row
 
    int lowRow = pocRow
    int highRow = pocRow
    float accumulated = bins.get(pocRow)
    float target = totalVol * valuePct / 100.0
 
    while accumulated < target and (lowRow > 0 or highRow < profileRows - 1)
        float below = lowRow > 0 ? bins.get(lowRow - 1) : -1.0
        float above = highRow < profileRows - 1 ? bins.get(highRow + 1) : -1.0
 
        if above >= below and highRow < profileRows - 1
            highRow += 1
            accumulated += bins.get(highRow)
        else if lowRow > 0
            lowRow -= 1
            accumulated += bins.get(lowRow)
        else
            highRow += 1
            accumulated += bins.get(highRow)
 
    float rawVAH = profileLow + profileSpan * (highRow + 1) / profileRows
    float rawVAL = profileLow + profileSpan * lowRow / profileRows
    float poc = profileLow + profileSpan * (pocRow + 0.5) / profileRows
 
    [rawVAH, rawVAL, poc]
 
// ┌────────────────────────────── BOSWaves ─ Developing Auction ─────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
int auctionBars = not na(auctionStart) ? bar_index - auctionStart + 1 : 0
auctionBars := math.min(auctionBars, maxAuctionBars)
 
float rawVAH = na
float rawVAL = na
float poc = na
 
if auctionBars >= 2
    [calcVAH, calcVAL, calcPOC] = f_value_area(auctionBars)
    rawVAH := calcVAH
    rawVAL := calcVAL
    poc := calcPOC
 
float upperRawHalf = not na(rawVAH) and not na(poc) ? rawVAH - poc : na
float lowerRawHalf = not na(rawVAL) and not na(poc) ? poc - rawVAL : na
 
float vah = not na(rawVAH) ? rawVAH + upperRawHalf * vaExpansion : na
float val = not na(rawVAL) ? rawVAL - lowerRawHalf * vaExpansion : na
 
float previousVAH = rawVAH[1]
float previousVAL = rawVAL[1]
float previousPOC = poc[1]
 
// ┌────────────────────────────── BOSWaves ─ Reversal State ─────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
var int excursionDir = 0
var int excursionCount = 0
var int excursionBar = na
 
var float excursionExtreme = na
var float excursionPeakVolume = 0.0
 
var bool upperArmed = true
var bool lowerArmed = true
 
var int lastReversalBar = na
var array<label> reversalMarks = array.new<label>()
 
bool upperReversal = false
bool lowerReversal = false
 
// Re-arm one side only after price travels back through the POC.
if not na(poc)
    if close < poc
        upperArmed := true
 
    if close > poc
        lowerArmed := true
 
// ┌────────────────────────────── BOSWaves ─ Acceptance State ───────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
var int acceptUp = 0
var int acceptDown = 0
var int acceptUpStart = na
var int acceptDownStart = na
 
bool shiftUp = false
bool shiftDown = false
 
// ┌────────────────────────────── BOSWaves ─ Auction Events ─────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
if barstate.isconfirmed and not na(previousVAH) and not na(previousVAL) and not na(previousPOC)
    float shiftDistance = atr * shiftBuffer
    float excursionDistance = atr * reversalBuffer
 
    bool acceptedAbove = close > previousVAH + shiftDistance
    bool acceptedBelow = close < previousVAL - shiftDistance
 
    acceptUp := acceptedAbove ? acceptUp + 1 : 0
    acceptDown := acceptedBelow ? acceptDown + 1 : 0
 
    if acceptedAbove and acceptUp == 1
        acceptUpStart := bar_index
 
    if acceptedBelow and acceptDown == 1
        acceptDownStart := bar_index
 
    if not acceptedAbove
        acceptUpStart := na
 
    if not acceptedBelow
        acceptDownStart := na
 
    bool outsideUpper = high > previousVAH + excursionDistance
    bool outsideLower = low < previousVAL - excursionDistance
 
    if excursionDir == 0
        if outsideUpper and upperArmed
            excursionDir := 1
            excursionCount := 1
            excursionBar := bar_index
            excursionExtreme := high
            excursionPeakVolume := relVol
 
        else if outsideLower and lowerArmed
            excursionDir := -1
            excursionCount := 1
            excursionBar := bar_index
            excursionExtreme := low
            excursionPeakVolume := relVol
 
    else
        excursionCount += 1
        excursionPeakVolume := math.max(excursionPeakVolume, relVol)
 
        if excursionDir == 1 and high > excursionExtreme
            excursionExtreme := high
            excursionBar := bar_index
 
        if excursionDir == -1 and low < excursionExtreme
            excursionExtreme := low
            excursionBar := bar_index
 
        bool returnedInside = close <= previousVAH and close >= previousVAL
 
        if returnedInside
            bool enoughBars = excursionCount >= reversalBars
            bool enoughVolume = excursionPeakVolume >= reversalVolume
            bool enoughSpacing = na(lastReversalBar) or excursionBar - lastReversalBar >= reversalSpacing
 
            if enoughBars and enoughVolume and enoughSpacing
                if excursionDir == 1 and acceptUp < acceptCloses
                    upperReversal := true
                    upperArmed := false
                    lastReversalBar := excursionBar
 
                    if showReversals
                        label mark = label.new(
                             excursionBar, excursionExtreme, "",
                             xloc=xloc.bar_index, yloc=yloc.price,
                             style=label.style_diamond,
                             size=size.small,
                             color=sellCol,
                             textcolor=sellCol)
 
                        reversalMarks.push(mark)
 
                if excursionDir == -1 and acceptDown < acceptCloses
                    lowerReversal := true
                    lowerArmed := false
                    lastReversalBar := excursionBar
 
                    if showReversals
                        label mark = label.new(
                             excursionBar, excursionExtreme, "",
                             xloc=xloc.bar_index, yloc=yloc.price,
                             style=label.style_diamond,
                             size=size.small,
                             color=buyCol,
                             textcolor=buyCol)
 
                        reversalMarks.push(mark)
 
            excursionDir := 0
            excursionCount := 0
            excursionBar := na
            excursionExtreme := na
            excursionPeakVolume := 0.0
 
    bool matureAuction = auctionBars >= minAuctionBars
 
    shiftUp := matureAuction and acceptUp >= acceptCloses and poc >= previousPOC
    shiftDown := matureAuction and acceptDown >= acceptCloses and poc <= previousPOC
 
    if shiftUp or shiftDown
        auctionStart := shiftUp ? nz(acceptUpStart, bar_index) : nz(acceptDownStart, bar_index)
 
        acceptUp := 0
        acceptDown := 0
        acceptUpStart := na
        acceptDownStart := na
 
        excursionDir := 0
        excursionCount := 0
        excursionBar := na
        excursionExtreme := na
        excursionPeakVolume := 0.0
 
        upperArmed := true
        lowerArmed := true
 
while reversalMarks.size() > 50
    label.delete(reversalMarks.shift())
 
// ┌────────────────────────────── BOSWaves ─ Value Area Gradient ────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
bool shiftEvent = shiftUp or shiftDown
 
float drawVAH = shiftEvent ? na : vah
float drawVAL = shiftEvent ? na : val
float drawPOC = shiftEvent ? na : poc
 
float u20 = not na(drawVAH) ? drawPOC + (drawVAH - drawPOC) * 0.20 : na
float u40 = not na(drawVAH) ? drawPOC + (drawVAH - drawPOC) * 0.40 : na
float u60 = not na(drawVAH) ? drawPOC + (drawVAH - drawPOC) * 0.60 : na
float u80 = not na(drawVAH) ? drawPOC + (drawVAH - drawPOC) * 0.80 : na
 
float l20 = not na(drawVAL) ? drawPOC - (drawPOC - drawVAL) * 0.20 : na
float l40 = not na(drawVAL) ? drawPOC - (drawPOC - drawVAL) * 0.40 : na
float l60 = not na(drawVAL) ? drawPOC - (drawPOC - drawVAL) * 0.60 : na
float l80 = not na(drawVAL) ? drawPOC - (drawPOC - drawVAL) * 0.80 : na
 
pVAH = plot(drawVAH, "VAH", color=color.new(sellCol, 4), linewidth=2)
pU80 = plot(u80, "", color=na, display=display.none)
pU60 = plot(u60, "", color=na, display=display.none)
pU40 = plot(u40, "", color=na, display=display.none)
pU20 = plot(u20, "", color=na, display=display.none)
 
pPOC = plot(showPOC ? drawPOC : na, "POC", color=color.new(chart.fg_color, 18), linewidth=2)
 
pL20 = plot(l20, "", color=na, display=display.none)
pL40 = plot(l40, "", color=na, display=display.none)
pL60 = plot(l60, "", color=na, display=display.none)
pL80 = plot(l80, "", color=na, display=display.none)
pVAL = plot(drawVAL, "VAL", color=color.new(buyCol, 4), linewidth=2)
 
fill(pVAH, pU80, color=showGradient ? color.new(sellCol, 82) : na)
fill(pU80, pU60, color=showGradient ? color.new(sellCol, 87) : na)
fill(pU60, pU40, color=showGradient ? color.new(sellCol, 91) : na)
fill(pU40, pU20, color=showGradient ? color.new(sellCol, 95) : na)
fill(pU20, pPOC, color=showGradient ? color.new(sellCol, 98) : na)
 
fill(pPOC, pL20, color=showGradient ? color.new(buyCol, 98) : na)
fill(pL20, pL40, color=showGradient ? color.new(buyCol, 95) : na)
fill(pL40, pL60, color=showGradient ? color.new(buyCol, 91) : na)
fill(pL60, pL80, color=showGradient ? color.new(buyCol, 87) : na)
fill(pL80, pVAL, color=showGradient ? color.new(buyCol, 82) : na)
 
// ┌────────────────────────────── BOSWaves ─ Inward Volume Bars ─────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
var array<line> volumeLines = array.new<line>()
 
if barstate.isconfirmed and not na(vah) and not na(val) and not na(poc)
    float upperHalf = math.max(vah - poc, syminfo.mintick)
    float lowerHalf = math.max(poc - val, syminfo.mintick)
 
    float candleSpan = math.max(high - low, syminfo.mintick)
    float closePosition = math.max(0.0, math.min(1.0, (close - low) / candleSpan))
 
    float volumeStrength = math.min(relVol / 2.5, 1.0)
 
    // Prevent normal-volume bars from becoming invisible.
    float visibleVolume = math.max(volumeStrength, lowVolumeBars ? 0.12 : 0.0)
 
    float sellWeight = 0.15 + (1.0 - closePosition) * 0.85
    float buyWeight = 0.15 + closePosition * 0.85
 
    float upperHeight = upperHalf * volumeScale * visibleVolume * sellWeight
    float lowerHeight = lowerHalf * volumeScale * visibleVolume * buyWeight
 
    bool highVolume = relVol >= 1.0
 
    int dynWidth = int(math.max(
         1,
         math.min(5, math.round(baseBarWidth + math.max(relVol - 1.0, 0.0) * 1.25))))
 
    int upperTransparency = highVolume
         ? int(math.max(4.0, 48.0 - math.min(relVol, 3.0) * 13.0))
         : 64
 
    int lowerTransparency = highVolume
         ? int(math.max(4.0, 48.0 - math.min(relVol, 3.0) * 13.0))
         : 64
 
    color upperColor = highVolume ? color.new(sellCol, upperTransparency) : color.new(neutralCol, 64)
    color lowerColor = highVolume ? color.new(buyCol, lowerTransparency) : color.new(neutralCol, 64)
 
    if upperHeight > syminfo.mintick and (highVolume or lowVolumeBars)
        volumeLines.push(line.new(
             bar_index, vah,
             bar_index, vah - upperHeight,
             xloc=xloc.bar_index,
             color=upperColor,
             width=dynWidth))
 
    if lowerHeight > syminfo.mintick and (highVolume or lowVolumeBars)
        volumeLines.push(line.new(
             bar_index, val,
             bar_index, val + lowerHeight,
             xloc=xloc.bar_index,
             color=lowerColor,
             width=dynWidth))
 
while volumeLines.size() > volumeHistory * 2
    line.delete(volumeLines.shift())
 
// ┌────────────────────────────── BOSWaves ─ Candle Coloring ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
color candleColor = na
 
if paintCandles and not na(vah) and not na(val) and not na(poc)
    if close > vah
        candleColor := buyCol
 
    else if close < val
        candleColor := sellCol
 
    else if close >= poc
        float strength = math.min(1.0, (close - poc) / math.max(vah - poc, syminfo.mintick))
        candleColor := color.from_gradient(strength, 0.0, 1.0, chart.fg_color, sellCol)
 
    else
        float strength = math.min(1.0, (poc - close) / math.max(poc - val, syminfo.mintick))
        candleColor := color.from_gradient(strength, 0.0, 1.0, chart.fg_color, buyCol)
 
plotcandle(open,high,low,close, "Candles", candleColor, candleColor, false,bordercolor = candleColor)
// ┌────────────────────────────── BOSWaves ─ Data Window ────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
plot(vah, "Developing VAH", display=display.data_window)
plot(val, "Developing VAL", display=display.data_window)
plot(poc, "Developing POC", display=display.data_window)
 
// ┌────────────────────────────── BOSWaves ─ Alerts ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
alertcondition(upperReversal, "Upper VA Reversal", "Developing VA rejected an upper excursion | {{exchange}}:{{ticker}}")
alertcondition(lowerReversal, "Lower VA Reversal", "Developing VA rejected a lower excursion | {{exchange}}:{{ticker}}")
alertcondition(shiftUp, "Bullish Value Shift", "Developing value accepted higher | {{exchange}}:{{ticker}}")
alertcondition(shiftDown, "Bearish Value Shift", "Developing value accepted lower | {{exchange}}:{{ticker}}")

← 返回列表