本页源码来自 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/
// © TakingProphets
//@version=6
indicator("Quarterly Theory True Opens [TakingProphets]", "Quarterly Theory True Opens [TakingProphets]", overlay=true, max_lines_count=500, max_labels_count=500)
// ============================================================================
// QUARTERLY THEORY TRUE OPENS
// ============================================================================
// Every cycle splits into four quarters, and its True Open is the open of Q2 — the
// price the rest of the cycle is measured from. Below a True Open is that cycle's
// discount, above it its premium.
//
// Session — each 6-hour session (18:00 / 00:00 / 06:00 / 12:00 New York) splits into
// four 90-minute quarters, so its True Open prints at 19:30, 01:30, 07:30, 13:30.
// Day — the day runs 18:00 → 18:00 in four sessions; Q2 (London) opens at 00:00.
// Week — Monday to Thursday are the quarters; Q2 opens Monday 18:00.
// Month — the month's full weeks are the quarters (a partial first week is dropped);
// Q2 is the week of the second Monday, opening Sunday 18:00.
// Year — calendar quarters; Q2 opens with the April 1 trading day.
//
// STACKED TRUE OPENS: when the lower cycle's open sits beyond the higher cycle's open
// in the same direction, with price beyond both, the opens are stacked. Price under a
// session open that is under the day open (and so on) is a discount stack;
// the mirror is a premium stack. The panel counts how many cycles stack.
// ============================================================================
const string TZ = "America/New_York"
var string G_GEN = "General"
var string G_OPENS = "True Opens"
var string G_PANEL = "Stack Panel"
var string G_VIS = "Visual"
// ─── General ───
showPrevious = input.bool(true, "Show Previous Opens", group=G_GEN, tooltip="Off: only the True Open of the cycle in play for each level.\nOn: the last few completed ones stay on the chart, stopped where their cycle ended.")
autoHide = input.bool(true, "Hide Opens Too Small For The Timeframe", group=G_GEN, tooltip="Session opens show on 15m and below, day opens on 1H and below, week opens on 4H and below, month opens on Daily and below.")
// ─── True Opens ───
showSess = input.bool(true, "Session", inline="ses", group=G_OPENS, tooltip="19:30 / 01:30 / 07:30 / 13:30 New York — the open of Q2 of each session.")
colSess = input.color(color.new(#787B86, 0), "", inline="ses", group=G_OPENS)
showDay = input.bool(true, "Day", inline="day", group=G_OPENS, tooltip="00:00 New York — the open of Q2 (London) of the 18:00 → 18:00 day.")
colDay = input.color(color.new(#363A45, 0), "", inline="day", group=G_OPENS)
showWeek = input.bool(true, "Week", inline="wk", group=G_OPENS, tooltip="Monday 18:00 New York — the open of Q2 (Tuesday's trading day) of the week.")
colWeek = input.color(#5B7FA6, "", inline="wk", group=G_OPENS)
showMonth = input.bool(true, "Month", inline="mo", group=G_OPENS, tooltip="Sunday 18:00 before the month's second Monday — the open of Q2 (week 2) of the month.")
colMonth = input.color(#8E7CA8, "", inline="mo", group=G_OPENS)
showYear = input.bool(true, "Year", inline="yr", group=G_OPENS, tooltip="The April 1 trading day open — the open of Q2 of the year.")
colYear = input.color(#B39B72, "", inline="yr", group=G_OPENS)
// ─── Stack Panel ───
showPanel = input.bool(true, "Show", inline="pnl", group=G_PANEL, tooltip="Each True Open with price above / below it, and how many cycles are stacked.")
panelPos = input.string("Top Right", "", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], inline="pnl", group=G_PANEL)
panelSize = input.string("Small", "", options=["Tiny", "Small", "Normal"], inline="pnl", group=G_PANEL)
// ─── Visual ───
lineStyleIn = input.string("Dotted", "Lines", options=["Solid", "Dashed", "Dotted"], inline="ln", group=G_VIS)
lineWidthIn = input.string("Thin", "", options=["Thin", "Medium", "Thick"], inline="ln", group=G_VIS)
showLabels = input.bool(true, "Labels", inline="lb", group=G_VIS)
labelText = input.string("Short", "", options=["Short", "Full"], inline="lb", group=G_VIS, tooltip="Short: TSO / TDO / TWO / TMO / TYO.\nFull: True Session Open, True Day Open, ...")
labelSizeIn = input.string("Small", "", options=["Tiny", "Small", "Normal", "Large"], inline="lb", group=G_VIS, display=display.none)
showPrice = input.bool(true, "Price In Labels", group=G_VIS)
// kept fixed to simplify settings
const int INTERNAL_KEEP_PREV = 4 // completed opens kept per cycle when Show Previous Opens is on
f_style(string s) =>
switch s
"Solid" => line.style_solid
"Dashed" => line.style_dashed
=> line.style_dotted
f_width(string s) =>
switch s
"Medium" => 2
"Thick" => 3
=> 1
f_size(string s) =>
switch s
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
=> size.small
lineStyle = f_style(lineStyleIn)
lineWidth = f_width(lineWidthIn)
lblSize = f_size(labelSizeIn)
// ============================================================================
// ENGINE — each open is computed on a stream whose bars start exactly on its boundary,
// so a True Open is always the open of the bar it prints on, whatever the chart
// timeframe: session / day on the chart up to 30m (a 30m stream above), week / month on
// the daily stream, year on the monthly stream.
// ============================================================================
// NY calendar date of the trading day a bar belongs to (18:00 starts the next day).
f_tradeDay(int t) =>
int s = t + 6 * 3600000
[year(s, TZ), month(s, TZ), dayofmonth(s, TZ), dayofweek(s, TZ)]
// True when this bar is the first at or after the NY clock time hh:mm today, inside the
// same 18:00 trading day. Without that, Sunday's 18:00 bar (previous bar Friday) would
// "cross" every earlier Sunday clock time and print false day and session opens.
f_crossed(int hh, int mm) =>
int ts = timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ), hh, mm)
bool sameDay = time - ts < 86400000 and dayofmonth(ts + 6 * 3600000, TZ) == dayofmonth(time + 6 * 3600000, TZ)
time >= ts and nz(time[1], 0) < ts and sameDay
// Session and day opens — need a stream fine enough for :30 boundaries.
f_fine() =>
var float sO = na, var int sT = na, var int sQ = na
var float dO = na, var int dT = na
bool cAsia = f_crossed(19, 30)
bool cLon = f_crossed(1, 30)
bool cNy = f_crossed(7, 30)
bool cPm = f_crossed(13, 30)
bool cDay = f_crossed(0, 0)
// Session True Opens: Asia 19:30, London 01:30, New York 07:30, PM 13:30
int q = cAsia ? 0 : cLon ? 1 : cNy ? 2 : cPm ? 3 : -1
if q >= 0
sO := open
sT := time
sQ := q
// Day: 00:00
if cDay
dO := open
dT := time
[sO, sT, sQ, dO, dT]
// Week and month opens sit on an 18:00 trading-day boundary, which is exactly where a
// daily bar opens, so the daily stream is exact on every chart.
f_coarse() =>
var float wO = na, var int wT = na
var float mO = na, var int mT = na
[ty, tm, td, tdw] = f_tradeDay(time)
[py, pm, pd, pdw] = f_tradeDay(nz(time[1], time))
bool newTradeDay = not na(time[1]) and (td != pd or tm != pm or ty != py)
if newTradeDay
// Week: Tuesday's trading day starts Monday 18:00
if tdw == dayofweek.tuesday
wO := open
wT := time
// Month: the trading week of the second Monday (Monday dated the 8th–14th)
if tdw == dayofweek.monday and td >= 8 and td <= 14
mO := open
mT := time
[wO, wT, mO, mT]
// Year: Q2 starts with April's first trading day, which is exactly where April's monthly
// bar opens. A monthly stream reaches back years even on a 1-minute chart.
// TradingView only loads higher-timeframe history near the chart's own range, so on a
// low chart April may never be walked through; each monthly bar therefore reaches back
// to the latest April directly.
f_year() =>
int mo = month(time + 6 * 3600000, TZ)
int k = mo >= 4 ? mo - 4 : mo + 8
[open[k], time[k]]
string fineTf = timeframe.in_seconds() <= 1800 ? timeframe.period : "30"
string coarseTf = "D"
[sO, sT, sQ, dO, dT] = request.security(syminfo.tickerid, fineTf, f_fine(), lookahead = barmerge.lookahead_off)
[wO, wT, mO, mT] = request.security(syminfo.tickerid, coarseTf, f_coarse(), lookahead = barmerge.lookahead_off)
[yO, yT] = request.security(syminfo.tickerid, "M", f_year(), lookahead = barmerge.lookahead_off)
// ============================================================================
// DRAWING
// ============================================================================
int chartSec = timeframe.in_seconds()
bool okSess = showSess and (not autoHide or chartSec <= 900)
bool okDay = showDay and (not autoHide or chartSec <= 3600)
bool okWeek = showWeek and (not autoHide or chartSec <= 14400)
bool okMonth = showMonth and (not autoHide or chartSec <= 86400)
bool okYear = showYear
f_name(string shortTxt, string fullTxt) =>
labelText == "Short" ? shortTxt : fullTxt
f_sessName(int q) =>
q == 0 ? "Asia" : q == 1 ? "London" : q == 2 ? "NY" : "PM"
// Where each cycle ends, so a level stops at its cycle's close.
f_endSess(int t) => t + 270 * 60000 // Q2 open + 4.5h = session end
f_endDay(int t) => t + 18 * 3600000 // 00:00 → 18:00
f_endWeek(int t) => t + (3 * 24 + 23) * 3600000 // Mon 18:00 → Fri 17:00
f_endMonth(int t) =>
int s = t + 6 * 3600000
int y = year(s, TZ)
int m = month(s, TZ)
timestamp(TZ, m == 12 ? y + 1 : y, m == 12 ? 1 : m + 1, 1, 0, 0) - 7 * 3600000
f_endYear(int t) =>
timestamp(TZ, year(t + 6 * 3600000, TZ), 12, 31, 17, 0)
// rank orders cycles from the highest (year) down, for merged labels and the panel
type TO
line ln
float px
int t0
int t1
color c
string nm
int rank
var array<TO> sessL = array.new<TO>()
var array<TO> dayL = array.new<TO>()
var array<TO> weekL = array.new<TO>()
var array<TO> monthL = array.new<TO>()
var array<TO> yearL = array.new<TO>()
// A new open replaces the current one. With Show Previous Opens on, the one before it stays
// and stops where its cycle ended; otherwise it is removed.
f_push(array<TO> arr, float px, int t0, int t1, color c, string nm, int rank) =>
if arr.size() > 0
TO old = arr.last()
if showPrevious
line.set_x2(old.ln, old.t1)
else
line.delete(old.ln)
arr.pop()
line ln = line.new(t0, px, t1, px, xloc = xloc.bar_time, color = c, style = lineStyle, width = lineWidth)
arr.push(TO.new(ln, px, t0, t1, c, nm, rank))
while arr.size() > INTERNAL_KEEP_PREV + 1
line.delete(arr.shift().ln)
if okSess and not na(sT) and sT != nz(sT[1])
f_push(sessL, sO, sT, f_endSess(sT), colSess, f_name("TSO " + f_sessName(sQ), "True Session Open · " + f_sessName(sQ)), 0)
if okDay and not na(dT) and dT != nz(dT[1])
f_push(dayL, dO, dT, f_endDay(dT), colDay, f_name("TDO", "True Day Open"), 1)
if okWeek and not na(wT) and wT != nz(wT[1])
f_push(weekL, wO, wT, f_endWeek(wT), colWeek, f_name("TWO", "True Week Open"), 2)
if okMonth and not na(mT) and mT != nz(mT[1])
f_push(monthL, mO, mT, f_endMonth(mT), colMonth, f_name("TMO", "True Month Open"), 3)
if okYear and not na(yT) and yT != nz(yT[1])
f_push(yearL, yO, yT, f_endYear(yT), colYear, f_name("TYO", "True Year Open"), 4)
// ============================================================================
// LABELS — on the end of each line; opens in play end just past the live bar.
// Labels that would sit on top of each other (same end, prices within a fraction of an
// ATR) merge into one, highest cycle first: "TDO + TSO London".
// ============================================================================
float mergeTol = ta.atr(14) * 0.35
var array<label> lbls = array.new<label>()
f_collect(array<TO> arr, int xLive, array<int> xs, array<float> ys, array<string> nms, array<color> cs) =>
int n = arr.size()
if n > 0
for i = n - 1 to 0
TO o = arr.get(i)
bool live = i == n - 1 and o.t1 > time
int x2 = live ? math.min(o.t1, xLive) : o.t1
line.set_x2(o.ln, x2)
xs.push(x2)
ys.push(o.px)
nms.push(o.nm)
cs.push(o.c)
if barstate.islast
int xLive = time + 3 * timeframe.in_seconds() * 1000
array<int> xs = array.new<int>()
array<float> ys = array.new<float>()
array<string> nms = array.new<string>()
array<color> cs = array.new<color>()
// highest cycle first, so it leads a merged label
f_collect(yearL, xLive, xs, ys, nms, cs)
f_collect(monthL, xLive, xs, ys, nms, cs)
f_collect(weekL, xLive, xs, ys, nms, cs)
f_collect(dayL, xLive, xs, ys, nms, cs)
f_collect(sessL, xLive, xs, ys, nms, cs)
while lbls.size() > 0
label.delete(lbls.pop())
int n = xs.size()
if showLabels and n > 0
array<bool> used = array.new_bool(n, false)
for i = 0 to n - 1
if not used.get(i)
used.set(i, true)
string txt = nms.get(i)
if i < n - 1
for j = i + 1 to n - 1
if not used.get(j) and xs.get(j) == xs.get(i) and math.abs(ys.get(j) - ys.get(i)) <= mergeTol
used.set(j, true)
txt += " + " + nms.get(j)
if showPrice
txt += " " + str.tostring(ys.get(i), format.mintick)
color c = cs.get(i)
lbls.push(label.new(xs.get(i), ys.get(i), txt, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(c, 100), textcolor = c, size = lblSize))
// ============================================================================
// STACK PANEL
// ============================================================================
var table panel = na
var int panelRows = 0
f_pos(string p) =>
switch p
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Left" => position.bottom_left
=> position.bottom_right
if barstate.islast and showPanel
sz = f_size(panelSize)
color txt = chart.fg_color
color dim = color.new(chart.fg_color, 45)
// opens in cycle order, lowest first, for the stack count
array<float> pxs = array.new<float>()
array<string> names = array.new<string>()
array<color> cols = array.new<color>()
if okSess and not na(sO)
pxs.push(sO), names.push("Session"), cols.push(colSess)
if okDay and not na(dO)
pxs.push(dO), names.push("Day"), cols.push(colDay)
if okWeek and not na(wO)
pxs.push(wO), names.push("Week"), cols.push(colWeek)
if okMonth and not na(mO)
pxs.push(mO), names.push("Month"), cols.push(colMonth)
if okYear and not na(yO)
pxs.push(yO), names.push("Year"), cols.push(colYear)
// stack: price beyond the lowest cycle's open, each open beyond the next higher one
int n = pxs.size()
int dir = n > 0 ? (close < pxs.get(0) ? 1 : close > pxs.get(0) ? -1 : 0) : 0
int depth = dir != 0 ? 1 : 0
if dir != 0 and n > 1
for i = 1 to n - 1
bool next = dir == 1 ? pxs.get(i - 1) < pxs.get(i) : pxs.get(i - 1) > pxs.get(i)
if next
depth += 1
else
break
string stackTxt = depth >= 2 ? (dir == 1 ? "Discount stack ×" : "Premium stack ×") + str.tostring(depth) : "Not stacked"
color stackCol = depth >= 2 ? (dir == 1 ? #2962FF : #F23645) : dim
// sized to exactly the rows in use, so there is no empty space under the last open
if na(panel) or panelRows != n + 1
table.delete(panel)
panel := table.new(f_pos(panelPos), 4, n + 1, bgcolor = color.new(chart.bg_color, 8), frame_color = color.new(chart.fg_color, 85), frame_width = 1, border_width = 1, border_color = color.new(chart.bg_color, 100))
panelRows := n + 1
table.cell(panel, 0, 0, "TRUE OPENS", text_color = dim, text_size = sz, text_halign = text.align_left)
table.cell(panel, 2, 0, stackTxt, text_color = stackCol, text_size = sz, text_halign = text.align_right)
table.merge_cells(panel, 0, 0, 1, 0)
table.merge_cells(panel, 2, 0, 3, 0)
if n > 0
for i = 0 to n - 1
float px = pxs.get(i)
table.cell(panel, 0, i + 1, "●", text_color = cols.get(i), text_size = sz)
table.cell(panel, 1, i + 1, names.get(i), text_color = txt, text_size = sz, text_halign = text.align_left)
table.cell(panel, 2, i + 1, str.tostring(px, format.mintick), text_color = txt, text_size = sz, text_halign = text.align_right)
table.cell(panel, 3, i + 1, close > px ? "▲ above" : close < px ? "▼ below" : "at", text_color = dim, text_size = sz, text_halign = text.align_right)
// Alerts: price crossing the open of each cycle
bool xDay = ta.cross(close, dO)
bool xWeek = ta.cross(close, wO)
bool xSess = ta.cross(close, sO)
alertcondition(okDay and xDay, "Cross True Day Open", "Price crossed the True Day Open.")
alertcondition(okWeek and xWeek, "Cross True Week Open", "Price crossed the True Week Open.")
alertcondition(okSess and xSess, "Cross True Session Open", "Price crossed the True Session Open.")