Nifty Day Session Lines

d4desired · study · 121 行 · 点赞 33 · TradingView 原页

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

Pine Script

//@version=6
indicator("Nifty Day Session Lines", shorttitle="Day Lines", overlay=true, max_lines_count=500, max_labels_count=500)
 
// ───────────── Inputs: how much history ─────────────
grpH      = "History"
weeksBack = input.int(4, "Show last N weeks (0 = all)", minval=0, maxval=104, group=grpH)
 
// ───────────── Inputs: per-day Start / End ─────────────
grpD = "Pick Start / End line for each day"
monS = input.bool(true,  "Monday    →  Start", group=grpD, inline="mon")
monE = input.bool(false, "",                   group=grpD, inline="mon")
tueS = input.bool(false, "Tuesday   →  Start", group=grpD, inline="tue")
tueE = input.bool(false, "",                   group=grpD, inline="tue")
wedS = input.bool(false, "Wednesday →  Start", group=grpD, inline="wed")
wedE = input.bool(false, "",                   group=grpD, inline="wed")
thuS = input.bool(false, "Thursday  →  Start", group=grpD, inline="thu")
thuE = input.bool(false, "",                   group=grpD, inline="thu")
friS = input.bool(false, "Friday    →  Start", group=grpD, inline="fri")
friE = input.bool(true,  "",                   group=grpD, inline="fri")
satS = input.bool(false, "Saturday  →  Start", group=grpD, inline="sat")
satE = input.bool(false, "",                   group=grpD, inline="sat")
sunS = input.bool(false, "Sunday    →  Start", group=grpD, inline="sun")
sunE = input.bool(false, "",                   group=grpD, inline="sun")
 
grpL      = "Line style"
startCol  = input.color(color.green, "Start line colour", group=grpL)
endCol    = input.color(color.red,   "End line colour",   group=grpL)
styleStr  = input.string("Dashed", "Line style", options=["Solid", "Dotted", "Dashed"], group=grpL)
lineWidth = input.int(1, "Line width", minval=1, maxval=4, group=grpL)
showLbl   = input.bool(false, "Show day name on Start lines", group=grpL)
 
grpT      = "Session end time (IST) - used for today's end line"
endHour   = input.int(15, "End hour",   minval=0, maxval=23, group=grpT)
endMinute = input.int(30, "End minute", minval=0, maxval=59, group=grpT)
 
grpS   = "Session shading (optional)"
showBg = input.bool(false, "Shade market session on selected days", group=grpS)
sessIn = input.session("0915-1530", "Session time", group=grpS)
bgCol  = input.color(color.new(color.blue, 85), "Shade colour", group=grpS)
 
tz = "Asia/Kolkata"
 
// ───────────── Helpers ─────────────
startOn(int d) =>
    switch d
        dayofweek.monday    => monS
        dayofweek.tuesday   => tueS
        dayofweek.wednesday => wedS
        dayofweek.thursday  => thuS
        dayofweek.friday    => friS
        dayofweek.saturday  => satS
        => sunS
 
endOn(int d) =>
    switch d
        dayofweek.monday    => monE
        dayofweek.tuesday   => tueE
        dayofweek.wednesday => wedE
        dayofweek.thursday  => thuE
        dayofweek.friday    => friE
        dayofweek.saturday  => satE
        => sunE
 
dayName(int d) =>
    switch d
        dayofweek.monday    => "Mon"
        dayofweek.tuesday   => "Tue"
        dayofweek.wednesday => "Wed"
        dayofweek.thursday  => "Thu"
        dayofweek.friday    => "Fri"
        dayofweek.saturday  => "Sat"
        => "Sun"
 
lStyle = switch styleStr
    "Solid"  => line.style_solid
    "Dotted" => line.style_dotted
    => line.style_dashed
 
// ───────────── History cutoff (Monday of the first week to show) ─────────────
lastDayStart = timestamp(tz, year(last_bar_time, tz), month(last_bar_time, tz), dayofmonth(last_bar_time, tz), 0, 0)
daysSinceMon = (dayofweek(last_bar_time, tz) + 5) % 7
cutoff       = lastDayStart - daysSinceMon * 86400000 - (weeksBack - 1) * 7 * 86400000
 
inRange     = weeksBack == 0 or time >= cutoff
inRangePrev = weeksBack == 0 or time[1] >= cutoff
 
// ───────────── Logic ─────────────
isIntraday = timeframe.isintraday
curDow = dayofweek(time, tz)
newDay = na(time[1]) or dayofmonth(time, tz) != dayofmonth(time[1], tz)
 
endTotal = endHour * 60 + endMinute
closeMin = hour(time_close, tz) * 60 + minute(time_close, tz)
 
startMark = isIntraday and newDay and inRange and startOn(curDow)
// End line of the PREVIOUS day, drawn on its last candle (found when the new day begins)
endPrev   = isIntraday and newDay and bar_index > 0 and inRangePrev and endOn(dayofweek(time[1], tz))
 
// ───────────── Drawing ─────────────
if startMark
    line.new(bar_index, low, bar_index, high + syminfo.mintick, extend=extend.both, color=startCol, style=lStyle, width=lineWidth)
    if showLbl
        label.new(bar_index, high, dayName(curDow), style=label.style_label_down, color=color.new(color.gray, 20), textcolor=color.white, size=size.small)
 
if endPrev
    line.new(bar_index - 1, low[1], bar_index - 1, high[1] + syminfo.mintick, extend=extend.both, color=endCol, style=lStyle, width=lineWidth)
 
// End line for the latest (current) day, once the closing candle is in
var line todayEnd = na
if barstate.islast
    line.delete(todayEnd)
    todayEnd := na
    if isIntraday and inRange and endOn(curDow) and closeMin >= endTotal
        todayEnd := line.new(bar_index, low, bar_index, high + syminfo.mintick, extend=extend.both, color=endCol, style=lStyle, width=lineWidth)
 
dayAny = startOn(curDow) or endOn(curDow)
inSess = isIntraday and inRange and dayAny and not na(time(timeframe.period, sessIn, tz))
bgcolor(showBg and inSess ? bgCol : na, title="Market session")
 
if not isIntraday and barstate.islast
    label.new(bar_index, high, "Switch to an intraday timeframe (5m - 1h)", color=color.orange, textcolor=color.black, style=label.style_label_down)

← 返回列表