Opening Range Breakout (TheKatalyst)

TheKatalyst · study · 90 行 · 点赞 363 · TradingView 原页

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

Pine Script

//@version=6
indicator('Opening Range Breakout', overlay = true)
 
// User input for the opening range duration
rangeOption = input.string('1m', title = 'Opening Range Duration', options = ['30s', '1m', '2m', '5m', '10m', '15m'])
 
// User inputs for line and label customization
lineColor = input.color(color.purple, title = 'Line Color')
lineTransparency = input.int(50, title = 'Line Transparency', minval = 0, maxval = 100)
lineStyleOption = input.string('Solid', title = 'Line Style', options = ['Solid', 'Dashed', 'Dotted'])
labelColorHigh = input.color(color.green, title = 'Label High Color')
labelColorLow = input.color(color.red, title = 'Label Low Color')
labelTransparency = input.int(0, title = 'Label Transparency', minval = 0, maxval = 100)
 
// No-Trade Zone inputs
enableNoTradeZone = input.bool(false, title = 'Enable No Trade Zone')
noTradeZoneColor = input.color(color.red, title = 'No Trade Zone Color')
noTradeZoneTransparency = input.int(90, title = 'No Trade Zone Transparency', minval = 0, maxval = 100)
 
// Map line style input
lineStyle = lineStyleOption == 'Solid' ? line.style_solid : lineStyleOption == 'Dashed' ? line.style_dashed : line.style_dotted
 
// Define session start time (8:30 AM CST)
sessionStartTime = timestamp('GMT-6', year, month, dayofmonth, 8, 30, 0)
 
// Convert user-selected duration to milliseconds
rangeDurationSec = rangeOption == '30s' ? 30 : rangeOption == '1m' ? 60 : rangeOption == '2m' ? 120 : rangeOption == '5m' ? 300 : rangeOption == '10m' ? 600 : 900 // 15 minutes = 900 seconds
 
sessionEndTime = sessionStartTime + rangeDurationSec * 1000
 
// Map user selection to Pine Script timeframe string
tf = rangeOption == '30s' ? '30S' : rangeOption == '1m' ? '1' : rangeOption == '2m' ? '2' : rangeOption == '5m' ? '5' : rangeOption == '10m' ? '10' : '15' // 15-minute timeframe
 
// Fetch high and low for the selected timeframe using `request.security`
openingRangeHigh = request.security(syminfo.tickerid, tf, high[0], lookahead = barmerge.lookahead_on)
openingRangeLow = request.security(syminfo.tickerid, tf, low[0], lookahead = barmerge.lookahead_on)
 
// Variables to store range lines, labels, and range values
var line upperLine = na
var line lowerLine = na
var linefill fillNoTradeZone = na
var float rangeHighFinal = na
var float rangeLowFinal = na
var label highLabel = na
var label lowLabel = na
 
// Check if we are within the opening range window
isOpeningRange = time >= sessionStartTime and time < sessionEndTime
 
// Update the range high and low during the window
if isOpeningRange
    rangeHighFinal := na(rangeHighFinal) ? openingRangeHigh : math.max(rangeHighFinal, openingRangeHigh)
    rangeLowFinal := na(rangeLowFinal) ? openingRangeLow : math.min(rangeLowFinal, openingRangeLow)
    rangeLowFinal
 
// Detect when the opening range window ends
isRangeComplete = time[1] < sessionEndTime and time >= sessionEndTime
 
if isRangeComplete
    // Delete old lines and fill
    if not na(upperLine)
        line.delete(upperLine)
    if not na(lowerLine)
        line.delete(lowerLine)
    if not na(fillNoTradeZone)
        linefill.delete(fillNoTradeZone)
 
    // Draw new lines for the opening range
    upperLine := line.new(bar_index, rangeHighFinal, bar_index + 1, rangeHighFinal, color = color.new(lineColor, lineTransparency), width = 2, extend = extend.right, style = lineStyle)
    lowerLine := line.new(bar_index, rangeLowFinal, bar_index + 1, rangeLowFinal, color = color.new(lineColor, lineTransparency), width = 2, extend = extend.right, style = lineStyle)
 
    // Create the No Trade Zone fill
    if enableNoTradeZone
        fillNoTradeZone := linefill.new(upperLine, lowerLine, color = color.new(noTradeZoneColor, noTradeZoneTransparency))
        fillNoTradeZone
 
    // Delete old labels
    if not na(highLabel)
        label.delete(highLabel)
    if not na(lowLabel)
        label.delete(lowLabel)
 
    // Create new labels
    highLabel := label.new(bar_index, rangeHighFinal, 'OR High: ' + str.tostring(rangeHighFinal), color = color.new(labelColorHigh, labelTransparency), textcolor = color.white, style = label.style_label_down)
    lowLabel := label.new(bar_index, rangeLowFinal, 'OR Low: ' + str.tostring(rangeLowFinal), color = color.new(labelColorLow, labelTransparency), textcolor = color.white, style = label.style_label_up)
 
    // Reset range values for the next session
    rangeHighFinal := na
    rangeLowFinal := na
    rangeLowFinal

← 返回列表