Modern RTH Gaps (RRR)

RickyRicardo18 · study · 118 行 · 点赞 26 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RickyRicardo18
 
//@version=6
indicator("Modern RTH Gaps (RRR)", shorttitle="Modern Gaps", overlay = true, max_boxes_count = 500)
 
// MODERN RTH GAPS (HYBRID TIMEFRAMES)
// -----------------------------------
// Intraday Charts: Utilizes "Modern" RTH/ETH logic. Measures from the exact 16:00 Close 
// to the 09:30 Open, and dynamically shrinks boxes during both ETH and RTH sessions.
// 
// Daily+ Charts: Automatically adapts to higher timeframes by comparing the current bar's 
// Open to the previous bar's Close.
 
// --- User Inputs ---
lookbackDays = input.int(30, title="Scan Period (Trading Days)", minval=1, group="Time Settings", tooltip="Limits how many days back the script will scan for unfilled gaps. Older gaps are automatically cleared from the chart.")
 
colorUp = input.color(color.new(color.teal, 75), title="Gap Up Color (Demand)", group="Style Settings")
colorDown = input.color(color.new(color.maroon, 75), title="Gap Down Color (Supply)", group="Style Settings")
 
showBorders = input.bool(true, title="Show Box Borders", group="Style Settings", tooltip="Adds a solid border to the gap zones for crisper price levels.")
showLabels = input.bool(true, title="Show 'Supply/Demand' Text", group="Label Settings")
showPrices = input.bool(true, title="Show Price Levels", group="Label Settings", tooltip="Displays the exact top and bottom coordinates of the gap. Updates dynamically as the gap shrinks.")
textColor = input.color(color.new(color.white, 30), title="Label Text Color", group="Label Settings")
 
// --- Custom Types ---
type Gap
    float top
    float bottom
    bool  isUp
    int   birthDay
    box   id
 
// --- State Variables ---
var array<Gap> openGaps     = array.new<Gap>()
var int        dayCount     = 0
var float      prevRTHClose = na
 
// --- Timeframe & Session Detection ---
bool isIntraday = timeframe.isintraday
bool inRTH      = session.ismarket
bool isNewDay   = ta.change(time("D")) != 0
 
// Trigger conditions differ based on timeframe
bool isFirstRTHBar = isIntraday ? (inRTH and (not inRTH[1] or isNewDay)) : true
 
// --- Helper Function: Generate Label Text ---
method getLabelText(Gap g, bool sLabels, bool sPrices) =>
    string txt = ""
    if sLabels
        txt := g.isUp ? "Demand" : "Supply"
    if sPrices
        string priceStr = str.tostring(g.top, format.mintick) + " - " + str.tostring(g.bottom, format.mintick)
        txt := txt == "" ? priceStr : txt + "\n" + priceStr
    txt
 
// --- 1. Gap Detection ---
if isFirstRTHBar
    dayCount += 1
    
    float currOpen = open
    float refClose = isIntraday ? prevRTHClose : close[1]
    
    if not na(refClose)
        bool isGapUp   = currOpen > refClose
        bool isGapDown = currOpen < refClose
        
        float top    = isGapUp ? currOpen : refClose
        float bottom = isGapUp ? refClose : currOpen
        
        if (isGapUp or isGapDown) and (top > bottom)
            color boxColor = isGapUp ? colorUp : colorDown
            color bdrColor = showBorders ? color.new(boxColor, 0) : na
            
            Gap newGap = Gap.new(top, bottom, isGapUp, dayCount, box(na))
            string boxText = newGap.getLabelText(showLabels, showPrices)
            
            newGap.id := box.new(left=bar_index, top=top, right=bar_index, bottom=bottom, border_color=bdrColor, bgcolor=boxColor, text=boxText, text_color=textColor, text_halign=text.align_right, text_valign=text.align_center, text_size=size.small)
            openGaps.push(newGap)
 
// Cache the RTH close for intraday logic
if isIntraday and inRTH
    prevRTHClose := close
 
// --- 2. Gap Management (Shrinking & Deletion) ---
if openGaps.size() > 0
    for i = openGaps.size() - 1 to 0
        Gap g = openGaps.get(i)
        
        g.id.set_right(bar_index)
        
        bool isClosed = false
        bool isShrunk = false
        
        if g.isUp
            if low < g.top
                g.top := low
                g.id.set_top(g.top)
                isShrunk := true
            if low <= g.bottom
                isClosed := true
        else
            if high > g.bottom
                g.bottom := high
                g.id.set_bottom(g.bottom)
                isShrunk := true
            if high >= g.top
                isClosed := true
                
        // If the gap shrunk but didn't close, update the price label dynamically
        if isShrunk and not isClosed and showPrices
            g.id.set_text(g.getLabelText(showLabels, showPrices))
                
        bool isExpired = (dayCount - g.birthDay) > lookbackDays
        
        if isClosed or isExpired
            g.id.delete()
            openGaps.remove(i)

← 返回列表