ATK/DEF Smart Volume Profile Heatmap

ATTDEFS · study · 164 行 · 点赞 94 · 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
//@version=6
indicator("ATK/DEF Smart Volume Profile Heatmap", shorttitle="ATK/DEF Smart Volume Profile Heatmap", overlay=true, 
     max_boxes_count=500, max_labels_count=100, max_lines_count=50, max_bars_back=5000)
 
// ═══════════════════════════ Inputs ═══════════════════════════
lookbackBars = input.int(150, "VRVP Lookback Period", minval=20, maxval=2000)
numRows      = input.int(50, "Price Rows (Precision)", minval=10, maxval=100)
profileWidth = input.int(30, "Max Profile Width", minval=5, maxval=100)
offsetBars   = input.int(5, "Right Offset (Bars)", minval=1, maxval=50)
direction    = input.string("Right", "Profile Direction", options=["Right", "Left"])
 
// ═══════════════════════════ VRVP Core Calculation ═══════════════════════════
float rangeHigh = ta.highest(high, lookbackBars)
float rangeLow  = ta.lowest(low, lookbackBars)
float priceRange = rangeHigh - rangeLow
float stepSize   = priceRange / numRows
 
var array<float> rowVols = array.new<float>(numRows, 0.0)
var int pocRow = 0 
var float currentRangeLow = 0.0
var float currentRangeHigh = 0.0
var float currentStepSize = 0.0
 
