本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("ORB With Signals", overlay = true, max_lines_count = 500, max_boxes_count = 500, max_labels_count = 500)
// Opening range
const string RANGE = "Opening Range"
string rangeMode = input.string("Duration from Start", "Range Mode",
options = ["Duration from Start", "Custom Range"], group = RANGE)
int startHour = input.int(8, "Start Hour", minval = 0, maxval = 23, group = RANGE, inline = "START")
int startMinute = input.int(28, "Start Minute", minval = 0, maxval = 59, group = RANGE, inline = "START")
int durationMinutes = input.int(30, "Time Period",
options = [2, 3, 5, 8, 10, 15, 30, 45, 60, 120, 180, 240, 480], group = RANGE)
string customRange = input.session("0830-0900", "Custom Range", group = RANGE)
string 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 = RANGE)
bool showHistorical = input.bool(true, "Show Historical Data", group = RANGE)
const string SIGNALS = "Signals"
int signalEndHour = input.int(15, "End Hour", minval = 0, maxval = 23, group = SIGNALS, inline = "END")
int signalEndMinute = input.int(0, "End Minute", minval = 0, maxval = 59, group = SIGNALS, inline = "END",
tooltip = "Uses the ORB time zone. An end time earlier than the range start falls on the next day.")
string markerStyle = input.string("Label", "Marker Style", options = ["Label", "Dot", "Arrow"], group = SIGNALS)
string markerSize = input.string(size.small, "Marker Size",
options = [size.tiny, size.small, size.normal, size.large, size.huge], group = SIGNALS)
// ORB appearance
const string HIGH = "ORB High"
color highColor = input.color(color.white, "Color", group = HIGH, inline = "HIGH")
int highWidth = input.int(2, "Thickness", minval = 1, maxval = 5, group = HIGH, inline = "HIGH")
string highStyle = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = HIGH)
const string MID = "ORB Midpoint"
bool showMid = input.bool(true, "Show Midpoint", group = MID)
color midColor = input.color(color.gray, "Color", group = MID, inline = "MID")
int midWidth = input.int(2, "Thickness", minval = 1, maxval = 5, group = MID, inline = "MID")
string midStyle = input.string("Dashed", "Style", options = ["Solid", "Dashed", "Dotted"], group = MID)
const string LOW = "ORB Low"
color lowColor = input.color(color.white, "Color", group = LOW, inline = "LOW")
int lowWidth = input.int(2, "Thickness", minval = 1, maxval = 5, group = LOW, inline = "LOW")
string lowStyle = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = LOW)
const string RANGE_BOX = "ORB Range Box"
bool showRangeBox = input.bool(true, "Show ORB Box", group = RANGE_BOX)
color boxColor = input.color(color.gray, "Box Color", group = RANGE_BOX)
int boxTransparency = input.int(65, "Box Transparency", minval = 0, maxval = 100, group = RANGE_BOX)
const string WADDAH = "Waddah Momentum"
int sensitivity = input.int(80, "Sensitivity", minval = 1, group = WADDAH)
int fastLength = input.int(8, "Fast EMA Length", minval = 1, group = WADDAH)
int slowLength = input.int(21, "Slow EMA Length", minval = 1, group = WADDAH)
string signalDisplay = input.string("Both", "Show Signals", options = ["Green only", "Yellow only", "Both"], group = SIGNALS)
color greenMarkerColor = input.color(#187B42, "Green Marker Color", group = SIGNALS)
color yellowMarkerColor = input.color(#F4C542, "Yellow Marker Color", group = SIGNALS)
string greenLabelText = input.string("Green Light", "Green Label Text", group = SIGNALS,
tooltip = "Text or emoji shown on Green signals when Marker Style is Label.")
string yellowLabelText = input.string("Yellow Light", "Yellow Label Text", group = SIGNALS,
tooltip = "Text or emoji shown on Yellow signals when Marker Style is Label.")
getLineStyle(string styleName) =>
switch styleName
"Dashed" => line.style_dashed
"Dotted" => line.style_dotted
=> line.style_solid
finishLine(line rangeLine, bool keepHistory) =>
if not na(rangeLine)
if keepHistory
line.set_extend(rangeLine, extend.none)
line.set_x2(rangeLine, bar_index - 1)
else
line.delete(rangeLine)
getSignalEndTime(int rangeAnchor, string zone) =>
int rangeYear = year(rangeAnchor, zone)
int rangeMonth = month(rangeAnchor, zone)
int rangeDay = dayofmonth(rangeAnchor, zone)
int cutoff = timestamp(zone, rangeYear, rangeMonth, rangeDay, signalEndHour, signalEndMinute, 0)
if cutoff < rangeAnchor
// Start from local noon so a daylight-saving change cannot move the result to the wrong date.
int nextDay = timestamp(zone, rangeYear, rangeMonth, rangeDay, 12, 0, 0) + 24 * 60 * 60 * 1000
cutoff := timestamp(zone, year(nextDay, zone), month(nextDay, zone), dayofmonth(nextDay, zone),
signalEndHour, signalEndMinute, 0)
cutoff
// Preserve the base ORB's bar-open inclusion rule in Duration from Start mode.
string tz = timezoneInput == "Exchange" ? syminfo.timezone : timezoneInput
int rangeStart = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), startHour, startMinute, 0)
int rangeEnd = rangeStart + durationMinutes * 60 * 1000
bool inDurationRange = time >= rangeStart and time < rangeEnd
bool inCustomRange = not na(time(timeframe.period, customRange, tz))
bool inRange = rangeMode == "Duration from Start" ? inDurationRange : inCustomRange
bool newRange = inRange and not inRange[1]
var float orbHigh = na
var float orbLow = na
var int signalEndTime = na
var line currentHighLine = na
var line currentMidLine = na
var line currentLowLine = na
var box currentRangeBox = na
var array<label> currentSignalLabels = array.new<label>()
if newRange
finishLine(currentHighLine, showHistorical)
finishLine(currentMidLine, showHistorical)
finishLine(currentLowLine, showHistorical)
if not showHistorical
if not na(currentRangeBox)
box.delete(currentRangeBox)
if barstate.isconfirmed
for previousLabel in currentSignalLabels
label.delete(previousLabel)
array.clear(currentSignalLabels)
orbHigh := high
orbLow := low
signalEndTime := getSignalEndTime(rangeMode == "Duration from Start" ? rangeStart : time, tz)
float midpoint = (orbHigh + orbLow) / 2.0
currentHighLine := line.new(bar_index, orbHigh, bar_index + 1, orbHigh,
extend = extend.right, color = highColor, style = getLineStyle(highStyle), width = highWidth)
if showMid
currentMidLine := line.new(bar_index, midpoint, bar_index + 1, midpoint,
extend = extend.right, color = midColor, style = getLineStyle(midStyle), width = midWidth)
currentLowLine := line.new(bar_index, orbLow, bar_index + 1, orbLow,
extend = extend.right, color = lowColor, style = getLineStyle(lowStyle), width = lowWidth)
if showRangeBox
currentRangeBox := box.new(bar_index, orbHigh, bar_index + 1, orbLow,
border_color = color.new(boxColor, 100), bgcolor = color.new(boxColor, boxTransparency))
else if inRange
orbHigh := math.max(orbHigh, high)
orbLow := math.min(orbLow, low)
float midpoint = (orbHigh + orbLow) / 2.0
line.set_y1(currentHighLine, orbHigh)
line.set_y2(currentHighLine, orbHigh)
if showMid
line.set_y1(currentMidLine, midpoint)
line.set_y2(currentMidLine, midpoint)
line.set_y1(currentLowLine, orbLow)
line.set_y2(currentLowLine, orbLow)
if showRangeBox
box.set_top(currentRangeBox, orbHigh)
box.set_bottom(currentRangeBox, orbLow)
box.set_right(currentRangeBox, bar_index + 1)
// Grades use the signed Waddah trend histogram. The Bollinger settings
// affect Waddah's separate explosion line.
float macd = ta.ema(close, fastLength) - ta.ema(close, slowLength)
float momentum = ta.change(macd) * sensitivity
bool hasSignalData = not na(volume) and not na(volume[1]) and not na(momentum) and not na(momentum[1])
bool volumeRising = hasSignalData and volume > volume[1]
bool momentumGrowing = hasSignalData and math.abs(momentum) > math.abs(momentum[1])
float body = math.abs(close - open)
float upperWick = high - math.max(open, close)
float lowerWick = math.min(open, close) - low
// Compare both closes with the same locked edge. A close at or across that edge
// re-arms it, and a full-range reversal can trigger the opposite side.
// The cutoff belongs to the active range, so a later calendar day cannot reopen it.
bool withinSignalWindow = not na(signalEndTime) and time_close <= signalEndTime
bool rangeReady = not inRange and not na(orbHigh) and not na(orbLow) and withinSignalWindow
bool breaksHigh = rangeReady and close > orbHigh and close[1] <= orbHigh
bool breaksLow = rangeReady and close < orbLow and close[1] >= orbLow
bool alignedLong = breaksHigh and hasSignalData and momentum > 0
bool alignedShort = breaksLow and hasSignalData and momentum < 0
bool alignedBreak = alignedLong or alignedShort
bool confirmationsGrowing = volumeRising and momentumGrowing
bool wickPass = (alignedLong ? upperWick : lowerWick) <= body
bool greenSignal = alignedBreak and confirmationsGrowing and wickPass
// Yellow covers mixed confirmation with an acceptable wick, or a long wick with both confirmations.
bool yellowSignal = alignedBreak and not greenSignal and (confirmationsGrowing or wickPass)
bool displayGreen = greenSignal and signalDisplay != "Yellow only"
bool displayYellow = yellowSignal and signalDisplay != "Green only"
if barstate.isconfirmed and (displayGreen or displayYellow)
string signalStyle = switch markerStyle
"Dot" => label.style_circle
"Arrow" => alignedLong ? label.style_arrowup : label.style_arrowdown
=> alignedLong ? label.style_label_up : label.style_label_down
string labelText = displayGreen ? greenLabelText : yellowLabelText
string signalText = markerStyle == "Label" ? labelText : ""
color signalColor = displayGreen ? greenMarkerColor : yellowMarkerColor
// Choose readable text against the marker fill and any visible chart background.
color chartBackground = chart.bg_color
float opacity = 1.0 - color.t(signalColor) / 100.0
float markerBrightness = 0.299 * color.r(signalColor) + 0.587 * color.g(signalColor) + 0.114 * color.b(signalColor)
float chartBrightness = 0.299 * color.r(chartBackground) + 0.587 * color.g(chartBackground) + 0.114 * color.b(chartBackground)
float visibleBrightness = markerBrightness * opacity + chartBrightness * (1.0 - opacity)
color signalTextColor = visibleBrightness >= 140.0 ? color.black : color.white
label signalLabel = label.new(bar_index, alignedLong ? low : high, signalText,
yloc = alignedLong ? yloc.belowbar : yloc.abovebar,
style = signalStyle, color = signalColor, textcolor = signalTextColor, size = markerSize)
if not showHistorical
array.push(currentSignalLabels, signalLabel)