本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
//@coinzada
indicator("Volume Zone Delta", overlay=false, format=format.volume, max_labels_count=500)
// SETTINGS
ticksPerRow = input.int(10, "Footprint ticks per row", minval=1)
showLabels = input.bool(true, "Show total delta labels")
labelSizeInput = input.string("Large", "Label size", options=["Small", "Normal", "Large", "Huge"])
bullColor = input.color(#089981, "Positive Delta")
bearColor = input.color(#f23645, "Negative Delta")
// FORMAT DELTA
formatDelta(float v) =>
string sign = v > 0 ? "+" : v < 0 ? "-" : ""
float a = math.abs(v)
string n = a >= 1000000 ? str.tostring(a / 1000000, "#.0") + "M" : a >= 1000 ? str.tostring(a / 1000, "#.0") + "K" : str.tostring(a, "#")
sign + n
// FOOTPRINT DATA
footprint fp = request.footprint(ticksPerRow)
bool valid = not na(fp)
float totalVol = valid ? fp.total_volume() : na
float totalDelta = valid ? fp.delta() : na
float lowVol = 0.0
float midVol = 0.0
float highVol = 0.0
float lowDelta = 0.0
float midDelta = 0.0
float highDelta = 0.0
if valid
array<volume_row> rows = fp.rows()
int n = array.size(rows)
if n > 0
// Determine price range from footprint rows
float minPrice = 1e20
float maxPrice = -1e20
for i = 0 to n - 1
volume_row r = array.get(rows, i)
minPrice := math.min(minPrice, r.down_price())
maxPrice := math.max(maxPrice, r.up_price())
float priceRange = math.max(maxPrice - minPrice, syminfo.mintick)
float cut1 = minPrice + priceRange / 3.0
float cut2 = minPrice + priceRange * 2.0 / 3.0
for i = 0 to n - 1
volume_row r = array.get(rows, i)
float price = (r.up_price() + r.down_price()) / 2.0
float v = r.total_volume()
float d = r.delta()
if price < cut1
lowVol += v
lowDelta += d
else if price < cut2
midVol += v
midDelta += d
else
highVol += v
highDelta += d
// STACKED VOLUME BOUNDARIES
float level1 = valid ? lowVol : na
float level2 = valid ? lowVol + midVol : na
float level3 = valid ? lowVol + midVol + highVol : na
// Draw each price-zone segment using floating columns
// Lower zone: 0 to level1
// Middle zone: level1 to level2
// Upper zone: level2 to level3
color lowColor = lowDelta >= 0 ? bullColor : bearColor
color midColor = midDelta >= 0 ? bullColor : bearColor
color highColor = highDelta >= 0 ? bullColor : bearColor
plotcandle(0, level1, 0, level1,
title="Lower Price Zone",
color=lowColor, wickcolor=lowColor, bordercolor=lowColor)
plotcandle(level1, level2, level1, level2,
title="Middle Price Zone",
color=midColor, wickcolor=midColor, bordercolor=midColor)
plotcandle(level2, level3, level2, level3,
title="Upper Price Zone",
color=highColor, wickcolor=highColor, bordercolor=highColor)
// DATA WINDOW
plot(totalVol, "Total Volume", display=display.data_window)
plot(totalDelta, "Total Delta", display=display.data_window)
plot(lowDelta, "Lower Delta", display=display.data_window)
plot(midDelta, "Middle Delta", display=display.data_window)
plot(highDelta, "Upper Delta", display=display.data_window)
/// LABELS
string textSize = labelSizeInput == "Huge" ? size.huge : labelSizeInput == "Large" ? size.large : labelSizeInput == "Normal" ? size.normal : size.small
if showLabels and valid and bar_index >= last_bar_index - 99
// Lower zone delta
if lowVol > 0
label.new(
bar_index,
lowVol / 2,
formatDelta(lowDelta),
style=label.style_none,
textcolor=color.white,
size=textSize
)
// Middle zone delta
if midVol > 0
label.new(
bar_index,
lowVol + midVol / 2,
formatDelta(midDelta),
style=label.style_none,
textcolor=color.white,
size=textSize
)
// Upper zone delta
if highVol > 0
label.new(
bar_index,
lowVol + midVol + highVol / 2,
formatDelta(highDelta),
style=label.style_none,
textcolor=color.white,
size=textSize
)
// Total delta above the volume candle
label.new(
bar_index,
totalVol,
formatDelta(totalDelta),
style=label.style_none,
textcolor=chart.fg_color,
size=textSize,
tooltip="Lower: " + formatDelta(lowDelta) + "\nMiddle: " + formatDelta(midDelta) + "\nUpper: " + formatDelta(highDelta)
)