SessionOne

nullnemoiam · study · 141 行 · 点赞 5 · TradingView 原页

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

Pine Script

//@version=6
indicator("SessionOne", overlay=true, max_boxes_count=500, max_labels_count=500)
 
// ──────────────────────────────────────────────
// General settings
// ──────────────────────────────────────────────
gGen = "General"
styleMode  = input.string("Box", "Display mode", options=["Box", "Background"], group=gGen)
showLabels = input.bool(true, "Show session labels", group=gGen)
txtColor   = input.color(color.new(#5F6672, 0), "Label color", group=gGen)
borderW    = input.int(1, "Box border width", minval=0, maxval=4, group=gGen)
 
boxMode = styleMode == "Box"
bgMode  = styleMode == "Background"
 
nameA = "Asia"
nameF = "Frankfurt"
nameL = "London"
nameN = "New York"
 
// ──────────────────────────────────────────────
// Asia
// ──────────────────────────────────────────────
gA     = "Asia"
showA  = input.bool(true, "Show session", group=gA)
sessA  = input.session("0900-1800", "Session time", group=gA)
tzA    = input.string("Asia/Tokyo", "Time zone", options=["Exchange", "UTC", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Europe/Kyiv", "Europe/Berlin", "Europe/London", "America/New_York"], group=gA)
colA   = input.color(color.new(#8E9AAF, 85), "Color", group=gA)
 
// ──────────────────────────────────────────────
// Frankfurt
// ──────────────────────────────────────────────
gF     = "Frankfurt"
showF  = input.bool(true, "Show session", group=gF)
sessF  = input.session("0800-1700", "Session time", group=gF)
tzF    = input.string("Europe/Berlin", "Time zone", options=["Exchange", "UTC", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Europe/Kyiv", "Europe/Berlin", "Europe/London", "America/New_York"], group=gF)
colF   = input.color(color.new(#C2B5A3, 85), "Color", group=gF)
 
// ──────────────────────────────────────────────
// London
// ──────────────────────────────────────────────
gL     = "London"
showL  = input.bool(true, "Show session", group=gL)
sessL  = input.session("0800-1630", "Session time", group=gL)
tzL    = input.string("Europe/London", "Time zone", options=["Exchange", "UTC", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Europe/Kyiv", "Europe/Berlin", "Europe/London", "America/New_York"], group=gL)
colL   = input.color(color.new(#7F9DBF, 85), "Color", group=gL)
 
// ──────────────────────────────────────────────
// New York
// ──────────────────────────────────────────────
gN     = "New York"
showN  = input.bool(true, "Show session", group=gN)
sessN  = input.session("0930-1600", "Session time", group=gN)
tzN    = input.string("America/New_York", "Time zone", options=["Exchange", "UTC", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Europe/Kyiv", "Europe/Berlin", "Europe/London", "America/New_York"], group=gN)
colN   = input.color(color.new(#93B09E, 85), "Color", group=gN)
 
// ──────────────────────────────────────────────
// Session duration in minutes (from a "0900-1800" string)
// ──────────────────────────────────────────────
f_dur(string s) =>
    int sh = int(str.tonumber(str.substring(s, 0, 2)))
    int sm = int(str.tonumber(str.substring(s, 2, 4)))
    int eh = int(str.tonumber(str.substring(s, 5, 7)))
    int em = int(str.tonumber(str.substring(s, 7, 9)))
    int d = (eh * 60 + em) - (sh * 60 + sm)
    int r = d < 0 ? d + 1440 : d
    r == 0 ? 1440 : r
 
// ──────────────────────────────────────────────
// Session calc: active, start, high, low, session midpoint
// ──────────────────────────────────────────────
f_calc(bool isOn, string sessionStr, string tzStr) =>
    var float hi = na
    var float lo = na
    var int ts = na
    var bool centerDone = false
    bool inSess = isOn and timeframe.isintraday and not na(time(timeframe.period, sessionStr, tzStr))
    bool sessStart = inSess and not inSess[1]
    if sessStart
        hi := high
        lo := low
        ts := time
        centerDone := false
    if inSess and not sessStart
        hi := math.max(hi, high)
        lo := math.min(lo, low)
    bool isCenter = false
    if inSess and not centerDone
        float halfMs = f_dur(sessionStr) * 30000.0
        if time >= ts + halfMs
            isCenter := true
            centerDone := true
    [inSess, sessStart, hi, lo, isCenter]
 
[inA, stA, hiA, loA, cenA] = f_calc(showA, sessA, tzA)
[inF, stF, hiF, loF, cenF] = f_calc(showF, sessF, tzF)
[inL, stL, hiL, loL, cenL] = f_calc(showL, sessL, tzL)
[inN, stN, hiN, loN, cenN] = f_calc(showN, sessN, tzN)
 
// ──────────────────────────────────────────────
// "Box" mode: box + label above the top-left corner
// ──────────────────────────────────────────────
f_draw(bool st, bool inS, float hi, float lo, color c, string nm) =>
    var box bx = na
    var label lb = na
    if boxMode and st
        bx := box.new(bar_index, hi, bar_index, lo)
        box.set_bgcolor(bx, c)
        box.set_border_color(bx, color.new(c, 30))
        box.set_border_width(bx, borderW)
        lb := na
        if showLabels
            lb := label.new(bar_index, hi, nm, style=label.style_label_lower_left, color=color.new(color.white, 100), textcolor=txtColor, size=size.small, textalign=text.align_left)
    if boxMode and inS and not st and not na(bx)
        box.set_top(bx, hi)
        box.set_bottom(bx, lo)
        box.set_right(bx, bar_index)
        if not na(lb)
            label.set_y(lb, hi)
    inS
 
f_draw(stA, inA, hiA, loA, colA, nameA)
f_draw(stF, inF, hiF, loF, colF, nameF)
f_draw(stL, inL, hiL, loL, colL, nameL)
f_draw(stN, inN, hiN, loN, colN, nameN)
 
// ──────────────────────────────────────────────
// "Background" mode: full-height highlight
// ──────────────────────────────────────────────
bgcolor(bgMode and inA ? colA : na, title="Asia background")
bgcolor(bgMode and inF ? colF : na, title="Frankfurt background")
bgcolor(bgMode and inL ? colL : na, title="London background")
bgcolor(bgMode and inN ? colN : na, title="New York background")
 
// ──────────────────────────────────────────────
// "Background" mode: label at the bottom, centered on the session
// ──────────────────────────────────────────────
plotchar(bgMode and showLabels and cenA, title="Asia label", char=" ", location=location.bottom, text="Asia", textcolor=txtColor)
plotchar(bgMode and showLabels and cenF, title="Frankfurt label", char=" ", location=location.bottom, text="Frankfurt", textcolor=txtColor)
plotchar(bgMode and showLabels and cenL, title="London label", char=" ", location=location.bottom, text="London", textcolor=txtColor)
plotchar(bgMode and showLabels and cenN, title="New York label", char=" ", location=location.bottom, text="New York", textcolor=txtColor)

← 返回列表