Prev Day + Premarket Levels

pashairfan · study · 241 行 · 点赞 4 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// Prev Day + Premarket Levels
// Plots the previous regular-session (RTH) high, low and close plus premarket, Asia, London and NY session highs/lows.
// • Futures mode: the trading day rolls at the 6 pm ET Globex open. PDH/PDL = the RTH session that just ended, PDC = the 5 pm ET close.
// • Asia, London and premarket levels build live once their session starts and are cleared at 4 pm ET (configurable);
//   after that only NY H/L and PDH/PDL/PDC remain until the next Globex open.
// • Auto daylight saving (on by default): Asia runs from the Tokyo 9:00 open to the London open, London from the 8:00 UK open,
//   so the sessions stay correct in ET when US or UK clocks change. Turn it off to use the fixed ET times in the inputs.
// • When the market is closed (weekend or holiday), the RTH session that just ended is shown as PDH/PDL/PDC right away.
// • A session line sitting on the same price as a higher-priority line is hidden (PD > NY > Premarket > London > Asia).
// • Equities mode (Futures Mode off): RTH 9:30–16:00 ET and premarket levels only.
indicator("Prev Day + Premarket Levels", overlay = true, max_lines_count = 100, max_labels_count = 100, max_boxes_count = 20)
 
// ─── INPUTS ───────────────────────────────────────────────────────────────────
gPD = "Previous Day (RTH)"
show_pd_high   = input.bool(true, "Show Prev Day High", group = gPD)
show_pd_low    = input.bool(true, "Show Prev Day Low", group = gPD)
pd_hl_col      = input.color(color.new(#E9A25F, 0), "Prev Day High/Low Color", group = gPD)
line_width     = input.int(2, "Line Width", minval = 1, maxval = 4, group = gPD)
line_style_inp = input.string("Dashed", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = gPD)
 
gPC = "Previous Day Close (5 pm ET)"
show_pd_close  = input.bool(true, "Show Prev Day Close", group = gPC)
pd_close_col   = input.color(color.new(#EDE0C8, 0), "Prev Day Close Color", group = gPC)
close_width    = input.int(2, "Line Width", minval = 1, maxval = 4, group = gPC)
close_style    = input.string("Dashed", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = gPC)
 
gPM = "Premarket"
show_pm_high   = input.bool(true, "Show Premarket High", group = gPM)
show_pm_low    = input.bool(true, "Show Premarket Low", group = gPM)
pm_start_inp   = input.int(400, "Start Time (HHMM ET)", minval = 0, maxval = 2359, group = gPM)
pm_end_inp     = input.int(930, "End Time (HHMM ET)", minval = 0, maxval = 2359, group = gPM)
pm_col         = input.color(color.new(#6FC2C0, 0), "Premarket High/Low Color", group = gPM)
pm_line_width  = input.int(2, "Line Width", minval = 1, maxval = 4, group = gPM)
pm_line_style  = input.string("Dotted", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = gPM)
 
gA = "Asia Session (Futures Only)"
show_asia      = input.bool(true, "Show Asia Session Box", group = gA)
asia_start_inp = input.int(2000, "Start Time (HHMM ET)", minval = 0, maxval = 2359, group = gA)
asia_end_inp   = input.int(300, "End Time (HHMM ET)", minval = 0, maxval = 2359, group = gA)
asia_col       = input.color(color.new(#7F8CC9, 90), "Session Color", group = gA)
asia_hl_col    = input.color(color.new(#7F8CC9, 0), "H/L Line Color", group = gA)
asia_hl_width  = input.int(1, "H/L Line Width", minval = 1, maxval = 4, group = gA)
asia_hl_style  = input.string("Dotted", "H/L Line Style", options = ["Solid", "Dashed", "Dotted"], group = gA)
show_asia_hl   = input.bool(true, "Show Asia H/L Lines", group = gA)
 
gL = "London Session (Futures Only)"
show_london    = input.bool(true, "Show London Session Box", group = gL)
lon_start_inp  = input.int(300, "Start Time (HHMM ET)", minval = 0, maxval = 2359, group = gL)
lon_end_inp    = input.int(830, "End Time (HHMM ET)", minval = 0, maxval = 2359, group = gL)
london_col     = input.color(color.new(#8FB996, 90), "Session Color", group = gL)
london_hl_col  = input.color(color.new(#8FB996, 0), "H/L Line Color", group = gL)
london_hl_width = input.int(1, "H/L Line Width", minval = 1, maxval = 4, group = gL)
london_hl_style = input.string("Dotted", "H/L Line Style", options = ["Solid", "Dashed", "Dotted"], group = gL)
show_london_hl = input.bool(true, "Show London H/L Lines", group = gL)
 
gN = "NY Session (Futures Only)"
show_ny        = input.bool(true, "Show NY Session Box", group = gN)
ny_start_inp   = input.int(930, "Start Time (HHMM ET)", minval = 0, maxval = 2359, group = gN)
ny_end_inp     = input.int(1600, "End Time (HHMM ET)", minval = 0, maxval = 2359, group = gN)
ny_col         = input.color(color.new(#D4A5A5, 90), "Session Color", group = gN)
ny_hl_col      = input.color(color.new(#D4A5A5, 0), "H/L Line Color", group = gN)
ny_hl_width    = input.int(1, "H/L Line Width", minval = 1, maxval = 4, group = gN)
ny_hl_style    = input.string("Dotted", "H/L Line Style", options = ["Solid", "Dashed", "Dotted"], group = gN)
show_ny_hl     = input.bool(true, "Show NY H/L Lines", group = gN)
 
gLb = "Labels"
label_size_inp = input.string("Small", "Label Size", options = ["Tiny", "Small", "Normal", "Large"], group = gLb)
show_labels    = input.bool(true, "Show Labels", group = gLb)
show_px        = input.bool(false, "Show price in labels", group = gLb)
 
gB = "Behaviour"
is_futures     = input.bool(true, "Futures Mode (RTH 9:30–16:15 ET, new day at the 6 pm Globex open)", group = gB, tooltip = "OFF = equities: RTH 9:30–16:00 ET, no Asia/London/NY sessions.")
hide_from      = input.int(1600, "Remove Asia / London / Premarket at (HHMM ET)", minval = 0, maxval = 2359, group = gB, tooltip = "From this time until the 6 pm Globex open only NY H/L and PDH/PDL/PDC are shown.")
dedupe         = input.bool(true, "Hide a line that sits on a PD line (PD wins, then NY, Premarket, London, Asia)", group = gB)
show_box_border = input.bool(false, "Show Session Box Border", group = gB)
auto_dst       = input.bool(true, "Auto daylight saving (Asia = Tokyo 9:00 open → London open, London = 8:00 UK open → London end time)", group = gB, tooltip = "ON: Asia and London follow the real Tokyo and London opens, so in ET they move when the US/UK clocks change (Asia 7 pm ET in winter / 8 pm ET in summer; London 3 am ET). The Asia start/end and London start times above are then ignored; the London end time is still used. OFF: use the fixed ET times above.")
 
// ─── HELPERS ──────────────────────────────────────────────────────────────────
f_style(string s) => s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
f_lbl_size(string s) => s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
f_in(int hhmm, int s, int e) => e < s ? (hhmm >= s or hhmm < e) : (hhmm >= s and hhmm < e)
 
type Sess
    float h
    float l
    int   b0
    int   b1
    bool  was
 
type Lvl
    float  p
    string n
    color  c
    int    w
    string st
    int    b0
    int    pr
 
f_reset(Sess s) =>
    s.h := na
    s.l := na
    s.b0 := na
    s.b1 := na
 
// builds the session high/low live while the session is active; freezes when it ends
f_upd(Sess s, bool inS) =>
    if inS
        if not s.was or na(s.h)
            s.h := high
            s.l := low
            s.b0 := time
        else
            s.h := math.max(s.h, high)
            s.l := math.min(s.l, low)
        s.b1 := time_close
    s.was := inS
 
f_box(Sess s, color col) =>
    box.new(s.b0, s.h, s.b1, s.l, xloc = xloc.bar_time, bgcolor = col, border_color = show_box_border ? color.new(col, 80) : na, border_width = show_box_border ? 1 : 0)
 
// ─── TIME (ET) ────────────────────────────────────────────────────────────────
TZ = "America/New_York"
hhmm = hour(time, TZ) * 100 + minute(time, TZ)
newDay = not na(time_tradingday[1]) and time_tradingday != time_tradingday[1]   // 6 pm ET Globex open on futures
rth_end = is_futures ? 1615 : 1600
in_rth = hhmm >= 930 and hhmm < rth_end
prev_close = close[1]
 
// ─── STATE ────────────────────────────────────────────────────────────────────
var float rth_h = na
var float rth_l = na
var float rth_c = na
var float pdh = na
var float pdl = na
var float pdc = na
var int   pd_bar = na
var Sess asia = Sess.new(na, na, na, na, false)
var Sess lon  = Sess.new(na, na, na, na, false)
var Sess pm   = Sess.new(na, na, na, na, false)
var Sess ny   = Sess.new(na, na, na, na, false)
 
if newDay
    if not na(rth_h)   // a session with no RTH (holiday) keeps the older PDH/PDL
        pdh := rth_h
        pdl := rth_l
    if not na(prev_close)
        pdc := prev_close   // 5 pm ET close: the last bar before the 6 pm Globex open
    pd_bar := time   // drawings are placed by time (xloc.bar_time), so no history-buffer limit on 1m charts
    rth_h := na
    rth_l := na
    rth_c := na
    f_reset(asia)
    f_reset(lon)
    f_reset(pm)
    f_reset(ny)
 
if in_rth
    rth_h := na(rth_h) ? high : math.max(rth_h, high)
    rth_l := na(rth_l) ? low : math.min(rth_l, low)
    rth_c := close   // last RTH bar's close on any timeframe
 
f_upd(pm, f_in(hhmm, pm_start_inp, pm_end_inp))
if is_futures
    int tkHM = hour(time, "Asia/Tokyo") * 100 + minute(time, "Asia/Tokyo")
    int ldHM = hour(time, "Europe/London") * 100 + minute(time, "Europe/London")
    bool inAsia = auto_dst ? (tkHM >= 900 and ldHM < 800) : f_in(hhmm, asia_start_inp, asia_end_inp)
    bool inLon = auto_dst ? (ldHM >= 800 and ldHM < 1700 and hhmm < lon_end_inp) : f_in(hhmm, lon_start_inp, lon_end_inp)
    f_upd(asia, inAsia)
    f_upd(lon, inLon)
    f_upd(ny, f_in(hhmm, ny_start_inp, ny_end_inp))
 
// ─── DRAW (last bar only) ─────────────────────────────────────────────────────
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
var array<box>   bxs = array.new<box>()
 
if barstate.islast
    for x in lns
        x.delete()
    for x in lbs
        x.delete()
    for x in bxs
        x.delete()
    lns.clear()
    lbs.clear()
    bxs.clear()
    // clock used for the 4 pm cleanup: now on a live bar, else the end of the last bar (works on 4H too)
    int tRef = barstate.isrealtime ? timenow : time_close - 1
    int nowHM = hour(tRef, TZ) * 100 + minute(tRef, TZ)
    bool late = nowHM >= hide_from and nowHM < 1800
    // market closed (weekend / holiday) after a completed RTH session: show that session as the previous day
    bool closedRoll = not barstate.isrealtime and (timenow - time_close) > 70 * 60000 and not na(rth_h) and hhmm >= rth_end and hhmm < 1800
    float dPDH = closedRoll ? rth_h : pdh
    float dPDL = closedRoll ? rth_l : pdl
    float dPDC = closedRoll ? close : pdc
    int dPDB = closedRoll ? ny.b0 : pd_bar
    array<Lvl> L = array.new<Lvl>()
    if show_pd_high and not na(dPDH)
        L.push(Lvl.new(dPDH, "PDH", pd_hl_col, line_width, f_style(line_style_inp), dPDB, 0))
    if show_pd_low and not na(dPDL)
        L.push(Lvl.new(dPDL, "PDL", pd_hl_col, line_width, f_style(line_style_inp), dPDB, 0))
    if show_pd_close and not na(dPDC)
        L.push(Lvl.new(dPDC, "PDC", pd_close_col, close_width, f_style(close_style), closedRoll ? time : pd_bar, 0))
    if is_futures and show_ny_hl and not na(ny.h) and not closedRoll
        L.push(Lvl.new(ny.h, "NY H", ny_hl_col, ny_hl_width, f_style(ny_hl_style), ny.b0, 1))
        L.push(Lvl.new(ny.l, "NY L", ny_hl_col, ny_hl_width, f_style(ny_hl_style), ny.b0, 1))
    if not late
        if show_pm_high and not na(pm.h)
            L.push(Lvl.new(pm.h, "PMH", pm_col, pm_line_width, f_style(pm_line_style), pm.b0, 2))
        if show_pm_low and not na(pm.l)
            L.push(Lvl.new(pm.l, "PML", pm_col, pm_line_width, f_style(pm_line_style), pm.b0, 2))
        if is_futures and show_london_hl and not na(lon.h)
            L.push(Lvl.new(lon.h, "Lon H", london_hl_col, london_hl_width, f_style(london_hl_style), lon.b0, 3))
            L.push(Lvl.new(lon.l, "Lon L", london_hl_col, london_hl_width, f_style(london_hl_style), lon.b0, 3))
        if is_futures and show_asia_hl and not na(asia.h)
            L.push(Lvl.new(asia.h, "Asia H", asia_hl_col, asia_hl_width, f_style(asia_hl_style), asia.b0, 4))
            L.push(Lvl.new(asia.l, "Asia L", asia_hl_col, asia_hl_width, f_style(asia_hl_style), asia.b0, 4))
    lbl_sz = f_lbl_size(label_size_inp)
    int nL = L.size()
    if nL > 0
        for i = 0 to nL - 1
            Lvl v = L.get(i)
            bool dup = false
            if dedupe
                for j = 0 to nL - 1
                    Lvl u = L.get(j)
                    if u.pr < v.pr and math.abs(u.p - v.p) <= syminfo.mintick * 0.5
                        dup := true
            if not dup
                int x0 = na(v.b0) ? time : v.b0
                lns.push(line.new(x0, v.p, time, v.p, xloc = xloc.bar_time, extend = extend.right, color = v.c, width = v.w, style = v.st))
                if show_labels
                    lbs.push(label.new(bar_index + 2, v.p, v.n + (show_px ? " " + str.tostring(v.p, format.mintick) : ""), color = v.c, textcolor = color.new(#0F1115, 0), style = label.style_label_left, size = lbl_sz))
    if is_futures
        if show_asia and not late and not na(asia.h)
            bxs.push(f_box(asia, asia_col))
        if show_london and not late and not na(lon.h)
            bxs.push(f_box(lon, london_col))
        if show_ny and not na(ny.h) and not closedRoll
            bxs.push(f_box(ny, ny_col))

← 返回列表