本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =============================================================================
// Asian & London Session High/Low Levels — Pine Script v6
// Conceptual rewrite of "AsiaSessionHighLowMidLines" (v4).
// + Previous Day (PDH/PDL) and Previous Week (PWH/PWL) levels.
//
// WHAT IT DOES
// • Tracks the Asian (default 18:00–02:00 CT) and London (default 02:00–08:30 CT)
// session highs/lows from real candle highs/lows, plus optional midpoints.
// • Levels develop live while the session is forming, then freeze at session end.
// • Frozen levels extend into the New York session (default 08:30–15:00 CT),
// to a custom time, or indefinitely to the right.
// • Each trading day's levels are an independent object set; the oldest sets are
// deleted automatically once "Historical Sessions to Display" is exceeded.
// • Previous Day High/Low (PDH/PDL) and Previous Week High/Low (PWH/PWL) are drawn
// as fixed horizontal levels, each with its own toggles, styling, extension,
// labels and history depth.
//
// ACCURACY NOTES / LIMITATIONS
// • All session math uses explicit timestamps in the chosen IANA timezone
// (default America/Chicago), so DST shifts are handled automatically.
// • A session belongs to the first New York close that follows it, so the prior
// evening's Asian session maps to the next NY day, and Friday's levels end at
// Friday's NY close (never carried into Monday unless "Extend Right" is used).
// • A session is only drawn if the chart contains the bar BEFORE its start, so
// a session cut off by the beginning of chart history is not drawn as a
// fabricated partial level. Sessions with no bars (weekends, holidays) are
// never drawn.
// • Bars are assigned to a session by their OPEN time. Results are exact when
// the chart timeframe divides 30 minutes evenly (1, 2, 3, 5, 10, 15, 30 min,
// or seconds charts). On other timeframes a bar can straddle a boundary; a
// warning is shown in that case. Use the 1-minute chart for exact levels.
// • How many days you can see depends on loaded chart history: a 1-minute ES/NQ
// chart holds roughly 1,380 bars per day, so 30 days needs ~42k bars.
// • Levels of completed sessions are never recalculated (no repainting). Only
// the session currently forming updates on the realtime bar.
//
// PREVIOUS DAY / WEEK NOTES
// • "Exchange Session (ETH)" mode reads the COMPLETED daily bar of the exchange
// session (CME: 17:00–16:00 CT incl. overnight) via request.security() with
// high[1]/low[1] + lookahead_on — the standard non-repainting idiom: values are
// fixed for the whole trading day and contain no future data. Monday uses
// Friday's session; holidays fall back to the last session that traded.
// • "RTH Only" mode builds the regular-session high/low from the chart's own
// bars (exact on 1-minute). The new level appears on the first bar after the
// RTH close and needs the full RTH session to be on the chart.
// • Weekly levels read the COMPLETED weekly bar (high[1]/low[1]), so they are
// available from the first bar of the new week (Sunday evening open) and
// automatically reflect shortened holiday weeks.
// • With "NY Session Close", PD lines end at that trading day's NY close and PW
// lines run through each NY close of the week, ending at the last one.
// =============================================================================
indicator("Asian & London Session Levels", shorttitle = "Session Levels", overlay = true,
max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// =============================================================================
// SECTION 1 — INPUTS
// =============================================================================
G_GEN = "Session Times"
G_DISP = "Display Controls"
G_END = "Line Extension"
G_ASIA = "Asian Session Style"
G_LDN = "London Session Style"
G_LBL = "Labels"
G_PDW = "Previous Day & Week Levels"
G_PDS = "Previous Day Style"
G_PWS = "Previous Week Style"
G_PDWE = "Previous Day & Week Line Extension"
G_PDWL = "Previous Day & Week Labels"
G_PDWH = "Previous Day & Week History"
// --- Session times ------------------------------------------------------------
tzInput = input.string("America/Chicago", "Timezone", group = G_GEN,
tooltip = "IANA timezone used for every session calculation. Daylight saving time is handled automatically.")
asiaSess = input.session("1800-0200", "Asian Session", group = G_GEN, tooltip = "May cross midnight.")
londonSess = input.session("0200-0830", "London Session", group = G_GEN)
nySess = input.session("0830-1500", "New York Session", group = G_GEN)
// --- Master display controls ---------------------------------------------------
enableAsia = input.bool(true, "Enable Asian Session", group = G_DISP)
enableLondon = input.bool(true, "Enable London Session", group = G_DISP)
showHighs = input.bool(true, "Show Session Highs", group = G_DISP)
showLows = input.bool(true, "Show Session Lows", group = G_DISP)
showMids = input.bool(true, "Show Midpoints", group = G_DISP)
showBoxes = input.bool(true, "Show Session Boxes", group = G_DISP)
showLabels = input.bool(true, "Show Session Labels", group = G_DISP)
showHist = input.bool(true, "Show Historical Sessions", group = G_DISP)
histDays = input.int(5, "Historical Sessions to Display", minval = 1, maxval = 30, group = G_DISP,
tooltip = "Number of trading days of Asian/London levels to keep on the chart. When 'Show Historical Sessions' is off, only the most recent day is shown.")
showNYBg = input.bool(false, "Shade New York Session", group = G_DISP, inline = "ny")
nyBgCol = input.color(color.new(color.gray, 92), "", group = G_DISP, inline = "ny")
// --- Line extension ------------------------------------------------------------
endMode = input.string("NY Session Close", "Lines End At",
options = ["NY Session Close", "Custom Time", "Extend Right"], group = G_END,
tooltip = "NY Session Close: lines stop at the NY close of the same trading day.\nCustom Time: lines stop at the first occurrence of the custom time after the session ends.\nExtend Right: lines extend indefinitely.")
customH = input.int(11, "Custom End Hour", minval = 0, maxval = 23, group = G_END, inline = "ce")
customM = input.int(0, "Minute", minval = 0, maxval = 59, group = G_END, inline = "ce")
// --- Asian session style ---------------------------------------------------------
aShowHi = input.bool(true, "Show Asian High (ASH)", group = G_ASIA)
aShowLo = input.bool(true, "Show Asian Low (ASL)", group = G_ASIA)
aShowMid = input.bool(true, "Show Asian Midpoint (ASM)", group = G_ASIA)
aShowBox = input.bool(true, "Show Asian Session Box", group = G_ASIA)
aHiCol = input.color(color.aqua, "High", group = G_ASIA, inline = "ac")
aLoCol = input.color(color.aqua, "Low", group = G_ASIA, inline = "ac")
aMidCol = input.color(color.orange, "Mid", group = G_ASIA, inline = "ac")
aWidth = input.int(1, "Thickness", minval = 1, maxval = 5, group = G_ASIA, inline = "aw")
aTransp = input.int(0, "Line Transparency", minval = 0, maxval = 100, group = G_ASIA, inline = "aw")
aStyle = input.string("Solid", "High/Low Style", options = ["Solid", "Dotted", "Dashed"], group = G_ASIA)
aMidSty = input.string("Dashed", "Midpoint Style", options = ["Solid", "Dotted", "Dashed"], group = G_ASIA)
aBoxCol = input.color(color.aqua, "Box Color", group = G_ASIA, inline = "ab")
aBoxTr = input.int(85, "Box Transparency", minval = 0, maxval = 100, group = G_ASIA, inline = "ab")
// --- London session style --------------------------------------------------------
lShowHi = input.bool(true, "Show London High (LDH)", group = G_LDN)
lShowLo = input.bool(true, "Show London Low (LDL)", group = G_LDN)
lShowMid = input.bool(true, "Show London Midpoint (LDM)", group = G_LDN)
lShowBox = input.bool(true, "Show London Session Box", group = G_LDN)
lHiCol = input.color(color.purple, "High", group = G_LDN, inline = "lc")
lLoCol = input.color(color.purple, "Low", group = G_LDN, inline = "lc")
lMidCol = input.color(color.yellow, "Mid", group = G_LDN, inline = "lc")
lWidth = input.int(1, "Thickness", minval = 1, maxval = 5, group = G_LDN, inline = "lw")
lTransp = input.int(0, "Line Transparency", minval = 0, maxval = 100, group = G_LDN, inline = "lw")
lStyle = input.string("Solid", "High/Low Style", options = ["Solid", "Dotted", "Dashed"], group = G_LDN)
lMidSty = input.string("Dashed", "Midpoint Style", options = ["Solid", "Dotted", "Dashed"], group = G_LDN)
lBoxCol = input.color(color.purple, "Box Color", group = G_LDN, inline = "lb")
lBoxTr = input.int(85, "Box Transparency", minval = 0, maxval = 100, group = G_LDN, inline = "lb")
// --- Labels ------------------------------------------------------------------------
lblPrice = input.bool(true, "Show Price Values", group = G_LBL)
lblSize = input.string("Small", "Label Size", options = ["Tiny", "Small", "Normal", "Large"], group = G_LBL)
lblUseLine = input.bool(true, "Text Matches Line Color", group = G_LBL, inline = "lt")
lblCol = input.color(color.gray, "Otherwise", group = G_LBL, inline = "lt")
// --- Previous Day & Week: display ---------------------------------------------------
enablePD = input.bool(true, "Enable Previous Day Levels", group = G_PDW)
pdShowHi = input.bool(true, "Show Previous Day High (PDH)", group = G_PDW)
pdShowLo = input.bool(true, "Show Previous Day Low (PDL)", group = G_PDW)
pdCalc = input.string("Exchange Session (ETH)", "Previous Day Calculation",
options = ["Exchange Session (ETH)", "RTH Only"], group = G_PDW,
tooltip = "Exchange Session (ETH): high/low of the exchange-defined daily session, including overnight trading (CME: 17:00–16:00 CT).\nRTH Only: high/low of the regular session defined below, built from chart bars (use the 1-minute chart).")
rthSess = input.session("0830-1515", "RTH Session (RTH Only mode)", group = G_PDW,
tooltip = "Regular trading hours in the Timezone above. CME equity-index futures RTH is 08:30–15:15 CT; use 0830-1500 to match the cash session.")
enablePW = input.bool(true, "Enable Previous Week Levels", group = G_PDW)
pwShowHi = input.bool(true, "Show Previous Week High (PWH)", group = G_PDW)
pwShowLo = input.bool(true, "Show Previous Week Low (PWL)", group = G_PDW)
// --- Previous Day style ---------------------------------------------------------------
pdHiCol = input.color(color.green, "PDH", group = G_PDS, inline = "pdc")
pdLoCol = input.color(color.red, "PDL", group = G_PDS, inline = "pdc")
pdWidth = input.int(1, "Thickness", minval = 1, maxval = 5, group = G_PDS, inline = "pdw")
pdTransp = input.int(0, "Transparency", minval = 0, maxval = 100, group = G_PDS, inline = "pdw")
pdStyle = input.string("Solid", "Line Style", options = ["Solid", "Dotted", "Dashed"], group = G_PDS)
// --- Previous Week style --------------------------------------------------------------
pwHiCol = input.color(color.blue, "PWH", group = G_PWS, inline = "pwc")
pwLoCol = input.color(color.purple, "PWL", group = G_PWS, inline = "pwc")
pwWidth = input.int(2, "Thickness", minval = 1, maxval = 5, group = G_PWS, inline = "pww")
pwTransp = input.int(0, "Transparency", minval = 0, maxval = 100, group = G_PWS, inline = "pww")
pwStyle = input.string("Dashed", "Line Style", options = ["Solid", "Dotted", "Dashed"], group = G_PWS)
// --- Previous Day & Week extension -------------------------------------------------------
pdEndMode = input.string("Follow Global Setting", "Previous Day Lines End At",
options = ["Follow Global Setting", "NY Session Close", "Custom Time", "Extend Right"], group = G_PDWE,
tooltip = "Follow Global Setting uses the 'Line Extension' setting above (including its custom time).")
pwEndMode = input.string("Follow Global Setting", "Previous Week Lines End At",
options = ["Follow Global Setting", "NY Session Close", "Custom Time", "Extend Right"], group = G_PDWE,
tooltip = "Weekly lines run through every NY close (or custom time) of the week and stop at the last one.")
htfCustH = input.int(11, "Own Custom End Hour", minval = 0, maxval = 23, group = G_PDWE, inline = "hce")
htfCustM = input.int(0, "Minute", minval = 0, maxval = 59, group = G_PDWE, inline = "hce",
tooltip = "Used only when a Previous Day/Week extension is set to 'Custom Time' (not 'Follow Global Setting').")
// --- Previous Day & Week labels ------------------------------------------------------------
htfShowLbl = input.bool(true, "Show Labels", group = G_PDWL)
htfLblPrice = input.bool(true, "Show Price Values", group = G_PDWL)
htfLblSize = input.string("Small", "Label Size", options = ["Tiny", "Small", "Normal", "Large"], group = G_PDWL)
htfLblMatch = input.bool(true, "Text Matches Line Color", group = G_PDWL, inline = "hlt")
htfLblCol = input.color(color.gray, "Otherwise", group = G_PDWL, inline = "hlt")
// --- Previous Day & Week history -------------------------------------------------------------
showHistPD = input.bool(true, "Show Historical Previous Day Levels", group = G_PDWH)
histPD = input.int(5, "Number of Historical Days to Display", minval = 1, maxval = 30, group = G_PDWH)
showHistPW = input.bool(true, "Show Historical Previous Week Levels", group = G_PDWH)
histPW = input.int(4, "Number of Historical Weeks to Display", minval = 1, maxval = 52, group = G_PDWH,
tooltip = "When a history toggle is off, only the currently applicable level is shown.")
// =============================================================================
// SECTION 2 — HELPER FUNCTIONS
// =============================================================================
const int DAY_MS = 86400000
f_lineStyle(string s) =>
switch s
"Dotted" => line.style_dotted
"Dashed" => line.style_dashed
=> line.style_solid
f_lblSize(string s) =>
switch s
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
=> size.small
// Parse "HHMM-HHMM" session strings.
f_hhmm(string s, int pos) =>
int(str.tonumber(str.substring(s, pos, pos + 2)))
// Timestamp of hour:minute on the local calendar date of `t`.
f_at(int t, int h, int m) =>
timestamp(tzInput, year(t, tzInput), month(t, tzInput), dayofmonth(t, tzInput), h, m, 0)
// A timestamp guaranteed to fall on the next / previous local calendar date (DST-safe:
// anchored at local noon, so a 23h or 25h day can never skip or repeat a date).
f_nextDay(int t) =>
f_at(t, 12, 0) + DAY_MS
f_prevDay(int t) =>
f_at(t, 12, 0) - DAY_MS
// First occurrence of hour:minute strictly after `t`.
f_nextAt(int t, int h, int m) =>
int x = f_at(t, h, m)
if x <= t
x := f_at(f_nextDay(t), h, m)
x
// Session window [start, end) containing or most recently started before `t`.
// Handles sessions that cross midnight.
f_window(int t, int sH, int sM, int eH, int eM) =>
int st = f_at(t, sH, sM)
if st > t
st := f_at(f_prevDay(t), sH, sM)
int en = f_at(st, eH, eM)
if en <= st
en := f_at(f_nextDay(st), eH, eM)
[st, en]
f_text(string tag, float p) =>
lblPrice ? tag + " " + str.tostring(p, format.mintick) : tag
// =============================================================================
// SECTION 3 — SESSION TIME PARSING
// =============================================================================
aSH = f_hhmm(asiaSess, 0), aSM = f_hhmm(asiaSess, 2), aEH = f_hhmm(asiaSess, 5), aEM = f_hhmm(asiaSess, 7)
lSH = f_hhmm(londonSess, 0), lSM = f_hhmm(londonSess, 2), lEH = f_hhmm(londonSess, 5), lEM = f_hhmm(londonSess, 7)
nSH = f_hhmm(nySess, 0), nSM = f_hhmm(nySess, 2), nEH = f_hhmm(nySess, 5), nEM = f_hhmm(nySess, 7)
rSH = f_hhmm(rthSess, 0), rSM = f_hhmm(rthSess, 2), rEH = f_hhmm(rthSess, 5), rEM = f_hhmm(rthSess, 7)
// End time for a finished session's extension lines.
f_lineEnd(int sessEnd) =>
endMode == "Custom Time" ? f_nextAt(sessEnd, customH, customM) : f_nextAt(sessEnd, nEH, nEM)
// =============================================================================
// SECTION 4 — TYPES
// =============================================================================
type SessCfg
bool enabled
int sH
int sM
int eH
int eM
bool showHi
bool showLo
bool showMid
bool showBox
color hiCol
color loCol
color midCol
color boxCol
int width
string style
string midStyle
string tagHi
string tagLo
string tagMid
// One trading day's set of drawings for one session.
type SessLevels
int sessStart
int firstBar
int lineEnd
float hi
float lo
line hiLn
line loLn
line midLn
label hiLb
label loLb
label midLb
box bx
// Configuration for one higher-timeframe level family (Previous Day / Previous Week).
type HtfCfg
bool enabled
bool showHi
bool showLo
color hiCol
color loCol
int width
string style
string mode // resolved: "NY Session Close" | "Custom Time" | "Extend Right"
int cH
int cM
bool rolling // line end advances to each new NY close / custom time in the period
string tagHi
string tagLo
// One period's (day or week) set of fixed drawings.
type HtfLevels
int start
int lineEnd
float hi
float lo
line hiLn
line loLn
label hiLb
label loLb
// =============================================================================
// SECTION 5 — DRAWING MANAGEMENT
// =============================================================================
f_newLine(int x1, int x2, float y, color c, string st, int w) =>
line.new(x1 = x1, y1 = y, x2 = x2, y2 = y, xloc = xloc.bar_time,
extend = endMode == "Extend Right" ? extend.right : extend.none,
color = c, style = st, width = w)
f_newLabel(int x, float y, string tag, color c) =>
label.new(x = x, y = y, text = f_text(tag, y), xloc = xloc.bar_time, yloc = yloc.price,
color = color.new(color.black, 100), style = label.style_label_left,
textcolor = lblUseLine ? color.new(c, 0) : lblCol, size = f_lblSize(lblSize))
method destroy(SessLevels this) =>
line.delete(this.hiLn)
line.delete(this.loLn)
line.delete(this.midLn)
label.delete(this.hiLb)
label.delete(this.loLb)
label.delete(this.midLb)
box.delete(this.bx)
this
// Create the drawing set on the first bar of a session.
f_create(SessCfg c, int st, int en) =>
SessLevels s = SessLevels.new(st, time, f_lineEnd(en), high, low)
float m = (high + low) / 2
if c.showHi
s.hiLn := f_newLine(time, s.lineEnd, high, c.hiCol, c.style, c.width)
if showLabels
s.hiLb := f_newLabel(s.lineEnd, high, c.tagHi, c.hiCol)
if c.showLo
s.loLn := f_newLine(time, s.lineEnd, low, c.loCol, c.style, c.width)
if showLabels
s.loLb := f_newLabel(s.lineEnd, low, c.tagLo, c.loCol)
if c.showMid
s.midLn := f_newLine(time, s.lineEnd, m, c.midCol, c.midStyle, c.width)
if showLabels
s.midLb := f_newLabel(s.lineEnd, m, c.tagMid, c.midCol)
if c.showBox
s.bx := box.new(left = time, top = high, right = time_close, bottom = low,
border_color = color.new(color.black, 100), border_width = 0,
xloc = xloc.bar_time, bgcolor = c.boxCol)
s
// Update a forming session. Objects are modified in place, never recreated.
method update(SessLevels this, SessCfg c) =>
if not na(this.bx)
this.bx.set_right(time_close)
if high > this.hi or low < this.lo
this.hi := math.max(this.hi, high)
this.lo := math.min(this.lo, low)
float m = (this.hi + this.lo) / 2
if not na(this.hiLn)
this.hiLn.set_y1(this.hi)
this.hiLn.set_y2(this.hi)
if not na(this.loLn)
this.loLn.set_y1(this.lo)
this.loLn.set_y2(this.lo)
if not na(this.midLn)
this.midLn.set_y1(m)
this.midLn.set_y2(m)
if not na(this.hiLb)
this.hiLb.set_y(this.hi)
this.hiLb.set_text(f_text(c.tagHi, this.hi))
if not na(this.loLb)
this.loLb.set_y(this.lo)
this.loLb.set_text(f_text(c.tagLo, this.lo))
if not na(this.midLb)
this.midLb.set_y(m)
this.midLb.set_text(f_text(c.tagMid, m))
if not na(this.bx)
this.bx.set_top(this.hi)
this.bx.set_bottom(this.lo)
this
// --- Previous Day / Week drawing helpers -----------------------------------------
// Resolve "Follow Global Setting" into the global extension mode.
f_htfMode(string sel) =>
sel == "Follow Global Setting" ? endMode : sel
f_htfText(string tag, float p) =>
htfLblPrice ? tag + " — " + str.tostring(p, format.mintick) : tag
f_htfLine(int x1, int x2, float y, color c, string st, int w, string ext) =>
line.new(x1 = x1, y1 = y, x2 = x2, y2 = y, xloc = xloc.bar_time,
extend = ext, color = c, style = st, width = w)
f_htfLabel(int x, float y, string tag, color c) =>
label.new(x = x, y = y, text = f_htfText(tag, y), xloc = xloc.bar_time, yloc = yloc.price,
color = color.new(color.black, 100), style = label.style_label_left,
textcolor = htfLblMatch ? color.new(c, 0) : htfLblCol, size = f_lblSize(htfLblSize))
// Build a resolved config.
f_htfCfg(bool en, bool sHi, bool sLo, color hc, color lc, int tr, int w, string sty,
string endSel, bool weekly, string tHi, string tLo) =>
string md = f_htfMode(endSel)
bool own = endSel != "Follow Global Setting"
int ch = own ? htfCustH : customH
int cm = own ? htfCustM : customM
// Weekly lines always roll forward through the week. Daily lines roll only when the
// end time lies inside the trading day (on/before NY close), so they can never run
// into the following day's levels; rolling also bridges holidays with no NY session.
bool rl = weekly or md != "Custom Time" or ch * 60 + cm <= nEH * 60 + nEM
HtfCfg.new(en and (sHi or sLo), sHi, sLo, color.new(hc, tr), color.new(lc, tr), w,
f_lineStyle(sty), md, ch, cm, rl, tHi, tLo)
// Initial line end for a period that starts at `t`: next NY close / custom time,
// skipping a weekend when the period starts late in the week (e.g. Friday RTH close).
f_htfInitEnd(HtfCfg c, int t) =>
int h = c.mode == "Custom Time" ? c.cH : nEH
int m = c.mode == "Custom Time" ? c.cM : nEM
int x = f_nextAt(t, h, m)
if dayofweek(t, tzInput) != dayofweek.sunday
int dX = dayofweek(x, tzInput)
if dX == dayofweek.saturday
x := f_at(f_nextDay(f_nextDay(x)), h, m)
else if dX == dayofweek.sunday
x := f_at(f_nextDay(x), h, m)
x
method wipe(HtfLevels this) =>
line.delete(this.hiLn)
line.delete(this.loLn)
label.delete(this.hiLb)
label.delete(this.loLb)
this
// Advance the line end of the ACTIVE period only. Prices never change.
method roll(HtfLevels this, HtfCfg c) =>
int h = c.mode == "Custom Time" ? c.cH : nEH
int m = c.mode == "Custom Time" ? c.cM : nEM
int cand = f_at(time, h, m)
if time < cand and cand > this.lineEnd
this.lineEnd := cand
if not na(this.hiLn)
this.hiLn.set_x2(cand)
if not na(this.loLn)
this.loLn.set_x2(cand)
if not na(this.hiLb)
this.hiLb.set_x(cand)
if not na(this.loLb)
this.loLb.set_x(cand)
this
// Create a period's drawing set on its first bar.
f_htfCreate(HtfCfg c, float hi, float lo) =>
HtfLevels s = HtfLevels.new(time, f_htfInitEnd(c, time), hi, lo)
string ext = c.mode == "Extend Right" ? extend.right : extend.none
if c.showHi
s.hiLn := f_htfLine(time, s.lineEnd, hi, c.hiCol, c.style, c.width, ext)
if htfShowLbl
s.hiLb := f_htfLabel(s.lineEnd, hi, c.tagHi, c.hiCol)
if c.showLo
s.loLn := f_htfLine(time, s.lineEnd, lo, c.loCol, c.style, c.width, ext)
if htfShowLbl
s.loLb := f_htfLabel(s.lineEnd, lo, c.tagLo, c.loCol)
s
// =============================================================================
// SECTION 6 — STATE
// =============================================================================
var SessCfg asiaCfg = SessCfg.new(enableAsia, aSH, aSM, aEH, aEM,
showHighs and aShowHi, showLows and aShowLo, showMids and aShowMid, showBoxes and aShowBox,
color.new(aHiCol, aTransp), color.new(aLoCol, aTransp), color.new(aMidCol, aTransp), color.new(aBoxCol, aBoxTr),
aWidth, f_lineStyle(aStyle), f_lineStyle(aMidSty), "ASH", "ASL", "ASM")
var SessCfg londonCfg = SessCfg.new(enableLondon, lSH, lSM, lEH, lEM,
showHighs and lShowHi, showLows and lShowLo, showMids and lShowMid, showBoxes and lShowBox,
color.new(lHiCol, lTransp), color.new(lLoCol, lTransp), color.new(lMidCol, lTransp), color.new(lBoxCol, lBoxTr),
lWidth, f_lineStyle(lStyle), f_lineStyle(lMidSty), "LDH", "LDL", "LDM")
var array<SessLevels> asiaLevels = array.new<SessLevels>()
var array<SessLevels> londonLevels = array.new<SessLevels>()
int keepDays = showHist ? histDays : 1
int prevTime = time[1]
// =============================================================================
// SECTION 7 — SESSION TRACKING
// =============================================================================
f_process(array<SessLevels> arr, SessCfg c, int keep) =>
if c.enabled
[st, en] = f_window(time, c.sH, c.sM, c.eH, c.eM)
if time >= st and time < en
SessLevels cur = na
if arr.size() > 0
cur := arr.last()
if not na(cur) and cur.sessStart == st
// Same session still forming: extend high/low.
cur.update(c)
else if not na(prevTime) and prevTime < st
// First bar of a new session (and the chart covers its full start).
arr.push(f_create(c, st, en))
while arr.size() > keep
arr.shift().destroy()
true
f_process(asiaLevels, asiaCfg, keepDays)
f_process(londonLevels, londonCfg, keepDays)
// =============================================================================
// SECTION 8 — NEW YORK SHADING & TIMEFRAME WARNING
// =============================================================================
[nyStart, nyEnd] = f_window(time, nSH, nSM, nEH, nEM)
bool inNY = time >= nyStart and time < nyEnd
bgcolor(showNYBg and inNY ? nyBgCol : na, title = "New York Session")
var table warnTbl = table.new(position.bottom_right, 1, 1)
if barstate.islast
int tfSec = timeframe.in_seconds()
if not timeframe.isintraday or tfSec > 1800 or 1800 % tfSec != 0
table.cell(warnTbl, 0, 0,
"AS/LD Levels: this timeframe does not align with session boundaries.\nUse a 1–30 minute chart (1 min recommended) for exact levels.",
text_color = color.white, bgcolor = color.new(color.red, 25), text_size = size.small)
// =============================================================================
// SECTION 9 — PREVIOUS DAY & WEEK DATA (non-repainting)
// =============================================================================
// Force the exchange (extended) session so ETH daily/weekly bars are used even if
// the chart itself is set to RTH. high[1]/low[1] with lookahead_on = values of the
// last COMPLETED bar, fixed for the entire current period, no future leakage.
// `time` (current HTF bar open) is known at the period's first bar and is used only
// to detect the period change.
string tickETH = ticker.modify(syminfo.tickerid, session.extended)
[dHi, dLo, dT] = request.security(tickETH, "D", [high[1], low[1], time], lookahead = barmerge.lookahead_on)
[wHi, wLo, wT] = request.security(tickETH, "W", [high[1], low[1], time], lookahead = barmerge.lookahead_on)
bool dNewETH = not na(dT) and (na(dT[1]) or dT != dT[1])
bool wNew = not na(wT) and (na(wT[1]) or wT != wT[1])
// --- RTH-only previous day, built from chart bars ---------------------------------------
bool useRTH = pdCalc == "RTH Only"
var int rthStart = na
var int rthEnd = na
var float rthHi = na
var float rthLo = na
var bool rthValid = false
var bool rthDone = true
bool rthFinal = false
float rthFinHi = na
float rthFinLo = na
if useRTH
// Finalize the completed RTH session on the first bar at/after its close.
if rthValid and not rthDone and time >= rthEnd
rthFinal := true
rthFinHi := rthHi
rthFinLo := rthLo
rthDone := true
[rs, re] = f_window(time, rSH, rSM, rEH, rEM)
if time >= rs and time < re
if na(rthStart) or rs != rthStart
// First bar of a new RTH session; only valid if the chart covers its start.
rthStart := rs
rthEnd := re
rthValid := not na(prevTime) and prevTime < rs
rthDone := false
rthHi := high
rthLo := low
else
rthHi := math.max(rthHi, high)
rthLo := math.min(rthLo, low)
bool pdNew = useRTH ? rthFinal : dNewETH
float pdH = useRTH ? rthFinHi : dHi
float pdL = useRTH ? rthFinLo : dLo
// =============================================================================
// SECTION 10 — PREVIOUS DAY & WEEK STATE AND TRACKING
// =============================================================================
var HtfCfg pdCfg = f_htfCfg(enablePD, pdShowHi, pdShowLo, pdHiCol, pdLoCol, pdTransp, pdWidth, pdStyle,
pdEndMode, false, "PDH", "PDL")
var HtfCfg pwCfg = f_htfCfg(enablePW, pwShowHi, pwShowLo, pwHiCol, pwLoCol, pwTransp, pwWidth, pwStyle,
pwEndMode, true, "PWH", "PWL")
var array<HtfLevels> pdLevels = array.new<HtfLevels>()
var array<HtfLevels> pwLevels = array.new<HtfLevels>()
int keepPD = showHistPD ? histPD : 1
int keepPW = showHistPW ? histPW : 1
// One drawing set per period, created once on the period's first bar. Older sets are
// frozen (never touched again) and the oldest are deleted beyond the history limit.
f_htfProcess(array<HtfLevels> arr, HtfCfg c, bool isNew, float hi, float lo, int keep) =>
if c.enabled
if isNew and not na(hi) and not na(lo)
arr.push(f_htfCreate(c, hi, lo))
while arr.size() > keep
arr.shift().wipe()
else if c.rolling and arr.size() > 0
arr.last().roll(c)
true
f_htfProcess(pdLevels, pdCfg, pdNew, pdH, pdL, keepPD)
f_htfProcess(pwLevels, pwCfg, wNew, wHi, wLo, keepPW)