Sessions & Liquidity | TheSilentEdgeFx

arianzamanii · study · 710 行 · 点赞 227 · TradingView 原页

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

Pine Script

//@version=6
// ════════════════════════════════════════════════════════════════════════════
//  Sessions & Liquidity  |  TheSilentEdgeFx
//  Design and development: FarhanFx
//
//  این اندیکاتور توسط FarhanFx تریدر تیم طراحی و توسعه داده شده و به صورت رایگان در اختیار عموم قرار گرفته.
//  در صورت هرگونه پیشنهاد / مشکل با ما جهت ارتقا کیفیت کار در ارتباط باشید  ➜  Telegram: @TheSilentEdgeFx
//
//  • Custom sessions, each in its own time zone (DST handled by TradingView)
//  • Session / Previous Day / Week / Month highs & lows as liquidity
//  • Untouched levels run to the current bar; a level stops on the candle that takes it
//  • Every line is labelled; labels never overlap
//  • Per-element timeframe filters, info panel, watermark
// ════════════════════════════════════════════════════════════════════════════
indicator("Sessions & Liquidity | TheSilentEdgeFx", "S&L | SilentEdge", overlay = true,
     max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
 
// ───────────────────────────── groups
const string GG = "General"
const string GS = "Sessions  (on · name · time · time zone · color)"
const string GL = "Liquidity levels"
const string GT = "Show on timeframes  (from → to)"
const string GP = "Panel"
const string GW = "Watermark & brand"
 
// ───────────────────────────── general
lookback     = input.int(3, "Days of session boxes", 1, 20, group = GG)
lvLookback   = input.int(10, "Days to search for untouched session liquidity", 1, 60, group = GG,
     tooltip = "Session highs/lows from this many days back are tracked; the ones price has not taken yet stay on the chart.")
boxTransp    = input.int(85, "Box transparency", 0, 100, group = GG)
extendActive = input.bool(true, "Draw running session to its planned end", group = GG)
showNames    = input.bool(true, "Session names on boxes", inline = "nm", group = GG)
nameSizeS    = input.string("Small", "", ["Tiny", "Small", "Normal", "Large"], inline = "nm", group = GG)
fontS        = input.string("Default", "Font", ["Default", "Monospace"], group = GG)
showDays     = input.bool(true, "Day separators", inline = "ds", group = GG)
sepColor     = input.color(color.new(color.gray, 50), "", inline = "ds", group = GG)
 
// ───────────────────────────── sessions
s1on = input.bool(true, "", inline = "1", group = GS)
s1nm = input.string("Tokyo AM", "", inline = "1", group = GS)
s1ss = input.session("0900-1130", "", inline = "1", group = GS)
s1tz = input.string("Tokyo", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "1", group = GS)
s1c  = input.color(#285ADC, "", inline = "1", group = GS)
 
s2on = input.bool(true, "", inline = "2", group = GS)
s2nm = input.string("SGE CHINA AM", "", inline = "2", group = GS)
s2ss = input.session("0900-1130", "", inline = "2", group = GS)
s2tz = input.string("Shanghai", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "2", group = GS)
s2c  = input.color(#DC283C, "", inline = "2", group = GS)
 
s3on = input.bool(true, "", inline = "3", group = GS)
s3nm = input.string("SGE CHINA PM", "", inline = "3", group = GS)
s3ss = input.session("1330-1530", "", inline = "3", group = GS)
s3tz = input.string("Shanghai", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "3", group = GS)
s3c  = input.color(#DC283C, "", inline = "3", group = GS)
 
s4on = input.bool(true, "", inline = "4", group = GS)
s4nm = input.string("London Open", "", inline = "4", group = GS)
s4ss = input.session("0200-0500", "", inline = "4", group = GS)
s4tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "4", group = GS)
s4c  = input.color(#E1506E, "", inline = "4", group = GS)
 
s5on = input.bool(true, "", inline = "5", group = GS)
s5nm = input.string("NY Active Pre-Market", "", inline = "5", group = GS)
s5ss = input.session("0800-0930", "", inline = "5", group = GS)
s5tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "5", group = GS)
s5c  = input.color(#E6B900, "", inline = "5", group = GS)
 
s6on = input.bool(true, "", inline = "6", group = GS)
s6nm = input.string("NY AM", "", inline = "6", group = GS)
s6ss = input.session("0930-1100", "", inline = "6", group = GS)
s6tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "6", group = GS)
s6c  = input.color(#F08214, "", inline = "6", group = GS)
 
s7on = input.bool(true, "", inline = "7", group = GS)
s7nm = input.string("NY Late Morning", "", inline = "7", group = GS)
s7ss = input.session("1100-1200", "", inline = "7", group = GS)
s7tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "7", group = GS)
s7c  = input.color(#A03CC8, "", inline = "7", group = GS)
 
s8on = input.bool(true, "", inline = "8", group = GS)
s8nm = input.string("NY Lunch", "", inline = "8", group = GS)
s8ss = input.session("1200-1300", "", inline = "8", group = GS)
s8tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "8", group = GS)
s8c  = input.color(#6E6E6E, "", inline = "8", group = GS)
 
s9on = input.bool(true, "", inline = "9", group = GS)
s9nm = input.string("NY Early Afternoon", "", inline = "9", group = GS)
s9ss = input.session("1300-1400", "", inline = "9", group = GS)
s9tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "9", group = GS)
s9c  = input.color(#5A82B4, "", inline = "9", group = GS)
 
s10on = input.bool(true, "", inline = "10", group = GS)
s10nm = input.string("NY PM", "", inline = "10", group = GS)
s10ss = input.session("1330-1600", "", inline = "10", group = GS)
s10tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "10", group = GS)
s10c  = input.color(#3C8CE6, "", inline = "10", group = GS)
 
s11on = input.bool(true, "", inline = "11", group = GS)
s11nm = input.string("NY Power Hour", "", inline = "11", group = GS)
s11ss = input.session("1500-1600", "", inline = "11", group = GS)
s11tz = input.string("New York", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "11", group = GS)
s11c  = input.color(#E63C8C, "", inline = "11", group = GS)
 
s12on = input.bool(false, "", inline = "12", group = GS)
s12nm = input.string("Custom", "", inline = "12", group = GS)
s12ss = input.session("0000-0100", "", inline = "12", group = GS)
s12tz = input.string("UTC", "", ["New York", "London", "Frankfurt", "Tokyo", "Shanghai", "Tehran", "UTC", "Exchange"], inline = "12", group = GS)
s12c  = input.color(#009682, "", inline = "12", group = GS)
 
// ───────────────────────────── liquidity
showSessLv  = input.bool(true, "Session high / low", inline = "sl", group = GL)
sessStyleS  = input.string("Solid", "", ["Solid", "Dashed", "Dotted"], inline = "sl", group = GL)
sessW       = input.int(1, "", 1, 4, inline = "sl", group = GL)
 
showPD   = input.bool(true, "Previous Day", inline = "pd", group = GL)
pdCount  = input.int(1, "", 1, 5, inline = "pd", group = GL)
pdColor  = input.color(#006EDC, "", inline = "pd", group = GL)
pdStyleS = input.string("Solid", "", ["Solid", "Dashed", "Dotted"], inline = "pd", group = GL)
pdW      = input.int(1, "", 1, 4, inline = "pd", group = GL)
 
showPW   = input.bool(true, "Previous Week", inline = "pw", group = GL)
pwCount  = input.int(1, "", 1, 5, inline = "pw", group = GL)
pwColor  = input.color(#D27800, "", inline = "pw", group = GL)
pwStyleS = input.string("Solid", "", ["Solid", "Dashed", "Dotted"], inline = "pw", group = GL)
pwW      = input.int(1, "", 1, 4, inline = "pw", group = GL)
 
showPM   = input.bool(true, "Previous Month", inline = "pm", group = GL)
pmCount  = input.int(1, "", 1, 5, inline = "pm", group = GL)
pmColor  = input.color(#9628AA, "", inline = "pm", group = GL)
pmStyleS = input.string("Solid", "", ["Solid", "Dashed", "Dotted"], inline = "pm", group = GL)
pmW      = input.int(1, "", 1, 4, inline = "pm", group = GL)
 
showSwept    = input.bool(true, "Swept levels: stop at the touch, style", inline = "sw", group = GL)
sweptStyleS  = input.string("Dashed", "", ["Solid", "Dashed", "Dotted"], inline = "sw", group = GL)
sweptLabels  = input.bool(true, "Short tag on swept levels", group = GL)
showLabels   = input.bool(true, "Level labels", inline = "lb", group = GL)
lblSizeS     = input.string("Small", "", ["Tiny", "Small", "Normal", "Large"], inline = "lb", group = GL)
lblSpacing   = input.float(0.35, "Label spacing (× ATR)", 0.05, 3.0, 0.05, group = GL, tooltip = "Minimum vertical gap between labels of nearby levels, so they never overlap.")
touchTicks   = input.int(0, "Touch tolerance (ticks)", 0, group = GL)
keepOpen     = input.bool(true, "Keep untouched session levels until price takes them", group = GL,
     tooltip = "On: an untouched session high/low stays on the chart (running to the current candle) even after it is older than 'Days of sessions to draw'. Swept levels are removed once they are older than that.")
 
// ───────────────────────────── timeframe filters
tBoxF = input.timeframe("1",   "Session boxes",  inline = "t1", group = GT)
tBoxT = input.timeframe("60",  "→",              inline = "t1", group = GT)
tNamF = input.timeframe("1",   "Session names",  inline = "t2", group = GT)
tNamT = input.timeframe("60",  "→",              inline = "t2", group = GT)
tSesF = input.timeframe("1",   "Session H/L",    inline = "t3", group = GT)
tSesT = input.timeframe("240", "→",              inline = "t3", group = GT)
tPDF  = input.timeframe("1",   "Previous Day",   inline = "t4", group = GT)
tPDT  = input.timeframe("D",   "→",              inline = "t4", group = GT)
tPWF  = input.timeframe("1",   "Previous Week",  inline = "t5", group = GT)
tPWT  = input.timeframe("W",   "→",              inline = "t5", group = GT)
tPMF  = input.timeframe("1",   "Previous Month", inline = "t6", group = GT)
tPMT  = input.timeframe("M",   "→",              inline = "t6", group = GT)
tLblF = input.timeframe("1",   "Level labels",   inline = "t7", group = GT)
tLblT = input.timeframe("M",   "→",              inline = "t7", group = GT)
tDayF = input.timeframe("1",   "Day separators", inline = "t8", group = GT)
tDayT = input.timeframe("240", "→",              inline = "t8", group = GT)
tWmF  = input.timeframe("1",   "Watermark",      inline = "t9", group = GT)
tWmT  = input.timeframe("12M", "→",              inline = "t9", group = GT)
 
// ───────────────────────────── panel
showPanel  = input.bool(true, "Show panel", group = GP)
panelPosS  = input.string("Top Right", "Position", ["Top Right", "Top Center", "Top Left", "Middle Right", "Middle Left", "Bottom Right", "Bottom Center", "Bottom Left"], group = GP)
panelSizeS = input.string("Small", "Text size", ["Tiny", "Small", "Normal", "Large"], group = GP)
compact    = input.bool(false, "Compact (sessions only)", group = GP)
themeS     = input.string("Auto", "Theme", ["Auto", "Light", "Dark"], group = GP)
 
// ───────────────────────────── watermark & brand
showWM  = input.bool(true, "Watermark", inline = "w0", group = GW)
wmPosS  = input.string("Top Center", "", ["Top Right", "Top Center", "Top Left", "Middle Right", "Middle Center", "Middle Left", "Bottom Right", "Bottom Center", "Bottom Left"], inline = "w0", group = GW)
wm1     = input.string("Farhan", "Line 1", inline = "w1", group = GW)
wm1c    = input.color(#2878F0, "", inline = "w1", group = GW)
wm1s    = input.string("Large", "", ["Tiny", "Small", "Normal", "Large", "Huge"], inline = "w1", group = GW)
wm2     = input.string("Trade Within | Silent | Patient", "Line 2", inline = "w2", group = GW)
wm2c    = input.color(color.new(color.gray, 20), "", inline = "w2", group = GW)
wm2s    = input.string("Small", "", ["Tiny", "Small", "Normal", "Large", "Huge"], inline = "w2", group = GW)
wm3     = input.string("No Confirmation No Entry", "Line 3", inline = "w3", group = GW)
wm3c    = input.color(color.new(color.gray, 35), "", inline = "w3", group = GW)
wm3s    = input.string("Tiny", "", ["Tiny", "Small", "Normal", "Large", "Huge"], inline = "w3", group = GW)
showBrand = input.bool(true, "Brand in bottom-right corner", group = GW)
 
// ═════════════════════════════ helpers
const int NS  = 12
const int DAY = 86400000
TRANSP = color.new(color.black, 100)
 
tzOf(string n) =>
    switch n
        "New York"  => "America/New_York"
        "London"    => "Europe/London"
        "Frankfurt" => "Europe/Berlin"
        "Tokyo"     => "Asia/Tokyo"
        "Shanghai"  => "Asia/Shanghai"
        "Tehran"    => "Asia/Tehran"
        "UTC"       => "UTC"
        => syminfo.timezone
 
lineStyle(string s) => s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
 
sizeOf(string s) =>
    switch s
        "Tiny"   => size.tiny
        "Small"  => size.small
        "Normal" => size.normal
        "Large"  => size.large
        => size.huge
 
posOf(string s) =>
    switch s
        "Top Right"     => position.top_right
        "Top Center"    => position.top_center
        "Top Left"      => position.top_left
        "Middle Right"  => position.middle_right
        "Middle Center" => position.middle_center
        "Middle Left"   => position.middle_left
        "Bottom Right"  => position.bottom_right
        "Bottom Center" => position.bottom_center
        => position.bottom_left
 
tfIn(string f, string t) =>
    s = timeframe.in_seconds(timeframe.period)
    s >= timeframe.in_seconds(f) and s <= timeframe.in_seconds(t)
 
fmtDur(int ms) =>
    int m = math.max(0, int(math.floor(ms / 60000)))
    int h = int(math.floor(m / 60))
    h > 0 ? str.format("{0}h {1}m", h, m % 60) : str.format("{0}m", m)
 
fmtT(int t) => str.format_time(t, "EEE dd HH:mm", syminfo.timezone)
 
// visibility on this chart
vBox  = tfIn(tBoxF, tBoxT)
vName = tfIn(tNamF, tNamT)
vSess = tfIn(tSesF, tSesT)
vPD   = tfIn(tPDF, tPDT)
vPW   = tfIn(tPWF, tPWT)
vPM   = tfIn(tPMF, tPMT)
vLbl  = tfIn(tLblF, tLblT)
vDay  = tfIn(tDayF, tDayT)
vWM   = tfIn(tWmF, tWmT)
 
fontF    = fontS == "Monospace" ? font.family_monospace : font.family_default
lblSize  = sizeOf(lblSizeS)
nameSize = sizeOf(nameSizeS)
sessSt   = lineStyle(sessStyleS)
sweptSt  = lineStyle(sweptStyleS)
barMs    = timeframe.in_seconds(timeframe.period) * 1000
tol      = touchTicks * syminfo.mintick
atrV     = ta.atr(50)
 
// ═════════════════════════════ types
type SessCfg
    bool   on
    string name
    string sess
    string tz
    color  c
    int    eh
    int    em
    int    sh
    int    sm
 
type Level
    line   ln
    label  lb
    float  price
    bool   isHigh
    bool   swept
    int    sweptT
    string name
    string tag
    color  c
    string dtag
    int    born
 
mkSess(bool on, string nm, string ss, string tzn, color c) =>
    SessCfg.new(on, nm, ss + ":1234567", tzOf(tzn), c,
         int(str.tonumber(str.substring(ss, 5, 7))), int(str.tonumber(str.substring(ss, 7, 9))),
         int(str.tonumber(str.substring(ss, 0, 2))), int(str.tonumber(str.substring(ss, 2, 4))))
 
tipOf(Level lv) =>
    lv.name + "  " + str.tostring(lv.price, format.mintick) + (lv.swept ? "  |  swept " + fmtT(lv.sweptT) : "  |  untouched")
 
newLevel(int t0, float p, bool isHi, string nm, string tg, color c, string st, int w, bool draw, string dtag) =>
    Level lv = Level.new(na, na, p, isHi, false, 0, nm, tg, c, dtag, time)
    if draw
        lv.ln := line.new(x1 = t0, y1 = p, x2 = time, y2 = p, xloc = xloc.bar_time, color = c, style = st, width = w)
        if showLabels and vLbl
            lv.lb := label.new(x = time, y = p, text = nm, xloc = xloc.bar_time, yloc = yloc.price, color = TRANSP,
                 style = label.style_label_left, textcolor = c, size = lblSize, tooltip = tipOf(lv), text_font_family = fontF)
    lv
 
// a level is taken: stop the line on this candle, swap the label for a short tag just before the touch
sweep(array<Level> arr) =>
    if arr.size() > 0
        for lv in arr
            if not lv.swept
                bool hit = lv.isHigh ? high >= lv.price - tol : low <= lv.price + tol
                if hit
                    lv.swept  := true
                    lv.sweptT := time
                    if not na(lv.ln)
                        if showSwept
                            lv.ln.set_x2(time)
                            lv.ln.set_style(sweptSt)
                        else
                            lv.ln.delete()
                    if not na(lv.lb)
                        if showSwept and sweptLabels
                            lv.lb.set_xy(time, lv.price)
                            lv.lb.set_text(lv.tag + " ")
                            lv.lb.set_style(lv.isHigh ? label.style_label_lower_right : label.style_label_upper_right)
                            lv.lb.set_size(size.tiny)
                            lv.lb.set_tooltip(tipOf(lv))
                        else
                            lv.lb.delete()
 
trim(array<Level> arr, int keep) =>
    while arr.size() > keep
        Level lv = arr.shift()
        if not na(lv.ln)
            lv.ln.delete()
        if not na(lv.lb)
            lv.lb.delete()
 
relabel(array<Level> arr, string word, string tg, bool isHi) =>
    int n = arr.size()
    if n > 0
        for j = 0 to n - 1
            Level lv = arr.get(j)
            int k = n - j
            string hl = isHi ? "High" : "Low"
            lv.name := k == 1 ? "Previous " + word + " " + hl : word + " " + hl + " (" + lv.dtag + ")"
            lv.tag  := k == 1 ? tg : tg + "-" + str.tostring(k)
            if not na(lv.lb)
                lv.lb.set_text(lv.swept ? lv.tag + " " : lv.name)
                lv.lb.set_tooltip(tipOf(lv))
 
collect(array<Level> dst, array<Level> src) =>
    if src.size() > 0
        for lv in src
            if not lv.swept
                dst.push(lv)
 
// ═════════════════════════════ state
var array<SessCfg> cfgs = array.from(
     mkSess(s1on, s1nm, s1ss, s1tz, s1c),    mkSess(s2on, s2nm, s2ss, s2tz, s2c),
     mkSess(s3on, s3nm, s3ss, s3tz, s3c),    mkSess(s4on, s4nm, s4ss, s4tz, s4c),
     mkSess(s5on, s5nm, s5ss, s5tz, s5c),    mkSess(s6on, s6nm, s6ss, s6tz, s6c),
     mkSess(s7on, s7nm, s7ss, s7tz, s7c),    mkSess(s8on, s8nm, s8ss, s8tz, s8c),
     mkSess(s9on, s9nm, s9ss, s9tz, s9c),    mkSess(s10on, s10nm, s10ss, s10tz, s10c),
     mkSess(s11on, s11nm, s11ss, s11tz, s11c), mkSess(s12on, s12nm, s12ss, s12tz, s12c))
 
var array<bool>  inPrev = array.new<bool>(NS, false)
var array<bool>  sTrk   = array.new<bool>(NS, false)
var array<box>   bx     = array.new<box>(NS)
var array<label> bxl    = array.new<label>(NS)
var array<float> sHi    = array.new<float>(NS)
var array<float> sLo    = array.new<float>(NS)
var array<int>   sHiT   = array.new<int>(NS, 0)
var array<int>   sLoT   = array.new<int>(NS, 0)
var array<int>   sSt    = array.new<int>(NS, 0)
var array<int>   sEnd   = array.new<int>(NS, 0)
var array<Level> lastH  = array.new<Level>(NS)
var array<Level> lastL  = array.new<Level>(NS)
 
var array<Level> sessLv = array.new<Level>()
var array<Level> dHi = array.new<Level>()
var array<Level> dLo = array.new<Level>()
var array<Level> wHi = array.new<Level>()
var array<Level> wLo = array.new<Level>()
var array<Level> mHi = array.new<Level>()
var array<Level> mLo = array.new<Level>()
 
inWin = time >= last_bar_time - lookback * DAY
inLvWin = time >= last_bar_time - math.max(lookback, lvLookback) * DAY
newD  = timeframe.change("D")
newW  = timeframe.change("W")
newM  = timeframe.change("M")
newDayTick = newD or barstate.islast
 
// ═════════════════════════════ sessions
for i = 0 to NS - 1
    SessCfg cfg = cfgs.get(i)
    if not cfg.on
        continue
    bool inS = not na(time(timeframe.period, cfg.sess, cfg.tz))
    bool was = inPrev.get(i)
 
    if inS and not was
        // a new session starts
        sHi.set(i, high)
        sLo.set(i, low)
        sHiT.set(i, time)
        sLoT.set(i, time)
        sSt.set(i, time)
        int endT = timestamp(cfg.tz, year(time, cfg.tz), month(time, cfg.tz), dayofmonth(time, cfg.tz), cfg.eh, cfg.em)
        if endT <= time
            endT += DAY
        sEnd.set(i, endT)
        sTrk.set(i, inLvWin)
        if inWin and vBox
            bx.set(i, box.new(left = time, top = high, right = extendActive ? endT : time + barMs, bottom = low,
                 xloc = xloc.bar_time, border_color = TRANSP, border_width = 0, bgcolor = color.new(cfg.c, boxTransp)))
            bxl.set(i, showNames and vName ? label.new(x = time, y = high, text = cfg.name, xloc = xloc.bar_time,
                 color = TRANSP, style = label.style_label_lower_left, textcolor = cfg.c, size = nameSize,
                 text_font_family = fontF) : na)
        else
            bx.set(i, na)
            bxl.set(i, na)
    else if inS
        // running session
        if high > sHi.get(i)
            sHi.set(i, high)
            sHiT.set(i, time)
        if low < sLo.get(i)
            sLo.set(i, low)
            sLoT.set(i, time)
        box b = bx.get(i)
        if not na(b)
            b.set_top(sHi.get(i))
            b.set_bottom(sLo.get(i))
            if not extendActive
                b.set_right(time + barMs)
        label nl = bxl.get(i)
        if not na(nl)
            nl.set_y(sHi.get(i))
 
    if not inS and was and sTrk.get(i) and showSessLv
        // session finished -> its high / low become liquidity
        string dtag = str.format_time(sSt.get(i), "EEE dd", cfg.tz)
        Level h = newLevel(sHiT.get(i), sHi.get(i), true,  "Previous " + cfg.name + " High", cfg.name + " H", cfg.c, sessSt, sessW, vSess, dtag)
        Level l = newLevel(sLoT.get(i), sLo.get(i), false, "Previous " + cfg.name + " Low",  cfg.name + " L", cfg.c, sessSt, sessW, vSess, dtag)
        Level ph = lastH.get(i)
        Level pl = lastL.get(i)
        if not na(ph)
            ph.name := cfg.name + " High (" + ph.dtag + ")"
            if not na(ph.lb) and not ph.swept
                ph.lb.set_text(ph.name)
        if not na(pl)
            pl.name := cfg.name + " Low (" + pl.dtag + ")"
            if not na(pl.lb) and not pl.swept
                pl.lb.set_text(pl.name)
        lastH.set(i, h)
        lastL.set(i, l)
        sessLv.push(h)
        sessLv.push(l)
 
    inPrev.set(i, inS)
 
// clean-up: old swept levels go away; untouched ones stay until price takes them
if sessLv.size() > 0 and newDayTick
    int limitT = time - (lookback + 1) * DAY
    for j = sessLv.size() - 1 to 0
        Level lv = sessLv.get(j)
        if lv.born < limitT and (lv.swept or not keepOpen)
            if not na(lv.ln)
                lv.ln.delete()
            if not na(lv.lb)
                lv.lb.delete()
            sessLv.remove(j)
// hard cap so TradingView's 500-object limit is never hit
while sessLv.size() > 200
    Level old = sessLv.shift()
    if not na(old.ln)
        old.ln.delete()
    if not na(old.lb)
        old.lb.delete()
 
// ═════════════════════════════ previous day / week / month
 
var float cdH = na
var float cdL = na
var int cdHt = 0
var int cdLt = 0
var int cdS = 0
var float cwH = na
var float cwL = na
var int cwHt = 0
var int cwLt = 0
var int cwS = 0
var float cmH = na
var float cmL = na
var int cmHt = 0
var int cmLt = 0
var int cmS = 0
 
if newD and not na(cdH)
    string tg = str.format_time(cdS, "EEE dd", syminfo.timezone)
    dHi.push(newLevel(cdHt, cdH, true,  "", "PDH", pdColor, lineStyle(pdStyleS), pdW, showPD and vPD, tg))
    dLo.push(newLevel(cdLt, cdL, false, "", "PDL", pdColor, lineStyle(pdStyleS), pdW, showPD and vPD, tg))
    trim(dHi, pdCount)
    trim(dLo, pdCount)
    relabel(dHi, "Day", "PDH", true)
    relabel(dLo, "Day", "PDL", false)
if newW and not na(cwH)
    string tg = str.format_time(cwS, "dd MMM", syminfo.timezone)
    wHi.push(newLevel(cwHt, cwH, true,  "", "PWH", pwColor, lineStyle(pwStyleS), pwW, showPW and vPW, tg))
    wLo.push(newLevel(cwLt, cwL, false, "", "PWL", pwColor, lineStyle(pwStyleS), pwW, showPW and vPW, tg))
    trim(wHi, pwCount)
    trim(wLo, pwCount)
    relabel(wHi, "Week", "PWH", true)
    relabel(wLo, "Week", "PWL", false)
if newM and not na(cmH)
    string tg = str.format_time(cmS, "MMM yyyy", syminfo.timezone)
    mHi.push(newLevel(cmHt, cmH, true,  "", "PMH", pmColor, lineStyle(pmStyleS), pmW, showPM and vPM, tg))
    mLo.push(newLevel(cmLt, cmL, false, "", "PML", pmColor, lineStyle(pmStyleS), pmW, showPM and vPM, tg))
    trim(mHi, pmCount)
    trim(mLo, pmCount)
    relabel(mHi, "Month", "PMH", true)
    relabel(mLo, "Month", "PML", false)
 
// running extremes of the current day / week / month
if newD or na(cdH)
    cdH := high
    cdL := low
    cdHt := time
    cdLt := time
    cdS := time
else
    if high > cdH
        cdH := high
        cdHt := time
    if low < cdL
        cdL := low
        cdLt := time
if newW or na(cwH)
    cwH := high
    cwL := low
    cwHt := time
    cwLt := time
    cwS := time
else
    if high > cwH
        cwH := high
        cwHt := time
    if low < cwL
        cwL := low
        cwLt := time
if newM or na(cmH)
    cmH := high
    cmL := low
    cmHt := time
    cmLt := time
    cmS := time
else
    if high > cmH
        cmH := high
        cmHt := time
    if low < cmL
        cmL := low
        cmLt := time
 
// ═════════════════════════════ sweeps
sweep(sessLv)
sweep(dHi)
sweep(dLo)
sweep(wHi)
sweep(wLo)
sweep(mHi)
sweep(mLo)
 
// ═════════════════════════════ day separators
if showDays and vDay and inWin and newD
    line.new(time, high, time, low, xloc = xloc.bar_time, extend = extend.both, color = sepColor, style = line.style_dotted)
    label.new(time, low, str.format_time(time, "EEEE | dd MMMM yyyy", syminfo.timezone), xloc = xloc.bar_time,
         yloc = yloc.belowbar, color = TRANSP, style = label.style_label_up, textcolor = color.gray, size = size.small)
 
// ═════════════════════════════ last bar: extend open levels, lay out labels, panel
isDark = themeS == "Dark" or (themeS == "Auto" and (color.r(chart.bg_color) * 299 + color.g(chart.bg_color) * 587 + color.b(chart.bg_color) * 114) / 1000 < 128)
pBg  = isDark ? #1C1E24 : #FCFCFC
pHd  = isDark ? #262930 : #EEF0F3
pTx  = isDark ? #E1E1E4 : #282A30
pDim = isDark ? #82868E : #919399
pAcc = isDark ? #2D78EB : #2878F0
pGrn = isDark ? #3CBE78 : #14965A
 
if barstate.islast
    // 1) open levels run to the current candle
    array<Level> openLv = array.new<Level>()
    collect(openLv, sessLv)
    collect(openLv, dHi)
    collect(openLv, dLo)
    collect(openLv, wHi)
    collect(openLv, wLo)
    collect(openLv, mHi)
    collect(openLv, mLo)
    array<Level> lbls = array.new<Level>()
    if openLv.size() > 0
        for lv in openLv
            if not na(lv.ln)
                lv.ln.set_x2(time)
            if not na(lv.lb)
                lbls.push(lv)
 
    // 2) labels: spread nearby ones apart (clusters centred on their lines) -> no overlaps
    int n = lbls.size()
    if n > 0
        array<float> prices = array.new<float>()
        for lv in lbls
            prices.push(lv.price)
        array<int> ord = prices.sort_indices(order.descending)
        float step = nz(atrV, syminfo.mintick * 10) * lblSpacing
        array<int>   cStart = array.new<int>()
        array<int>   cCnt   = array.new<int>()
        array<float> cTop   = array.new<float>()
        for k = 0 to n - 1
            cStart.push(k)
            cCnt.push(1)
            cTop.push(prices.get(ord.get(k)))
            while cTop.size() > 1 and cTop.get(cTop.size() - 2) - cCnt.get(cCnt.size() - 2) * step < cTop.last()
                int a = cTop.size() - 2
                int cnt = cCnt.get(a) + cCnt.last()
                float sum = 0.0
                for m = 0 to cnt - 1
                    sum += prices.get(ord.get(cStart.get(a) + m)) + m * step
                cCnt.set(a, cnt)
                cTop.set(a, sum / cnt)
                cStart.pop()
                cCnt.pop()
                cTop.pop()
        int xL = time + 3 * barMs
        for c = 0 to cTop.size() - 1
            for m = 0 to cCnt.get(c) - 1
                Level lv = lbls.get(ord.get(cStart.get(c) + m))
                lv.lb.set_xy(xL, cTop.get(c) - m * step)
                lv.lb.set_tooltip(tipOf(lv))
 
// panel
var table pnl = table.new(posOf(panelPosS), 2, 40, bgcolor = pBg, border_color = pHd, border_width = 1, frame_color = pHd, frame_width = 1)
if barstate.islast and showPanel
    pnl.clear(0, 0, 1, 39)
    psz = sizeOf(panelSizeS)
    int r = 0
    pnl.cell(0, r, "SESSIONS & LIQUIDITY", text_color = pTx, bgcolor = pHd, text_size = psz, text_halign = text.align_left)
    pnl.cell(1, r, "", bgcolor = pHd, text_size = psz)
    r += 1
    int nAct = 0
    int bestNext = -1
    int bestT = 0
    for i = 0 to NS - 1
        SessCfg cfg = cfgs.get(i)
        if not cfg.on
            continue
        if inPrev.get(i) and timenow < sEnd.get(i)
            nAct += 1
            pnl.cell(0, r, "● " + cfg.name, text_color = cfg.c, text_size = psz, text_halign = text.align_left)
            pnl.cell(1, r, fmtDur(sEnd.get(i) - timenow) + " left", text_color = pDim, text_size = psz, text_halign = text.align_right)
            r += 1
        else
            int st = timestamp(cfg.tz, year(timenow, cfg.tz), month(timenow, cfg.tz), dayofmonth(timenow, cfg.tz), cfg.sh, cfg.sm)
            if st <= timenow
                st += DAY
            if bestNext < 0 or st < bestT
                bestNext := i
                bestT := st
    if nAct == 0
        pnl.cell(0, r, "No active session", text_color = pDim, text_size = psz, text_halign = text.align_left)
        pnl.cell(1, r, "", text_size = psz)
        r += 1
    if bestNext >= 0
        pnl.cell(0, r, "Next:  " + cfgs.get(bestNext).name, text_color = pTx, text_size = psz, text_halign = text.align_left)
        pnl.cell(1, r, "in " + fmtDur(bestT - timenow), text_color = pDim, text_size = psz, text_halign = text.align_right)
        r += 1
    if not compact
        pnl.cell(0, r, "LIQUIDITY", text_color = pDim, bgcolor = pHd, text_size = psz, text_halign = text.align_left)
        pnl.cell(1, r, "", bgcolor = pHd, text_size = psz)
        r += 1
        array<Level> show = array.new<Level>()
        if dHi.size() > 0
            show.push(dHi.last())
            show.push(dLo.last())
        if wHi.size() > 0
            show.push(wHi.last())
            show.push(wLo.last())
        if mHi.size() > 0
            show.push(mHi.last())
            show.push(mLo.last())
        if show.size() > 0
            for lv in show
                pnl.cell(0, r, "● " + lv.tag + "   " + str.tostring(lv.price, format.mintick), text_color = lv.swept ? pDim : lv.c,
                     text_size = psz, text_halign = text.align_left)
                float d = lv.price - close
                pnl.cell(1, r, lv.swept ? "swept " + fmtT(lv.sweptT) : "open  " + (d >= 0 ? "+" : "") + str.tostring(d, format.mintick),
                     text_color = lv.swept ? pDim : pGrn, text_size = psz, text_halign = text.align_right)
                r += 1
    pnl.cell(0, r, "Telegram: @TheSilentEdgeFx", text_color = pAcc, text_size = psz, text_halign = text.align_left)
    pnl.cell(1, r, "", text_size = psz)
    r += 1
    pnl.cell(0, r, "Design and development: FarhanFx", text_color = pDim, text_size = size.tiny, text_halign = text.align_left)
    pnl.cell(1, r, "", text_size = size.tiny)
 
// watermark
var table wm = table.new(posOf(wmPosS), 1, 3, bgcolor = TRANSP, frame_width = 0, border_width = 0)
if barstate.islast and showWM and vWM
    if wm1 != ""
        wm.cell(0, 0, wm1, text_color = wm1c, text_size = sizeOf(wm1s), bgcolor = TRANSP)
    if wm2 != ""
        wm.cell(0, 1, wm2, text_color = wm2c, text_size = sizeOf(wm2s), bgcolor = TRANSP)
    if wm3 != ""
        wm.cell(0, 2, wm3, text_color = wm3c, text_size = sizeOf(wm3s), bgcolor = TRANSP)
 
// brand
var table br = table.new(position.bottom_right, 1, 2, bgcolor = TRANSP, frame_width = 0, border_width = 0)
if barstate.islast and showBrand
    br.cell(0, 0, "@TheSilentEdgeFx", text_color = pAcc, text_size = size.small, bgcolor = TRANSP, text_halign = text.align_right)
    br.cell(0, 1, "Design and development: FarhanFx", text_color = pDim, text_size = size.tiny, bgcolor = TRANSP, text_halign = text.align_right)

← 返回列表