本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Rounded Numbers", overlay = true, max_lines_count = 500)
// Settings groups
const string GROUP_LEVELS = "Level Calculation"
const string GROUP_REGULAR = "Regular Levels"
const string GROUP_HIGHLIGHT = "Highlighted Levels"
const string GROUP_DISPLAY = "Display"
// Level calculation
stepSize = input.float(
50.0,
"Step size",
minval = 0.00000001,
step = 0.01,
group = GROUP_LEVELS
)
offsetLevels = input.int(
2,
"Offset (extra levels above/below)",
minval = 0,
maxval = 250,
group = GROUP_LEVELS
)
// Regular level style
styleInput = input.string(
"Dashed",
"Line style",
options = ["Solid", "Dashed", "Dotted"],
group = GROUP_REGULAR
)
lineWidth = input.int(1, "Line width", minval = 1, maxval = 5, group = GROUP_REGULAR)
lineColor = input.color(color.gray, "Line color", group = GROUP_REGULAR)
// Highlighted level style
highlightEvery = input.float(
100.0,
"Highlight every",
minval = 0.00000001,
step = 0.01,
group = GROUP_HIGHLIGHT
)
highlightStyleInput = input.string(
"Solid",
"Line style",
options = ["Solid", "Dashed", "Dotted"],
group = GROUP_HIGHLIGHT
)
highlightWidth = input.int(1, "Line width", minval = 1, maxval = 5, group = GROUP_HIGHLIGHT)
highlightColor = input.color(color.gray, "Line color", group = GROUP_HIGHLIGHT)
// Display
maxLines = input.int(250, "Maximum lines", minval = 1, maxval = 500, group = GROUP_DISPLAY)
// Convert the selected options to Pine Script line styles
string lineStyle = switch styleInput
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_solid
string highlightStyle = switch highlightStyleInput
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_solid
// Store drawn lines and the visible price range
var array<line> levelLines = array.new<line>()
var float visibleLow = na
var float visibleHigh = na
// Find the high and low of the visible bars
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
visibleLow := na(visibleLow) ? low : math.min(visibleLow, low)
visibleHigh := na(visibleHigh) ? high : math.max(visibleHigh, high)
// Draw levels within the visible range, including the offset
if barstate.islast
for levelLine in levelLines
line.delete(levelLine)
array.clear(levelLines)
if not na(visibleLow) and not na(visibleHigh)
int firstIndex = int(math.ceil(visibleLow / stepSize)) - offsetLevels
int lastIndex = int(math.floor(visibleHigh / stepSize)) + offsetLevels
int totalLevels = lastIndex - firstIndex + 1
if totalLevels > 0
int linesToDraw = math.min(totalLevels, maxLines)
int startOffset = int(math.floor((totalLevels - linesToDraw) / 2.0))
for i = 0 to linesToDraw - 1
float priceLevel = (firstIndex + startOffset + i) * stepSize
float nearestHighlight = math.round(priceLevel / highlightEvery) * highlightEvery
float tolerance = math.max(math.abs(priceLevel), highlightEvery) * 1e-10
bool isHighlighted = math.abs(priceLevel - nearestHighlight) <= tolerance
color currentColor = isHighlighted ? highlightColor : lineColor
string currentStyle = isHighlighted ? highlightStyle : lineStyle
int currentWidth = isHighlighted ? highlightWidth : lineWidth
line newLine = line.new(
x1 = chart.left_visible_bar_time,
y1 = priceLevel,
x2 = chart.right_visible_bar_time,
y2 = priceLevel,
xloc = xloc.bar_time,
extend = extend.both,
color = currentColor,
style = currentStyle,
width = currentWidth
)
array.push(levelLines, newLine)