本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("NQ ES Session Volume Profile V5", overlay=true, max_bars_back=5000, max_labels_count=20, max_boxes_count=500)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupProfile = "Volume Profile"
ticksPerRow = input.int(4, "Ticks Per Row", minval=1, maxval=100, group=groupProfile)
vaPercent = input.float(70.0, "Value Area %", minval=50, maxval=99, step=1, group=groupProfile)
sessionMode = input.string("RTH", "Session", options=["RTH", "ETH"], group=groupProfile)
groupDisplay = "Display"
showCurrentLevels = input.bool(true, "Show Current VAH / VAL", group=groupDisplay)
showPreviousLevels = input.bool(true, "Show Previous Session Levels", group=groupDisplay)
showLabels = input.bool(true, "Show Level Labels", group=groupDisplay)
showPocHistory = input.bool(true, "Show Developing POC History", group=groupDisplay)
showCurrentProfile = input.bool(true, "Show Current Profile", group=groupDisplay)
showPreviousProfile = input.bool(true, "Show Previous Profile", group=groupDisplay)
groupHistogram = "Histogram"
profileWidth = input.int(24, "Maximum Profile Width (bars)", minval=5, maxval=100, group=groupHistogram)
profileOffset = input.int(8, "Distance From Current Bar", minval=1, maxval=100, group=groupHistogram)
maxRows = input.int(180, "Maximum Display Groups", minval=20, maxval=220, group=groupHistogram)
groupDiagnostics = "Diagnostics"
showDiagnostics = input.bool(true, "Show POC Diagnostics", group=groupDiagnostics)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SESSION AND FOOTPRINT
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
string sessionHours = sessionMode == "RTH" ? "0930-1600:23456" : "1800-1700:12345"
bool inSession = not na(time(timeframe.period, sessionHours, "America/New_York"))
bool newSession = inSession and not inSession[1]
float rowSize = syminfo.mintick * ticksPerRow
// One native footprint request per indicator.
footprint fp = request.footprint(ticksPerRow, vaPercent)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PROFILE STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var map<int, float> currentProfile = map.new<int, float>()
var map<int, float> previousProfile = map.new<int, float>()
// Save the completed session before starting the next one.
if newSession
if map.size(currentProfile) > 0
previousProfile := map.copy(currentProfile)
map.clear(currentProfile)
// Accumulate every footprint row from every bar in the session.
if inSession and not na(fp)
array<volume_row> footprintRows = fp.rows()
for row in footprintRows
float lowerPrice = row.down_price()
float rowVolume = row.total_volume()
int priceKey = int(math.round(lowerPrice / rowSize))
float oldVolume = map.contains(currentProfile, priceKey) ? map.get(currentProfile, priceKey) : 0.0
map.put(currentProfile, priceKey, oldVolume + rowVolume)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// CURRENT SESSION STATISTICS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
float currentPOC = na
float currentVAH = na
float currentVAL = na
float currentTotalVolume = 0.0
float highestRowVolume = 0.0
float pocRowVolume = 0.0
int currentPriceRows = map.size(currentProfile)
float lowestPriceRow = na
float highestPriceRow = na
int currentPocKey = na
array<int> currentKeys = map.keys(currentProfile)
array.sort(currentKeys, order.ascending)
if array.size(currentKeys) > 0
lowestPriceRow := array.get(currentKeys, 0) * rowSize
highestPriceRow := (array.get(currentKeys, array.size(currentKeys) - 1) + 1) * rowSize
int pocIndex = 0
for i = 0 to array.size(currentKeys) - 1
int key = array.get(currentKeys, i)
float vol = map.get(currentProfile, key)
currentTotalVolume += vol
if vol > highestRowVolume
highestRowVolume := vol
currentPocKey := key
pocIndex := i
if not na(currentPocKey)
pocRowVolume := map.get(currentProfile, currentPocKey)
currentPOC := (currentPocKey + 0.5) * rowSize
// Expand value area from POC, selecting the larger adjacent row.
int lowIndex = pocIndex
int highIndex = pocIndex
float accumulatedVolume = pocRowVolume
float targetVolume = currentTotalVolume * vaPercent / 100.0
while accumulatedVolume < targetVolume and (lowIndex > 0 or highIndex < array.size(currentKeys) - 1)
float lowerVolume = lowIndex > 0 ? map.get(currentProfile, array.get(currentKeys, lowIndex - 1)) : -1.0
float upperVolume = highIndex < array.size(currentKeys) - 1 ? map.get(currentProfile, array.get(currentKeys, highIndex + 1)) : -1.0
if upperVolume > lowerVolume
highIndex += 1
accumulatedVolume += upperVolume
else
if lowIndex > 0
lowIndex -= 1
accumulatedVolume += lowerVolume
else
highIndex += 1
accumulatedVolume += upperVolume
currentVAL := array.get(currentKeys, lowIndex) * rowSize
currentVAH := (array.get(currentKeys, highIndex) + 1) * rowSize
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PREVIOUS SESSION STATISTICS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
float previousPOC = na
float previousVAH = na
float previousVAL = na
float previousTotalVolume = 0.0
int previousPriceRows = map.size(previousProfile)
int previousPocKey = na
array<int> previousKeys = map.keys(previousProfile)
array.sort(previousKeys, order.ascending)
if array.size(previousKeys) > 0
float previousMaxVolume = 0.0
int previousPocIndex = 0
for i = 0 to array.size(previousKeys) - 1
int key = array.get(previousKeys, i)
float vol = map.get(previousProfile, key)
previousTotalVolume += vol
if vol > previousMaxVolume
previousMaxVolume := vol
previousPocKey := key
previousPocIndex := i
if not na(previousPocKey)
previousPOC := (previousPocKey + 0.5) * rowSize
float previousTargetVolume = previousTotalVolume * vaPercent / 100.0
float previousAccumulatedVolume = previousMaxVolume
int previousLowIndex = previousPocIndex
int previousHighIndex = previousPocIndex
while previousAccumulatedVolume < previousTargetVolume and (previousLowIndex > 0 or previousHighIndex < array.size(previousKeys) - 1)
float lowerVolume = previousLowIndex > 0 ? map.get(previousProfile, array.get(previousKeys, previousLowIndex - 1)) : -1.0
float upperVolume = previousHighIndex < array.size(previousKeys) - 1 ? map.get(previousProfile, array.get(previousKeys, previousHighIndex + 1)) : -1.0
if upperVolume > lowerVolume
previousHighIndex += 1
previousAccumulatedVolume += upperVolume
else
if previousLowIndex > 0
previousLowIndex -= 1
previousAccumulatedVolume += lowerVolume
else
previousHighIndex += 1
previousAccumulatedVolume += upperVolume
previousVAL := array.get(previousKeys, previousLowIndex) * rowSize
previousVAH := (array.get(previousKeys, previousHighIndex) + 1) * rowSize
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DEVELOPING POC HISTORY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// This plots the POC value calculated on every bar.
// The stepped line makes each POC change visible through the session.
plot(showPocHistory and inSession ? currentPOC : na,
title="Developing POC History",
color=color.orange,
linewidth=2,
style=plot.style_stepline)
// Current POC is a separate horizontal line at the latest POC price.
var line currentPocLine = na
if barstate.islast
if not na(currentPOC)
if na(currentPocLine)
currentPocLine := line.new(bar_index, currentPOC, bar_index + profileOffset + profileWidth, currentPOC, xloc=xloc.bar_index, color=color.red, width=2)
else
line.set_xy1(currentPocLine, bar_index, currentPOC)
line.set_xy2(currentPocLine, bar_index + profileOffset + profileWidth, currentPOC)
line.set_color(currentPocLine, color.red)
else
if not na(currentPocLine)
line.delete(currentPocLine)
currentPocLine := na
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LEVEL PLOTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(showCurrentLevels ? currentVAH : na, "Current VAH", color=color.lime, linewidth=2, style=plot.style_stepline)
plot(showCurrentLevels ? currentVAL : na, "Current VAL", color=color.aqua, linewidth=2, style=plot.style_stepline)
plot(showPreviousLevels ? previousPOC : na, "Previous POC", color=color.new(color.red, 35), linewidth=1, style=plot.style_line)
plot(showPreviousLevels ? previousVAH : na, "Previous VAH", color=color.new(color.lime, 35), linewidth=1, style=plot.style_line)
plot(showPreviousLevels ? previousVAL : na, "Previous VAL", color=color.new(color.aqua, 35), linewidth=1, style=plot.style_line)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LEVEL LABELS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var label currentPocLabel = na
var label currentVahLabel = na
var label currentValLabel = na
var label previousPocLabel = na
var label previousVahLabel = na
var label previousValLabel = na
if barstate.islast
if not na(currentPocLabel)
label.delete(currentPocLabel)
if not na(currentVahLabel)
label.delete(currentVahLabel)
if not na(currentValLabel)
label.delete(currentValLabel)
if not na(previousPocLabel)
label.delete(previousPocLabel)
if not na(previousVahLabel)
label.delete(previousVahLabel)
if not na(previousValLabel)
label.delete(previousValLabel)
int labelX = bar_index + profileOffset + profileWidth + 1
if showLabels
if not na(currentPOC)
currentPocLabel := label.new(labelX, currentPOC, "POC", xloc=xloc.bar_index, style=label.style_label_left, color=color.red, textcolor=color.white, size=size.tiny)
if not na(currentVAH)
currentVahLabel := label.new(labelX, currentVAH, "VAH", xloc=xloc.bar_index, style=label.style_label_left, color=color.lime, textcolor=color.black, size=size.tiny)
if not na(currentVAL)
currentValLabel := label.new(labelX, currentVAL, "VAL", xloc=xloc.bar_index, style=label.style_label_left, color=color.aqua, textcolor=color.black, size=size.tiny)
if showPreviousLevels
if not na(previousPOC)
previousPocLabel := label.new(labelX, previousPOC, "POC PREV", xloc=xloc.bar_index, style=label.style_label_left, color=color.new(color.red, 35), textcolor=color.white, size=size.tiny)
if not na(previousVAH)
previousVahLabel := label.new(labelX, previousVAH, "VAH PREV", xloc=xloc.bar_index, style=label.style_label_left, color=color.new(color.lime, 25), textcolor=color.black, size=size.tiny)
if not na(previousVAL)
previousValLabel := label.new(labelX, previousVAL, "VAL PREV", xloc=xloc.bar_index, style=label.style_label_left, color=color.new(color.aqua, 25), textcolor=color.black, size=size.tiny)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// HISTOGRAM BOX STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var array<box> currentBoxes = array.new<box>()
var array<box> previousBoxes = array.new<box>()
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DRAW CURRENT PROFILE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if barstate.islast
// Delete old current profile boxes.
while array.size(currentBoxes) > 0
box.delete(array.pop(currentBoxes))
if showCurrentProfile and array.size(currentKeys) > 0
int groupSize = int(math.ceil(array.size(currentKeys) / float(maxRows)))
int groupCount = int(math.ceil(array.size(currentKeys) / float(groupSize)))
float maxGroupVolume = 0.0
// Find the largest grouped volume to normalize the histogram width.
for g = 0 to groupCount - 1
int startIndex = g * groupSize
int endIndex = math.min(startIndex + groupSize - 1, array.size(currentKeys) - 1)
float groupVolume = 0.0
for j = startIndex to endIndex
groupVolume += map.get(currentProfile, array.get(currentKeys, j))
maxGroupVolume := math.max(maxGroupVolume, groupVolume)
// Draw all groups across the complete session price range.
for g = 0 to groupCount - 1
int startIndex = g * groupSize
int endIndex = math.min(startIndex + groupSize - 1, array.size(currentKeys) - 1)
int firstKey = array.get(currentKeys, startIndex)
int lastKey = array.get(currentKeys, endIndex)
float groupVolume = 0.0
for j = startIndex to endIndex
groupVolume += map.get(currentProfile, array.get(currentKeys, j))
float groupBottom = firstKey * rowSize
float groupTop = (lastKey + 1) * rowSize
int barWidth = maxGroupVolume > 0 ? math.max(1, int(math.round(groupVolume / maxGroupVolume * profileWidth))) : 1
bool containsPOC = not na(currentPocKey) and currentPocKey >= firstKey and currentPocKey <= lastKey
float groupMid = (groupBottom + groupTop) / 2.0
bool insideVA = not na(currentVAL) and not na(currentVAH) and groupMid >= currentVAL and groupMid <= currentVAH
color groupColor = containsPOC ? color.new(color.red, 15) : insideVA ? color.new(color.blue, 20) : color.new(color.gray, 35)
box b = box.new(
left=bar_index + profileOffset,
top=groupTop,
right=bar_index + profileOffset + barWidth,
bottom=groupBottom,
xloc=xloc.bar_index,
border_color=groupColor,
bgcolor=groupColor)
array.push(currentBoxes, b)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DRAW PREVIOUS PROFILE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if barstate.islast
// Delete old previous profile boxes.
while array.size(previousBoxes) > 0
box.delete(array.pop(previousBoxes))
if showPreviousProfile and array.size(previousKeys) > 0
int previousGroupSize = int(math.ceil(array.size(previousKeys) / float(maxRows)))
int previousGroupCount = int(math.ceil(array.size(previousKeys) / float(previousGroupSize)))
float previousMaxGroupVolume = 0.0
for g = 0 to previousGroupCount - 1
int startIndex = g * previousGroupSize
int endIndex = math.min(startIndex + previousGroupSize - 1, array.size(previousKeys) - 1)
float groupVolume = 0.0
for j = startIndex to endIndex
groupVolume += map.get(previousProfile, array.get(previousKeys, j))
previousMaxGroupVolume := math.max(previousMaxGroupVolume, groupVolume)
for g = 0 to previousGroupCount - 1
int startIndex = g * previousGroupSize
int endIndex = math.min(startIndex + previousGroupSize - 1, array.size(previousKeys) - 1)
int firstKey = array.get(previousKeys, startIndex)
int lastKey = array.get(previousKeys, endIndex)
float groupVolume = 0.0
for j = startIndex to endIndex
groupVolume += map.get(previousProfile, array.get(previousKeys, j))
float groupBottom = firstKey * rowSize
float groupTop = (lastKey + 1) * rowSize
int barWidth = previousMaxGroupVolume > 0 ? math.max(1, int(math.round(groupVolume / previousMaxGroupVolume * profileWidth))) : 1
bool containsPOC = not na(previousPocKey) and previousPocKey >= firstKey and previousPocKey <= lastKey
float groupMid = (groupBottom + groupTop) / 2.0
bool insideVA = not na(previousVAL) and not na(previousVAH) and groupMid >= previousVAL and groupMid <= previousVAH
color groupColor = containsPOC ? color.new(color.red, 65) : insideVA ? color.new(color.blue, 65) : color.new(color.gray, 70)
box b = box.new(
left=bar_index + profileOffset,
top=groupTop,
right=bar_index + profileOffset + barWidth,
bottom=groupBottom,
xloc=xloc.bar_index,
border_color=groupColor,
bgcolor=groupColor)
array.push(previousBoxes, b)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// POC MOVEMENT DIAGNOSTICS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
bool pocMoved = not na(currentPOC) and not na(currentPOC[1]) and currentPOC != currentPOC[1] and not newSession
var int pocMigrationCount = 0
if newSession
pocMigrationCount := 0
else if pocMoved
pocMigrationCount += 1
string pocMovement = na(currentPOC) ? "WAITING" : newSession ? "INITIALIZING" : pocMoved ? "MOVED" : "UNCHANGED"
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DIAGNOSTIC TABLE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table diagnostics = table.new(position.top_right, 2, 9, border_width=1)
if barstate.islast
if showDiagnostics
table.cell(diagnostics, 0, 0, "POC DIAGNOSTICS", bgcolor=color.black, text_color=color.white)
table.cell(diagnostics, 1, 0, syminfo.ticker, bgcolor=color.black, text_color=color.white)
table.cell(diagnostics, 0, 1, "Current POC")
table.cell(diagnostics, 1, 1, na(currentPOC) ? "—" : str.tostring(currentPOC, format.mintick))
table.cell(diagnostics, 0, 2, "Highest row volume")
table.cell(diagnostics, 1, 2, str.tostring(highestRowVolume, format.volume))
table.cell(diagnostics, 0, 3, "POC row volume")
table.cell(diagnostics, 1, 3, str.tostring(pocRowVolume, format.volume))
table.cell(diagnostics, 0, 4, "Total session volume")
table.cell(diagnostics, 1, 4, str.tostring(currentTotalVolume, format.volume))
table.cell(diagnostics, 0, 5, "Price rows")
table.cell(diagnostics, 1, 5, str.tostring(currentPriceRows))
table.cell(diagnostics, 0, 6, "POC movement")
table.cell(diagnostics, 1, 6, pocMovement, text_color=pocMoved ? color.lime : color.orange)
table.cell(diagnostics, 0, 7, "POC migrations")
table.cell(diagnostics, 1, 7, str.tostring(pocMigrationCount))
table.cell(diagnostics, 0, 8, "Session price range")
table.cell(diagnostics, 1, 8, na(lowestPriceRow) ? "—" : str.tostring(lowestPriceRow, format.mintick) + " - " + str.tostring(highestPriceRow, format.mintick))
else
table.clear(diagnostics, 0, 0, 1, 8)