Premium Discount Matrix [BOSWaves]

BOSWaves · study · 270 行 · 点赞 261 · 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("Premium Discount Matrix [BOSWaves]", overlay=true, max_boxes_count=100, max_lines_count=20, max_labels_count=20)
 
// ┌────────────────────────────── BOSWaves ─ Inputs ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
const string g1 = "Dealing Range"
const string g2 = "Structure Matrix"
const string g3 = "Display"
 
int swingLen          = input.int(8, "Swing Length", minval = 2, maxval = 30, group = g1,
     tooltip = "Number of bars on each side required to confirm a swing high or low. Higher values define a larger, slower-updating dealing range.")
int maxRangeBars      = input.int(250, "Maximum Range History", minval = 50, maxval = 1000, group = g1,
     tooltip = "Maximum number of bars back the dealing range and matrix can extend. Limits how far left the zones are drawn.")
int atrLen            = input.int(14, "ATR Length", minval = 5, group = g1,
     tooltip = "Number of bars used to calculate the Average True Range, used to normalize pivot rejection strength.")
 
int rows              = input.int(16, "Matrix Rows", minval = 8, maxval = 24, group = g2,
     tooltip = "Number of horizontal rows the dealing range is divided into. Higher values produce a finer, more detailed matrix.")
int internalPivotLen  = input.int(3, "Internal Pivot Length", minval = 1, maxval = 10, group = g2,
     tooltip = "Number of bars on each side required to confirm an internal pivot. Lower values detect more minor structure inside the range.")
int pivotMemory       = input.int(80, "Internal Pivot Memory", minval = 20, maxval = 200, group = g2,
     tooltip = "Maximum number of recent internal pivots stored and used to score each matrix row. Older pivots are discarded first.")
float densityWeight   = input.float(0.60, "Pivot Density Weight", minval = 0.0, maxval = 1.0, step = 0.05, group = g2,
     tooltip = "Influence of pivot count on each row's structure score. Higher values emphasize rows where price has pivoted repeatedly.")
float rejectionWeight = input.float(0.40, "Rejection Weight", minval = 0.0, maxval = 1.0, step = 0.05, group = g2,
     tooltip = "Influence of pivot rejection quality on each row's structure score. Higher values emphasize rows with strong wick rejections and follow-through.")
float eqWidth         = input.float(0.10, "Equilibrium Width", minval = 0.02, maxval = 0.30, step = 0.01, group = g2,
     tooltip = "Width of the equilibrium band around the range midpoint, as a percentage of the total range size.")
 
int extendBars        = input.int(20, "Right Extension", minval = 5, maxval = 100, group = g3,
     tooltip = "Number of bars the matrix, range lines, and labels are projected forward from the current bar.")
bool showRangeEdges   = input.bool(true, "Show Range High / Low", group = g3,
     tooltip = "Toggles visibility of the dealing range high and low lines.")
bool showLabels       = input.bool(true, "Show Labels", group = g3,
     tooltip = "Toggles visibility of the range high, range low, premium, equilibrium, and discount labels.")
bool showInternalPivots = input.bool(false, "Show Internal Pivots", group = g3,
     tooltip = "Toggles markers on the internal pivots used to score the matrix rows.")
 
color premiumCol      = input.color(#FF0066, "Premium", inline = "c1", group = g3,
     tooltip = "Color used for the premium zone and range high elements.")
color discountCol     = input.color(#00FF00, "Discount", inline = "c1", group = g3,
     tooltip = "Color used for the discount zone and range low elements.")
color eqCol           = input.color(#FFFFFF, "Equilibrium", inline = "c1", group = g3,
     tooltip = "Color used for the equilibrium band and midpoint line.")
color textCol         = input.color(#FFFFFF, "Text", group = g3,
     tooltip = "Color used for label text.")
 
// ┌────────────────────────────── BOSWaves ─ Swing Range ────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float atr = ta.atr(atrLen)
float swingHigh = ta.pivothigh(high, swingLen, swingLen)
float swingLow = ta.pivotlow(low, swingLen, swingLen)
 
var float rangeHigh = na
var float rangeLow = na
var int rangeHighBar = na
var int rangeLowBar = na
 
if not na(swingHigh)
    rangeHigh := swingHigh
    rangeHighBar := bar_index - swingLen
 
if not na(swingLow)
    rangeLow := swingLow
    rangeLowBar := bar_index - swingLen
 
bool rangeReady = not na(rangeHigh) and not na(rangeLow) and rangeHigh > rangeLow
float rangeSize = rangeReady ? rangeHigh - rangeLow : na
float equilibrium = rangeReady ? (rangeHigh + rangeLow) * 0.5 : na
 
int rangeStart = rangeReady ? math.max(math.min(rangeHighBar, rangeLowBar), bar_index - maxRangeBars) : bar_index
 
