ORB [ASÜ]

asumanixd · study · 454 行 · 点赞 652 · TradingView 原页

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

Pine Script

//@version=6
indicator("ORB [ASÜ]", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=500)
 
//==============================================================================
// INPUTS
//==============================================================================
 
// Timezone Settings
string i_timezone = input.string("GMT-5 (EST)", "Timezone", options=["GMT-5 (EST)", "GMT-4 (EDT)"], group="Timezone", tooltip="GMT-5 for EST (Nov-Mar), GMT-4 for EDT (Mar-Nov)")
 
// ORB Time Settings
int i_orbStartHour = input.int(8, "ORB Start Hour", minval=0, maxval=23, group="ORB Time Settings")
int i_orbStartMin = input.int(0, "ORB Start Minute", minval=0, maxval=59, group="ORB Time Settings")
int i_orbDuration = input.int(15, "ORB Duration (Minutes)", minval=1, maxval=240, group="ORB Time Settings")
int i_extendHour = input.int(11, "Extend To Hour", minval=0, maxval=23, group="ORB Time Settings")
int i_extendMinute = input.int(0, "Extend To Minute", minval=0, maxval=59, group="ORB Time Settings")
 
// Color Settings
color i_borderColor = input.color(#FFD700, "Top/Bottom Line Color", group="Colors")
color i_midlineColor = input.color(#FFD700, "Midline Color", group="Colors")
color i_bgColor = input.color(color.new(#2962FF, 100), "Background Color", group="Colors")
int i_borderWidth = input.int(1, "Border Width", minval=1, maxval=5, group="Colors")
int i_midlineWidth = input.int(1, "Midline Width", minval=1, maxval=5, group="Colors")
string i_midlineStyle = input.string("dotted", "Midline Style", options=["solid", "dashed", "dotted"], group="Colors")
 
// Vertical Line Settings
bool i_showVertLine = input.bool(false, "Show Vertical Line", group="Vertical Line")
int i_vertLineHour = input.int(9, "Vertical Line Hour", minval=0, maxval=23, group="Vertical Line")
int i_vertLineMinute = input.int(30, "Vertical Line Minute", minval=0, maxval=59, group="Vertical Line")
color i_vertLineColor = input.color(color.red, "Vertical Line Color", group="Vertical Line")
string i_vertLineStyle = input.string("dashed", "Vertical Line Style", options=["solid", "dashed", "dotted"], group="Vertical Line")
int i_vertLineWidth = input.int(1, "Vertical Line Width", minval=1, maxval=5, group="Vertical Line")
 
// Display Settings
bool i_showHistorical = input.bool(true, "Show Historical ORBs", group="Display")
string i_maxTimeframe = input.string("15m", "Max Timeframe", options=["1m", "3m", "5m", "15m", "30m", "1H", "2H", "4H"], group="Display")
bool i_showSizeLabel = input.bool(true, "Show ORB Size Label", group="Display")
string i_sizeLabelSize = input.string("normal", "ORB Size Label Size", options=["tiny", "small", "normal", "large", "huge"], group="Display")
 
// Info Panel Settings
bool i_showPanel = input.bool(false, "Show Info Panel", group="Info Panel")
string i_panelSize = input.string("normal", "Panel Text Size", options=["tiny", "small", "normal", "large", "huge"], group="Info Panel")
string i_panelPosV = input.string("top", "Vertical Position", options=["top", "middle", "bottom"], group="Info Panel")
string i_panelPosH = input.string("right", "Horizontal Position", options=["left", "center", "right"], group="Info Panel")
 
// ORB Size Threshold
float i_threshold = input.float(19.5, "Size Warning Threshold (Points)", minval=0, step=0.5, group="ORB Size Settings")
 
//==============================================================================
// TIMEZONE - FIXED
//==============================================================================
 
string tz = i_timezone == "GMT-4 (EDT)" ? "Etc/GMT+4" : "Etc/GMT+5"
 
//==============================================================================
// TIMEFRAME VALIDATION
//==============================================================================
 
int maxTfMins = switch i_maxTimeframe
    "1m" => 1
    "3m" => 3
    "5m" => 5
    "15m" => 15
    "30m" => 30
    "1H" => 60
    "2H" => 120
    "4H" => 240
    => 15
 
int currTfMins = timeframe.isintraday ? timeframe.multiplier : 1440
bool isValidTf = currTfMins <= maxTfMins
 
//==============================================================================
// LINE STYLE FUNCTIONS
//==============================================================================
 
getMidlineStyle() =>
    switch i_midlineStyle
        "solid" => line.style_solid
        "dashed" => line.style_dashed
        "dotted" => line.style_dotted
        => line.style_dashed
 
getVertLineStyle() =>
    switch i_vertLineStyle
        "solid" => line.style_solid
        "dashed" => line.style_dashed
        "dotted" => line.style_dotted
        => line.style_dashed
 
//==============================================================================
// TIME FUNCTIONS
//==============================================================================
 
currHour = hour(time, tz)
currMinute = minute(time, tz)
 
// ORB end time calculation
int orbEndHour = i_orbStartHour
int orbEndMin = i_orbStartMin + i_orbDuration
if orbEndMin >= 60
    orbEndHour := orbEndHour + 1
    orbEndMin := orbEndMin - 60
 
// Check if bar is within ORB formation period
isOrbTime() =>
    h = hour(time, tz)
    m = minute(time, tz)
    totalMins = h * 60 + m
    startMins = i_orbStartHour * 60 + i_orbStartMin
    endMins = startMins + i_orbDuration
    totalMins >= startMins and totalMins < endMins
 
// Check if this is first bar of new ORB session
isNewOrbSession() =>
    h = hour(time, tz)
    m = minute(time, tz)
    h_prev = hour(time[1], tz)
    m_prev = minute(time[1], tz)
    
    totalMins = h * 60 + m
    prevTotalMins = h_prev * 60 + m_prev
    startMins = i_orbStartHour * 60 + i_orbStartMin
    endMins = startMins + i_orbDuration
    
    currentInOrb = totalMins >= startMins and totalMins < endMins
    prevInOrb = prevTotalMins >= startMins and prevTotalMins < endMins
    
    currentInOrb and not prevInOrb
 
// Check if ORB just ended
orbJustEnded() =>
    h = hour(time, tz)
    m = minute(time, tz)
    h_prev = hour(time[1], tz)
    m_prev = minute(time[1], tz)
    
    totalMins = h * 60 + m
    prevTotalMins = h_prev * 60 + m_prev
    startMins = i_orbStartHour * 60 + i_orbStartMin
    endMins = startMins + i_orbDuration
    
    currentInOrb = totalMins >= startMins and totalMins < endMins
    prevInOrb = prevTotalMins >= startMins and prevTotalMins < endMins
    
    not currentInOrb and prevInOrb
 
// Check if we're past the extend time
isPastExtendTime() =>
    h = hour(time, tz)
    m = minute(time, tz)
    totalMins = h * 60 + m
    extendMins = i_extendHour * 60 + i_extendMinute
    totalMins > extendMins
 
// Check if we JUST passed the extend time
justPassedExtendTime() =>
    h = hour(time, tz)
    m = minute(time, tz)
    h_prev = hour(time[1], tz)
    m_prev = minute(time[1], tz)
    
    totalMins = h * 60 + m
    prevTotalMins = h_prev * 60 + m_prev
    extendMins = i_extendHour * 60 + i_extendMinute
    
    prevTotalMins <= extendMins and totalMins > extendMins
 
// Calculate the timestamp for vertical line time based on ORB start time
getVertLineTimestamp(int orbStart) =>
    int vertMins = i_vertLineHour * 60 + i_vertLineMinute
    int orbStartMins = i_orbStartHour * 60 + i_orbStartMin
    int minsFromOrbStart = vertMins - orbStartMins
    orbStart + (minsFromOrbStart * 60 * 1000)
 
//==============================================================================
// ORB TRACKING
//==============================================================================
 
var float orbHigh = na
var float orbLow = na
var int orbStartTime = na
var int orbEndTime = na
var int orbDay = na
var bool orbLocked = false
var bool extensionEnded = false
 
newSession = isNewOrbSession()
 
// Reset for new ORB session
if newSession and isValidTf
    orbHigh := high
    orbLow := low
    orbStartTime := time
    orbDay := dayofmonth(time, tz)
    orbLocked := false
    extensionEnded := false
    orbEndTime := na
 
// Continue building ORB during formation period
if isOrbTime() and not newSession and isValidTf and not na(orbHigh) and dayofmonth(time, tz) == orbDay
    orbHigh := math.max(orbHigh, high)
    orbLow := math.min(orbLow, low)
 
// Lock ORB when formation period ends
if orbJustEnded() and not orbLocked
    orbLocked := true
 
// Mark extension as ended when we pass the extend time
if justPassedExtendTime() and not extensionEnded and dayofmonth(time, tz) == orbDay and orbLocked
    extensionEnded := true
    orbEndTime := time[1]
 
//==============================================================================
// ORB CALCULATIONS
//==============================================================================
 
float orbSize = (not na(orbHigh) and not na(orbLow)) ? (orbHigh - orbLow) : na
float orbMid = (not na(orbHigh) and not na(orbLow)) ? (orbHigh + orbLow) / 2 : na
bool isOversized = not na(orbSize) and orbSize > i_threshold
 
//==============================================================================
// SIZE FUNCTIONS
//==============================================================================
 
getLabelSize() =>
    switch i_sizeLabelSize
        "tiny" => size.tiny
        "small" => size.small
        "normal" => size.normal
        "large" => size.large
        "huge" => size.huge
        => size.small
 
//==============================================================================
// DRAWING VARIABLES
//==============================================================================
 
var box currentBox = na
var line currentMidline = na
var label currentSizeLabel = na
var line currentVertLine = na
var bool vertLineDrawn = false
 
// Far future time for extending to the right (24 hours ahead)
int futureTime = time + 86400000
 
//==============================================================================
// DRAW ORB
//==============================================================================
 
if isValidTf and not na(orbHigh) and not na(orbLow) and not na(orbStartTime)
    
    // Calculate right edge time
    int rightTime = futureTime  // Default: extend infinitely to the right
    if extensionEnded and not na(orbEndTime)
        rightTime := orbEndTime  // Freeze at extension end time
    
    // Delete old drawings if not showing historical and new session starts
    if newSession and not i_showHistorical
        if not na(currentBox)
            box.delete(currentBox)
        if not na(currentMidline)
            line.delete(currentMidline)
        if not na(currentSizeLabel)
            label.delete(currentSizeLabel)
        if not na(currentVertLine)
            line.delete(currentVertLine)
        
        currentBox := na
        currentMidline := na
        currentSizeLabel := na
        currentVertLine := na
        vertLineDrawn := false
    
    // Reset vertical line flag on new session
    if newSession
        vertLineDrawn := false
    
    // Create new drawings if new session or no current box
    if newSession or na(currentBox)
        currentBox := box.new(
             left=orbStartTime, 
             top=orbHigh, 
             right=rightTime, 
             bottom=orbLow,
             bgcolor=i_bgColor, 
             border_color=i_borderColor, 
             border_width=i_borderWidth,
             xloc=xloc.bar_time
             )
        
        currentMidline := line.new(
             x1=orbStartTime, 
             y1=orbMid, 
             x2=rightTime, 
             y2=orbMid,
             color=i_midlineColor, 
             width=i_midlineWidth, 
             style=getMidlineStyle(),
             xloc=xloc.bar_time
             )
        
        if i_showSizeLabel
            currentSizeLabel := label.new(
                 x=orbStartTime, 
                 y=orbHigh,
                 text=str.tostring(orbSize, "#.##") + " pts",
                 style=label.style_label_down,
                 color=isOversized ? color.red : color.green,
                 textcolor=color.white,
                 size=getLabelSize(),
                 xloc=xloc.bar_time
                 )
    
    // Update existing drawings
    else if not na(currentBox)
        box.set_top(currentBox, orbHigh)
        box.set_bottom(currentBox, orbLow)
        box.set_right(currentBox, rightTime)
        
        if not na(currentMidline)
            line.set_y1(currentMidline, orbMid)
            line.set_y2(currentMidline, orbMid)
            line.set_x2(currentMidline, rightTime)
        
        if not na(currentSizeLabel) and i_showSizeLabel
            label.set_text(currentSizeLabel, str.tostring(orbSize, "#.##") + " pts")
            label.set_y(currentSizeLabel, orbHigh)
            label.set_color(currentSizeLabel, isOversized ? color.red : color.green)
 
//==============================================================================
// VERTICAL LINE - DRAW IMMEDIATELY WHEN ORB IS LOCKED
//==============================================================================
 
if isValidTf and i_showVertLine and orbLocked and not extensionEnded and not vertLineDrawn and not na(orbHigh) and not na(orbLow) and not na(orbStartTime) and dayofmonth(time, tz) == orbDay
    // Delete previous vertical line if not showing historical
    if not i_showHistorical and not na(currentVertLine)
        line.delete(currentVertLine)
    
    // Calculate the vertical line timestamp
    int vertLineTime = getVertLineTimestamp(orbStartTime)
    
    currentVertLine := line.new(
         x1=vertLineTime, 
         y1=orbLow, 
         x2=vertLineTime, 
         y2=orbHigh,
         color=i_vertLineColor, 
         width=i_vertLineWidth, 
         style=getVertLineStyle(),
         xloc=xloc.bar_time
         )
    
    vertLineDrawn := true
 
// Update vertical line Y values if ORB levels change (shouldn't happen after lock, but just in case)
if not na(currentVertLine) and orbLocked and not extensionEnded
    line.set_y1(currentVertLine, orbLow)
    line.set_y2(currentVertLine, orbHigh)
 
//==============================================================================
// INFO PANEL
//==============================================================================
 
getPosition() =>
    if i_panelPosV == "top"
        if i_panelPosH == "left"
            position.top_left
        else if i_panelPosH == "center"
            position.top_center
        else
            position.top_right
    else if i_panelPosV == "middle"
        if i_panelPosH == "left"
            position.middle_left
        else if i_panelPosH == "center"
            position.middle_center
        else
            position.middle_right
    else
        if i_panelPosH == "left"
            position.bottom_left
        else if i_panelPosH == "center"
            position.bottom_center
        else
            position.bottom_right
 
getTextSize() =>
    switch i_panelSize
        "tiny" => size.tiny
        "small" => size.small
        "normal" => size.normal
        "large" => size.large
        "huge" => size.huge
        => size.normal
 
var table infoPanel = na
 
if i_showPanel and isValidTf and barstate.islast
    if not na(infoPanel)
        table.delete(infoPanel)
    
    infoPanel := table.new(
         position=getPosition(),
         columns=2,
         rows=5,
         bgcolor=color.new(#1E1E1E, 10),
         frame_width=2,
         frame_color=color.gray,
         border_width=1,
         border_color=color.new(color.gray, 50)
         )
    
    textSize = getTextSize()
    
    // Header
    table.cell(infoPanel, 0, 0, "ORB📈 Info", text_color=color.white, text_size=textSize, bgcolor=color.new(#2962FF, 30))
    table.merge_cells(infoPanel, 0, 0, 1, 0)
    
    // ORB Size
    table.cell(infoPanel, 0, 1, "ORB Size:", text_color=color.white, text_size=textSize, text_halign=text.align_right)
    sizeStr = na(orbSize) ? "N/A" : str.tostring(orbSize, "#.##") + " pts"
    sizeColor = na(orbSize) ? color.gray : isOversized ? color.red : color.green
    table.cell(infoPanel, 1, 1, sizeStr, text_color=sizeColor, text_size=textSize, text_halign=text.align_left)
    
    // Status
    table.cell(infoPanel, 0, 2, "Status:", text_color=color.white, text_size=textSize, text_halign=text.align_right)
    statusStr = na(orbSize) ? "Waiting..." : isOversized ? "⚠ NO TRADE" : "✓ Valid"
    statusColor = na(orbSize) ? color.gray : isOversized ? color.red : color.green
    table.cell(infoPanel, 1, 2, statusStr, text_color=statusColor, text_size=textSize, text_halign=text.align_left)
    
    // Extend Time
    table.cell(infoPanel, 0, 3, "Extend To:", text_color=color.white, text_size=textSize, text_halign=text.align_right)
    extendStr = str.tostring(i_extendHour, "00") + ":" + str.tostring(i_extendMinute, "00")
    table.cell(infoPanel, 1, 3, extendStr, text_color=color.white, text_size=textSize, text_halign=text.align_left)
    
    // Timezone
    table.cell(infoPanel, 0, 4, "TZ:", text_color=color.white, text_size=textSize, text_halign=text.align_right)
    table.cell(infoPanel, 1, 4, i_timezone, text_color=color.white, text_size=textSize, text_halign=text.align_left)
 
//==============================================================================
// DEBUG PLOTS (Data Window Only)
//==============================================================================
 
plot(orbHigh, "ORB High", display=display.data_window)
plot(orbLow, "ORB Low", display=display.data_window)
plot(orbMid, "ORB Mid", display=display.data_window)
plot(orbSize, "ORB Size", display=display.data_window)
plot(isOrbTime() ? 1 : 0, "Is ORB Time", display=display.data_window)
plot(extensionEnded ? 1 : 0, "Extension Ended", display=display.data_window)
plot(orbLocked ? 1 : 0, "ORB Locked", display=display.data_window)
plot(currHour, "Current Hour (TZ)", display=display.data_window)
plot(currMinute, "Current Minute", display=display.data_window)

← 返回列表