Global Sessions & Key Levels

Cedric_Invests · study · 179 行 · 点赞 35 · TradingView 原页

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

Pine Script

//@version=6
indicator("Session Structure | Sydney Tokyo London New York [V1.1]", "Session Structure V1.1", overlay = true, max_boxes_count = 200, max_lines_count = 500, max_labels_count = 500)
 
// -----------------------------------------------------------------------------
// SESSION STRUCTURE V1.1
// Four independent market sessions. All times are interpreted in each market's
// own IANA time zone, so DST changes are handled by TradingView automatically.
// No trade signals, dashboard, or automated support/resistance zones.
// Designed for intraday charts (ideally 1m through 30m).
// -----------------------------------------------------------------------------
 
string gGeneral = "Appearance"
bool showBoxes = input.bool(true, "Shaded session boxes", group = gGeneral)
bool showLevels = input.bool(true, "Session high / low lines", group = gGeneral)
bool showOpen = input.bool(true, "Session open lines", group = gGeneral)
bool showTags = input.bool(true, "High / low / open labels", group = gGeneral)
bool showPrices = input.bool(false, "Show prices in labels", group = gGeneral)
bool showRange = input.bool(false, "Show range in session header", group = gGeneral)
int opacity = input.int(87, "Box transparency", minval = 0, maxval = 100, group = gGeneral)
int borderTransparency = input.int(32, "Box border transparency", minval = 0, maxval = 100, group = gGeneral)
int levelWidth = input.int(1, "High / low line width", minval = 1, maxval = 4, group = gGeneral)
string openStyleInput = input.string("Dashed", "Open line style", options = ["Solid", "Dashed", "Dotted"], group = gGeneral)
string tagSizeInput = input.string("Tiny", "Label size", options = ["Tiny", "Small", "Normal", "Large"], group = gGeneral)
int historyCount = input.int(8, "Keep last N sessions (per market)", minval = 1, maxval = 30, group = gGeneral)
int projectBars = input.int(3, "Extend level lines (bars)", minval = 0, maxval = 100, group = gGeneral, tooltip = "Extends each high/low/open level by this many chart-bar durations beyond its last session bar. The shaded session box is never projected into the future.")
 
string gSydney = "Sydney"
bool onSydney = input.bool(true, "Show Sydney", group = gSydney)
string hoursSydney = input.session("0700-1600", "Local trading hours", group = gSydney)
color colorSydney = input.color(color.rgb(123, 141, 158), "Color", group = gSydney)
 
string gTokyo = "Tokyo"
bool onTokyo = input.bool(true, "Show Tokyo", group = gTokyo)
string hoursTokyo = input.session("0900-1800", "Local trading hours", group = gTokyo)
color colorTokyo = input.color(color.rgb(90, 137, 163), "Color", group = gTokyo)
 
string gLondon = "London"
bool onLondon = input.bool(true, "Show London", group = gLondon)
string hoursLondon = input.session("0800-1700", "Local trading hours", group = gLondon)
color colorLondon = input.color(color.rgb(128, 132, 140), "Color", group = gLondon)
 
string gNewYork = "New York"
bool onNewYork = input.bool(true, "Show New York", group = gNewYork)
string hoursNewYork = input.session("0800-1700", "Local trading hours", group = gNewYork)
color colorNewYork = input.color(color.rgb(83, 93, 110), "Color", group = gNewYork)
 
string openStyle = switch openStyleInput
    "Solid" => line.style_solid
    "Dotted" => line.style_dotted
    => line.style_dashed
 
string tagSize = switch tagSizeInput
    "Small" => size.small
    "Normal" => size.normal
    "Large" => size.large
    => size.tiny
 
