本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Session Candles (3 per day)", shorttitle="Session Candles", overlay=true, max_boxes_count=100, max_lines_count=200, max_labels_count=50)
// ───────────────────────── Inputs ─────────────────────────
tz = input.string("America/New_York", "Timezone", group="Sessions", tooltip="America/New_York follows EST/EDT automatically.")
s1 = input.session("1800-0100", "Candle 1", group="Sessions", inline="s1")
s2 = input.session("0100-0800", "Candle 2", group="Sessions", inline="s2")
s3 = input.session("0800-1500", "Candle 3", group="Sessions", inline="s3")
n1 = input.string("Asia", "Label 1", group="Labels", inline="n1")
n2 = input.string("London", "Label 2", group="Labels", inline="n2")
n3 = input.string("NY", "Label 3", group="Labels", inline="n3")
offset = input.int(5, "Offset from last bar (bars)", minval=1, group="Layout")
bodyW = input.int(5, "Candle width (bars, odd number looks best)", minval=1, group="Layout")
gapW = input.int(2, "Gap between candles (bars)", minval=0, group="Layout")
showLbl = input.bool(true, "Show labels", group="Layout")
upCol = input.color(color.new(#26a69a, 0), "Bullish", group="Colors", inline="c")
dnCol = input.color(color.new(#ef5350, 0), "Bearish", group="Colors", inline="c")
showHL = input.bool(true, "Show high/low lines into next candle", group="High/Low Lines")
hlCol = input.color(color.new(color.gray, 0), "Line colour", group="High/Low Lines")
hlStyle = input.string("Solid", "Line style", options=["Solid", "Dashed", "Dotted"], group="High/Low Lines")
// ───────────────────────── State ─────────────────────────
var float[] O = array.new_float(3, na)
var float[] H = array.new_float(3, na)
var float[] L = array.new_float(3, na)
var float[] C = array.new_float(3, na)
var bool[] wasIn = array.new_bool(3, false)
var array<box> boxes = array.new<box>()
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()
// ───────────────────────── Session tracking ─────────────────────────
update(int i, bool inSess) =>
if inSess
if not array.get(wasIn, i)
array.set(O, i, open)
array.set(H, i, high)
array.set(L, i, low)
else
array.set(H, i, math.max(array.get(H, i), high))
array.set(L, i, math.min(array.get(L, i), low))
array.set(C, i, close)
array.set(wasIn, i, inSess)
in1 = not na(time(timeframe.period, s1, tz))
in2 = not na(time(timeframe.period, s2, tz))
in3 = not na(time(timeframe.period, s3, tz))
// New cycle starts when candle 1 begins: wipe candles 2 and 3 from the previous day
if in1 and not array.get(wasIn, 0)
for j = 1 to 2
array.set(O, j, na)
array.set(H, j, na)
array.set(L, j, na)
array.set(C, j, na)
update(0, in1)
update(1, in2)
update(2, in3)
// ───────────────────────── Drawing ─────────────────────────
lineStyle(string s) =>
s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
if barstate.islast
for b in boxes
box.delete(b)
for l in lines
line.delete(l)
for lb in labels
label.delete(lb)
array.clear(boxes)
array.clear(lines)
array.clear(labels)
names = array.from(n1, n2, n3)
step = bodyW + gapW
x0 = bar_index + offset
// candles
for i = 0 to 2
o = array.get(O, i)
if not na(o)
h = array.get(H, i)
l = array.get(L, i)
c = array.get(C, i)
col = c >= o ? upCol : dnCol
left = x0 + i * step
right = left + bodyW - 1
mid = left + int(bodyW / 2)
array.push(lines, line.new(mid, h, mid, l, xloc=xloc.bar_index, color=col, width=1))
array.push(boxes, box.new(left, math.max(o, c), right, math.min(o, c), xloc=xloc.bar_index, border_color=col, bgcolor=col))
if showLbl
array.push(labels, label.new(mid, l, array.get(names, i), xloc=xloc.bar_index, style=label.style_label_up, color=color.new(color.gray, 100), textcolor=chart.fg_color, size=size.small))
// high/low of each candle, starting at its wick and extending left to right
// through the NEXT candle only (candle 1 -> candle 2, candle 2 -> candle 3, candle 3 has none)
if showHL
for i = 0 to 1
if not na(array.get(O, i))
hi = array.get(H, i)
lo = array.get(L, i)
xL = x0 + i * step + int(bodyW / 2)
xR = x0 + (i + 1) * step + bodyW - 1
array.push(lines, line.new(xL, hi, xR, hi, xloc=xloc.bar_index, color=hlCol, style=lineStyle(hlStyle), width=1))
array.push(lines, line.new(xL, lo, xR, lo, xloc=xloc.bar_index, color=hlCol, style=lineStyle(hlStyle), width=1))