SNP420:Bank_Market_Sessions(BMS)_v2.0

SNP_420 · study · 147 行 · 点赞 9 · TradingView 原页

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

Pine Script

// This Pine Script(R) code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) SNP420
// Author: SNP420
// Company: Finexus s.r.o.
// Original code. The minimalist session-line look was inspired by the open-source "FX Market Sessions" by boitoki (no code reused).
 
//@version=6
indicator("SNP420:Bank_Market_Sessions(BMS)", "SNP420:Bank_Market_Sessions(BMS)", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ---------- Inputs ----------
G1 = "Session 1"
G2 = "Session 2"
G3 = "Session 3"
G4 = "Session 4"
GS = "Style"
GL = "Levels"
 
s1On  = input.bool(true, "", inline = "a", group = G1, display = display.none)
s1Nm  = input.string("Tokyo", "", inline = "a", group = G1, display = display.none)
s1Col = input.color(color.rgb(51, 255, 0), "", inline = "a", group = G1, display = display.none)
s1Ses = input.session("0900-1800", "Time", inline = "b", group = G1, display = display.none)
s1Tz  = input.string("Asia/Tokyo", "", options = ["UTC", "Europe/London", "Europe/Berlin", "Europe/Prague", "America/New_York", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Australia/Sydney"], inline = "b", group = G1, display = display.none)
 
s2On  = input.bool(true, "", inline = "a", group = G2, display = display.none)
s2Nm  = input.string("London", "", inline = "a", group = G2, display = display.none)
s2Col = input.color(#FFFF00, "", inline = "a", group = G2, display = display.none)
s2Ses = input.session("0800-1700", "Time", inline = "b", group = G2, display = display.none)
s2Tz  = input.string("Europe/London", "", options = ["UTC", "Europe/London", "Europe/Berlin", "Europe/Prague", "America/New_York", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Australia/Sydney"], inline = "b", group = G2, display = display.none)
 
s3On  = input.bool(true, "", inline = "a", group = G3, display = display.none)
s3Nm  = input.string("New York", "", inline = "a", group = G3, display = display.none)
s3Col = input.color(color.rgb(255, 0, 0), "", inline = "a", group = G3, display = display.none)
s3Ses = input.session("0800-1700", "Time", inline = "b", group = G3, display = display.none)
s3Tz  = input.string("America/New_York", "", options = ["UTC", "Europe/London", "Europe/Berlin", "Europe/Prague", "America/New_York", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Australia/Sydney"], inline = "b", group = G3, display = display.none)
 
s4On  = input.bool(false, "", inline = "a", group = G4, display = display.none)
s4Nm  = input.string("Sydney", "", inline = "a", group = G4, display = display.none)
s4Col = input.color(#FFFF00, "", inline = "a", group = G4, display = display.none)
s4Ses = input.session("0700-1600", "Time", inline = "b", group = G4, display = display.none)
s4Tz  = input.string("Australia/Sydney", "", options = ["UTC", "Europe/London", "Europe/Berlin", "Europe/Prague", "America/New_York", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Singapore", "Australia/Sydney"], inline = "b", group = G4, display = display.none)
 
lvlMode = input.string("Low", "Line level", options = ["Low", "High"], group = GS, display = display.none)
lnStyle = input.string("Dashed", "Line style", options = ["Solid", "Dashed", "Dotted"], group = GS, display = display.none)
lnWidth = input.int(1, "Line width", minval = 1, maxval = 4, group = GS, display = display.none)
lbShow  = input.bool(true, "Show session name", group = GS, display = display.none)
lbSize  = input.string("Small", "Name size", options = ["Tiny", "Small", "Normal", "Large"], group = GS, display = display.none)
history = input.int(10, "Sessions kept per market", minval = 1, maxval = 100, group = GS, display = display.none)
extendOn = input.bool(false, "Extend the last finished level until price breaks it", group = GL, display = display.none)
 
// ---------- Constants ----------
NONE     = color.new(color.black, 100)
USE_HIGH = lvlMode == "High"
LN_STYLE = lnStyle == "Solid" ? line.style_solid : lnStyle == "Dotted" ? line.style_dotted : line.style_dashed
LB_SIZE  = switch lbSize
    "Tiny"   => size.tiny
    "Normal" => size.normal
    "Large"  => size.large
    => size.small
 
// ---------- Session object ----------
type Sess
    string       name
    string       sess
    string       tz
    color        col
    array<line>  lines
    array<label> labels
    bool         wasIn  = false
    int          x1
    int          xEnd
    float        lvl
    line         ln
    label        lb
    line         lastLn
    float        lastLvl
    bool         broken = true
 
// Session end as a timestamp (handles sessions crossing midnight)
f_endTime(string sess, string tz) =>
    eH = int(str.tonumber(str.substring(sess, 5, 7)))
    eM = int(str.tonumber(str.substring(sess, 7, 9)))
    t  = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), eH, eM)
    t <= time ? t + 86400000 : t
 
// Session finished: its final level becomes the level to watch
method finish(Sess s) =>
    s.lastLn  := s.ln
    s.lastLvl := s.lvl
    s.broken  := false
    alert(s.name + " session ended", alert.freq_once_per_bar)
 
method step(Sess s, bool inS) =>
    if inS and (not s.wasIn or time >= s.xEnd)
        if s.wasIn
            s.finish()
 
        s.x1   := time
        s.xEnd := f_endTime(s.sess, s.tz)
        s.lvl  := USE_HIGH ? high : low
        s.ln   := line.new(s.x1, s.lvl, s.xEnd, s.lvl, xloc = xloc.bar_time, color = s.col, style = LN_STYLE, width = lnWidth)
        s.lb   := label.new(math.round((s.x1 + s.xEnd) / 2), s.lvl, lbShow ? s.name : "", xloc = xloc.bar_time, color = lbShow ? chart.bg_color : NONE, textcolor = s.col, style = label.style_label_center, size = LB_SIZE, text_formatting = text.format_bold)
 
        s.lines.push(s.ln)
        s.labels.push(s.lb)
        if s.lines.size() > history
            s.lines.shift().delete()
            s.labels.shift().delete()
 
        alert(s.name + " session started", alert.freq_once_per_bar)
 
    else if inS
        newLvl = USE_HIGH ? math.max(s.lvl, high) : math.min(s.lvl, low)
        if newLvl != s.lvl
            s.lvl := newLvl
            s.ln.set_y1(newLvl)
            s.ln.set_y2(newLvl)
            s.lb.set_y(newLvl)
 
    else if s.wasIn
        s.finish()
 
    // Watch the last finished level until price trades through it
    if not inS and not s.broken
        if extendOn
            s.lastLn.set_x2(math.max(time, s.xEnd))
        hit = USE_HIGH ? high > s.lastLvl : low < s.lastLvl
        if hit
            s.broken := true
            alert(s.name + (USE_HIGH ? " high " : " low ") + str.tostring(s.lastLvl, format.mintick) + " broken", alert.freq_once_per_bar)
 
    s.wasIn := inS
 
// ---------- Run ----------
var Sess sA = Sess.new(s1Nm, s1Ses, s1Tz, s1Col, array.new<line>(), array.new<label>())
var Sess sB = Sess.new(s2Nm, s2Ses, s2Tz, s2Col, array.new<line>(), array.new<label>())
var Sess sC = Sess.new(s3Nm, s3Ses, s3Tz, s3Col, array.new<line>(), array.new<label>())
var Sess sD = Sess.new(s4Nm, s4Ses, s4Tz, s4Col, array.new<line>(), array.new<label>())
 
in1 = timeframe.isintraday and not na(time(timeframe.period, s1Ses, s1Tz))
in2 = timeframe.isintraday and not na(time(timeframe.period, s2Ses, s2Tz))
in3 = timeframe.isintraday and not na(time(timeframe.period, s3Ses, s3Tz))
in4 = timeframe.isintraday and not na(time(timeframe.period, s4Ses, s4Tz))
 
sA.step(s1On and in1)
sB.step(s2On and in2)
sC.step(s3On and in3)
sD.step(s4On and in4)

← 返回列表