ORB - Opening Range Breakout + Alerts

VeroTradeX · study · 116 行 · 点赞 2,354 · TradingView 原页

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

Pine Script

//@version=6
indicator("ORB - Opening Range Breakout + Alerts", overlay=true)
 
// User inputs for customization
rangeDuration = input.int(15, "Opening Range Duration (Minutes)", minval=1)
highColor = input.color(color.new(#ff00dd, 0), "High Ray Color") // Bright pink/purple for opening range high
highThickness = input.int(3, "High Ray Thickness", minval=1) // Thickness 3
lowColor = input.color(color.new(#ff00dd, 0), "Low Ray Color") // Bright pink/purple for opening range low
lowThickness = input.int(3, "Low Ray Thickness", minval=1) // Thickness 3
showAlerts = input.bool(true, "Show Breakout Alerts") // Toggle for showing breakout alerts
 
// Define regular session (Eastern Time, 9:30 AM to 4:00 PM, all days of the week)
var string regularSession = "0930-1600:1234567"
 
// Check if the current bar is in the regular session
isRegular = not na(time(timeframe.period, regularSession))
 
// Calculate the opening range period (e.g., 9:30 AM to 9:45 AM for 15 minutes)
var int sessionStartTime = 0
var int sessionEndTime = 0
isNewDay = dayofmonth != dayofmonth[1] // Explicitly check for day change
if isNewDay
    sessionStartTime := timestamp(year, month, dayofmonth, 9, 30)
    sessionEndTime := sessionStartTime + (rangeDuration * 60 * 1000) // Convert minutes to milliseconds
 
// Check if the current bar is within the opening range period
isInOpeningRange = time >= sessionStartTime and time < sessionEndTime and isRegular
 
// Variables to store the opening range high, low, and their bar indices
var float openingHigh = na
var float openingLow = na
var int highBarIndex = na
var int lowBarIndex = na
 
// Persistent lines for the rays
var line highRay = na
var line lowRay = na
 
// Persistent label for breakout alerts
var label breakoutLabel = na
 
// Variables to track breakout triggers
var bool longTriggered = false
var bool shortTriggered = false
 
// Reset opening range high/low, bar indices, and breakout triggers at the start of a new day
if isNewDay
    openingHigh := na
    openingLow := na
    highBarIndex := na
    lowBarIndex := na
    longTriggered := false
    shortTriggered := false
    line.delete(highRay)
    line.delete(lowRay)
    label.delete(breakoutLabel)
 
// Update opening range high/low and their bar indices during the opening range period
if isInOpeningRange
    if na(openingHigh) or high > openingHigh
        openingHigh := high
        highBarIndex := bar_index
    if na(openingLow) or low < openingLow
        openingLow := low
        lowBarIndex := bar_index
 
// At the end of the opening range period, draw rays for the opening range high and low
isOpeningRangeEnd = ta.change(isInOpeningRange) and not isInOpeningRange
if isOpeningRangeEnd
    // Delete previous rays
    line.delete(highRay)
    line.delete(lowRay)
    
    // Debug: Add a label if data is missing
    if na(openingHigh) or na(openingLow) or na(highBarIndex) or na(lowBarIndex)
        label.new(bar_index, high, "Missing Data for Opening Range", color=color.red, style=label.style_label_down, textcolor=color.white)
    
    // Draw new rays starting at the candles where the high and low occurred
    if not na(openingHigh) and not na(highBarIndex)
        highRay := line.new(highBarIndex, openingHigh, bar_index + 500, openingHigh, color=highColor, width=highThickness)
    if not na(openingLow) and not na(lowBarIndex)
        lowRay := line.new(lowBarIndex, openingLow, bar_index + 500, openingLow, color=lowColor, width=lowThickness)
 
// Fetch the 5-minute close, high, and low prices to detect breakouts
[close_5m, high_5m, low_5m] = request.security(syminfo.tickerid, "5", [close, high, low], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
 
// Calculate crossover and crossunder globally
longBreakoutSignal = ta.crossover(close_5m, openingHigh)
shortBreakoutSignal = ta.crossunder(close_5m, openingLow)
 
// Detect breakouts after the opening range period
var bool longBreakout = false
var bool shortBreakout = false
isFiveMinuteTimeframe = timeframe.period == "5"
if not isInOpeningRange and time >= sessionEndTime and not na(openingHigh) and not na(openingLow)
    // Check for LONG breakout (5m candle closes above opening range high)
    if longBreakoutSignal and not longTriggered
        longTriggered := true
        shortTriggered := false
        longBreakout := true
        if showAlerts and isFiveMinuteTimeframe
            label.delete(breakoutLabel)
            breakoutLabel := label.new(bar_index - 3, high_5m, "LONG", color=color.green, textcolor=color.white, style=label.style_label_right, size=size.large)
    
    // Check for SHORT breakout (5m candle closes below opening range low)
    if shortBreakoutSignal and not shortTriggered
        shortTriggered := true
        longTriggered := false
        shortBreakout := true
        if showAlerts and isFiveMinuteTimeframe
            label.delete(breakoutLabel)
            breakoutLabel := label.new(bar_index - 3, low_5m, "SHORT", color=color.red, textcolor=color.white, style=label.style_label_right, size=size.large)
 
// Define alert conditions in global scope
alertcondition(showAlerts and isFiveMinuteTimeframe and longBreakout, title="ORB Long Breakout", message="Opening Range Breakout: LONG at {{close}}")
alertcondition(showAlerts and isFiveMinuteTimeframe and shortBreakout, title="ORB Short Breakout", message="Opening Range Breakout: SHORT at {{close}}")

← 返回列表