本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MoksNQ Sessions", overlay = true, max_boxes_count = 500, max_lines_count = 500)
// ── Settings ──────────────────────────────────────────────
tz = input.string("America/New_York", "Timezone", display = display.none)
showBoxes = input.bool(true, "Show session boxes", display = display.none)
fillTr = input.int(90, "Box transparency", minval = 0, maxval = 100, display = display.none)
showLevels = input.bool(true, "Show untaken highs/lows", display = display.none)
showSwept = input.bool(true, "Keep swept levels (greyed out)", display = display.none)
sweptCol = input.color(color.new(color.gray, 60), "Swept level colour", display = display.none)
keepDays = input.int(3, "Keep untaken levels for (days)", minval = 1, maxval = 30, display = display.none)
maxLevels = 10
maxBars = math.round(keepDays * 86400 / timeframe.in_seconds())
asiaOn = input.bool(true, "Asia", inline = "asia", display = display.none)
asiaSess = input.session("1800-0000", "", inline = "asia", display = display.none)
asiaCol = input.color(#2962FF, "", inline = "asia", display = display.none)
ldnOn = input.bool(true, "London", inline = "ldn", display = display.none)
ldnSess = input.session("0200-0500", "", inline = "ldn", display = display.none)
ldnCol = input.color(#FF5252, "", inline = "ldn", display = display.none)
nyOn = input.bool(true, "NY AM", inline = "ny", display = display.none)
nySess = input.session("0930-1200", "", inline = "ny", display = display.none)
nyCol = input.color(#4CAF50, "", inline = "ny", display = display.none)
// ── Helpers ───────────────────────────────────────────────
trimOldest(array<line> arr, int n) =>
while array.size(arr) > n
line.delete(array.shift(arr))
// Extend live levels to the current bar; when price trades through one, stop it and grey it out
checkLevels(array<line> live, array<line> swept, bool isHigh, string name) =>
if array.size(live) > 0
for i = array.size(live) - 1 to 0
l = array.get(live, i)
lvl = line.get_y1(l)
line.set_x2(l, bar_index)
if bar_index - line.get_x1(l) > maxBars
array.remove(live, i)
line.delete(l)
else if isHigh ? high > lvl : low < lvl
array.remove(live, i)
if showSwept
line.set_color(l, sweptCol)
line.set_style(l, line.style_dotted)
array.push(swept, l)
else
line.delete(l)
alert(syminfo.ticker + ": " + name + (isHigh ? " high" : " low") + " swept (" + str.tostring(lvl, format.mintick) + ")", alert.freq_once_per_bar)
// ── One session ───────────────────────────────────────────
sessionLevels(bool on, string sess, color col, string name) =>
var float hi = na
var float lo = na
var int hiBar = na
var int loBar = na
var box bx = na
var array<line> liveH = array.new<line>()
var array<line> liveL = array.new<line>()
var array<line> sweptL = array.new<line>()
inS = on and timeframe.isintraday and not na(time(timeframe.period, sess, tz))
start = inS and not inS[1]
ended = not inS and inS[1]
if start
hi := high
lo := low
hiBar := bar_index
loBar := bar_index
if showBoxes
bx := box.new(bar_index, hi, bar_index, lo, border_color = color.new(col, 40), border_style = line.style_dotted, border_width = 1, bgcolor = color.new(col, fillTr))
else if inS
if high > hi
hi := high
hiBar := bar_index
if low < lo
lo := low
loBar := bar_index
if showBoxes
box.set_top(bx, hi)
box.set_bottom(bx, lo)
box.set_right(bx, bar_index)
if ended and showLevels
array.push(liveH, line.new(hiBar, hi, bar_index, hi, color = col, width = 1))
array.push(liveL, line.new(loBar, lo, bar_index, lo, color = col, width = 1))
trimOldest(liveH, maxLevels)
trimOldest(liveL, maxLevels)
if not inS
checkLevels(liveH, sweptL, true, name)
checkLevels(liveL, sweptL, false, name)
trimOldest(sweptL, maxLevels)
sessionLevels(asiaOn, asiaSess, asiaCol, "Asia")
sessionLevels(ldnOn, ldnSess, ldnCol, "London")
sessionLevels(nyOn, nySess, nyCol, "NY AM")