Xalgo - Sessions Dashboard

SahilTradeSchool · study · 270 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("Xalgo - Sessions Dashboard", shorttitle="Xalgo - Sessions Dashboard", overlay=true, max_boxes_count=500, max_labels_count=500)
 
// ============================================================================
//  SETTINGS  -  2 groups: SESSIONS (boxes on chart) + DASHBOARD (table)
//  NEW: "Clean White Premium" theme (default) + redesigned 6-column dashboard
// ============================================================================
grpSes  = "01 · TRADING SESSIONS"
grpDash = "02 · SESSIONS DASHBOARD"
 
// ---- 01 · TRADING SESSIONS ----
sesMaster = input.bool(true, "Show session boxes on chart", group=grpSes, tooltip="Master switch for the four session boxes on the chart.\nThe dashboard has its own switch in section 02.")
sesAOn    = input.bool(true, "", inline="sesA", group=grpSes)
sesAName  = input.string("New York", "", inline="sesA", group=grpSes)
sesATime  = input.session("1300-2200", "", inline="sesA", group=grpSes)
sesAColor = input.color(#ff5d00, "", inline="sesA", group=grpSes)
sesBOn    = input.bool(true, "", inline="sesB", group=grpSes)
sesBName  = input.string("London", "", inline="sesB", group=grpSes)
sesBTime  = input.session("0700-1600", "", inline="sesB", group=grpSes)
sesBColor = input.color(#2157f3, "", inline="sesB", group=grpSes)
sesCOn    = input.bool(true, "", inline="sesC", group=grpSes)
sesCName  = input.string("Tokyo", "", inline="sesC", group=grpSes)
sesCTime  = input.session("0000-0900", "", inline="sesC", group=grpSes)
sesCColor = input.color(#e91e63, "", inline="sesC", group=grpSes)
sesDOn    = input.bool(true, "", inline="sesD", group=grpSes)
sesDName  = input.string("Sydney", "", inline="sesD", group=grpSes)
sesDTime  = input.session("2100-0600", "", inline="sesD", group=grpSes)
sesDColor = input.color(#f59e0b, "", inline="sesD", group=grpSes)
sesTzSel  = input.string("UTC+0", "Session time zone", ["Exchange", "UTC-12", "UTC-11", "UTC-10", "UTC-9", "UTC-8", "UTC-7", "UTC-6", "UTC-5", "UTC-4", "UTC-3", "UTC-2", "UTC-1", "UTC+0", "UTC+1", "UTC+2", "UTC+3", "UTC+4", "UTC+5", "UTC+6", "UTC+7", "UTC+8", "UTC+9", "UTC+10", "UTC+11", "UTC+12", "UTC+13", "UTC+14"], group=grpSes, tooltip="Time zone used to read the session hours above.\nExchange = the symbol's exchange time zone.\nUTC+x = a fixed UTC offset.")
 
sesFillTransp    = input.int(100, "Box fill transparency", 0, 100, group=grpSes, tooltip="0 = solid fill, 100 = no fill (borders only).")
sesBorderW       = input.int(1, "Border width", 1, 4, group=grpSes)
sesBorderStyleIn = input.string("Dotted", "Border style", ["Dotted", "Dashed", "Solid"], group=grpSes)
sesBorderTransp  = input.int(0, "Border transparency", 0, 100, group=grpSes, tooltip="0 = solid border, 100 = invisible border.")
sesBorderStyle   = sesBorderStyleIn == "Solid" ? line.style_solid : sesBorderStyleIn == "Dashed" ? line.style_dashed : line.style_dotted
sesHistory       = input.int(40, "Session boxes kept on chart", 20, 200, group=grpSes, tooltip="How many past session boxes (and name labels) stay on the chart. Older ones are removed.")
sesLabel         = input.bool(true, "Show session name labels", group=grpSes)
 
// ---- 02 · SESSIONS DASHBOARD ----
showDash   = input.bool(true, "Show sessions dashboard", group=grpDash)
dbTheme    = input.string("Clean White Premium", "Theme", ["Clean White Premium", "Black Gold Noir", "Crimson Executive"], group=grpDash)
dbPosition = input.string("Top right", "Position", ["Top right", "Top left", "Middle right", "Middle left", "Bottom right", "Bottom left"], group=grpDash)
dbSizeSel  = input.string("Small", "Text size", ["Tiny", "Small", "Normal"], group=grpDash)
dashTzSel  = input.string("India (IST)", "Display time zone", ["Session time zone", "India (IST)", "UTC", "New York", "London", "Dubai", "Singapore", "Tokyo", "Sydney"], group=grpDash, tooltip="Time zone used to show the session start / end times and the clock in the dashboard.\nThe session hours themselves are still read in the Session time zone from section 01.")
dbSeconds  = input.bool(true, "Countdown seconds", group=grpDash, tooltip="Seconds update on every price tick (not a real clock).")
dbShowSum  = input.bool(true, "Show summary (open count, next session, overlap)", group=grpDash)
 
// ============================================================================
// THEME   (th: 2 = Clean White Premium, 1 = Black Gold Noir, 0 = Crimson Executive)
// ============================================================================
int   th      = dbTheme == "Clean White Premium" ? 2 : dbTheme == "Black Gold Noir" ? 1 : 0
color tBg     = th == 2 ? #FFFFFF : th == 1 ? #0A0A0A : #0B0506
color tHead   = th == 2 ? #0F172A : th == 1 ? #16120E : #C1121F
color tHeadTx = th == 2 ? #FFFFFF : th == 1 ? #D9B76E : #FFFFFF
color tSec    = th == 2 ? #F1F5F9 : th == 1 ? #14110F : #5A0A12
color tSecTx  = th == 2 ? #475569 : th == 1 ? #D9B76E : #FFB3BD
color tLbl    = th == 2 ? #64748B : th == 1 ? #B8B8B8 : #E8D5D8
color tVal    = th == 2 ? #0F172A : #F2E3B3
color tFrame  = th == 2 ? #CBD5E1 : th == 1 ? #D9B76E : #7A0A14
color tBorder = th == 2 ? #E2E8F0 : th == 1 ? #1F1F1F : #1A0A0C
color colUp   = th == 2 ? #16A34A : #12B76A
color colDn   = th == 2 ? #DC2626 : #F04438
color colWarn = th == 2 ? #D97706 : #F79009
color colOff  = th == 2 ? #94A3B8 : #4B5563
 
// ============================================================================
// SESSION BOXES (range box on chart)
// ============================================================================
sesTz = sesTzSel == "Exchange" ? syminfo.timezone : sesTzSel
 
bool sesAActive = sesMaster and sesAOn and not na(time(timeframe.period, sesATime, sesTz))
bool sesBActive = sesMaster and sesBOn and not na(time(timeframe.period, sesBTime, sesTz))
bool sesCActive = sesMaster and sesCOn and not na(time(timeframe.period, sesCTime, sesTz))
bool sesDActive = sesMaster and sesDOn and not na(time(timeframe.period, sesDTime, sesTz))
 
var array<box>   sesBoxList   = array.new<box>()
var array<label> sesLabelList = array.new<label>()
 
// Draws and grows one box (high-to-low range) while the session is active
f_sessionBox(bool active, string nm, color col) =>
    var int   startT = 0
    var float sHigh  = na
    var float sLow   = na
    var box   bx     = na
    var label lb     = na
    bool started = active and not active[1]
    if started
        startT := time
        sHigh  := high
        sLow   := low
        bx := box.new(bar_index, sHigh, bar_index, sLow, bgcolor = color.new(col, sesFillTransp), border_color = color.new(col, sesBorderTransp), border_width = sesBorderW, border_style = sesBorderStyle)
        if sesLabel
            lb := label.new(startT, sHigh, nm, xloc = xloc.bar_time, textcolor = col, style = label.style_label_down, color = #00000000, size = size.tiny)
        sesBoxList.push(bx)
        if sesLabel
            sesLabelList.push(lb)
        if sesBoxList.size() > sesHistory
            box.delete(sesBoxList.shift())
        if sesLabelList.size() > sesHistory
            label.delete(sesLabelList.shift())
    else if active
        sHigh := math.max(high, sHigh)
        sLow  := math.min(low, sLow)
        if not na(bx)
            bx.set_top(sHigh)
            bx.set_rightbottom(bar_index, sLow)
        if sesLabel and not na(lb)
            lb.set_xy(int(math.avg(startT, time)), sHigh)
    active
 
f_sessionBox(sesAActive, sesAName, sesAColor)
f_sessionBox(sesBActive, sesBName, sesBColor)
f_sessionBox(sesCActive, sesCName, sesCColor)
f_sessionBox(sesDActive, sesDName, sesDColor)
 
// ============================================================================
// SESSIONS DASHBOARD
// ============================================================================
string dispTzId = switch dashTzSel
    "India (IST)" => "Asia/Kolkata"
    "UTC"         => "UTC"
    "New York"    => "America/New_York"
    "London"      => "Europe/London"
    "Dubai"       => "Asia/Dubai"
    "Singapore"   => "Asia/Singapore"
    "Tokyo"       => "Asia/Tokyo"
    "Sydney"      => "Australia/Sydney"
    => sesTz
 
string dispTzShort = switch dashTzSel
    "India (IST)" => "IST"
    "UTC"         => "UTC"
    "New York"    => "NY"
    "London"      => "LON"
    "Dubai"       => "DXB"
    "Singapore"   => "SGT"
    "Tokyo"       => "JST"
    "Sydney"      => "SYD"
    => sesTzSel == "Exchange" ? "EXCH" : sesTzSel
 
// Session text "HHMM-HHMM" -> minutes from midnight
f_startMin(string s) => nz(int(str.tonumber(str.substring(s, 0, 2)))) * 60 + nz(int(str.tonumber(str.substring(s, 2, 4))))
f_endMin(string s)   => nz(int(str.tonumber(str.substring(s, 5, 7)))) * 60 + nz(int(str.tonumber(str.substring(s, 7, 9))))
 
// Is the current minute of the day inside the session? (handles sessions that cross midnight)
f_isOpen(int cur, int s, int e) => s < e ? (cur >= s and cur < e) : s > e ? (cur >= s or cur < e) : true
 
// Seconds from now until a given minute-of-day (always 0 .. 86399)
f_secsTo(int curSec, int targetMin) => ((targetMin * 60 - curSec) % 86400 + 86400) % 86400
 
// Minutes -> "HH:MM" (wraps around midnight)
f_hm(int m) =>
    int mm = ((m % 1440) + 1440) % 1440
    str.tostring(math.floor(mm / 60), "00") + ":" + str.tostring(mm % 60, "00")
 
// Minutes -> "9h 00m"
f_dur(int m) =>
    int d = m <= 0 ? 1440 : m
    str.tostring(math.floor(d / 60)) + "h " + str.tostring(d % 60, "00") + "m"
 
// Seconds -> "2h 15m 30s"
f_cd(int t, bool withSec) =>
    int h = int(math.floor(t / 3600))
    int m = int(math.floor((t % 3600) / 60))
    int s = t % 60
    string out = h > 0 ? str.tostring(h) + "h " + str.tostring(m, "00") + "m" : str.tostring(m) + "m"
    withSec ? out + " " + str.tostring(s, "00") + "s" : out
 
var dash_loc  = dbPosition == "Top right" ? position.top_right : dbPosition == "Top left" ? position.top_left : dbPosition == "Middle right" ? position.middle_right : dbPosition == "Middle left" ? position.middle_left : dbPosition == "Bottom right" ? position.bottom_right : position.bottom_left
var dash_size = dbSizeSel == "Small" ? size.small : dbSizeSel == "Normal" ? size.normal : size.tiny
 
// 6 columns: Dot | Session | Time | Length | Status | Countdown   (13 rows)
var dash      = table.new(dash_loc, 6, 13, tBg, tFrame, 1, tBorder, 1)
 
// Full-width row (merged across all 6 columns)
dFull(int r, string txt, color tx, color bg, string al) =>
    for c = 0 to 5
        table.cell(dash, c, r, c == 0 ? txt : "", 0, 0, tx, al == "left" ? text.align_left : text.align_center, text.align_center, dash_size, bg, text_formatting = text.format_bold)
    table.merge_cells(dash, 0, r, 5, r)
 
// Normal cell. Column 0 = dot (centered), column 1 = name (left), others centered
dCell(int c, int r, string txt, color tx, color bg, bool bold) =>
    string t = c == 0 ? txt : " " + txt + " "
    table.cell(dash, c, r, t, 0, 0, tx, c == 1 ? text.align_left : text.align_center, text.align_center, dash_size, bg, text_formatting = bold ? text.format_bold : text.format_none)
 
// Summary row: label (cols 0-1) + value (cols 2-5)
dSum(int r, string lbl, string val, color vc) =>
    dCell(0, r, "", tLbl, tBg, false)
    dCell(1, r, lbl, tLbl, tBg, false)
    dCell(2, r, val, vc, tBg, true)
    table.merge_cells(dash, 2, r, 5, r)
 
if barstate.islast and showDash
    // ---- current time ----
    int curSes    = hour(timenow, sesTz) * 60 + minute(timenow, sesTz)
    int curSesSec = curSes * 60 + second(timenow, sesTz)
    int curDisp   = hour(timenow, dispTzId) * 60 + minute(timenow, dispTzId)
    // shift (minutes) to convert a time read in the session time zone into the display time zone
    int shift     = ((curDisp - curSes + 1440 + 720) % 1440) - 720
    string clockTxt = str.format_time(timenow, "EEE dd MMM yyyy  ·  HH:mm:ss", dispTzId) + " " + dispTzShort
 
    // ---- session data ----
    array<bool>   ons  = array.from(sesAOn, sesBOn, sesCOn, sesDOn)
    array<string> nms  = array.from(sesAName, sesBName, sesCName, sesDName)
    array<string> tms  = array.from(sesATime, sesBTime, sesCTime, sesDTime)
    array<color>  cols = array.from(sesAColor, sesBColor, sesCColor, sesDColor)
 
    // header + clock
    dFull(0, "SESSIONS DASHBOARD", tHeadTx, tHead, "center")
    dFull(1, clockTxt, tLbl, tSec, "center")
 
    // column titles
    dCell(0, 2, "", tSecTx, tSec, true)
    dCell(1, 2, "SESSION", tSecTx, tSec, true)
    dCell(2, 2, "TIME (" + dispTzShort + ")", tSecTx, tSec, true)
    dCell(3, 2, "LENGTH", tSecTx, tSec, true)
    dCell(4, 2, "STATUS", tSecTx, tSec, true)
    dCell(5, 2, "COUNTDOWN", tSecTx, tSec, true)
 
    int    openCnt   = 0
    int    enabCnt   = 0
    string openNames = ""
    string nextName  = ""
    int    nextSecs  = na
 
    for i = 0 to 3
        bool   on  = ons.get(i)
        string nm  = nms.get(i)
        string tm  = tms.get(i)
        color  cl  = cols.get(i)
        int    s   = f_startMin(tm)
        int    e   = f_endMin(tm)
        bool   op  = f_isOpen(curSes, s, e)
        int    row = 3 + i
 
        dCell(0, row, "●", on ? cl : colOff, tBg, true)
        dCell(1, row, nm, on ? tVal : colOff, tBg, true)
        dCell(2, row, f_hm(s + shift) + " - " + f_hm(e + shift), on ? tVal : colOff, tBg, false)
        dCell(3, row, f_dur(((e - s) % 1440 + 1440) % 1440), on ? tLbl : colOff, tBg, false)
 
        if not on
            dCell(4, row, "OFF", color.white, colOff, true)
            dCell(5, row, "-", colOff, tBg, false)
        else
            enabCnt += 1
            if op
                openCnt += 1
                openNames := openNames == "" ? nm : openNames + " + " + nm
                dCell(4, row, "OPEN", color.white, colUp, true)
                dCell(5, row, "Closes in " + f_cd(f_secsTo(curSesSec, e), dbSeconds), tVal, tBg, false)
            else
                int toOpen = f_secsTo(curSesSec, s)
                dCell(4, row, "CLOSED", color.white, colDn, true)
                dCell(5, row, "Opens in " + f_cd(toOpen, dbSeconds), tLbl, tBg, false)
                if na(nextSecs) or toOpen < nextSecs
                    nextSecs := toOpen
                    nextName := nm
 
    // summary
    int endRow = 7
    if dbShowSum
        dFull(7, "SUMMARY", tSecTx, tSec, "left")
        dSum(8,  "Total sessions", str.tostring(enabCnt) + " enabled / 4", tVal)
        dSum(9,  "Open now",       openCnt == 0 ? "None (off session)" : str.tostring(openCnt) + " · " + openNames, openCnt == 0 ? colWarn : colUp)
        dSum(10, "Next to open",   na(nextSecs) ? "-" : nextName + " in " + f_cd(nextSecs, dbSeconds), tVal)
        dSum(11, "Overlap",        openCnt >= 2 ? "YES · " + openNames : "No overlap", openCnt >= 2 ? colWarn : tLbl)
        endRow := 12
 
    // footer status bar
    dFull(endRow, openCnt == 0 ? "NO SESSION OPEN" : openCnt >= 2 ? str.tostring(openCnt) + " SESSIONS OVERLAPPING" : openNames + " SESSION ACTIVE", color.white, openCnt == 0 ? colOff : openCnt >= 2 ? colWarn : colUp, "center")

← 返回列表