本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Simple & Clean ORB", overlay = true, max_lines_count = 500)
//──────────────────────────────────────────────────────────────────────────────
// RANGE SETTINGS
//──────────────────────────────────────────────────────────────────────────────
groupRange = "Opening Range"
rangeMode = input.string(
"Duration from Start",
"Range Mode",
options = ["Duration from Start", "Custom Range"],
group = groupRange)
startHour = input.int(
8,
"Start Hour",
minval = 0,
maxval = 23,
group = groupRange,
inline = "START")
startMinute = input.int(
30,
"Start Minute",
minval = 0,
maxval = 59,
group = groupRange,
inline = "START")
durationMinutes = input.int(
30,
"Time Period",
options = [2, 3, 5, 8, 10, 15, 30, 45, 60, 120, 180, 240, 480],
group = groupRange)
customRange = input.session(
"0830-0900",
"Custom Range",
group = groupRange)
timezoneInput = input.string(
"America/Chicago",
"Time Zone",
options = [
"Exchange",
"America/New_York",
"America/Chicago",
"America/Denver",
"America/Los_Angeles",
"UTC-10",
"UTC-8",
"UTC-7",
"UTC-6",
"UTC-5",
"UTC-4",
"UTC-3",
"UTC+0",
"UTC+1",
"UTC+2",
"UTC+3",
"UTC+03:30",
"UTC+4",
"UTC+5",
"UTC+05:30",
"UTC+6",
"UTC+7",
"UTC+8",
"UTC+9",
"UTC+10",
"UTC+11",
"UTC+12"
],
group = groupRange)
tz = timezoneInput == "Exchange" ? syminfo.timezone : timezoneInput
showHistorical = input.bool(
true,
"Show Historical Data",
group = groupRange)
//──────────────────────────────────────────────────────────────────────────────
// HIGH LINE SETTINGS
//──────────────────────────────────────────────────────────────────────────────
groupHigh = "ORB High"
highColor = input.color(
color.white,
"Color",
group = groupHigh,
inline = "HIGH")
highWidth = input.int(
2,
"Thickness",
minval = 1,
maxval = 5,
group = groupHigh,
inline = "HIGH")
highStyleInput = input.string(
"Solid",
"Style",
options = ["Solid", "Dashed", "Dotted"],
group = groupHigh)
//──────────────────────────────────────────────────────────────────────────────
// MID LINE SETTINGS
//──────────────────────────────────────────────────────────────────────────────
groupMid = "ORB Midpoint"
showMid = input.bool(
true,
"Show Midpoint",
group = groupMid)
midColor = input.color(
color.gray,
"Color",
group = groupMid,
inline = "MID")
midWidth = input.int(
1,
"Thickness",
minval = 1,
maxval = 5,
group = groupMid,
inline = "MID")
midStyleInput = input.string(
"Dashed",
"Style",
options = ["Solid", "Dashed", "Dotted"],
group = groupMid)
//──────────────────────────────────────────────────────────────────────────────
// LOW LINE SETTINGS
//──────────────────────────────────────────────────────────────────────────────
groupLow = "ORB Low"
lowColor = input.color(
color.white,
"Color",
group = groupLow,
inline = "LOW")
lowWidth = input.int(
2,
"Thickness",
minval = 1,
maxval = 5,
group = groupLow,
inline = "LOW")
lowStyleInput = input.string(
"Solid",
"Style",
options = ["Solid", "Dashed", "Dotted"],
group = groupLow)
//──────────────────────────────────────────────────────────────────────────────
// HELPERS
//──────────────────────────────────────────────────────────────────────────────
getLineStyle(styleInput) =>
styleInput == "Dashed" ? line.style_dashed :
styleInput == "Dotted" ? line.style_dotted :
line.style_solid
highLineStyle = getLineStyle(highStyleInput)
midLineStyle = getLineStyle(midStyleInput)
lowLineStyle = getLineStyle(lowStyleInput)
//──────────────────────────────────────────────────────────────────────────────
// RANGE DETECTION
//──────────────────────────────────────────────────────────────────────────────
// Duration mode: build today's opening-range timestamps in the selected timezone.
barYear = year(time, tz)
barMonth = month(time, tz)
barDay = dayofmonth(time, tz)
rangeStart = timestamp(tz, barYear, barMonth, barDay, startHour, startMinute, 0)
rangeEnd = rangeStart + durationMinutes * 60 * 1000
inDurationRange = time >= rangeStart and time < rangeEnd
// Custom mode: TradingView session picker determines start and end.
inCustomRange = not na(time(timeframe.period, customRange, tz))
inRange = rangeMode == "Duration from Start" ? inDurationRange : inCustomRange
newRange = inRange and not inRange[1]
//──────────────────────────────────────────────────────────────────────────────
// ORB CALCULATION + DRAWING
//──────────────────────────────────────────────────────────────────────────────
var float orbHigh = na
var float orbLow = na
var line currentHighLine = na
var line currentMidLine = na
var line currentLowLine = na
if newRange
// Finish or remove the previous range.
if not na(currentHighLine)
if showHistorical
line.set_extend(currentHighLine, extend.none)
line.set_x2(currentHighLine, bar_index - 1)
else
line.delete(currentHighLine)
if not na(currentMidLine)
if showHistorical
line.set_extend(currentMidLine, extend.none)
line.set_x2(currentMidLine, bar_index - 1)
else
line.delete(currentMidLine)
if not na(currentLowLine)
if showHistorical
line.set_extend(currentLowLine, extend.none)
line.set_x2(currentLowLine, bar_index - 1)
else
line.delete(currentLowLine)
// Reset the new opening range.
orbHigh := high
orbLow := low
orbMid = (orbHigh + orbLow) / 2.0
currentHighLine := line.new(
bar_index,
orbHigh,
bar_index + 1,
orbHigh,
extend = extend.right,
color = highColor,
style = highLineStyle,
width = highWidth)
if showMid
currentMidLine := line.new(
bar_index,
orbMid,
bar_index + 1,
orbMid,
extend = extend.right,
color = midColor,
style = midLineStyle,
width = midWidth)
else
currentMidLine := na
currentLowLine := line.new(
bar_index,
orbLow,
bar_index + 1,
orbLow,
extend = extend.right,
color = lowColor,
style = lowLineStyle,
width = lowWidth)
else if inRange
// Keep updating the range until the opening-range window closes.
orbHigh := math.max(orbHigh, high)
orbLow := math.min(orbLow, low)
orbMid = (orbHigh + orbLow) / 2.0
if not na(currentHighLine)
line.set_y1(currentHighLine, orbHigh)
line.set_y2(currentHighLine, orbHigh)
if showMid and not na(currentMidLine)
line.set_y1(currentMidLine, orbMid)
line.set_y2(currentMidLine, orbMid)
if not na(currentLowLine)
line.set_y1(currentLowLine, orbLow)
line.set_y2(currentLowLine, orbLow)