// 1 session = 1 box, up to 3 lines, up to 3 level labels, 1 session-name label.
// `var` storage inside this function is independent for each written call site.
f_drawSession(bool enabled, string title, string abbreviation, string sessionHours, string tz, color sessionColor) =>
    int sessionBar = time(timeframe.period, sessionHours, tz)
    bool inside = enabled and timeframe.isintraday and not na(sessionBar)
    bool newTradingDay = not na(time[1]) and (year(time, tz) != year(time[1], tz) or month(time, tz) != month(time[1], tz) or dayofmonth(time, tz) != dayofmonth(time[1], tz))
    bool started = inside and (not inside[1] or newTradingDay)
    int barMs = int(timeframe.in_seconds() * 1000)
    int rightNow = na(time_close) ? time + barMs : time_close
    int projectedRight = rightNow + projectBars * barMs
 
    var float sessionHigh = na
    var float sessionLow = na
    var float sessionOpen = na
    var int sessionStart = na
    var box currentBox = na
    var line highLine = na
    var line lowLine = na
    var line openLine = na
    var label highTag = na
    var label lowTag = na
    var label openTag = na
    var label nameTag = na
 
    var array<box> boxes = array.new<box>()
    var array<line> lines = array.new<line>()
    var array<label> tags = array.new<label>()
    var array<label> names = array.new<label>()
 
    if started
        sessionHigh := high
        sessionLow := low
        sessionOpen := open
        sessionStart := time
 
        currentBox := na
        highLine := na
        lowLine := na
        openLine := na
        highTag := na
        lowTag := na
        openTag := na
        nameTag := na
 
        if showBoxes
            currentBox := box.new(left = sessionStart, top = sessionHigh, right = rightNow, bottom = sessionLow, xloc = xloc.bar_time, border_color = color.new(sessionColor, borderTransparency), bgcolor = color.new(sessionColor, opacity))
            array.push(boxes, currentBox)
 
        if showLevels
            highLine := line.new(x1 = sessionStart, y1 = sessionHigh, x2 = projectedRight, y2 = sessionHigh, xloc = xloc.bar_time, color = sessionColor, width = levelWidth)
            lowLine := line.new(x1 = sessionStart, y1 = sessionLow, x2 = projectedRight, y2 = sessionLow, xloc = xloc.bar_time, color = sessionColor, width = levelWidth)
            array.push(lines, highLine)
            array.push(lines, lowLine)
 
            if showTags
                highTag := label.new(x = projectedRight, y = sessionHigh, text = abbreviation + " H", xloc = xloc.bar_time, style = label.style_none, textcolor = sessionColor, size = tagSize)
                lowTag := label.new(x = projectedRight, y = sessionLow, text = abbreviation + " L", xloc = xloc.bar_time, style = label.style_none, textcolor = sessionColor, size = tagSize)
                array.push(tags, highTag)
                array.push(tags, lowTag)
 
        // The opening price remains fixed for the entire session.
        // It is controlled independently from high/low visibility.
        if showOpen
            openLine := line.new(x1 = sessionStart, y1 = sessionOpen, x2 = projectedRight, y2 = sessionOpen, xloc = xloc.bar_time, color = sessionColor, width = levelWidth, style = openStyle)
            array.push(lines, openLine)
            if showTags
                openTag := label.new(x = projectedRight, y = sessionOpen, text = abbreviation + " O", xloc = xloc.bar_time, style = label.style_none, textcolor = sessionColor, size = tagSize)
                array.push(tags, openTag)
 
        if showBoxes
            nameTag := label.new(x = sessionStart, y = sessionHigh, text = title, xloc = xloc.bar_time, style = label.style_none, textcolor = sessionColor, size = tagSize)
            array.push(names, nameTag)
 
        // Each market retains its own last N sessions.
        while array.size(boxes) > historyCount
            box.delete(array.shift(boxes))
        while array.size(lines) > historyCount * 3
            line.delete(array.shift(lines))
        while array.size(tags) > historyCount * 3
            label.delete(array.shift(tags))
        while array.size(names) > historyCount
            label.delete(array.shift(names))
 
    if inside and not na(sessionStart)
        sessionHigh := math.max(sessionHigh, high)
        sessionLow := math.min(sessionLow, low)
 
        if not na(currentBox)
            box.set_right(currentBox, rightNow)
            box.set_top(currentBox, sessionHigh)
            box.set_bottom(currentBox, sessionLow)
 
        if not na(highLine)
            line.set_y1(highLine, sessionHigh)
            line.set_y2(highLine, sessionHigh)
            line.set_x2(highLine, projectedRight)
        if not na(lowLine)
            line.set_y1(lowLine, sessionLow)
            line.set_y2(lowLine, sessionLow)
            line.set_x2(lowLine, projectedRight)
 
        if not na(openLine)
            line.set_x2(openLine, projectedRight)
        if not na(openTag)
            label.set_xy(openTag, projectedRight, sessionOpen)
            label.set_text(openTag, abbreviation + " O" + (showPrices ? " " + str.tostring(sessionOpen, format.mintick) : ""))
 
        if not na(highTag)
            label.set_xy(highTag, projectedRight, sessionHigh)
            label.set_text(highTag, abbreviation + " H" + (showPrices ? " " + str.tostring(sessionHigh, format.mintick) : ""))
        if not na(lowTag)
            label.set_xy(lowTag, projectedRight, sessionLow)
            label.set_text(lowTag, abbreviation + " L" + (showPrices ? " " + str.tostring(sessionLow, format.mintick) : ""))
        if not na(nameTag)
            label.set_y(nameTag, sessionHigh)
            label.set_text(nameTag, title + (showRange ? " " + str.tostring(sessionHigh - sessionLow, format.mintick) : ""))
    0
 
f_drawSession(onSydney, "Sydney", "SYD", hoursSydney, "Australia/Sydney", colorSydney)
f_drawSession(onTokyo, "Tokyo", "TKY", hoursTokyo, "Asia/Tokyo", colorTokyo)
f_drawSession(onLondon, "London", "LDN", hoursLondon, "Europe/London", colorLondon)
f_drawSession(onNewYork, "New York", "NY", hoursNewYork, "America/New_York", colorNewYork)

← 返回列表