Dynamic Multi-DTE Call Wall Levels (RRR)

RickyRicardo18 · study · 121 行 · 点赞 28 · 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("Dynamic Multi-DTE Call Wall Levels (RRR)", overlay=true)
 
string underlying = syminfo.root
 
// TOGGLE: Leave false for single stocks (AAPL/TSLA). Set true for daily index ETFs (SPY/QQQ).
bool isDailyOption = input.bool(true, "Asset has Daily Expirations (SPY/QQQ)")
 
int strikeStep    = input.int(1, "Strike Step (Points)")
int scanRange     = input.int(6, "Strikes to Scan (+/- ATM)", maxval=6) 
 
// Format integers to two-digit strings (e.g., 9 -> "09")
pad_zero(int val) =>
    val < 10 ? "0" + str.tostring(val) : str.tostring(val)
 
// Format strike to standard OPRA 8-character string
format_strike(float strike) =>
    string s = str.tostring(math.round(strike * 1000))
    string pad = str.substring("00000000", 0, math.max(0, 8 - str.length(s)))
    pad + s
 
// Dynamic Date Generator for the 3 Time Horizons
get_dynamic_expDates() =>
    int dow = dayofweek(time, syminfo.timezone)
    int msPerDay = 86400000
    int t_midday = time + 43200000 
    
    // Bucket A: Nearest Expiry (0DTE equivalent)
    int daysTo0 = 0
    if not isDailyOption
        daysTo0 := (13 - dow) % 7 
    else
        if dow == dayofweek.saturday
            daysTo0 := 2
        else if dow == dayofweek.sunday
            daysTo0 := 1
            
    int t_0dte = t_midday + (daysTo0 * msPerDay)
    
    // Bucket B: Nearest Friday Expiration (7DTE equivalent)
    int daysToFriday = (13 - dow) % 7
    int t_7dte = t_midday + (daysToFriday * msPerDay)
    
    // Conflict Resolution: Push Bucket B out one week if dates collide
    if t_7dte == t_0dte
        t_7dte += (7 * msPerDay)
        
    // Bucket C: Nearest Expiry + 30 Days (30DTE equivalent)
    int t_30dte = t_0dte + (28 * msPerDay)
    
    array<int> timestamps = array.from(t_0dte, t_7dte, t_30dte)
    array<string> dates = array.new<string>()
    
    for ts in timestamps
        int y = year(ts, syminfo.timezone) % 100
        string yStr = pad_zero(y)
        string mStr = pad_zero(month(ts, syminfo.timezone))
        string dStr = pad_zero(dayofmonth(ts, syminfo.timezone))
        array.push(dates, yStr + mStr + dStr)
        
    dates
 
float baseStrike = math.round(close / strikeStep) * strikeStep
 
// Persistent Memory Variables
var float activeWall0DTE  = na
var float activeWall7DTE  = na
var float activeWall30DTE = na
 
// GATEKEEPER: Only fetch live data on the most recent bar AND during active market hours
bool isMarketOpen = session.ismarket
bool isLiveBar = barstate.islast
 
if isLiveBar and isMarketOpen
    array<string> expDates = get_dynamic_expDates()
    array<float> currentWalls = array.new<float>()
    
    for exp in expDates
        array<float> strikesArray = array.new<float>()
        array<float> volumesArray = array.new<float>()
        
        for i = -scanRange to scanRange
            float currentStrike = baseStrike + (i * strikeStep)
            
            if currentStrike > 0
                string tickerString = "OPRA:" + underlying + exp + "C" + format_strike(currentStrike)
                
                // CRITICAL FIX: ignore_invalid_symbol=true prevents script crashes on skipped/unlisted strikes
                float optVol = request.security(tickerString, timeframe.period, volume, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
                
                array.push(strikesArray, currentStrike)
                array.push(volumesArray, nz(optVol))
            
        float maxVol = array.max(volumesArray)
        int maxVolIndex = array.indexof(volumesArray, maxVol)
        
        float wallStrike = na
        if maxVolIndex != -1 and maxVol > 0
            wallStrike := array.get(strikesArray, maxVolIndex)
            
        array.push(currentWalls, wallStrike)
        
    float temp0  = array.get(currentWalls, 0)
    float temp7  = array.get(currentWalls, 1)
    float temp30 = array.get(currentWalls, 2)
    
    // Update the persistent variables only if a valid volume peak was found
    if not na(temp0)
        activeWall0DTE := temp0
    if not na(temp7)
        activeWall7DTE := temp7
    if not na(temp30)
        activeWall30DTE := temp30
 
// PLOTTING: trackprice=true forces the current level to draw a continuous horizontal line across the entire screen
plot(activeWall0DTE, "Nearest Expiry Wall", color=color.red, style=plot.style_linebr, linewidth=1, trackprice=true)
plot(activeWall7DTE, "Nearest Friday Wall", color=color.orange, style=plot.style_linebr, linewidth=2, trackprice=true)
plot(activeWall30DTE, "+30 Days Wall", color=color.fuchsia, style=plot.style_linebr, linewidth=3, trackprice=true)

← 返回列表