本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sbtnc - original "Multi-Timeframe Period Separators"
// Modified by Grandoc - "Grandoc's MTF Separators", version 2.3
//
// Changes from the original:
// - Day / Week / Month / Year opens anchored to the 09:00 Sydney open (Australia/Sydney clock, DST automatic)
// - Week = Sunday-evening forex week open (first Sydney open of the week)
// - Five sessions (Sydney, Tokyo, Frankfurt, London, New York), each read on its own city clock (DST automatic)
// - Per-session open/close lines, optional session high/low boxes and editable closing-range boxes
//@version=6
indicator("Grandoc's MTF Separators", "Grandoc's MTF Separators", overlay = true, max_lines_count = 500, max_boxes_count = 500)
//#region Constants & helpers
string SYD_TZ = "Australia/Sydney"
int DAY_MS = 86400000
string WEEKDAYS = ":23456" // Mon-Fri in the session's own city clock (1 = Sunday ... 7 = Saturday)
f_style(string s) =>
s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
f_vline(int t, color c, int w, string s, array<line> arr, int keep) =>
if keep > 0
arr.push(line.new(t, high, t, low, xloc.bar_time, extend.both, c, f_style(s), w))
while arr.size() > keep
arr.shift().delete()
f_trimBoxes(array<box> arr, int keep) =>
while arr.size() > keep
arr.shift().delete()
//#endregion
//#region Inputs - general
string G0 = "General"
int i_dayHour = input.int(9, "Day open hour (Sydney time)", 0, 23, group = G0, tooltip = "The trading day, week, month and year all start at this hour on the Sydney clock. Daylight saving is followed automatically.")
int i_dayMin = input.int(0, "Day open minute", 0, 59, group = G0)
int i_maxTF = input.int(60, "Hide session lines/boxes/closing ranges above (minutes)", 1, 1440, group = G0, tooltip = "On bigger candles a session or a 30-minute window cannot be drawn accurately.")
int i_keepDays = input.int(20, "Sessions to keep (per session)", 1, 45, group = G0, tooltip = "How many past sessions keep their lines / boxes / closing ranges.")
//#endregion
//#region Inputs - period opens
string GD = "Day open"
bool i_showD = input.bool(true, "Show", inline = "Da", group = GD)
color i_colD = input.color(color.white, "", inline = "Da", group = GD)
int i_wD = input.int(1, "Width", 1, 10, inline = "Da", group = GD)
string i_stD = input.string("Dotted", "Style", ["Solid", "Dashed", "Dotted"], inline = "Db", group = GD)
int i_lbD = input.int(30, "Lookback", 0, 200, inline = "Db", group = GD)
string GW = "Week open"
bool i_showW = input.bool(true, "Show", inline = "Wa", group = GW)
color i_colW = input.color(color.green, "", inline = "Wa", group = GW)
int i_wW = input.int(2, "Width", 1, 10, inline = "Wa", group = GW)
string i_stW = input.string("Dashed", "Style", ["Solid", "Dashed", "Dotted"], inline = "Wb", group = GW)
int i_lbW = input.int(12, "Lookback", 0, 200, inline = "Wb", group = GW)
string GM = "Month open"
bool i_showM = input.bool(true, "Show", inline = "Ma", group = GM)
color i_colM = input.color(color.new(#FF5252, 60), "", inline = "Ma", group = GM)
int i_wM = input.int(3, "Width", 1, 10, inline = "Ma", group = GM)
string i_stM = input.string("Solid", "Style", ["Solid", "Dashed", "Dotted"], inline = "Mb", group = GM)
int i_lbM = input.int(12, "Lookback", 0, 200, inline = "Mb", group = GM)
string GY = "Year open"
bool i_showY = input.bool(true, "Show", inline = "Ya", group = GY)
color i_colY = input.color(color.new(color.purple, 60), "", inline = "Ya", group = GY)
int i_wY = input.int(3, "Width", 1, 10, inline = "Ya", group = GY)
string i_stY = input.string("Solid", "Style", ["Solid", "Dashed", "Dotted"], inline = "Yb", group = GY)
int i_lbY = input.int(5, "Lookback", 0, 200, inline = "Yb", group = GY)
//#endregion
//#region Inputs - sessions
string G_syd = "Sydney session"
bool i_sydShow = input.bool(true, "Show Sydney", group = G_syd, tooltip = "Master switch: untick to hide everything for this session (open, close, box, closing range).")
string i_sydSess = input.session("0900-1800", "Hours (local Sydney time)", group = G_syd)
string i_sydTz = input.string("Australia/Sydney", "City time zone", group = G_syd, tooltip = "Leave as is: hours above are read on this clock so daylight saving adjusts by itself.")
color i_sydCol = input.color(color.new(#2e9d5b, 0), "Line colour", group = G_syd)
bool i_sydOpen = input.bool(true, "Open line", inline = "sydo", group = G_syd)
int i_sydOW = input.int(1, "W", 1, 10, inline = "sydo", group = G_syd)
string i_sydOS = input.string("Dashed", "", ["Solid", "Dashed", "Dotted"], inline = "sydo", group = G_syd)
bool i_sydClose = input.bool(false, "Close line", inline = "sydc", group = G_syd)
int i_sydCW = input.int(1, "W", 1, 10, inline = "sydc", group = G_syd)
string i_sydCS = input.string("Dotted", "", ["Solid", "Dashed", "Dotted"], inline = "sydc", group = G_syd)
bool i_sydBox = input.bool(false, "Session box", inline = "sydb", group = G_syd)
color i_sydBCol = input.color(color.new(#2e9d5b, 0), "", inline = "sydb", group = G_syd)
int i_sydBTr = input.int(90, "Fill transp.", 0, 100, inline = "sydb", group = G_syd)
int i_sydBBw = input.int(1, "Border", 0, 5, inline = "sydb", group = G_syd)
bool i_sydCR = input.bool(true, "Closing range", inline = "sydr", group = G_syd)
string i_sydCRS = input.session("1730-1800", "", inline = "sydr", group = G_syd, tooltip = "Window in local Sydney time. Box marks the high and low inside it.")
color i_sydCRCol = input.color(color.new(#2e9d5b, 0), "", inline = "sydr2", group = G_syd)
int i_sydCRTr = input.int(80, "Fill transp.", 0, 100, inline = "sydr2", group = G_syd)
int i_sydCRBw = input.int(1, "Border", 0, 5, inline = "sydr2", group = G_syd)
string G_tyo = "Tokyo session"
bool i_tyoShow = input.bool(true, "Show Tokyo", group = G_tyo, tooltip = "Master switch: untick to hide everything for this session (open, close, box, closing range).")
string i_tyoSess = input.session("0900-1800", "Hours (local Tokyo time)", group = G_tyo)
string i_tyoTz = input.string("Asia/Tokyo", "City time zone", group = G_tyo, tooltip = "Leave as is: hours above are read on this clock so daylight saving adjusts by itself.")
color i_tyoCol = input.color(color.new(#d9822b, 0), "Line colour", group = G_tyo)
bool i_tyoOpen = input.bool(true, "Open line", inline = "tyoo", group = G_tyo)
int i_tyoOW = input.int(1, "W", 1, 10, inline = "tyoo", group = G_tyo)
string i_tyoOS = input.string("Dashed", "", ["Solid", "Dashed", "Dotted"], inline = "tyoo", group = G_tyo)
bool i_tyoClose = input.bool(false, "Close line", inline = "tyoc", group = G_tyo)
int i_tyoCW = input.int(1, "W", 1, 10, inline = "tyoc", group = G_tyo)
string i_tyoCS = input.string("Dotted", "", ["Solid", "Dashed", "Dotted"], inline = "tyoc", group = G_tyo)
bool i_tyoBox = input.bool(false, "Session box", inline = "tyob", group = G_tyo)
color i_tyoBCol = input.color(color.new(#d9822b, 0), "", inline = "tyob", group = G_tyo)
int i_tyoBTr = input.int(90, "Fill transp.", 0, 100, inline = "tyob", group = G_tyo)
int i_tyoBBw = input.int(1, "Border", 0, 5, inline = "tyob", group = G_tyo)
bool i_tyoCR = input.bool(true, "Closing range", inline = "tyor", group = G_tyo)
string i_tyoCRS = input.session("1730-1800", "", inline = "tyor", group = G_tyo, tooltip = "Window in local Tokyo time. Box marks the high and low inside it.")
color i_tyoCRCol = input.color(color.new(#d9822b, 0), "", inline = "tyor2", group = G_tyo)
int i_tyoCRTr = input.int(80, "Fill transp.", 0, 100, inline = "tyor2", group = G_tyo)
int i_tyoCRBw = input.int(1, "Border", 0, 5, inline = "tyor2", group = G_tyo)
string G_fra = "Frankfurt session"
bool i_fraShow = input.bool(true, "Show Frankfurt", group = G_fra, tooltip = "Master switch: untick to hide everything for this session (open, close, box, closing range).")
string i_fraSess = input.session("0800-1700", "Hours (local Frankfurt time)", group = G_fra)
string i_fraTz = input.string("Europe/Berlin", "City time zone", group = G_fra, tooltip = "Leave as is: hours above are read on this clock so daylight saving adjusts by itself.")
color i_fraCol = input.color(color.new(#8e44ad, 0), "Line colour", group = G_fra)
bool i_fraOpen = input.bool(true, "Open line", inline = "frao", group = G_fra)
int i_fraOW = input.int(1, "W", 1, 10, inline = "frao", group = G_fra)
string i_fraOS = input.string("Dashed", "", ["Solid", "Dashed", "Dotted"], inline = "frao", group = G_fra)
bool i_fraClose = input.bool(false, "Close line", inline = "frac", group = G_fra)
int i_fraCW = input.int(1, "W", 1, 10, inline = "frac", group = G_fra)
string i_fraCS = input.string("Dotted", "", ["Solid", "Dashed", "Dotted"], inline = "frac", group = G_fra)
bool i_fraBox = input.bool(false, "Session box", inline = "frab", group = G_fra)
color i_fraBCol = input.color(color.new(#8e44ad, 0), "", inline = "frab", group = G_fra)
int i_fraBTr = input.int(90, "Fill transp.", 0, 100, inline = "frab", group = G_fra)
int i_fraBBw = input.int(1, "Border", 0, 5, inline = "frab", group = G_fra)
bool i_fraCR = input.bool(true, "Closing range", inline = "frar", group = G_fra)
string i_fraCRS = input.session("1630-1700", "", inline = "frar", group = G_fra, tooltip = "Window in local Frankfurt time. Box marks the high and low inside it.")
color i_fraCRCol = input.color(color.new(#8e44ad, 0), "", inline = "frar2", group = G_fra)
int i_fraCRTr = input.int(80, "Fill transp.", 0, 100, inline = "frar2", group = G_fra)
int i_fraCRBw = input.int(1, "Border", 0, 5, inline = "frar2", group = G_fra)
string G_lon = "London session"
bool i_lonShow = input.bool(true, "Show London", group = G_lon, tooltip = "Master switch: untick to hide everything for this session (open, close, box, closing range).")
string i_lonSess = input.session("0800-1700", "Hours (local London time)", group = G_lon)
string i_lonTz = input.string("Europe/London", "City time zone", group = G_lon, tooltip = "Leave as is: hours above are read on this clock so daylight saving adjusts by itself.")
color i_lonCol = input.color(color.new(#1f6fd1, 0), "Line colour", group = G_lon)
bool i_lonOpen = input.bool(true, "Open line", inline = "lono", group = G_lon)
int i_lonOW = input.int(1, "W", 1, 10, inline = "lono", group = G_lon)
string i_lonOS = input.string("Dashed", "", ["Solid", "Dashed", "Dotted"], inline = "lono", group = G_lon)
bool i_lonClose = input.bool(false, "Close line", inline = "lonc", group = G_lon)
int i_lonCW = input.int(1, "W", 1, 10, inline = "lonc", group = G_lon)
string i_lonCS = input.string("Dotted", "", ["Solid", "Dashed", "Dotted"], inline = "lonc", group = G_lon)
bool i_lonBox = input.bool(false, "Session box", inline = "lonb", group = G_lon)
color i_lonBCol = input.color(color.new(#1f6fd1, 0), "", inline = "lonb", group = G_lon)
int i_lonBTr = input.int(90, "Fill transp.", 0, 100, inline = "lonb", group = G_lon)
int i_lonBBw = input.int(1, "Border", 0, 5, inline = "lonb", group = G_lon)
bool i_lonCR = input.bool(true, "Closing range", inline = "lonr", group = G_lon)
string i_lonCRS = input.session("1630-1700", "", inline = "lonr", group = G_lon, tooltip = "Window in local London time. Box marks the high and low inside it.")
color i_lonCRCol = input.color(color.new(#1f6fd1, 0), "", inline = "lonr2", group = G_lon)
int i_lonCRTr = input.int(80, "Fill transp.", 0, 100, inline = "lonr2", group = G_lon)
int i_lonCRBw = input.int(1, "Border", 0, 5, inline = "lonr2", group = G_lon)
string G_ny = "New York session"
bool i_nyShow = input.bool(true, "Show New York", group = G_ny, tooltip = "Master switch: untick to hide everything for this session (open, close, box, closing range).")
string i_nySess = input.session("0800-1700", "Hours (local New York time)", group = G_ny)
string i_nyTz = input.string("America/New_York", "City time zone", group = G_ny, tooltip = "Leave as is: hours above are read on this clock so daylight saving adjusts by itself.")
color i_nyCol = input.color(color.new(#c0392b, 0), "Line colour", group = G_ny)
bool i_nyOpen = input.bool(true, "Open line", inline = "nyo", group = G_ny)
int i_nyOW = input.int(1, "W", 1, 10, inline = "nyo", group = G_ny)
string i_nyOS = input.string("Dashed", "", ["Solid", "Dashed", "Dotted"], inline = "nyo", group = G_ny)
bool i_nyClose = input.bool(false, "Close line", inline = "nyc", group = G_ny)
int i_nyCW = input.int(1, "W", 1, 10, inline = "nyc", group = G_ny)
string i_nyCS = input.string("Dotted", "", ["Solid", "Dashed", "Dotted"], inline = "nyc", group = G_ny)
bool i_nyBox = input.bool(false, "Session box", inline = "nyb", group = G_ny)
color i_nyBCol = input.color(color.new(#c0392b, 0), "", inline = "nyb", group = G_ny)
int i_nyBTr = input.int(90, "Fill transp.", 0, 100, inline = "nyb", group = G_ny)
int i_nyBBw = input.int(1, "Border", 0, 5, inline = "nyb", group = G_ny)
bool i_nyCR = input.bool(true, "Closing range", inline = "nyr", group = G_ny)
string i_nyCRS = input.session("1630-1700", "", inline = "nyr", group = G_ny, tooltip = "Window in local New York time. Box marks the high and low inside it.")
color i_nyCRCol = input.color(color.new(#c0392b, 0), "", inline = "nyr2", group = G_ny)
int i_nyCRTr = input.int(80, "Fill transp.", 0, 100, inline = "nyr2", group = G_ny)
int i_nyCRBw = input.int(1, "Border", 0, 5, inline = "nyr2", group = G_ny)
//#endregion
//#region Types
type Sess
string sess
string tz
color col
bool showOpen
int openW
string openS
bool showClose
int closeW
string closeS
bool showBox
color boxCol
int boxTr
int boxBw
bool showCR
string crSess
color crCol
int crTr
int crBw
array<line> lines
array<box> boxes
array<box> crs
box curBox = na
box curCR = na
bool wasIn = false
bool wasInCR = false
int lastClose = na
// Called on every bar (keeps time() history consistent).
method run(Sess this, bool allowed) =>
bool inS = not na(time(timeframe.period, this.sess + WEEKDAYS, this.tz))
bool inCR = not na(time(timeframe.period, this.crSess + WEEKDAYS, this.tz))
if allowed
// session open / close lines
if inS and not this.wasIn and this.showOpen
f_vline(time, this.col, this.openW, this.openS, this.lines, i_keepDays * 2)
if not inS and this.wasIn and this.showClose and not na(this.lastClose)
f_vline(this.lastClose, this.col, this.closeW, this.closeS, this.lines, i_keepDays * 2)
// session box (open -> close, high / low)
if this.showBox
if inS and not this.wasIn
this.curBox := box.new(time, high, time_close, low, this.boxCol, this.boxBw, xloc = xloc.bar_time, bgcolor = color.new(this.boxCol, this.boxTr))
this.boxes.push(this.curBox)
f_trimBoxes(this.boxes, i_keepDays)
else if inS and not na(this.curBox)
this.curBox.set_right(time_close)
this.curBox.set_top(math.max(this.curBox.get_top(), high))
this.curBox.set_bottom(math.min(this.curBox.get_bottom(), low))
// closing range (high / low inside the window)
if this.showCR
if inCR and not this.wasInCR
this.curCR := box.new(time, high, time_close, low, this.crCol, this.crBw, xloc = xloc.bar_time, bgcolor = color.new(this.crCol, this.crTr))
this.crs.push(this.curCR)
f_trimBoxes(this.crs, i_keepDays)
else if inCR and not na(this.curCR)
this.curCR.set_right(time_close)
this.curCR.set_top(math.max(this.curCR.get_top(), high))
this.curCR.set_bottom(math.min(this.curCR.get_bottom(), low))
if not inS
this.curBox := na
if not inCR
this.curCR := na
if inS
this.lastClose := time_close
this.wasIn := inS
this.wasInCR := inCR
//#endregion
//#region Period opens (Sydney clock)
bool higherTF = not timeframe.isintraday
// Daily+ candles start before the Sydney open on forex feeds, so read them by their close.
int refT = higherTF ? time_close - 1 : time
int openRef = timestamp(SYD_TZ, year(refT, SYD_TZ), month(refT, SYD_TZ), dayofmonth(refT, SYD_TZ), i_dayHour, i_dayMin, 0)
bool afterOpen = refT >= openRef
int tdStart = afterOpen ? openRef : openRef - DAY_MS // start of the Sydney trading day this candle belongs to
int tdY = year(tdStart, SYD_TZ)
int tdM = month(tdStart, SYD_TZ)
int tdD = dayofmonth(tdStart, SYD_TZ)
int dayNum = int(math.floor(timestamp("UTC", tdY, tdM, tdD, 0, 0, 0) / DAY_MS))
int weekNum = int(math.floor((dayNum + 3) / 7.0)) // week starts on the Sydney Monday open = Sunday evening in Europe / Africa
int monthNum = tdY * 12 + tdM
var int lastDay = na
var int lastWeek = na
var int lastMonth = na
var int lastYear = na
// Intraday: only fire on a candle at/after a real Sydney open, so early broker
// Sunday candles (before 09:00 Sydney) never create a false open.
bool armed = higherTF or afterOpen
bool newDay = false
bool newWeek = false
bool newMonth = false
bool newYear = false
if armed and not na(time) and (na(lastDay) or dayNum != lastDay)
bool first = na(lastDay)
newDay := not first
newWeek := not first and weekNum != lastWeek
newMonth := not first and monthNum != lastMonth
newYear := not first and tdY != lastYear
lastDay := dayNum
lastWeek := weekNum
lastMonth := monthNum
lastYear := tdY
var array<line> linesD = array.new<line>()
var array<line> linesW = array.new<line>()
var array<line> linesM = array.new<line>()
var array<line> linesY = array.new<line>()
// Draw only the biggest period that opens on this candle (avoids stacked duplicates).
if newYear and i_showY
f_vline(time, i_colY, i_wY, i_stY, linesY, i_lbY)
else if newMonth and i_showM
f_vline(time, i_colM, i_wM, i_stM, linesM, i_lbM)
else if newWeek and i_showW
f_vline(time, i_colW, i_wW, i_stW, linesW, i_lbW)
else if newDay and i_showD and timeframe.in_seconds() < 86400
f_vline(time, i_colD, i_wD, i_stD, linesD, i_lbD)
//#endregion
//#region Sessions
var Sess s_syd = Sess.new(i_sydSess, i_sydTz, i_sydCol, i_sydOpen, i_sydOW, i_sydOS, i_sydClose, i_sydCW, i_sydCS, i_sydBox, i_sydBCol, i_sydBTr, i_sydBBw, i_sydCR, i_sydCRS, i_sydCRCol, i_sydCRTr, i_sydCRBw, array.new<line>(), array.new<box>(), array.new<box>())
var Sess s_tyo = Sess.new(i_tyoSess, i_tyoTz, i_tyoCol, i_tyoOpen, i_tyoOW, i_tyoOS, i_tyoClose, i_tyoCW, i_tyoCS, i_tyoBox, i_tyoBCol, i_tyoBTr, i_tyoBBw, i_tyoCR, i_tyoCRS, i_tyoCRCol, i_tyoCRTr, i_tyoCRBw, array.new<line>(), array.new<box>(), array.new<box>())
var Sess s_fra = Sess.new(i_fraSess, i_fraTz, i_fraCol, i_fraOpen, i_fraOW, i_fraOS, i_fraClose, i_fraCW, i_fraCS, i_fraBox, i_fraBCol, i_fraBTr, i_fraBBw, i_fraCR, i_fraCRS, i_fraCRCol, i_fraCRTr, i_fraCRBw, array.new<line>(), array.new<box>(), array.new<box>())
var Sess s_lon = Sess.new(i_lonSess, i_lonTz, i_lonCol, i_lonOpen, i_lonOW, i_lonOS, i_lonClose, i_lonCW, i_lonCS, i_lonBox, i_lonBCol, i_lonBTr, i_lonBBw, i_lonCR, i_lonCRS, i_lonCRCol, i_lonCRTr, i_lonCRBw, array.new<line>(), array.new<box>(), array.new<box>())
var Sess s_ny = Sess.new(i_nySess, i_nyTz, i_nyCol, i_nyOpen, i_nyOW, i_nyOS, i_nyClose, i_nyCW, i_nyCS, i_nyBox, i_nyBCol, i_nyBTr, i_nyBBw, i_nyCR, i_nyCRS, i_nyCRCol, i_nyCRTr, i_nyCRBw, array.new<line>(), array.new<box>(), array.new<box>())
bool sessAllowed = timeframe.isintraday and timeframe.in_seconds() <= i_maxTF * 60
s_syd.run(sessAllowed and i_sydShow)
s_tyo.run(sessAllowed and i_tyoShow)
s_fra.run(sessAllowed and i_fraShow)
s_lon.run(sessAllowed and i_lonShow)
s_ny.run(sessAllowed and i_nyShow)
//#endregion