Session Highs & Lows [Defined Edge]

DefinedEdge · study · 206 行 · 点赞 37 · TradingView 原页

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

Pine Script

//@version=6
indicator("Session Highs & Lows", "Session H/L", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ============================================================
//  Marks the high and low of the Asia, London and New York
//  trading sessions, each in its own colour. The most recent
//  session of each type is drawn bright and projected to the
//  current bar, with its price labels on the RIGHT. Every
//  earlier session is drawn in a faded shade of the same
//  colour, with its price labels on the LEFT, at the bar where
//  that session began. Built for intraday timeframes.
// ============================================================
 
// ---------- Appearance ----------
gS = "Appearance"
wLatest      = input.int(2, "Latest line width",     minval = 1, maxval = 5, group = gS)
wHist        = input.int(1, "Historical line width", minval = 1, maxval = 5, group = gS)
styleStr     = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = gS)
projectRight = input.bool(true,  "Project latest levels to current bar", group = gS)
showLbl      = input.bool(true,  "Price labels on latest levels (right)",    group = gS)
lblHist      = input.bool(true,  "Price labels on historical levels (left)", group = gS)
labelMode    = input.string("Price", "Label text", options = ["Price", "Tag + price"], group = gS)
keepN        = input.int(10, "Historical sessions kept (per session)", minval = 0, maxval = 60, group = gS)
shadeBg      = input.bool(false, "Shade active session background", group = gS)
 
// ---------- Info box ----------
gI = "Info box"
showInfo = input.bool(true,          "Show info box", group = gI)
infoPosS = input.string("Top right", "Position",  options = ["Top right", "Top left", "Bottom right", "Bottom left", "Middle right"], group = gI)
infoSzS  = input.string("Small",     "Text size", options = ["Tiny", "Small", "Normal"], group = gI)
 
