本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Prior Close Percent Grid (Session-Aware)", shorttitle = "PC% Grid", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ── Inputs ──
step = input.float(1.0, "Grid step (%)", minval = 0.05, step = 0.05)
minRange = input.float(30, "Current session: draw at least ± (%)", minval = 0, tooltip = "0 = only cover the range price has actually traded")
nightH = input.int(20, "Overnight session starts at hour", minval = 0, maxval = 23, inline = "n")
nightM = input.int(0, "min", minval = 0, maxval = 59, inline = "n", tooltip = "Exchange time zone. US overnight (BOATS) trading starts at 20:00 ET.")
showHist = input.bool(true, "Draw grid on past sessions")
upCol = input.color(color.green, "Up color")
dnCol = input.color(color.red, "Down color")
lblSize = input.string("Small", "Label size", options = ["Small", "Normal", "Large"])
lblSz = lblSize == "Small" ? size.small : lblSize == "Large" ? size.large : size.normal
// ── Regular-session daily data: close of the current day (c0) and the prior day (c1), with their close times ──
tz = syminfo.timezone
intraday = timeframe.isintraday
regTid = ticker.modify(syminfo.tickerid, session.regular)
tf = intraday ? "D" : timeframe.period
[c0, c1, tc0, tc1] = request.security(regTid, tf, [close, close[1], time_close, time_close[1]], lookahead = barmerge.lookahead_on)
// After the regular close and before the overnight session starts = post-market
int nightStart = na
if not na(tc0)
nightStart := timestamp(tz, year(tc0, tz), month(tc0, tz), dayofmonth(tc0, tz), nightH, nightM, 0)
afterClose = intraday and not na(tc0) and time >= tc0
isPost = afterClose and time < nightStart
// Baseline (0%):
// pre-market and regular hours -> previous regular close
// post-market -> still the previous regular close
// overnight -> today's regular close
// In other words, the trading day starts with the overnight session,
// and the baseline is always the regular close of the previous trading day.
float base = isPost ? c1 : afterClose ? c0 : c1
int baseId = isPost ? tc1 : afterClose ? tc0 : tc1
float pct = (close / base - 1) * 100
// ── Outputs: baseline line, and % change vs baseline shown in the status line on hover ──
plot(base, "Baseline (0%)", color = color.gray, style = plot.style_stepline)
plot(pct, "Change vs baseline (%)", color = pct >= 0 ? upCol : dnCol, display = display.status_line + display.data_window)
// ── Percent grid: limited to each session's own traded range so the candles are not squashed ──
drawGrid(int x1, int x2, float b, float lo, float hi, bool live, array<line> ls, array<label> lbs) =>
if not na(b) and b > 0 and not na(lo) and not na(hi)
int kLo = math.floor((lo / b - 1) * 100 / step)
int kHi = math.ceil((hi / b - 1) * 100 / step)
if live
int kMin = math.ceil(minRange / step)
kLo := math.min(kLo, -kMin)
kHi := math.max(kHi, kMin)
kHi := math.min(kHi, kLo + 150)
for k = kLo to kHi
float p = b * (1 + k * step / 100)
color c = k == 0 ? color.gray : k > 0 ? upCol : dnCol
ls.push(line.new(x1, p, x2, p, extend = live ? extend.right : extend.none, color = color.new(c, 40), style = k == 0 ? line.style_solid : line.style_dotted))
string t = (k > 0 ? "+" : "") + str.tostring(k * step, "#.##") + "%"
lbs.push(label.new(live ? x2 + 2 : x1, p, t, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = live ? lblSz : size.tiny))
newSeg = not na(baseId) and (na(baseId[1]) or baseId != baseId[1])
var int segStart = na
var float segHi = na
var float segLo = na
var float segBase = na
var histL = array.new<line>()
var histLb = array.new<label>()
var curL = array.new<line>()
var curLb = array.new<label>()
if newSeg
if showHist and intraday and not na(segStart)
drawGrid(segStart, bar_index - 1, segBase, segLo, segHi, false, histL, histLb)
histL.clear()
histLb.clear()
segStart := bar_index
segHi := high
segLo := low
segBase := base
else if not na(segStart)
segHi := math.max(segHi, high)
segLo := math.min(segLo, low)
// ── Current session: redrawn on every update; info box shows the baseline and its date ──
var tbl = table.new(position.top_right, 1, 1)
if barstate.islast and not na(segStart)
for l in curL
l.delete()
for lb in curLb
lb.delete()
curL.clear()
curLb.clear()
drawGrid(segStart, bar_index, base, segLo, segHi, true, curL, curLb)
txt = "Base " + str.tostring(base, format.mintick) + " (" + str.format_time(baseId, "MMM d", tz) + " close) " + (pct >= 0 ? "+" : "") + str.tostring(pct, "#.##") + "%"
tbl.cell(0, 0, txt, text_color = pct >= 0 ? upCol : dnCol, text_size = size.normal, bgcolor = color.new(color.gray, 85))