Session High/Low Lines/ better version/ jmt

jmcahokson42 · study · 93 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator("Session High/Low - Last 10 Days", overlay=true, max_lines_count=100)
 
timezone = input.string("Etc/UTC", "Session Timezone")
 
asiaSession   = input.session("0000-0800", "Asia Session")
londonSession = input.session("0800-1600", "London Session")
nySession     = input.session("1300-2100", "New York Session")
 
asiaColor   = input.color(color.yellow, "Asia Color")
londonColor = input.color(color.rgb(255, 215, 0), "London Color")
nyColor     = input.color(color.blue, "New York Color")
 
lineWidth   = input.int(1, "Line Thickness", minval=1, maxval=5)
 
// Keep the latest 60 lines: approximately 10 days of 3 sessions × 2 lines.
var array<line> sessionLines = array.new<line>()
maxStoredLines = 60
 
f_storeLine(line newLine) =>
    sessionLines.push(newLine)
    if sessionLines.size() > maxStoredLines
        line.delete(sessionLines.shift())
 
dayKey = year(time, timezone) * 10000 + month(time, timezone) * 100 + dayofmonth(time, timezone)
 
f_updateSession(bool inSession, color lineColor, int currentDay, int width) =>
    var float sessionHigh = na
    var float sessionLow = na
    var int sessionStartBar = na
    var int trackedDay = na
    var line highLine = na
    var line lowLine = na
 
    bool dayChanged = not na(trackedDay) and currentDay != trackedDay
 
    if dayChanged
        if not na(highLine)
            highLine.set_x2(bar_index - 1)
        if not na(lowLine)
            lowLine.set_x2(bar_index - 1)
 
        highLine := na
        lowLine := na
        sessionHigh := na
        sessionLow := na
        sessionStartBar := na
 
    trackedDay := currentDay
 
    bool sessionStart = inSession and (barstate.isfirst or not inSession[1])
    bool sessionEnd = not inSession and inSession[1]
 
    if sessionStart
        sessionHigh := high
        sessionLow := low
        sessionStartBar := bar_index
 
    if inSession
        sessionHigh := math.max(nz(sessionHigh, high), high)
        sessionLow := math.min(nz(sessionLow, low), low)
 
    // Create fixed-level lines once the session has ended.
    if sessionEnd and not na(sessionStartBar)
        highLine := line.new(
             sessionStartBar, sessionHigh, bar_index - 1, sessionHigh,
             xloc=xloc.bar_index, extend=extend.none,
             color=lineColor, width=width)
 
        lowLine := line.new(
             sessionStartBar, sessionLow, bar_index - 1, sessionLow,
             xloc=xloc.bar_index, extend=extend.none,
             color=lineColor, width=width)
 
        f_storeLine(highLine)
        f_storeLine(lowLine)
 
    // Extend the completed session lines forward only, until the day changes.
    if not inSession
        if not na(highLine)
            highLine.set_x2(bar_index)
        if not na(lowLine)
            lowLine.set_x2(bar_index)
 
    0
 
asiaActive   = not na(time(timeframe.period, asiaSession, timezone))
londonActive = not na(time(timeframe.period, londonSession, timezone))
nyActive     = not na(time(timeframe.period, nySession, timezone))
 
f_updateSession(asiaActive, asiaColor, dayKey, lineWidth)
f_updateSession(londonActive, londonColor, dayKey, lineWidth)
f_updateSession(nyActive, nyColor, dayKey, lineWidth)

← 返回列表