Bar Counter From Date/Time with MaxNum

toicecream · study · 41 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("Bar Counter From Date/Time", overlay=true)
 
// ===== Inputs =====
startDate  = input.time(timestamp("2024-01-01 00:00"), "Start Date/Time")
textColor  = input.color(color.white, "Text Color")
textSize   = input.string("normal", "Font Size", options=["tiny", "small", "normal", "large", "huge"])
showEvery  = input.int(1, "Show Every N Bars", minval=1)
maxBars    = input.int(50, "Max Number of Counted Bars to Display", minval=1, tooltip="只显示从起始时间开始计数的前 N 个 Bar 的标签")
 
// ===== Bar Counter =====
var int barCount = na
 
if time >= startDate
    barCount := na(barCount) ? 1 : barCount + 1
 
// ===== Label Size Mapping =====
labelSize = switch textSize
    "tiny"   => size.tiny
    "small"  => size.small
    "normal" => size.normal
    "large"  => size.large
    "huge"   => size.huge
    => size.normal
 
// ===== Display =====
// 仅当计数存在、计数未超过上限、并且满足间隔条件时绘制标签
if not na(barCount) and barCount <= maxBars and barCount % showEvery == 0
    label.new(
         x         = bar_index,
         y         = high,
         text      = str.tostring(barCount),
         xloc      = xloc.bar_index,
         yloc      = yloc.belowbar,
         color     = color.new(color.black, 100),
         textcolor = textColor,
         size      = labelSize,
         style     = label.style_label_down)
 
// ===== Optional: show current count in Data Window =====
plot(barCount, "Bar Count", display=display.data_window)

← 返回列表