Session Ranges [OrbitOne]

orbitonetv · study · 185 行 · 点赞 16 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © orbitonetv
//@version=6
indicator("Session Ranges [OrbitOne]", "Session Ranges [OrbitOne]", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 50)
 
// ════════ SESSIONS ════════
gA    = "Asia (Tokyo)"
aOn   = input.bool(true, "Show", inline = "a", group = gA)
aCol  = input.color(#F7B500, "", inline = "a", group = gA)
aSess = input.session("0900-1800", "Session hours", group = gA)
aTz   = input.string("Asia/Tokyo", "Time zone", group = gA, tooltip = "IANA time zone of the session's city (e.g. Asia/Tokyo, Europe/London, America/New_York) or UTC. Using the city's own zone keeps daylight saving time correct automatically.")
 
gL    = "London"
lOn   = input.bool(true, "Show", inline = "l", group = gL)
lCol  = input.color(#2962FF, "", inline = "l", group = gL)
lSess = input.session("0800-1700", "Session hours", group = gL)
lTz   = input.string("Europe/London", "Time zone", group = gL)
 
gN    = "New York"
nOn   = input.bool(true, "Show", inline = "n", group = gN)
nCol  = input.color(#E91E63, "", inline = "n", group = gN)
nSess = input.session("0800-1700", "Session hours", group = gN)
nTz   = input.string("America/New_York", "Time zone", group = gN)
 
gC    = "Custom session"
cOn   = input.bool(false, "Show", inline = "c", group = gC)
cCol  = input.color(#00BFA5, "", inline = "c", group = gC)
cName = input.string("Custom", "Name", group = gC)
cSess = input.session("1400-1600", "Session hours", group = gC)
cTz   = input.string("UTC", "Time zone", group = gC)
 
daysI = input.string("Auto", "Trading days", options = ["Auto", "Mon–Fri", "Every day"], group = "General", tooltip = "Auto: every day for crypto (it trades 24/7), Monday to Friday for everything else.")
 
// ════════ STYLE ════════
gS      = "Display"
maxDays = input.int(10, "Sessions kept per market", minval = 1, maxval = 50, group = gS)
boxTr   = input.int(88, "Box transparency", minval = 50, maxval = 98, group = gS)
showTxt = input.bool(true, "Show name and range in box", group = gS)
showMid = input.bool(true, "Show session midpoint", group = gS)
extLv   = input.bool(true, "Extend session high / low until next session", group = gS)
showDb  = input.bool(true, "Show session dashboard", group = gS)
dbPos   = input.string("Top right", "Dashboard position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gS)
 
// ════════ TYPES ════════
type Sess
    string       name
    color        col
    box          bx
    line         mid
    line         hiL
    line         loL
    float        hi
    float        lo
    int          st
    float        lastHi
    float        lastLo
    bool         upDone = true
    bool         dnDone = true
    array<box>   boxes
    array<line>  lines
    array<float> ranges
 
f_new(string name, color col) =>
    Sess.new(name = name, col = col, boxes = array.new<box>(), lines = array.new<line>(), ranges = array.new<float>())
 
var Sess sA = f_new("Asia", aCol)
var Sess sL = f_new("London", lCol)
var Sess sN = f_new("New York", nCol)
var Sess sC = f_new(cName, cCol)
 
// ════════ SESSION MEMBERSHIP ════════
days     = daysI == "Every day" or (daysI == "Auto" and syminfo.type == "crypto") ? "1234567" : "23456"
intraday = timeframe.isintraday
inA = aOn and intraday and not na(time(timeframe.period, aSess + ":" + days, aTz))
inL = lOn and intraday and not na(time(timeframe.period, lSess + ":" + days, lTz))
inN = nOn and intraday and not na(time(timeframe.period, nSess + ":" + days, nTz))
inC = cOn and intraday and not na(time(timeframe.period, cSess + ":" + days, cTz))
 
okBar = barstate.isconfirmed
 
f_txt(Sess s) =>
    float r = s.hi - s.lo
    s.name + "  " + str.tostring(r, format.mintick) + "  (" + str.tostring(r / s.lo * 100, "0.00") + "%)"
 
// returns [breakUp, breakDown] of the last completed session's high / low
f_update(Sess s, bool inS, bool wasIn) =>
    bool brkUp = false
    bool brkDn = false
    if inS and not wasIn
        s.hi  := high
        s.lo  := low
        s.st  := bar_index
        s.hiL := na
        s.loL := na
        s.bx  := box.new(bar_index, high, bar_index, low, border_color = color.new(s.col, 40), bgcolor = color.new(s.col, boxTr), text = showTxt ? s.name : "", text_size = size.small, text_color = s.col, text_halign = text.align_left, text_valign = text.align_top)
        s.boxes.push(s.bx)
        if showMid
            s.mid := line.new(bar_index, hl2, bar_index, hl2, color = color.new(s.col, 30), style = line.style_dashed)
            s.lines.push(s.mid)
    else if inS and not na(s.bx)
        s.hi := math.max(s.hi, high)
        s.lo := math.min(s.lo, low)
        box.set_top(s.bx, s.hi)
        box.set_bottom(s.bx, s.lo)
        box.set_right(s.bx, bar_index)
        if showMid and not na(s.mid)
            float m = (s.hi + s.lo) / 2
            line.set_xy1(s.mid, s.st, m)
            line.set_xy2(s.mid, bar_index, m)
    if inS and showTxt and not na(s.bx)
        box.set_text(s.bx, f_txt(s))
    // session just ended
    if wasIn and not inS and not na(s.hi)
        s.lastHi := s.hi
        s.lastLo := s.lo
        s.upDone := false
        s.dnDone := false
        s.ranges.push(s.hi - s.lo)
        if s.ranges.size() > 20
            s.ranges.shift()
        if extLv
            s.hiL := line.new(bar_index - 1, s.hi, bar_index, s.hi, color = color.new(s.col, 20), style = line.style_dotted, width = 1)
            s.loL := line.new(bar_index - 1, s.lo, bar_index, s.lo, color = color.new(s.col, 20), style = line.style_dotted, width = 1)
            s.lines.push(s.hiL)
            s.lines.push(s.loL)
    // keep last session's levels running until the next one starts
    if not inS and not na(s.hiL)
        line.set_x2(s.hiL, bar_index)
        line.set_x2(s.loL, bar_index)
    // first close beyond the last completed session's range
    if not inS and okBar and not na(s.lastHi)
        if not s.upDone and close > s.lastHi
            s.upDone := true
            brkUp := true
        if not s.dnDone and close < s.lastLo
            s.dnDone := true
            brkDn := true
    while s.boxes.size() > maxDays
        box.delete(s.boxes.shift())
    while s.lines.size() > maxDays * 3
        line.delete(s.lines.shift())
    [brkUp, brkDn]
 
[upA, dnA] = f_update(sA, inA, inA[1])
[upL, dnL] = f_update(sL, inL, inL[1])
[upN, dnN] = f_update(sN, inN, inN[1])
[upC, dnC] = f_update(sC, inC, inC[1])
 
// ════════ DASHBOARD ════════
var table db = table.new(dbPos == "Top right" ? position.top_right : dbPos == "Bottom right" ? position.bottom_right : dbPos == "Top left" ? position.top_left : position.bottom_left, 4, 5, border_width = 1)
 
f_cell(int c, int r, string t, color bg) =>
    table.cell(db, c, r, t, bgcolor = bg, text_color = color.white, text_size = size.tiny)
 
f_row(int r, Sess s, bool on, bool inS) =>
    if on
        float rng = inS ? s.hi - s.lo : s.lastHi - s.lastLo
        float avg = s.ranges.size() > 0 ? s.ranges.avg() : na
        f_cell(0, r, s.name, color.new(s.col, 20))
        f_cell(1, r, inS ? "● Open" : "Closed", inS ? color.new(s.col, 40) : color.new(color.gray, 40))
        f_cell(2, r, na(rng) ? "—" : str.tostring(rng, format.mintick), color.new(color.gray, 40))
        f_cell(3, r, na(rng) or na(avg) or avg == 0 ? "—" : str.tostring(rng / avg * 100, "#") + "%", color.new(color.gray, 40))
 
if showDb and intraday and barstate.islast
    f_cell(0, 0, "Session", color.new(color.black, 30))
    f_cell(1, 0, "Status", color.new(color.black, 30))
    f_cell(2, 0, "Range", color.new(color.black, 30))
    f_cell(3, 0, "vs 20 avg", color.new(color.black, 30))
    f_row(1, sA, aOn, inA)
    f_row(2, sL, lOn, inL)
    f_row(3, sN, nOn, inN)
    f_row(4, sC, cOn, inC)
 
// ════════ WARNING ON HIGHER TIMEFRAMES ════════
var label warn = na
if barstate.islast and not intraday
    label.delete(warn)
    warn := label.new(bar_index, high, "Session Ranges works on intraday timeframes (1m – 4h).", style = label.style_label_down, color = color.new(color.gray, 20), textcolor = color.white, size = size.small)
 
// ════════ ALERTS ════════
alertcondition(inA and not inA[1], "Asia session open", "Asia session opened | {{ticker}} | {{close}}")
alertcondition(inL and not inL[1], "London session open", "London session opened | {{ticker}} | {{close}}")
alertcondition(inN and not inN[1], "New York session open", "New York session opened | {{ticker}} | {{close}}")
alertcondition(upA or upL or upN or upC, "Break above last session high", "Price closed above the last session high | {{ticker}} | {{interval}} | {{close}}")
alertcondition(dnA or dnL or dnN or dnC, "Break below last session low", "Price closed below the last session low | {{ticker}} | {{interval}} | {{close}}")

← 返回列表