// ┌────────────────────────────── BOSWaves ─ Internal Structure ─────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float iph = ta.pivothigh(high, internalPivotLen, internalPivotLen)
float ipl = ta.pivotlow(low, internalPivotLen, internalPivotLen)
 
var float[] pivotPrice = array.new_float()
var float[] pivotStrength = array.new_float()
var int[] pivotBar = array.new_int()
var int[] pivotType = array.new_int()
 
if not na(iph)
    int pBar = bar_index - internalPivotLen
    float candleRange = math.max(high[internalPivotLen] - low[internalPivotLen], syminfo.mintick)
    float upperWick = high[internalPivotLen] - math.max(open[internalPivotLen], close[internalPivotLen])
    float wickScore = math.min(1.0, upperWick / candleRange * 2.0)
    float rejectDist = atr > 0 ? math.abs(iph - close) / atr : 0.0
    float rejectScore = math.min(1.0, rejectDist / 2.0)
    float quality = math.min(1.0, wickScore * 0.55 + rejectScore * 0.45)
 
    array.push(pivotPrice, iph)
    array.push(pivotStrength, quality)
    array.push(pivotBar, pBar)
    array.push(pivotType, -1)
 
if not na(ipl)
    int pBar = bar_index - internalPivotLen
    float candleRange = math.max(high[internalPivotLen] - low[internalPivotLen], syminfo.mintick)
    float lowerWick = math.min(open[internalPivotLen], close[internalPivotLen]) - low[internalPivotLen]
    float wickScore = math.min(1.0, lowerWick / candleRange * 2.0)
    float rejectDist = atr > 0 ? math.abs(close - ipl) / atr : 0.0
    float rejectScore = math.min(1.0, rejectDist / 2.0)
    float quality = math.min(1.0, wickScore * 0.55 + rejectScore * 0.45)
 
    array.push(pivotPrice, ipl)
    array.push(pivotStrength, quality)
    array.push(pivotBar, pBar)
    array.push(pivotType, 1)
 
while array.size(pivotPrice) > pivotMemory
    array.shift(pivotPrice)
    array.shift(pivotStrength)
    array.shift(pivotBar)
    array.shift(pivotType)
 
// ┌────────────────────────────── BOSWaves ─ Matrix Storage ─────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
var box[] matrixBoxes = array.new_box()
var box[] coreBoxes = array.new_box()
 
if barstate.isfirst
    for i = 0 to 23
        array.push(matrixBoxes, box.new(bar_index, close, bar_index, close, xloc=xloc.bar_index, bgcolor=na, border_color=na))
        array.push(coreBoxes, box.new(bar_index, close, bar_index, close, xloc=xloc.bar_index, bgcolor=na, border_color=na))
 
// ┌────────────────────────────── BOSWaves ─ Range Objects ──────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
var line highLine = na
var line lowLine = na
var line eqLine = na
 
var label highTag = na
var label lowTag = na
var label premiumTag = na
var label eqTag = na
var label discountTag = na
 
if barstate.isfirst
    highLine := line.new(bar_index, close, bar_index, close, xloc=xloc.bar_index)
    lowLine := line.new(bar_index, close, bar_index, close, xloc=xloc.bar_index)
    eqLine := line.new(bar_index, close, bar_index, close, xloc=xloc.bar_index)
 
    highTag := label.new(bar_index, close, "", style=label.style_label_left)
    lowTag := label.new(bar_index, close, "", style=label.style_label_left)
    premiumTag := label.new(bar_index, close, "", style=label.style_label_left)
    eqTag := label.new(bar_index, close, "", style=label.style_label_left)
    discountTag := label.new(bar_index, close, "", style=label.style_label_left)
 
