ORB Break & Retest - 15m v1.1

QuantCrawler · study · 179 行 · 点赞 2,571 · TradingView 原页

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

Pine Script

//@version=6
indicator("ORB Break & Retest - 15m v1.1", overlay=true)
 
// === INPUTS ===
breakout_distance = input.float(2.0, "Breakout Distance (Points)", minval=0.0, maxval=1000.0, 
     tooltip="Distance in points/dollars outside OR zone to trigger breakout")
 
session_timezone = input.string("America/New_York", "Timezone", 
     options=["America/New_York", "Europe/London", "Asia/Tokyo", "Asia/Hong_Kong", "America/Chicago"],
     tooltip="Select your trading session timezone")
 
or_session = input.session("0800-0815", "Opening Range Time", 
     tooltip="First 15 minutes to capture OR (default: 8:00-8:15 premarket)")
 
session_end = input.session("1600-1600", "Session End Time",
     tooltip="When to stop monitoring for signals (default: 4:00 PM)")
 
// === TIME DETECTION ===
bar_hour = hour(time, session_timezone)
bar_minute = minute(time, session_timezone)
 
// Parse OR session string (format: "HHMM-HHMM")
or_start_str = str.substring(or_session, 0, 2) + str.substring(or_session, 2, 4)
or_start_hour = str.tonumber(str.substring(or_session, 0, 2))
or_start_minute = str.tonumber(str.substring(or_session, 2, 4))
or_end_hour = str.tonumber(str.substring(or_session, 5, 7))
or_end_minute = str.tonumber(str.substring(or_session, 7, 9))
 
// Parse session end string
session_end_hour = str.tonumber(str.substring(session_end, 0, 2))
session_end_minute = str.tonumber(str.substring(session_end, 2, 4))
 
// Check if we're at OR capture time (end of OR period)
is_capture_time = bar_hour == or_end_hour and bar_minute == or_end_minute
 
// Check if we're at OR start time (for line drawing)
is_or_start = bar_hour == or_start_hour and bar_minute == or_start_minute
 
// Trading hours = after OR ends until session close
is_after_or = (bar_hour > or_end_hour) or (bar_hour == or_end_hour and bar_minute >= or_end_minute)
is_before_close = (bar_hour < session_end_hour) or (bar_hour == session_end_hour and bar_minute < session_end_minute)
is_trading_hours = is_after_or and is_before_close
 
// === STATE VARIABLES ===
var float or_high = na
var float or_low = na
var float or_mid = na
var bool long_breakout = false
var bool short_breakout = false
var bool long_ready = false
var bool short_ready = false
var int or_start_bar = na  // Track bar index at 9:30
 
// === ALERT CONDITIONS ===
var bool long_signal = false
var bool short_signal = false
 
// === LINES ===
var line or_high_line = na
var line or_low_line = na
var line or_mid_line = na
 
// Reset at new day
if ta.change(dayofmonth) != 0
    line.delete(or_high_line)
    line.delete(or_low_line)
    line.delete(or_mid_line)
    
    or_high := na
    or_low := na
    or_mid := na
    long_breakout := false
    short_breakout := false
    long_ready := false
    short_ready := false
    or_start_bar := na
 
// Capture bar index at OR start time
if is_or_start and na(or_start_bar)
    or_start_bar := bar_index
 
// === CAPTURE OR FROM 15M DATA ===
if is_capture_time and na(or_high)
    // Pull the completed 15m candle (9:30-9:45) at 9:45
    [high_15m, low_15m] = request.security(syminfo.tickerid, "15", [high[1], low[1]], lookahead=barmerge.lookahead_on)
    
    or_high := high_15m
    or_low := low_15m
    or_mid := (or_high + or_low) / 2
 
// === DRAW OR ZONE ===
if not na(or_high) and not na(or_start_bar)
    if na(or_high_line)
        // Draw lines starting at 9:30 bar
        or_high_line := line.new(or_start_bar, or_high, bar_index, or_high, color=color.green, width=3)
        or_low_line := line.new(or_start_bar, or_low, bar_index, or_low, color=color.red, width=3)
        or_mid_line := line.new(or_start_bar, or_mid, bar_index, or_mid, color=color.orange, width=2, style=line.style_dotted)
    else
        line.set_x2(or_high_line, bar_index)
        line.set_x2(or_low_line, bar_index)
        line.set_x2(or_mid_line, bar_index)
 
// Reset signal flags each bar
long_signal := false
short_signal := false
 
// === BREAKOUT LOGIC ===
if not na(or_high) and is_trading_hours
    or_range = or_high - or_low
    
    // Check for breakout (close price beyond zone + fixed distance)
    if not long_breakout and close >= or_high + breakout_distance
        long_breakout := true
    
    if not short_breakout and close <= or_low - breakout_distance
        short_breakout := true
    
    // === LONG ENTRY ===
    if long_breakout
        // Price moved away from midpoint
        if close > or_mid
            long_ready := true
        
        // Price touched midpoint - ENTRY
        if long_ready and low <= or_mid
            label.new(bar_index, low, "🟢 LONG", 
                 color=color.new(color.green, 0), textcolor=color.white, 
                 style=label.style_label_up, size=size.small)
            
            long_signal := true
            
            // RESET flags so we can detect next breakout
            long_breakout := false
            long_ready := false
    
    // === SHORT ENTRY ===
    if short_breakout
        // Price moved away from midpoint
        if close < or_mid
            short_ready := true
        
        // Price touched midpoint - ENTRY
        if short_ready and high >= or_mid
            label.new(bar_index, high, "🔴 SHORT", 
                 color=color.new(color.red, 0), textcolor=color.white, 
                 style=label.style_label_down, size=size.small)
            
            short_signal := true
            
            // RESET flags so we can detect next breakout
            short_breakout := false
            short_ready := false
 
// === ALERTS ===
alertcondition(long_signal, title="Long Entry", message="ORB 15m Long Entry Signal - Price retested midpoint after bullish breakout")
alertcondition(short_signal, title="Short Entry", message="ORB 15m Short Entry Signal - Price retested midpoint after bearish breakout")
alertcondition(long_signal or short_signal, title="Any Entry", message="ORB 15m Entry Signal Triggered")
 
// === STATUS BOX ===
var table status = table.new(position.bottom_right, 1, 1, bgcolor=color.new(color.black, 10), border_width=2)
 
if barstate.islast and not na(or_high)
    status_text = ""
    status_color = color.gray
    
    if not is_trading_hours
        status_text := "⏸️ Closed"
        status_color := color.gray
        
    else if long_breakout or short_breakout
        status_text := "✅ Active | Entry: " + str.tostring(or_mid, format.mintick)
        status_color := color.orange
        
    else
        status_text := "👀 Watching"
        status_color := color.gray
    
    table.cell(status, 0, 0, status_text, text_color=color.white, 
         bgcolor=color.new(status_color, 70), text_size=size.small)

← 返回列表