ShanksONE Session Levels

ShanksONE- · study · 252 行 · 点赞 19 · TradingView 原页

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

Pine Script

//@version=6
indicator("ShanksONE Session Levels v4.3", "ShanksONE Sessions v4.3", overlay=true, max_boxes_count=250, max_lines_count=500, max_labels_count=250)
 
// Session-only visual overlay. Times follow New York daylight saving automatically.
grpS = "Sessions | New York time"
tz = input.string("America/New_York", "Timezone", group=grpS)
showAsia = input.bool(true, "Asia", group=grpS, inline="a")
asiaTime = input.session("2000-0000", "Hours", group=grpS, inline="a")
showLondon = input.bool(true, "London killzone", group=grpS, inline="l")
londonTime = input.session("0200-0500", "Hours", group=grpS, inline="l")
showNY = input.bool(true, "New York AM", group=grpS, inline="n")
nyTime = input.session("0700-1000", "Hours", group=grpS, inline="n")
showClose = input.bool(true, "London close", group=grpS, inline="c")
closeTime = input.session("1000-1200", "Hours", group=grpS, inline="c")
 
grpD = "Display"
showBoxes = input.bool(true, "Session range boxes", group=grpD)
showLevels = input.bool(true, "Session high / low lines", group=grpD)
extendLevels = input.bool(true, "Extend completed levels until next Asia session", group=grpD)
showNames = input.bool(true, "Session names inside boxes", group=grpD)
showEdgeLabels = input.bool(true, "Right-edge price labels", group=grpD)
labelOffset = input.int(3, "Label offset (bars)", minval=1, maxval=40, group=grpD)
stackATR = input.float(0.35, "Minimum label spacing (ATR)", minval=0.05, maxval=2, step=0.05, group=grpD)
showConnectors = input.bool(true, "Connect stacked labels to exact levels", group=grpD)
showPD = input.bool(true, "Previous day high / low", group=grpD)
showMidnight = input.bool(true, "New York midnight open", group=grpD)
keepDays = input.int(3, "Session cycles to keep", minval=1, maxval=15, group=grpD)
maxTF = input.int(60, "Hide drawings above timeframe (minutes)", minval=1, maxval=1440, group=grpD)
showPanel = input.bool(true, "Session status panel", group=grpD)
panelPosition = input.string("top_right", "Panel position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpD)
 
panelTextSize = input.string("normal", "Panel text size", options=["small", "normal", "large"], group=grpD)
panelSize = panelTextSize == "large" ? size.large : panelTextSize == "normal" ? size.normal : size.small
 
grpC = "Colours"
cAsia = input.color(#7E57C2, "Asia", group=grpC)
cLondon = input.color(#F23645, "London", group=grpC)
cNY = input.color(#089981, "New York", group=grpC)
cClose = input.color(#FF9800, "London close", group=grpC)
cKey = input.color(#C5CDD5, "Daily reference levels", group=grpC)
boxTransparency = input.int(90, "Box transparency", minval=0, maxval=100, group=grpC)
 
bool drawOK = timeframe.in_seconds() <= maxTF * 60
inSession(string sess) =>
    not na(time(timeframe.period, sess + ":1234567", tz))
 
// Historical objects are retained by session cycle, then cleaned up.
var box[] boxes = array.new_box()
var int[] boxCycles = array.new_int()
var line[] lines = array.new_line()
var int[] lineCycles = array.new_int()
var int cycle = 0
 
addBox(box b) =>
    array.push(boxes, b)
    array.push(boxCycles, cycle)
    b
addLine(line l) =>
    array.push(lines, l)
    array.push(lineCycles, cycle)
    l
 
cleanup() =>
    while array.size(boxes) > 0 and array.get(boxCycles, 0) <= cycle - keepDays
        box.delete(array.shift(boxes))
        array.shift(boxCycles)
    while array.size(lines) > 0 and array.get(lineCycles, 0) <= cycle - keepDays
        line.delete(array.shift(lines))
        array.shift(lineCycles)
 
// Each session owns its box and two range lines.
type Session
    string title
    color tint
    bool active
    float hi
    float lo
    int firstBar
    box rangeBox
    line highLine
    line lowLine
 
updateSession(Session s, bool activeNow) =>
    bool started = activeNow and not s.active
    bool ended = not activeNow and s.active
    if started
        s.active := true
        s.firstBar := bar_index
        s.hi := high
        s.lo := low
        s.rangeBox := na
        s.highLine := na
        s.lowLine := na
        if drawOK and showBoxes
            s.rangeBox := addBox(box.new(bar_index, high, bar_index, low, border_color=color.new(s.tint, 45), bgcolor=color.new(s.tint, boxTransparency), text=showNames ? s.title : "", text_color=s.tint, text_size=size.tiny, text_halign=text.align_left, text_valign=text.align_top))
        if drawOK and showLevels
            s.highLine := addLine(line.new(bar_index, high, bar_index, high, color=s.tint, width=1))
            s.lowLine := addLine(line.new(bar_index, low, bar_index, low, color=s.tint, width=1))
    else if activeNow
        s.hi := math.max(s.hi, high)
        s.lo := math.min(s.lo, low)
        if not na(s.rangeBox)
            box.set_right(s.rangeBox, bar_index)
            box.set_top(s.rangeBox, s.hi)
            box.set_bottom(s.rangeBox, s.lo)
        if not na(s.highLine)
            line.set_x2(s.highLine, bar_index)
            line.set_y1(s.highLine, s.hi)
            line.set_y2(s.highLine, s.hi)
            line.set_x2(s.lowLine, bar_index)
            line.set_y1(s.lowLine, s.lo)
            line.set_y2(s.lowLine, s.lo)
    if ended
        s.active := false
    [started, ended]
 
var Session asia = Session.new("ASIA", cAsia, false, na, na, na, na, na, na)
var Session london = Session.new("LONDON", cLondon, false, na, na, na, na, na, na)
var Session ny = Session.new("NY AM", cNY, false, na, na, na, na, na, na)
var Session lc = Session.new("LONDON CLOSE", cClose, false, na, na, na, na, na, na)
 
bool inAsia = showAsia and inSession(asiaTime)
bool inLondon = showLondon and inSession(londonTime)
bool inNY = showNY and inSession(nyTime)
bool inLC = showClose and inSession(closeTime)
 
// New cycle begins at Asia open, or at the New York calendar-day change if Asia is disabled.
int nyDate = year(time, tz) * 10000 + month(time, tz) * 100 + dayofmonth(time, tz)
bool newNYDay = not na(nyDate[1]) and nyDate != nyDate[1]
bool asiaStart = inAsia and not inAsia[1]
bool newCycle = showAsia ? asiaStart : newNYDay
 
var line pdhLine = na
var line pdlLine = na
var line midnightLine = na
var float midnightPrice = na
[prevHigh, prevLow] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead=barmerge.lookahead_on)
 
if newCycle
    cycle += 1
    cleanup()
    if extendLevels
        // Freeze completed lines from previous cycle at the boundary.
        for s in array.from(asia, london, ny, lc)
            if not s.active
                s.highLine := na
                s.lowLine := na
    pdhLine := na
    pdlLine := na
    if drawOK and showPD and not na(prevHigh)
        pdhLine := addLine(line.new(bar_index, prevHigh, bar_index, prevHigh, color=cKey, style=line.style_dotted))
        pdlLine := addLine(line.new(bar_index, prevLow, bar_index, prevLow, color=cKey, style=line.style_dotted))
 
[_, _aEnd] = updateSession(asia, inAsia)
[_lStart, _lEnd] = updateSession(london, inLondon)
[_nStart, _nEnd] = updateSession(ny, inNY)
[_cStart, _cEnd] = updateSession(lc, inLC)
 
if drawOK and showLevels and extendLevels
    for s in array.from(asia, london, ny, lc)
        if not s.active and not na(s.highLine)
            line.set_x2(s.highLine, bar_index)
            line.set_x2(s.lowLine, bar_index)
 
// Midnight reference is reset on New York calendar date changes.
if newNYDay or barstate.isfirst
    midnightPrice := open
    midnightLine := na
    if drawOK and showMidnight
        midnightLine := addLine(line.new(bar_index, open, bar_index, open, color=cKey, style=line.style_dashed))
if not na(midnightLine)
    line.set_x2(midnightLine, bar_index)
if not na(pdhLine)
    line.set_x2(pdhLine, bar_index)
    line.set_x2(pdlLine, bar_index)
 
string activeText = inAsia ? "ASIA" : inLondon ? "LONDON" : inNY ? "NEW YORK AM" : inLC ? "LONDON CLOSE" : "BETWEEN SESSIONS"
color activeColor = inAsia ? cAsia : inLondon ? cLondon : inNY ? cNY : inLC ? cClose : color.gray
fmt(float x) => na(x) ? "—" : str.tostring(x, format.mintick)
 
var table panel = table.new(panelPosition, 2, 7, bgcolor=color.new(color.black, 25), frame_color=color.new(color.gray, 75), frame_width=1)
if barstate.islast
    table.clear(panel, 0, 0, 1, 6)
    if showPanel
        table.cell(panel, 0, 0, "SHANKSONE SESSIONS", text_color=color.white, bgcolor=color.new(activeColor, 45), text_size=panelSize)
        table.cell(panel, 1, 0, activeText, text_color=color.white, bgcolor=color.new(activeColor, 45), text_size=panelSize)
        table.cell(panel, 0, 1, "Asia H / L", text_color=cAsia, text_size=panelSize)
        table.cell(panel, 1, 1, fmt(asia.hi) + " / " + fmt(asia.lo), text_color=color.white, text_size=panelSize)
        table.cell(panel, 0, 2, "London H / L", text_color=cLondon, text_size=panelSize)
        table.cell(panel, 1, 2, fmt(london.hi) + " / " + fmt(london.lo), text_color=color.white, text_size=panelSize)
        table.cell(panel, 0, 3, "NY AM H / L", text_color=cNY, text_size=panelSize)
        table.cell(panel, 1, 3, fmt(ny.hi) + " / " + fmt(ny.lo), text_color=color.white, text_size=panelSize)
        table.cell(panel, 0, 4, "London close H / L", text_color=cClose, text_size=panelSize)
        table.cell(panel, 1, 4, fmt(lc.hi) + " / " + fmt(lc.lo), text_color=color.white, text_size=panelSize)
        table.cell(panel, 0, 5, "PDH / PDL", text_color=cKey, text_size=panelSize)
        table.cell(panel, 1, 5, fmt(prevHigh) + " / " + fmt(prevLow), text_color=color.white, text_size=panelSize)
        table.cell(panel, 0, 6, "NY midnight open", text_color=cKey, text_size=panelSize)
        table.cell(panel, 1, 6, fmt(midnightPrice), text_color=color.white, text_size=panelSize)
 
// Right-edge labels: vertically staggered to avoid overlapping.
// Connector lines show the true price when a label is displaced.
var label[] edgeLabels = array.new_label()
var line[] edgeConnectors = array.new_line()
var float[] levelPrices = array.new_float()
var string[] levelNames = array.new_string()
var color[] levelColors = array.new_color()
 
addEdge(float price, string name, color tint) =>
    if not na(price)
        array.push(levelPrices, price)
        array.push(levelNames, name)
        array.push(levelColors, tint)
    0
 
float edgeATR = ta.atr(14)
if barstate.islast
    for lb in edgeLabels
        label.delete(lb)
    for ln in edgeConnectors
        line.delete(ln)
    array.clear(edgeLabels)
    array.clear(edgeConnectors)
    array.clear(levelPrices)
    array.clear(levelNames)
    array.clear(levelColors)
    if drawOK and showEdgeLabels
        if showLevels
            for s in array.from(asia, london, ny, lc)
                if not na(s.highLine)
                    addEdge(s.hi, s.title + " H", s.tint)
                    addEdge(s.lo, s.title + " L", s.tint)
        if showPD and not na(pdhLine)
            addEdge(prevHigh, "PDH", cKey)
            addEdge(prevLow, "PDL", cKey)
        if showMidnight and not na(midnightLine)
            addEdge(midnightPrice, "NY MIDNIGHT", cKey)
        int count = array.size(levelPrices)
        if count > 0
            int[] sorted = array.sort_indices(levelPrices, order.descending)
            float minGap = math.max(nz(edgeATR, syminfo.mintick * 20) * stackATR, syminfo.mintick * 12)
            float lastY = na
            for k = 0 to count - 1
                int idx = array.get(sorted, k)
                float price = array.get(levelPrices, idx)
                string name = array.get(levelNames, idx)
                color tint = array.get(levelColors, idx)
                float labelY = na(lastY) ? price : math.min(price, lastY - minGap)
                lastY := labelY
                int x = bar_index + labelOffset
                array.push(edgeLabels, label.new(x, labelY, name + "  " + str.tostring(price, format.mintick), style=label.style_label_left, color=color.new(tint, 76), textcolor=tint, size=size.small))
                if showConnectors and math.abs(labelY - price) > syminfo.mintick
                    array.push(edgeConnectors, line.new(bar_index, price, x, labelY, color=color.new(tint, 35), width=1, style=line.style_dotted))

← 返回列表