// ┌────────────────────────────── BOSWaves ─ Matrix ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
if barstate.islast
    for i = 0 to 23
        box.set_bgcolor(array.get(matrixBoxes, i), na)
        box.set_border_color(array.get(matrixBoxes, i), na)
        box.set_bgcolor(array.get(coreBoxes, i), na)
        box.set_border_color(array.get(coreBoxes, i), na)
 
    if rangeReady
        float rowSize = rangeSize / rows
        float eqHalf = rangeSize * eqWidth * 0.5
 
        for r = 0 to rows - 1
            float bottom = rangeLow + rowSize * r
            float top = bottom + rowSize
            float mid = (top + bottom) * 0.5
 
            int pivotCount = 0
            float totalReaction = 0.0
 
            if array.size(pivotPrice) > 0
                for p = 0 to array.size(pivotPrice) - 1
                    float pp = array.get(pivotPrice, p)
                    int pb = array.get(pivotBar, p)
 
                    if pb >= rangeStart and pp >= bottom and pp <= top
                        pivotCount += 1
                        totalReaction += array.get(pivotStrength, p)
 
            float densityScore = math.min(1.0, pivotCount / 4.0)
            float reactionScore = pivotCount > 0 ? math.min(1.0, totalReaction / pivotCount) : 0.0
            float structureScore = math.min(1.0, densityScore * densityWeight + reactionScore * rejectionWeight)
 
            bool isEq = mid >= equilibrium - eqHalf and mid <= equilibrium + eqHalf
            bool isPremium = mid > equilibrium + eqHalf
            bool isDiscount = mid < equilibrium - eqHalf
 
            color rowCol = isEq ? eqCol : isPremium ? premiumCol : discountCol
 
            int baseTransparency = isEq ? 94 : 95
            int transparency = int(math.round(baseTransparency - structureScore * 50.0))
            transparency := math.max(30, math.min(98, transparency))
 
            int coreTransparency = int(math.round(97.0 - structureScore * 72.0))
            coreTransparency := math.max(16, math.min(100, coreTransparency))
 
            box rowBox = array.get(matrixBoxes, r)
            box coreBox = array.get(coreBoxes, r)
 
            box.set_left(rowBox, rangeStart)
            box.set_right(rowBox, bar_index + extendBars)
            box.set_top(rowBox, top)
            box.set_bottom(rowBox, bottom)
            box.set_bgcolor(rowBox, color.new(rowCol, transparency))
            box.set_border_color(rowBox, color.new(rowCol, 100))
 
            float coreHeight = rowSize * 0.24
 
            box.set_left(coreBox, rangeStart)
            box.set_right(coreBox, bar_index + extendBars)
            box.set_top(coreBox, mid + coreHeight * 0.5)
            box.set_bottom(coreBox, mid - coreHeight * 0.5)
            box.set_bgcolor(coreBox, structureScore > 0.05 ? color.new(rowCol, coreTransparency) : na)
            box.set_border_color(coreBox, na)
 
        int right = bar_index + extendBars
 
        line.set_xy1(highLine, rangeStart, rangeHigh)
        line.set_xy2(highLine, right, rangeHigh)
        line.set_color(highLine, showRangeEdges ? color.new(premiumCol, 12) : na)
        line.set_width(highLine, 2)
 
        line.set_xy1(lowLine, rangeStart, rangeLow)
        line.set_xy2(lowLine, right, rangeLow)
        line.set_color(lowLine, showRangeEdges ? color.new(discountCol, 12) : na)
        line.set_width(lowLine, 2)
 
        line.set_xy1(eqLine, rangeStart, equilibrium)
        line.set_xy2(eqLine, right, equilibrium)
        line.set_color(eqLine, color.new(eqCol, 58))
        line.set_style(eqLine, line.style_dashed)
 
        label.set_xy(highTag, right + 1, rangeHigh)
        label.set_text(highTag, showLabels ? "RANGE HIGH" : "")
        label.set_color(highTag, showLabels ? color.new(premiumCol, 25) : na)
        label.set_textcolor(highTag, textCol)
 
        label.set_xy(lowTag, right + 1, rangeLow)
        label.set_text(lowTag, showLabels ? "RANGE LOW" : "")
        label.set_color(lowTag, showLabels ? color.new(discountCol, 25) : na)
        label.set_textcolor(lowTag, textCol)
 
        float premiumY = equilibrium + rangeSize * 0.25
        float discountY = equilibrium - rangeSize * 0.25
 
        label.set_xy(premiumTag, right + 1, premiumY)
        label.set_text(premiumTag, showLabels ? "PREMIUM" : "")
        label.set_color(premiumTag, showLabels ? color.new(premiumCol, 58) : na)
        label.set_textcolor(premiumTag, textCol)
 
        label.set_xy(eqTag, right + 1, equilibrium)
        label.set_text(eqTag, showLabels ? "EQ" : "")
        label.set_color(eqTag, showLabels ? color.new(eqCol, 72) : na)
        label.set_textcolor(eqTag, textCol)
 
        label.set_xy(discountTag, right + 1, discountY)
        label.set_text(discountTag, showLabels ? "DISCOUNT" : "")
        label.set_color(discountTag, showLabels ? color.new(discountCol, 58) : na)
        label.set_textcolor(discountTag, textCol)
 
// ┌────────────────────────────── BOSWaves ─ Internal Pivots ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
plotshape(showInternalPivots and not na(iph), title="Internal High", offset=-internalPivotLen, location=location.abovebar, style=shape.diamond, size=size.tiny, color=premiumCol)
plotshape(showInternalPivots and not na(ipl), title="Internal Low", offset=-internalPivotLen, location=location.belowbar, style=shape.diamond, size=size.tiny, color=discountCol)
 
alertcondition(not na(swingHigh), "New Range High", "New dealing range swing high confirmed")
alertcondition(not na(swingLow), "New Range Low", "New dealing range swing low confirmed")

← 返回列表