if barstate.islast
    currentStepSize := stepSize
    currentRangeLow := rangeLow
    currentRangeHigh := rangeHigh
 
    array.clear(rowVols)
    for i = 0 to numRows - 1
        array.push(rowVols, 0.0)
        
    for i = 0 to lookbackBars - 1
        float cHigh = high[i]
        float cLow  = low[i]
        float cVol  = volume[i]
        
        if priceRange > 0
            int highRow = int(math.round((cHigh - rangeLow) / stepSize))
            int lowRow  = int(math.round((cLow - rangeLow) / stepSize))
            
            highRow := math.min(numRows - 1, math.max(0, highRow))
            lowRow  := math.min(numRows - 1, math.max(0, lowRow))
            
            int rowCount = highRow - lowRow + 1
            
            if rowCount > 0
                float volPerRow = cVol / rowCount
                for r = lowRow to highRow
                    float currentVol = array.get(rowVols, r)
                    array.set(rowVols, r, currentVol + volPerRow)
 
    float maxVol = 0.0
    pocRow := 0 
    for i = 0 to numRows - 1
        float v = array.get(rowVols, i)
        if v > maxVol
            maxVol := v
            pocRow := i
    maxVol := maxVol == 0 ? 1.0 : maxVol
 
    // ═══════════════════════════ Draw Profile ═══════════════════════════
    var array<box> vrvpBoxes = array.new<box>()
    if array.size(vrvpBoxes) > 0
        for b in vrvpBoxes
            box.delete(b)
        array.clear(vrvpBoxes)
        
    var array<label> vrvpLabels = array.new<label>()
    if array.size(vrvpLabels) > 0
        for lb in vrvpLabels
            label.delete(lb)
        array.clear(vrvpLabels)
 
    int drawBarStart = bar_index + offsetBars
    
    for i = 0 to numRows - 1
        float yBottom = rangeLow + (stepSize * i)
        float yTop    = yBottom + stepSize
        float vol     = array.get(rowVols, i)
        
        float volRatio = vol / maxVol
        int barWidth = math.max(1, int(volRatio * profileWidth))
        
        color boxColor = na
        if volRatio < 0.5
            boxColor := color.from_gradient(volRatio, 0.0, 0.5, color.new(#311B92, 50), color.new(#00BCD4, 30))
        else
            boxColor := color.from_gradient(volRatio, 0.5, 1.0, color.new(#00BCD4, 30), color.new(#FF1744, 10))
            
        if i == pocRow
            boxColor := color.new(#FFEA00, 0)
 
        int leftX = drawBarStart
        int rightX = drawBarStart + barWidth
        if direction == "Left"
            leftX  := bar_index - offsetBars - barWidth
            rightX := bar_index - offsetBars
 
        box b = box.new(left=leftX, top=yTop, right=rightX, bottom=yBottom, 
             xloc=xloc.bar_index, bgcolor=boxColor, border_color=color.new(color.black, 100), border_width=1)
        array.push(vrvpBoxes, b)
        
        if i == pocRow
            string pocTxt = "POC: " + str.tostring(vol, format.volume)
            int lblX = direction == "Right" ? rightX + 1 : leftX - 1
            label lb = label.new(lblX, (yTop + yBottom) / 2, pocTxt, 
                 xloc=xloc.bar_index, style=direction == "Right" ? label.style_label_left : label.style_label_right, 
                 textcolor=color.black, color=color.new(#FFEA00, 0), size=size.small)
            array.push(vrvpLabels, lb)
 
    // ═══════════════════════════ Thermometer & Signals ═══════════════════════════
    float currentPos = (close - rangeLow) / priceRange
    
    int currentRow = int(math.round((close - rangeLow) / stepSize))
    currentRow := math.min(numRows - 1, math.max(0, currentRow))
    float currentVol = array.get(rowVols, currentRow)
    float volRatio = currentVol / maxVol
    
    int thermoX = drawBarStart + profileWidth + 5
    if direction == "Left"
        thermoX := bar_index - offsetBars - profileWidth - 5
 
    line.new(thermoX, rangeHigh, thermoX, rangeLow, xloc=xloc.bar_index, color=color.gray, style=line.style_dashed)
    
    box thermoFill = box.new(left=thermoX - 1, top=close, right=thermoX + 1, bottom=rangeLow, 
         xloc=xloc.bar_index, bgcolor=color.new(#FFEA00, 50), border_color=color.new(color.black, 100))
 
    string signalTxt = "Ranging"
    color  signalCol = color.gray
    string signalDetail = "Normal Area"
    
    if currentPos <= 0.20 and volRatio >= 0.3
        signalTxt := "Bottom Accumulation"
        signalCol := color.rgb(11, 242, 11)
        signalDetail := "Strong Support, Look for Longs"
    else if currentPos >= 0.80 and volRatio >= 0.3
        signalTxt := "Top Distribution"
        signalCol := color.red
        signalDetail := "Strong Resistance, Look for Shorts"
    else if math.abs(currentPos - (pocRow / numRows)) <= 0.05
        signalTxt := "POC Battle"
        signalCol := color.yellow
        signalDetail := "Wait for Breakout"
 
    label thermoLabel = label.new(thermoX + 2, close, "🌡️ " + signalTxt + "\n" + str.tostring(currentPos * 100, "#.#") + "%\n" + signalDetail,
         xloc=xloc.bar_index, style=label.style_label_left, color=color.new(#131722, 10), textcolor=signalCol, size=size.small)
    array.push(vrvpLabels, thermoLabel)
 
// ═══════════════════════════ Panel Info ═══════════════════════════
var table panel = table.new(position.top_right, 2, 4, bgcolor=color.new(#131722, 20), border_width=1)
if barstate.islast
    table.cell(panel, 0, 0, "Range High", text_color=color.white, text_size=size.small)
    table.cell(panel, 1, 0, str.tostring(currentRangeHigh, format.mintick), text_color=color.red, text_size=size.small)
    
    table.cell(panel, 0, 1, "Range Low", text_color=color.white, text_size=size.small)
    table.cell(panel, 1, 1, str.tostring(currentRangeLow, format.mintick), text_color=color.green, text_size=size.small)
    
    table.cell(panel, 0, 2, "POC Price", text_color=color.white, text_size=size.small)
    table.cell(panel, 1, 2, str.tostring(currentRangeLow + (currentStepSize * (pocRow + 0.5)), format.mintick), text_color=color.yellow, text_size=size.small)
    
    table.cell(panel, 0, 3, "Current Temp", text_color=color.white, text_size=size.small)
    table.cell(panel, 1, 3, str.tostring((close - currentRangeLow) / (currentRangeHigh - currentRangeLow) * 100, "#.#") + "%", text_color=color.orange, text_size=size.small)

← 返回列表