Kalashnikover Sessions

EugeneKalashnikov · study · 149 行 · 点赞 4 · TradingView 原页

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

Pine Script

/// © Kalashnikover 2026. All rights reserved.
// Created by Kalashnikover. Unauthorized copying, redistribution or resale is prohibited.
 
//@version=6
indicator("Kalashnikover Sessions", shorttitle="Kalashnikover", overlay=true, max_boxes_count=500, max_lines_count=500)
 
// ---------- Inputs ----------
tz        = input.string("America/New_York", "Timezone", options=["America/New_York", "America/Chicago", "America/Los_Angeles"])
mainSess  = input.session("2000-0500", "Main range (20:00 - 05:00)", group="Sessions")
asiaSess  = input.session("2000-0000", "Asia (20:00 - 00:00)", group="Sessions")
lonSess   = input.session("0200-0500", "London (02:00 - 05:00)", group="Sessions")
 
showMain  = input.bool(true, "Show Main range", group="Display")
showAsia  = input.bool(true, "Show Asia", group="Display")
showLon   = input.bool(true, "Show London", group="Display")
showHist  = input.bool(false, "Show past 7 days (off = today only)", group="Display")
maxDays   = showHist ? 7 : 1
 
mainCol   = input.color(color.gray, "Main range color", group="Colors")
subCol    = input.color(color.orange, "Asia + London color (shared)", group="Colors")
opacity   = input.int(15, "Opacity %", minval=0, maxval=100, group="Colors")
 
// Fib settings
showFib     = input.bool(true, "Show Fibs on Main range", group="Fibs")
fibStyle    = input.string("Solid", "Fib line style", options=["Solid", "Dashed", "Dotted"], group="Fibs")
fibExtend   = input.bool(true, "Extend fib lines to end of day", group="Fibs")
fibEndHour  = input.int(21, "End of day hour (US time)", minval=0, maxval=23, group="Fibs")
midEndHour  = input.int(12, "0.5 line end hour (US time)", minval=0, maxval=23, group="Fibs")
fibFlip     = input.bool(false, "Flip direction (0 = range high, 1 = range low)", group="Fibs")
 
// Regular levels (-0.5, 0.25, 0.75, 1.5)
fibCol      = input.color(color.gray, "Regular line color", group="Fib: Regular")
fibOpacity  = input.int(45, "Regular line opacity %", minval=0, maxval=100, group="Fib: Regular")
fibWidth    = input.int(1, "Regular line thickness", minval=1, maxval=10, group="Fib: Regular")
 
// Key levels (0.5, -1, 2) share one thickness
keyWidth    = input.int(2, "0.5 / -1 / 2 line thickness", minval=1, maxval=10, group="Fib: Key levels")
midCol      = input.color(color.blue, "0.5 line color", group="Fib: Key levels")
midOpacity  = input.int(60, "0.5 line opacity %", minval=0, maxval=100, group="Fib: Key levels")
extremeCol  = input.color(color.red, "-1 and 2 line color", group="Fib: Key levels")
extremeOpac = input.int(50, "-1 and 2 line opacity %", minval=0, maxval=100, group="Fib: Key levels")
 
// Opacity % -> transparency
fillMain   = color.new(mainCol, 100 - opacity)
fillSub    = color.new(subCol, 100 - opacity)
normalLine = color.new(fibCol, 100 - fibOpacity)
midLine    = color.new(midCol, 100 - midOpacity)
redLine    = color.new(extremeCol, 100 - extremeOpac)
 
lineStyle = switch fibStyle
    "Dashed" => line.style_dashed
    "Dotted" => line.style_dotted
    => line.style_solid
 
// ---------- Session detection (US time) ----------
inSession(sess) => not na(time(timeframe.period, sess, tz))
 
inMain = inSession(mainSess)
inAsia = inSession(asiaSess)
inLon  = inSession(lonSess)
 
// ---------- Box drawing: no border, no text ----------
drawSession(active, col) =>
    var box b = na
    var box[] hist = array.new<box>()
    var float hi = na
    var float lo = na
    started = active and not active[1]
    if started
        hi := high
        lo := low
        b := box.new(bar_index, high, bar_index, low, border_color=na, border_width=0, bgcolor=col)
        array.push(hist, b)
        while array.size(hist) > maxDays
            box.delete(array.shift(hist))
    else if active and not na(b)
        hi := math.max(hi, high)
        lo := math.min(lo, low)
        box.set_right(b, bar_index)
        box.set_top(b, hi)
        box.set_bottom(b, lo)
    0
 
// Called on every bar (not inside an if) so history references stay consistent
drawSession(timeframe.isintraday and showMain and inMain, fillMain)
drawSession(timeframe.isintraday and showAsia and inAsia, fillSub)
drawSession(timeframe.isintraday and showLon and inLon, fillSub)
 
// ---------- Fibs on Main range (only the selected levels) ----------
var float[] levels = array.from(-1.0, -0.5, 0.25, 0.5, 0.75, 1.5, 2.0)
var line[] fibLines = array.new<line>()
var line[] allLines = array.new<line>()
var float mHi = na
var float mLo = na
var int mStartT = na
var int fibEnd = na
var int midEnd = na
 
tfMs = timeframe.in_seconds() * 1000
mainStarted = inMain and not inMain[1]
 
if timeframe.isintraday and showFib
    if mainStarted
        // finish the previous day's lines at their own end times
        if array.size(fibLines) > 0 and not na(fibEnd) and fibExtend
            for i = 0 to array.size(fibLines) - 1
                prevRatio = array.get(levels, i)
                line.set_x2(array.get(fibLines, i), prevRatio == 0.5 ? midEnd : fibEnd)
 
        mHi := high
        mLo := low
        mStartT := time
        dayShift = hour(time, tz) >= 12 ? 1 : 0
        fibEnd := timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz) + dayShift, fibEndHour, 0)
        midEnd := timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz) + dayShift, midEndHour, 0)
 
        array.clear(fibLines)
        for i = 0 to array.size(levels) - 1
            ratio = array.get(levels, i)
            isExtreme = ratio == -1.0 or ratio == 2.0
            isMid = ratio == 0.5
            col = isExtreme ? redLine : isMid ? midLine : normalLine
            w = (isExtreme or isMid) ? keyWidth : fibWidth
            newLine = line.new(time, close, time, close, xloc=xloc.bar_time, color=col, width=w, style=lineStyle)
            array.push(fibLines, newLine)
            array.push(allLines, newLine)
 
        // keep only today's lines (or the last 7 days when toggled on)
        while array.size(allLines) > maxDays * array.size(levels)
            line.delete(array.shift(allLines))
 
    if array.size(fibLines) > 0
        keepGoing = inMain or (fibExtend and nz(time[1]) <= fibEnd)
        if keepGoing
            if inMain
                mHi := math.max(mHi, high)
                mLo := math.min(mLo, low)
            rng = mHi - mLo
            for i = 0 to array.size(levels) - 1
                ratio = array.get(levels, i)
                l = array.get(fibLines, i)
                lineEnd = ratio == 0.5 ? midEnd : fibEnd
                x2 = fibExtend ? math.min(lineEnd, time + 499 * tfMs) : time
                lvl = inMain ? (fibFlip ? mHi - rng * ratio : mLo + rng * ratio) : line.get_y1(l)
                line.set_xy1(l, mStartT, lvl)
                line.set_xy2(l, x2, lvl)
 
if not timeframe.isintraday and barstate.islast
    label.new(bar_index, high, "Use an intraday timeframe (1m - 4h)", style=label.style_label_down, color=color.red, textcolor=color.white)

← 返回列表