本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Futures Sessions & PDH/PDL [Trader Edge Suite] - build 261110B12
// Free and open-source (Mozilla Public License 2.0). Same session logic as the Trader Edge Suite Sessions module.
// Educational tool only - not financial advice.
indicator("Futures Sessions & PDH/PDL [Trader Edge Suite]", shorttitle = "Sessions & PDL", overlay = true, max_lines_count = 500, max_labels_count = 500)
// =====================================================================
// Inputs
// =====================================================================
string G_SES = "1. Sessions"
string G_SHL = "2. Session highs / lows"
string G_PDL = "3. Previous day levels"
string G_PST = "4. Level style"
bool i_bg = input.bool(true, "1.1 Backgrounds", group = G_SES)
bool i_wm = input.bool(true, "1.2 Session names", group = G_SES)
bool i_s1On = input.bool(true, "1.3 Asia", inline = "s1", group = G_SES)
color i_s1Col = input.color(color.purple, "", inline = "s1", group = G_SES)
string i_s1Tm = input.session("1800-0259", "", inline = "s1", group = G_SES)
bool i_s2On = input.bool(true, "1.4 London", inline = "s2", group = G_SES)
color i_s2Col = input.color(color.teal, "", inline = "s2", group = G_SES)
string i_s2Tm = input.session("0300-0929", "", inline = "s2", group = G_SES)
bool i_s3On = input.bool(true, "1.5 New York", inline = "s3", group = G_SES)
color i_s3Col = input.color(#FF9800, "", inline = "s3", group = G_SES)
string i_s3Tm = input.session("0930-1559", "", inline = "s3", group = G_SES, tooltip = "Each row: on/off, color, time window (New York time, daylight saving handled automatically).")
string i_wmSize = input.string("Huge", "1.6 Name size", options = ["Normal", "Large", "Huge"], group = G_SES)
bool i_hl = input.bool(true, "2.1 Session high / low lines", group = G_SHL)
string i_hlSt = input.string("Dashed", "2.2 Line style", options = ["Solid", "Dotted", "Dashed"], group = G_SHL)
int i_hlW = input.int(2, "2.3 Line width", minval = 1, maxval = 5, group = G_SHL)
string i_hlSize = input.string("Normal", "2.4 Label size", options = ["Tiny", "Small", "Normal", "Large"], group = G_SHL)
bool i_ext = input.bool(true, "2.5 Extend lines to end of day", group = G_SHL)
int i_keep = input.int(10, "2.6 Days of history", minval = 1, maxval = 50, group = G_SHL)
int i_maxTF = input.int(60, "2.7 Hide sessions above (minutes)", minval = 1, maxval = 1440, group = G_SHL)
string i_sSwSt = input.string("Dotted", "2.8 Swept style", options = ["Dotted", "Dashed", "Unchanged"], group = G_SHL, tooltip = "A session high / low is swept when price trades through it from below (high) or from above (low) after the session ends. The line then changes style, fades and its label gets a check mark.")
int i_sSwOp = input.int(50, "2.9 Swept opacity %", minval = 0, maxval = 100, group = G_SHL)
bool i_pdOn = input.bool(true, "3.1 PDH / PDL", inline = "pd", group = G_PDL)
color i_pdCol = input.color(color.lime, "", inline = "pd", group = G_PDL)
int i_pdKeep = input.int(5, "3.2 Days of history", minval = 1, maxval = 50, group = G_PDL)
string i_pdFrom = input.string("Candle that made it", "3.3 Line starts at", options = ["Candle that made it", "Day open"], group = G_PDL, tooltip = "Day open: the line starts when the new day opens (18:00 New York), where the level becomes the previous day level. Candle that made it: the line starts on the candle that made the high / low.")
string i_pdSt = input.string("Solid", "4.1 Line style", options = ["Solid", "Dotted", "Dashed"], group = G_PST)
int i_pdW = input.int(3, "4.2 Line width", minval = 1, maxval = 5, group = G_PST)
string i_pdSize = input.string("Normal", "4.3 Label size", options = ["Tiny", "Small", "Normal", "Large"], group = G_PST)
string i_pdSwSt = input.string("Dotted", "4.4 Swept style", options = ["Dotted", "Dashed", "Unchanged"], group = G_PST, tooltip = "PDH is swept when price trades up through it from below; PDL when price trades down through it from above. The line then changes style, fades and its label gets a check mark.")
int i_pdSwOp = input.int(50, "4.5 Swept opacity %", minval = 0, maxval = 100, group = G_PST)
// =====================================================================
// Helpers
// =====================================================================
f_style(string s) =>
s == "Solid" ? line.style_solid : s == "Dashed" ? line.style_dashed : line.style_dotted
f_size(string s) =>
switch s
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.huge
string tz = "America/New_York"
bool showSess = timeframe.isintraday and timeframe.in_seconds() <= i_maxTF * 60
bool showPD = timeframe.isintraday
string hlStyle = f_style(i_hlSt)
string pdStyle = f_style(i_pdSt)
string hlSize = f_size(i_hlSize)
string pdSize = f_size(i_pdSize)
string wmSize = f_size(i_wmSize)
string sSwSt = i_sSwSt == "Unchanged" ? hlStyle : f_style(i_sSwSt)
string pdSwSt = i_pdSwSt == "Unchanged" ? pdStyle : f_style(i_pdSwSt)
// Sweep state (same rule as the Trader Edge Suite levels): 0 = waiting, 1 = armed (price traded on the near side), 2 = swept (traded through it from the near side)
f_sweep(int sw, float lvl, bool isHigh) =>
int r = sw
if sw < 2 and not na(lvl)
if (sw == 1 or (isHigh ? open < lvl : open > lvl)) and (isHigh ? high >= lvl : low <= lvl)
r := 2
else if isHigh ? low < lvl : high > lvl
r := 1
r
// =====================================================================
// Futures day (18:00 New York opens the next day; Sunday evening belongs to Monday)
// =====================================================================
int tt = time + 6 * 3600000
if dayofweek(tt, tz) == dayofweek.sunday
tt := tt + 86400000
int dKey = year(tt, tz) * 10000 + month(tt, tz) * 100 + dayofmonth(tt, tz)
var int lastKey = na
bool newDay = na(lastKey) or dKey != lastKey
lastKey := dKey
// =====================================================================
// Session backgrounds (same as the Trader Edge Suite Sessions module)
// =====================================================================
bool aAct = not na(time(timeframe.period, i_s1Tm, tz))
bool lAct = not na(time(timeframe.period, i_s2Tm, tz))
bool nAct = not na(time(timeframe.period, i_s3Tm, tz))
int bgT = color.r(chart.bg_color) + color.g(chart.bg_color) + color.b(chart.bg_color) < 384 ? 92 : 96
color bg = na
if i_bg and showSess
if nAct and i_s3On
bg := color.new(i_s3Col, bgT)
else if lAct and i_s2On
bg := color.new(i_s2Col, bgT)
else if aAct and i_s1On
bg := color.new(i_s1Col, bgT)
bgcolor(bg, title = "Session backgrounds")
// =====================================================================
// Sessions: name + high / low lines.
// Each line starts on the exact candle whose wick made the session high (or low).
// =====================================================================
type Sess
line hiL
line loL
label lbH
label lbL
label wm
float hi
float lo
int hiBar
int loBar
int startBar
bool active
bool extending
bool watching
int swH
int swL
string tH
string tL
color col
array<line> lns
array<label> lbs
newSess() =>
Sess.new(na, na, na, na, na, na, na, na, na, na, false, false, false, 0, 0, "", "", na, array.new<line>(), array.new<label>())
method update(Sess s, bool enabled, bool inWin, string nm, string tH, string tL, color col) =>
bool inS = inWin and enabled and showSess
if newDay
s.extending := false
s.watching := false
if inS and not s.active
s.hi := high
s.lo := low
s.hiBar := bar_index
s.loBar := bar_index
s.startBar := bar_index
s.extending := false
s.watching := false
s.swH := 0
s.swL := 0
s.tH := tH
s.tL := tL
s.col := col
if i_wm
s.wm := label.new(bar_index, hl2, nm, style = label.style_label_center, color = color.new(color.white, 100), textcolor = color.new(chart.fg_color, 80), size = wmSize)
s.lbs.push(s.wm)
else
s.wm := na
if i_hl
s.hiL := line.new(bar_index, high, bar_index, high, color = col, width = i_hlW, style = hlStyle)
s.loL := line.new(bar_index, low, bar_index, low, color = col, width = i_hlW, style = hlStyle)
s.lns.push(s.hiL)
s.lns.push(s.loL)
s.lbH := label.new(bar_index, high, tH, style = label.style_label_lower_left, color = color.new(color.white, 100), textcolor = col, size = hlSize)
s.lbL := label.new(bar_index, low, tL, style = label.style_label_upper_left, color = color.new(color.white, 100), textcolor = col, size = hlSize)
s.lbs.push(s.lbH)
s.lbs.push(s.lbL)
else
s.hiL := na
s.loL := na
s.lbH := na
s.lbL := na
while s.lns.size() > i_keep * 2
line.delete(s.lns.shift())
while s.lbs.size() > i_keep * 3
label.delete(s.lbs.shift())
else if inS
if high > s.hi
s.hi := high
s.hiBar := bar_index
if low < s.lo
s.lo := low
s.loBar := bar_index
if not na(s.wm)
label.set_xy(s.wm, math.floor((s.startBar + bar_index) / 2), (s.hi + s.lo) / 2)
else if s.active
s.extending := i_ext and not newDay
s.watching := not newDay
if s.watching and not inS and not na(s.hiL)
// after the session: a sweep changes the line style, fades it and adds a check mark
int nH = f_sweep(s.swH, s.hi, true)
int nL = f_sweep(s.swL, s.lo, false)
if nH == 2 and s.swH < 2
line.set_style(s.hiL, sSwSt)
line.set_color(s.hiL, color.new(s.col, 100 - i_sSwOp))
label.set_text(s.lbH, s.tH + " ✓")
if nL == 2 and s.swL < 2
line.set_style(s.loL, sSwSt)
line.set_color(s.loL, color.new(s.col, 100 - i_sSwOp))
label.set_text(s.lbL, s.tL + " ✓")
s.swH := nH
s.swL := nL
if not na(s.hiL) and (inS or (s.extending and not inS))
// high line: from the wick that made the high to the current bar; same for the low
line.set_xy1(s.hiL, s.hiBar, s.hi)
line.set_xy2(s.hiL, bar_index, s.hi)
line.set_xy1(s.loL, s.loBar, s.lo)
line.set_xy2(s.loL, bar_index, s.lo)
// labels sit on the wick that made the high / low, so each line is named where it starts
label.set_xy(s.lbH, s.hiBar, s.hi)
label.set_xy(s.lbL, s.loBar, s.lo)
s.active := inS
s
var Sess s1 = newSess()
var Sess s2 = newSess()
var Sess s3 = newSess()
s1.update(i_s1On, aAct, "ASIA", "Asia H", "Asia L", i_s1Col)
s2.update(i_s2On, lAct, "LONDON", "London H", "London L", i_s2Col)
s3.update(i_s3On, nAct, "NY RTH", "NY H", "NY L", i_s3Col)
// =====================================================================
// Previous day levels (built from confirmed chart bars - no request.security, no lookahead)
// Same look as the Trader Edge Suite levels: one color, solid, label on the right, swept = dotted + faded + check mark.
// =====================================================================
type Lvl
line ln
label lb
float lvl
int sw
bool isHigh
string tag
array<line> lns
array<label> lbs
newLvl(bool isHigh, string tag) =>
Lvl.new(na, na, na, 0, isHigh, tag, array.new<line>(), array.new<label>())
method start(Lvl l, float price, int fromBar) =>
if not na(l.ln)
// yesterday's level stops where the new day begins; its label moves to the start of its line
line.set_extend(l.ln, extend.none)
label.set_x(l.lb, line.get_x1(l.ln))
label.set_style(l.lb, label.style_label_lower_left)
l.lvl := price
l.sw := 0
if i_pdOn and not na(price) and not na(fromBar)
l.ln := line.new(fromBar, price, bar_index, price, color = i_pdCol, width = i_pdW, style = pdStyle, extend = extend.right)
l.lb := label.new(bar_index, price, l.tag, style = label.style_label_left, color = color.new(color.white, 100), textcolor = i_pdCol, size = pdSize)
l.lns.push(l.ln)
l.lbs.push(l.lb)
while l.lns.size() > i_pdKeep
line.delete(l.lns.shift())
while l.lbs.size() > i_pdKeep
label.delete(l.lbs.shift())
else
l.ln := na
l.lb := na
l
method follow(Lvl l) =>
if not na(l.ln)
line.set_x2(l.ln, bar_index)
label.set_x(l.lb, bar_index + 2)
int n = f_sweep(l.sw, l.lvl, l.isHigh)
if n == 2 and l.sw < 2
line.set_style(l.ln, pdSwSt)
line.set_color(l.ln, color.new(i_pdCol, 100 - i_pdSwOp))
label.set_text(l.lb, l.tag + " ✓")
l.sw := n
l
var int dayCount = 0
var float dH = na
var float dL = na
var int dHBar = na
var int dLBar = na
var float pdH = na
var float pdL = na
var Lvl lvH = newLvl(true, "PDH")
var Lvl lvL = newLvl(false, "PDL")
if newDay
dayCount += 1
// dayCount >= 3: skip the first (possibly partial) day loaded on the chart
if dayCount >= 3
pdH := dH
pdL := dL
if showPD
bool fromOpen = i_pdFrom == "Day open"
lvH.start(pdH, fromOpen ? bar_index : dHBar)
lvL.start(pdL, fromOpen ? bar_index : dLBar)
dH := high
dL := low
dHBar := bar_index
dLBar := bar_index
else
if high > dH
dH := high
dHBar := bar_index
if low < dL
dL := low
dLBar := bar_index
if showPD and not newDay
lvH.follow()
lvL.follow()
// =====================================================================
// Alerts (on bar close is recommended when creating the alert)
// =====================================================================
alertcondition(ta.crossover(close, pdH) and showPD, "Close above PDH", "{{ticker}} closed above the previous day high")
alertcondition(ta.crossunder(close, pdL) and showPD, "Close below PDL", "{{ticker}} closed below the previous day low")