// ---------- Asia ----------
gA = "Asia session"
aOn   = input.bool(true,           "Enable",   group = gA, inline = "a1")
aSes  = input.session("0900-1800", "Hours",    group = gA, inline = "a1")
aTz   = input.string("Asia/Tokyo", "Timezone", group = gA)
aTag  = input.string("ASIA",       "Tag",      group = gA)
aColL = input.color(#f5a623,                "Latest / historical", group = gA, inline = "a2")
aColH = input.color(color.new(#f5a623, 55), "",                    group = gA, inline = "a2")
aBg   = input.color(color.new(#f5a623, 90), "Shade", group = gA)
 
// ---------- London ----------
gL = "London session"
lOn   = input.bool(true,              "Enable",   group = gL, inline = "l1")
lSes  = input.session("0800-1630",    "Hours",    group = gL, inline = "l1")
lTz   = input.string("Europe/London", "Timezone", group = gL)
lTag  = input.string("LON",           "Tag",      group = gL)
lColL = input.color(#2962ff,                "Latest / historical", group = gL, inline = "l2")
lColH = input.color(color.new(#2962ff, 55), "",                    group = gL, inline = "l2")
lBg   = input.color(color.new(#2962ff, 90), "Shade", group = gL)
 
// ---------- New York ----------
gN = "New York session"
nOn   = input.bool(true,                 "Enable",   group = gN, inline = "n1")
nSes  = input.session("0800-1700",       "Hours",    group = gN, inline = "n1")
nTz   = input.string("America/New_York", "Timezone", group = gN)
nTag  = input.string("NY",               "Tag",      group = gN)
nColL = input.color(#00c853,                "Latest / historical", group = gN, inline = "n2")
nColH = input.color(color.new(#00c853, 55), "",                    group = gN, inline = "n2")
nBg   = input.color(color.new(#00c853, 90), "Shade", group = gN)
 
lnStyle  = styleStr == "Dashed" ? line.style_dashed : styleStr == "Dotted" ? line.style_dotted : line.style_solid
intraday = not timeframe.isdwm
 
infoPos = infoPosS == "Top left" ? position.top_left : infoPosS == "Bottom right" ? position.bottom_right : infoPosS == "Bottom left" ? position.bottom_left : infoPosS == "Middle right" ? position.middle_right : position.top_right
infoSz  = infoSzS == "Tiny" ? size.tiny : infoSzS == "Normal" ? size.normal : size.small
 
// Label text: the price value, optionally prefixed with the session tag.
f_txt(string tag, float px) =>
    labelMode == "Tag + price" ? tag + " " + str.tostring(px, format.mintick) : str.tostring(px, format.mintick)
 
// ---------- Per-session state ----------
type Sess
    float hi     = na
    float lo     = na
    int   xStart = na   // bar where this session opened (left anchor)
    int   xEnd   = na   // last bar of this session (right anchor)
    line  hiLn   = na
    line  loLn   = na
    label hiLb   = na   // live right-side price label
    label loLb   = na
    color cL     = na   // latest colour
    color cH     = na   // historical colour
    array<line>  pLn
    array<label> pLb
 
var Sess sa = Sess.new(cL = aColL, cH = aColH, pLn = array.new<line>(), pLb = array.new<label>())
var Sess sl = Sess.new(cL = lColL, cH = lColH, pLn = array.new<line>(), pLb = array.new<label>())
var Sess sn = Sess.new(cL = nColL, cH = nColH, pLn = array.new<line>(), pLb = array.new<label>())
 
// ---------- Session detection (evaluated every bar) ----------
f_in(simple string ses, simple string tz) => not na(time(timeframe.period, ses, tz))
aIn = f_in(aSes, aTz)
lIn = f_in(lSes, lTz)
nIn = f_in(nSes, nTz)
 
aStart = aIn and not aIn[1]
lStart = lIn and not lIn[1]
nStart = nIn and not nIn[1]
 
// ---------- Helpers ----------
mkRight(float y, string tag, color c) =>
    showLbl ? label.new(bar_index, y, f_txt(tag, y), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c, size = size.small) : na
 
// Fade the current latest session to its historical colour, drop its right labels,
// and stamp left-side price labels at the bar where the session began.
demote(Sess s, string tag) =>
    if not na(s.hiLn)
        ex = na(s.xEnd) ? line.get_x2(s.hiLn) : s.xEnd
        line.set_color(s.hiLn, s.cH), line.set_color(s.loLn, s.cH)
        line.set_width(s.hiLn, wHist), line.set_width(s.loLn, wHist)
        line.set_x2(s.hiLn, ex),       line.set_x2(s.loLn, ex)
        if not na(s.hiLb)
            label.delete(s.hiLb)
        if not na(s.loLb)
            label.delete(s.loLb)
        array.push(s.pLn, s.hiLn), array.push(s.pLn, s.loLn)
        if lblHist
            lh = label.new(s.xStart, s.hi, f_txt(tag, s.hi), xloc = xloc.bar_index, style = label.style_label_right, color = color.new(color.black, 100), textcolor = s.cH, size = size.small)
            ll = label.new(s.xStart, s.lo, f_txt(tag, s.lo), xloc = xloc.bar_index, style = label.style_label_right, color = color.new(color.black, 100), textcolor = s.cH, size = size.small)
            array.push(s.pLb, lh), array.push(s.pLb, ll)
        while array.size(s.pLn) > keepN * 2
            line.delete(array.shift(s.pLn))
        while array.size(s.pLb) > keepN * 2
            label.delete(array.shift(s.pLb))
    s.hiLb := na
    s.loLb := na
 
// Fill one info-box row with a session's live stats. "●" dot when currently open.
infoRow(table t, int r, bool en, bool isIn, string tag, float hi, float lo, color cL, color cH) =>
    txtCol  = chart.fg_color
    nameCol = not en ? color.new(cH, 65) : isIn ? cL : cH
    nameTxt = (en and isIn ? "● " : "") + tag
    rng     = hi - lo
    pct     = not na(lo) and lo != 0 ? rng / lo * 100 : na
    hiTxt   = en and not na(hi)  ? str.tostring(hi,  format.mintick) : "-"
    loTxt   = en and not na(lo)  ? str.tostring(lo,  format.mintick) : "-"
    rgTxt   = en and not na(rng) ? str.tostring(rng, format.mintick) : "-"
    pcTxt   = en and not na(pct) ? str.tostring(pct, "#.##") + "%"   : "-"
    table.cell(t, 0, r, nameTxt, text_color = nameCol, text_size = infoSz, text_halign = text.align_left)
    table.cell(t, 1, r, hiTxt,   text_color = txtCol,  text_size = infoSz, text_halign = text.align_right)
    table.cell(t, 2, r, loTxt,   text_color = txtCol,  text_size = infoSz, text_halign = text.align_right)
    table.cell(t, 3, r, rgTxt,   text_color = txtCol,  text_size = infoSz, text_halign = text.align_right)
    table.cell(t, 4, r, pcTxt,   text_color = txtCol,  text_size = infoSz, text_halign = text.align_right)
 
handle(Sess s, bool en, bool isIn, bool isStart, string tag) =>
    if en and intraday
        if isStart
            demote(s, tag)
            s.hi := high, s.lo := low
            s.xStart := bar_index
            s.xEnd := bar_index
            s.hiLn := line.new(bar_index, high, bar_index, high, xloc = xloc.bar_index, color = s.cL, width = wLatest, style = lnStyle)
            s.loLn := line.new(bar_index, low,  bar_index, low,  xloc = xloc.bar_index, color = s.cL, width = wLatest, style = lnStyle)
            s.hiLb := mkRight(high, tag, s.cL)
            s.loLb := mkRight(low,  tag, s.cL)
        else if isIn and not na(s.hiLn)
            if high > s.hi
                s.hi := high
                line.set_y1(s.hiLn, high), line.set_y2(s.hiLn, high)
                if not na(s.hiLb)
                    label.set_y(s.hiLb, high), label.set_text(s.hiLb, f_txt(tag, high))
            if low < s.lo
                s.lo := low
                line.set_y1(s.loLn, low), line.set_y2(s.loLn, low)
                if not na(s.loLb)
                    label.set_y(s.loLb, low), label.set_text(s.loLb, f_txt(tag, low))
        if not na(s.hiLn)
            if isIn
                s.xEnd := bar_index
                line.set_x2(s.hiLn, bar_index), line.set_x2(s.loLn, bar_index)
                if not na(s.hiLb)
                    label.set_x(s.hiLb, bar_index), label.set_x(s.loLb, bar_index)
            else if projectRight
                line.set_x2(s.hiLn, bar_index), line.set_x2(s.loLn, bar_index)
                if not na(s.hiLb)
                    label.set_x(s.hiLb, bar_index), label.set_x(s.loLb, bar_index)
 
handle(sa, aOn, aIn, aStart, aTag)
handle(sl, lOn, lIn, lStart, lTag)
handle(sn, nOn, nIn, nStart, nTag)
 
// ---------- Session background shading (optional) ----------
bgcolor(shadeBg and intraday and aOn and aIn ? aBg : na)
bgcolor(shadeBg and intraday and lOn and lIn ? lBg : na)
bgcolor(shadeBg and intraday and nOn and nIn ? nBg : na)
 
// ---------- Info box ----------
var table info = table.new(infoPos, 5, 4, frame_width = 1, frame_color = color.new(color.gray, 55), border_width = 1, border_color = color.new(color.gray, 80))
if showInfo and intraday and barstate.islast
    hBg = color.new(color.gray, 78)
    hFg = chart.fg_color
    table.cell(info, 0, 0, "Session", text_color = hFg, text_size = infoSz, text_halign = text.align_left,  bgcolor = hBg)
    table.cell(info, 1, 0, "High",    text_color = hFg, text_size = infoSz, text_halign = text.align_right, bgcolor = hBg)
    table.cell(info, 2, 0, "Low",     text_color = hFg, text_size = infoSz, text_halign = text.align_right, bgcolor = hBg)
    table.cell(info, 3, 0, "Range",   text_color = hFg, text_size = infoSz, text_halign = text.align_right, bgcolor = hBg)
    table.cell(info, 4, 0, "Range %", text_color = hFg, text_size = infoSz, text_halign = text.align_right, bgcolor = hBg)
    infoRow(info, 1, aOn, aIn, aTag, sa.hi, sa.lo, aColL, aColH)
    infoRow(info, 2, lOn, lIn, lTag, sl.hi, sl.lo, lColL, lColH)
    infoRow(info, 3, nOn, nIn, nTag, sn.hi, sn.lo, nColL, nColH)
 
// ---------- Timeframe reminder ----------
var label warn = na
if not intraday and barstate.islast
    label.delete(warn)
    warn := label.new(bar_index, high, "Session Highs & Lows needs an intraday timeframe", xloc = xloc.bar_index, style = label.style_label_down, color = color.new(color.red, 15), textcolor = color.white, size = size.normal)

← 返回列表