本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Session Levels + Value Areas", shorttitle="SLVA", overlay=true, max_lines_count=500, max_labels_count=500)
// ============================================================================
// Draws:
// - Tokyo High / Low (default 18:00-04:00 ET)
// - London High / Low (default 03:00-12:00 ET)
// - Previous day NY session High / Low and VAH / VAL / POC (NY session bars only)
// - Previous week High / Low, VAH / VAL and POC (NY session bars only)
// - CURRENT session profile POC / VAH / VAL: built from every bar since the
// Tokyo session started (18:00 ET) and drawn from that bar to the current bar.
//
// Notes:
// - Use on an INTRADAY chart (1m to 5m recommended; lower = more accurate profile).
// - Pine has no tick data, so the volume profile is built from chart bars:
// each bar's volume is spread evenly across the price rows its High-Low range covers.
// ============================================================================
if not timeframe.isintraday
runtime.error("Session Levels + Value Areas must be used on an intraday timeframe.")
// ------------------------------- Inputs -------------------------------------
gSess = "Sessions"
tz = input.string("America/New_York", "Timezone", group=gSess)
tokSess = input.session("1800-0400", "Tokyo session (also starts the current-session profile)", group=gSess)
lonSess = input.session("0300-1200", "London session", group=gSess)
nySess = input.session("0930-1600", "NY session (RTH) - used for PD / PW value areas", group=gSess)
gProf = "Volume Profile"
rows = input.int(120, "Profile rows", minval=20, maxval=500, group=gProf)
vaPct = input.float(70.0, "Value Area %", minval=10.0, maxval=100.0, group=gProf) / 100.0
gShow = "Show / Hide"
showTok = input.bool(true, "Tokyo High / Low", group=gShow)
showLon = input.bool(true, "London High / Low", group=gShow)
showPdVa = input.bool(true, "Prev Day VAH / VAL", group=gShow)
showPdPoc = input.bool(true, "Prev Day POC", group=gShow)
showWkVa = input.bool(true, "Prev Week VAH / VAL", group=gShow)
showWkPoc = input.bool(true, "Prev Week POC", group=gShow)
showPdHl = input.bool(true, "Prev Day High / Low", group=gShow)
showWkHl = input.bool(true, "Prev Week High / Low", group=gShow)
showSvp = input.bool(true, "Current session POC / VAH / VAL (since Tokyo start)", group=gShow)
showLbl = input.bool(true, "Show labels", group=gShow)
lblSize = input.string("normal", "Label size", options=["small", "normal", "large"], group=gShow)
showTable = input.bool(false, "Show level table (all values in a corner box)", group=gShow)
tablePos = input.string(position.top_right, "Table position", options=[position.top_right, position.top_left, position.bottom_right, position.bottom_left], group=gShow)
lineMode = input.string("Rays in right margin", "Line mode", options=["Rays in right margin", "Full lines (with history)"], group=gShow, tooltip="Rays = short line per level drawn only in the empty space right of price, with its label in the middle (keeps the candles clean). Full lines = lines run across the chart from where each level formed. The current-session POC/VAH/VAL lines always run from the Tokyo start to the current bar.")
rayGap = input.int(12, "Ray: gap after last bar (bars)", minval=0, maxval=100, group=gShow, tooltip="Leave room for the current-session labels that sit at the end of the session lines.")
rayLen = input.int(30, "Ray: length (bars)", minval=4, maxval=200, group=gShow)
lblPos = input.string("Right edge of screen", "Label position (Full lines mode only)", options=["Right edge of screen", "Center of screen", "Right of last bar"], group=gShow)
lblOffset = input.int(3, "Offset (bars) - only for 'Right of last bar'", minval=0, maxval=50, group=gShow)
gSty = "Style"
cTok = input.color(color.new(color.fuchsia, 0), "Tokyo", group=gSty)
cLon = input.color(color.new(color.blue, 0), "London", group=gSty)
cPdVa = input.color(color.new(color.orange, 0), "Prev Day VAH/VAL", group=gSty)
cPdPoc = input.color(color.new(color.red, 0), "Prev Day POC", group=gSty)
cWkVa = input.color(color.new(color.teal, 0), "Prev Week VAH/VAL", group=gSty)
cWkPoc = input.color(color.new(color.green, 0), "Prev Week POC", group=gSty)
cPdHl = input.color(color.new(color.gray, 0), "Prev Day High/Low", group=gSty)
cWkHl = input.color(color.new(#3f51b5, 0), "Prev Week High/Low", group=gSty)
cSvpVa = input.color(color.new(#008b8b, 0), "Current session VAH/VAL", group=gSty)
cSvpPoc= input.color(color.new(#b8860b, 0), "Current session POC", group=gSty)
lw = input.int(1, "Line width", minval=1, maxval=4, group=gSty)
raysMode = lineMode == "Rays in right margin"
lblSz = lblSize == "small" ? size.small : lblSize == "large" ? size.large : size.normal
// ------------------------------ Functions -----------------------------------
// Builds a volume profile from stored bars and returns [POC, VAH, VAL]
f_profile(float[] hs, float[] ls, float[] vs, int nRows, float pct) =>
float poc = na
float vah = na
float val = na
int n = array.size(hs)
if n > 0
float hi = array.max(hs)
float lo = array.min(ls)
float step = (hi - lo) / nRows
if step > 0
float[] bins = array.new_float(nRows, 0.0)
for i = 0 to n - 1
float h = array.get(hs, i)
float l = array.get(ls, i)
float v = array.get(vs, i)
int i0 = math.max(0, math.min(nRows - 1, int(math.floor((l - lo) / step))))
int i1 = math.max(0, math.min(nRows - 1, int(math.floor((h - lo) / step))))
float share = v / (i1 - i0 + 1)
for j = i0 to i1
array.set(bins, j, array.get(bins, j) + share)
int pi = array.indexof(bins, array.max(bins))
float total = array.sum(bins)
float cum = array.get(bins, pi)
int up = pi
int dn = pi
while cum < total * pct and (up < nRows - 1 or dn > 0)
float upv = up < nRows - 1 ? array.get(bins, up + 1) : -1.0
float dnv = dn > 0 ? array.get(bins, dn - 1) : -1.0
if upv >= dnv
up += 1
cum += upv
else
dn -= 1
cum += dnv
poc := lo + (pi + 0.5) * step
vah := lo + (up + 1) * step
val := lo + dn * step
[poc, vah, val]
// Closes off the old line (keeps it as history) and starts a new one extending right.
// In rays mode no full-width lines are drawn (returns na).
f_roll(line ln, int x1, float y, color c, int w, sty) =>
line res = na
if not raysMode
if not na(ln)
line.set_extend(ln, extend.none)
line.set_x2(ln, bar_index)
res := line.new(x1, y, bar_index + 1, y, extend=extend.right, color=c, width=w, style=sty)
res
// Adds a level to the label list. Levels within 2 ticks of each other are merged into one
// label (e.g. "PD VAH / PW VAH") so one never hides behind the other.
f_add(float[] ps, string[] ns, color[] cs, float p, string t, color c) =>
if not na(p)
int hit = -1
int n = array.size(ps)
if n > 0
for i = 0 to n - 1
if hit == -1 and math.abs(array.get(ps, i) - p) <= syminfo.mintick * 2
hit := i
if hit >= 0
array.set(ns, hit, array.get(ns, hit) + " / " + t)
else
array.push(ps, p)
array.push(ns, t)
array.push(cs, c)
f_row(table t, int r, string name, float p, color c) =>
table.cell(t, 0, r, name, text_color=color.white, bgcolor=c, text_size=size.small, text_halign=text.align_left)
table.cell(t, 1, r, na(p) ? "n/a" : str.tostring(p, format.mintick), text_color=color.white, bgcolor=color.new(c, 35), text_size=size.small, text_halign=text.align_right)
// ------------------------------ Session flags -------------------------------
inTok = not na(time(timeframe.period, tokSess + ":1234567", tz))
inLon = not na(time(timeframe.period, lonSess + ":1234567", tz))
inNY = not na(time(timeframe.period, nySess + ":1234567", tz))
tokStart = inTok and not inTok[1]
lonStart = inLon and not inLon[1]
nyEnd = inNY[1] and not inNY
newWeek = timeframe.change("W")
// ------------------------------ Tokyo / London ------------------------------
var float tHi = na
var float tLo = na
var line tHiLn = na
var line tLoLn = na
if tokStart
tHi := high
tLo := low
if showTok
tHiLn := f_roll(tHiLn, bar_index, tHi, cTok, lw, line.style_solid)
tLoLn := f_roll(tLoLn, bar_index, tLo, cTok, lw, line.style_solid)
else if inTok
tHi := math.max(tHi, high)
tLo := math.min(tLo, low)
if showTok and not na(tHiLn)
line.set_y1(tHiLn, tHi)
line.set_y2(tHiLn, tHi)
line.set_y1(tLoLn, tLo)
line.set_y2(tLoLn, tLo)
var float lHi = na
var float lLo = na
var line lHiLn = na
var line lLoLn = na
if lonStart
lHi := high
lLo := low
if showLon
lHiLn := f_roll(lHiLn, bar_index, lHi, cLon, lw, line.style_solid)
lLoLn := f_roll(lLoLn, bar_index, lLo, cLon, lw, line.style_solid)
else if inLon
lHi := math.max(lHi, high)
lLo := math.min(lLo, low)
if showLon and not na(lHiLn)
line.set_y1(lHiLn, lHi)
line.set_y2(lHiLn, lHi)
line.set_y1(lLoLn, lLo)
line.set_y2(lLoLn, lLo)
// ------------------------- Previous day NY profile --------------------------
var float[] dH = array.new_float()
var float[] dL = array.new_float()
var float[] dV = array.new_float()
var float pdPoc = na
var float pdVah = na
var float pdVal = na
var line pdPocLn = na
var line pdVahLn = na
var line pdValLn = na
var float pdHigh = na
var float pdLow = na
var line pdHighLn = na
var line pdLowLn = na
if nyEnd
if array.size(dH) > 0
pdHigh := array.max(dH)
pdLow := array.min(dL)
if showPdHl
pdHighLn := f_roll(pdHighLn, bar_index, pdHigh, cPdHl, lw, line.style_solid)
pdLowLn := f_roll(pdLowLn, bar_index, pdLow, cPdHl, lw, line.style_solid)
[p, vh, vl] = f_profile(dH, dL, dV, rows, vaPct)
if not na(p)
pdPoc := p
pdVah := vh
pdVal := vl
if showPdVa
pdVahLn := f_roll(pdVahLn, bar_index, pdVah, cPdVa, lw, line.style_dashed)
pdValLn := f_roll(pdValLn, bar_index, pdVal, cPdVa, lw, line.style_dashed)
if showPdPoc
pdPocLn := f_roll(pdPocLn, bar_index, pdPoc, cPdPoc, lw, line.style_solid)
array.clear(dH)
array.clear(dL)
array.clear(dV)
if inNY
array.push(dH, high)
array.push(dL, low)
array.push(dV, nz(volume))
// ---------------------------- Previous week profile -------------------------
var float[] wH = array.new_float()
var float[] wL = array.new_float()
var float[] wV = array.new_float()
var float wkPoc = na
var float wkVah = na
var float wkVal = na
var line wkPocLn = na
var line wkVahLn = na
var line wkValLn = na
var float pwHigh = na
var float pwLow = na
var line pwHighLn = na
var line pwLowLn = na
if newWeek
if array.size(wH) > 0
pwHigh := array.max(wH)
pwLow := array.min(wL)
if showWkHl
pwHighLn := f_roll(pwHighLn, bar_index, pwHigh, cWkHl, lw + 1, line.style_solid)
pwLowLn := f_roll(pwLowLn, bar_index, pwLow, cWkHl, lw + 1, line.style_solid)
[p, vh, vl] = f_profile(wH, wL, wV, rows, vaPct)
if not na(p)
wkPoc := p
wkVah := vh
wkVal := vl
if showWkVa
wkVahLn := f_roll(wkVahLn, bar_index, wkVah, cWkVa, lw + 1, line.style_dotted)
wkValLn := f_roll(wkValLn, bar_index, wkVal, cWkVa, lw + 1, line.style_dotted)
if showWkPoc
wkPocLn := f_roll(wkPocLn, bar_index, wkPoc, cWkPoc, lw + 1, line.style_dotted)
array.clear(wH)
array.clear(wL)
array.clear(wV)
if inNY
array.push(wH, high)
array.push(wL, low)
array.push(wV, nz(volume))
// ------------------- Current session profile (since Tokyo start) ------------
// Collects every bar from the start of the Tokyo session onward. The profile is
// only calculated on the last bar, so it stays fast.
var float[] sH = array.new_float()
var float[] sL = array.new_float()
var float[] sV = array.new_float()
var int sessStart = na
if tokStart
array.clear(sH)
array.clear(sL)
array.clear(sV)
sessStart := bar_index
if not na(sessStart)
array.push(sH, high)
array.push(sL, low)
array.push(sV, nz(volume))
// ------------------------------ Labels + Table -------------------------------
var label[] lbls = array.new_label()
var line[] rays = array.new_line()
var label[] svpLbls = array.new_label()
var line[] svpLines = array.new_line()
var table tbl = na
if barstate.islast
for lb in lbls
label.delete(lb)
array.clear(lbls)
for lb in svpLbls
label.delete(lb)
array.clear(svpLbls)
for rl in rays
line.delete(rl)
array.clear(rays)
for sl in svpLines
line.delete(sl)
array.clear(svpLines)
if not na(tbl)
table.delete(tbl)
// ---- Current session profile: lines from Tokyo start to current bar ----
float svpPoc = na
float svpVah = na
float svpVal = na
if showSvp and not na(sessStart)
[sp, sv, sl2] = f_profile(sH, sL, sV, rows, vaPct)
svpPoc := sp
svpVah := sv
svpVal := sl2
if not na(sp)
int xEnd = bar_index > sessStart ? bar_index : bar_index + 1
array.push(svpLines, line.new(sessStart, svpVah, xEnd, svpVah, color=cSvpVa, width=lw + 1, style=line.style_dashed))
array.push(svpLines, line.new(sessStart, svpVal, xEnd, svpVal, color=cSvpVa, width=lw + 1, style=line.style_dashed))
array.push(svpLines, line.new(sessStart, svpPoc, xEnd, svpPoc, color=cSvpPoc, width=lw + 1, style=line.style_solid))
if showLbl
array.push(svpLbls, label.new(bar_index + 1, svpVah, "SVP VAH " + str.tostring(svpVah, format.mintick), style=label.style_label_left, color=cSvpVa, textcolor=color.black, size=lblSz))
array.push(svpLbls, label.new(bar_index + 1, svpVal, "SVP VAL " + str.tostring(svpVal, format.mintick), style=label.style_label_left, color=cSvpVa, textcolor=color.black, size=lblSz))
array.push(svpLbls, label.new(bar_index + 1, svpPoc, "SVP POC " + str.tostring(svpPoc, format.mintick), style=label.style_label_left, color=cSvpPoc, textcolor=color.black, size=lblSz))
// ---- Other levels ----
float[] ps = array.new_float()
string[] ns = array.new_string()
color[] cs = array.new_color()
if showTok
f_add(ps, ns, cs, tHi, "Tokyo H", cTok)
f_add(ps, ns, cs, tLo, "Tokyo L", cTok)
if showLon
f_add(ps, ns, cs, lHi, "London H", cLon)
f_add(ps, ns, cs, lLo, "London L", cLon)
if showPdVa
f_add(ps, ns, cs, pdVah, "PD VAH", cPdVa)
f_add(ps, ns, cs, pdVal, "PD VAL", cPdVa)
if showPdPoc
f_add(ps, ns, cs, pdPoc, "PD POC", cPdPoc)
if showPdHl
f_add(ps, ns, cs, pdHigh, "PDH", cPdHl)
f_add(ps, ns, cs, pdLow, "PDL", cPdHl)
if showWkVa
f_add(ps, ns, cs, wkVah, "PW VAH", cWkVa)
f_add(ps, ns, cs, wkVal, "PW VAL", cWkVa)
if showWkPoc
f_add(ps, ns, cs, wkPoc, "PW POC", cWkPoc)
if showWkHl
f_add(ps, ns, cs, pwHigh, "PWH", cWkHl)
f_add(ps, ns, cs, pwLow, "PWL", cWkHl)
if raysMode and array.size(ps) > 0
for i = 0 to array.size(ps) - 1
float rp = array.get(ps, i)
array.push(rays, line.new(bar_index + rayGap, rp, bar_index + rayGap + rayLen, rp, color=array.get(cs, i), width=lw + 1, style=line.style_solid))
if showLbl and array.size(ps) > 0
int tL = chart.left_visible_bar_time
int tR = chart.right_visible_bar_time
bool byTime = (not raysMode) and lblPos != "Right of last bar"
int lx = raysMode ? bar_index + rayGap + int(rayLen / 2) : lblPos == "Center of screen" ? int((tL + tR) / 2) : lblPos == "Right edge of screen" ? tR : bar_index + lblOffset
lxloc = byTime ? xloc.bar_time : xloc.bar_index
lsty = raysMode ? label.style_label_center : lblPos == "Center of screen" ? label.style_label_center : lblPos == "Right edge of screen" ? label.style_label_right : label.style_label_left
for i = 0 to array.size(ps) - 1
float pr = array.get(ps, i)
array.push(lbls, label.new(lx, pr, array.get(ns, i) + " " + str.tostring(pr, format.mintick), xloc=lxloc, style=lsty, color=array.get(cs, i), textcolor=color.white, size=lblSz))
if showTable
tbl := table.new(tablePos, 2, 18, border_width=1)
int r = 0
if showSvp
f_row(tbl, r, "SVP VAH", svpVah, cSvpVa)
f_row(tbl, r + 1, "SVP POC", svpPoc, cSvpPoc)
f_row(tbl, r + 2, "SVP VAL", svpVal, cSvpVa)
r += 3
if showTok
f_row(tbl, r, "Tokyo H", tHi, cTok)
f_row(tbl, r + 1, "Tokyo L", tLo, cTok)
r += 2
if showLon
f_row(tbl, r, "London H", lHi, cLon)
f_row(tbl, r + 1, "London L", lLo, cLon)
r += 2
if showPdVa
f_row(tbl, r, "PD VAH", pdVah, cPdVa)
f_row(tbl, r + 1, "PD VAL", pdVal, cPdVa)
r += 2
if showPdPoc
f_row(tbl, r, "PD POC", pdPoc, cPdPoc)
r += 1
if showPdHl
f_row(tbl, r, "PDH", pdHigh, cPdHl)
f_row(tbl, r + 1, "PDL", pdLow, cPdHl)
r += 2
if showWkVa
f_row(tbl, r, "PW VAH", wkVah, cWkVa)
f_row(tbl, r + 1, "PW VAL", wkVal, cWkVa)
r += 2
if showWkPoc
f_row(tbl, r, "PW POC", wkPoc, cWkPoc)
r += 1
if showWkHl
f_row(tbl, r, "PWH", pwHigh, cWkHl)
f_row(tbl, r + 1, "PWL", pwLow, cWkHl)
r += 2