DTE ATM Straddle & Flow

KinetiCapital · study · 147 行 · 点赞 23 · 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/
// © KinetiCapital
 
//@version=6
indicator("DTE ATM Straddle & Flow", shorttitle="DTE Straddle", overlay=false)
 
// ==========================================
// USER INPUTS
// ==========================================
string i_underlying   = input.string("QQQ", "Underlying Symbol", options=["QQQ", "SPY", "SPX"], group="Contract Selection")
string i_expiry       = input.string("", "DTE Expiry (YYMMDD)", tooltip="Leave empty to auto-build today's 0DTE date in NY time", group="Contract Selection")
float  i_strike       = input.float(760.0, "Strike Price", step=1.0, group="Contract Selection", tooltip="Set to active strike (e.g., 760 for QQQ, 580 for SPY, 5800 for SPX)")
 
// Plot Toggles
bool   i_show_net     = input.bool(false, "Show Net Premium Line (Call - Put)", group="Display Options")
bool   i_show_flips   = input.bool(true, "Highlight Hedging Flip Bars", group="Display Options")
 
// Visual Customizations
color  col_call       = input.color(color.rgb(24, 144, 219), "Call Price", group="Visuals", inline="c1")
color  col_put        = input.color(color.rgb(229, 57, 53), "Put Price", group="Visuals", inline="c1")
color  col_straddle   = input.color(color.rgb(0, 0, 0), "Total Straddle", group="Visuals", inline="c2")
color  col_net        = input.color(color.rgb(155, 39, 176, 60), "Net Premium (C-P)", group="Visuals", inline="c2")
color  col_text       = input.color(color.rgb(54, 58, 69), "Table Text", group="Visuals", inline="c3")
color  col_bg         = color.rgb(54, 58, 69, 90)
 
// ==========================================
// DATE & SESSION HANDLING
// ==========================================
string tz = "America/New_York"
int    currYY = year(time, tz) % 100
int    currMM = month(time, tz)
int    currDD = dayofmonth(time, tz)
 
// Auto-generate YYMMDD string if input is blank
string auto_expiry   = str.format("{0,number,00}{1,number,00}{2,number,00}", currYY, currMM, currDD)
string active_expiry = str.length(i_expiry) == 6 ? i_expiry : auto_expiry
 
// Regular Trading Hours (9:30 AM - 4:00 PM EST)
bool is_today = (year(time, tz) == year(timenow, tz)) and (month(time, tz) == month(timenow, tz)) and (dayofmonth(time, tz) == dayofmonth(timenow, tz))
bool in_rth   = is_today and time >= timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), 9, 30) and time < timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), 16, 0)
bool is_open  = in_rth and not in_rth[1]
 
// ==========================================
// DYNAMIC OPRA TICKER MAPPING
// ==========================================
// Convert SPX to OPRA weekly root (SPXW) for daily 0DTE contracts
string base_symbol = i_underlying == "SPX" ? "SPXW" : i_underlying
 
f_sym(_u, _exp, _cp, _k) =>
    "OPRA:" + _u + _exp + _cp + str.format("{0,number,0.0}", _k)
 
string c_ticker = f_sym(base_symbol, active_expiry, "C", i_strike)
string p_ticker = f_sym(base_symbol, active_expiry, "P", i_strike)
 
// Request security calls for call and put options
[call_close, call_vol] = request.security(c_ticker, timeframe.period, [close, volume], barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol=true)
[put_close,  put_vol]  = request.security(p_ticker, timeframe.period, [close, volume], barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol=true)
 
// Fallbacks for non-existent bars
float c_px = nz(call_close, 0.0)
float p_px = nz(put_close, 0.0)
float straddle_price = c_px + p_px
float net_premium    = c_px - p_px
 
// Lock 9:30 AM open prices for decay tracking
var float open_call     = na
var float open_put      = na
var float open_straddle = na
 
if is_open or (in_rth and na(open_straddle))
    open_call     := c_px > 0 ? c_px : na
    open_put      := p_px > 0 ? p_px : na
    open_straddle := straddle_price > 0 ? straddle_price : na
 
// Decay percentages
float call_decay     = not na(open_call) and open_call > 0         ? ((c_px - open_call) / open_call) * 100.0 : 0.0
float put_decay      = not na(open_put) and open_put > 0           ? ((p_px - open_put) / open_put) * 100.0 : 0.0
float straddle_decay = not na(open_straddle) and open_straddle > 0 ? ((straddle_price - open_straddle) / open_straddle) * 100.0 : 0.0
 
// Breakeven levels
float upper_be = i_strike + c_px
float lower_be = i_strike - p_px
 
// Secondary Metrics: Ratio & Volatility Status
float call_put_ratio = p_px > 0 ? c_px / p_px : na
string vol_status    = straddle_decay > 10.0 ? "VOL EXPANSION" : straddle_decay < -20.0 ? "THETA CRUSH" : "NEUTRAL DECAY"
 
// ==========================================
// MM HEDGING FLIP LOGIC
// ==========================================
// Detect Net Premium Zero-Line Cross (Skew Flip)
bool net_flip_bull = ta.crossover(net_premium, 0)
bool net_flip_bear = ta.crossunder(net_premium, 0)
 
// Market Maker Hedging Bias Classification
string mm_hedge_state = "NEUTRAL / CHOP"
color  mm_color       = color.gray
 
if net_premium > 0 and c_px > (p_px * 2.0)
    mm_hedge_state := "ACCEL BUYING (CALL SQUEEZE)"
    mm_color       := col_call
else if net_premium < 0 and p_px > (c_px * 2.0)
    mm_hedge_state := "ACCEL SELLING (PUT DUMP)"
    mm_color       := col_put
else if net_premium > 0
    mm_hedge_state := "MILD BUY HEDGE"
    mm_color       := color.new(col_call, 30)
else if net_premium < 0
    mm_hedge_state := "MILD SELL HEDGE"
    mm_color       := color.new(col_put, 30)
 
// Highlight chart background on skew flip
bgcolor(i_show_flips and net_flip_bull ? color.new(color.blue, 80) : i_show_flips and net_flip_bear ? color.new(color.red, 80) : na)
 
// ==========================================
// PLOTS & DISPLAY
// ==========================================
plot(in_rth and c_px > 0 ? c_px : na,                     title="Call Option Price",   color=col_call,     linewidth=2)
plot(in_rth and p_px > 0 ? p_px : na,                     title="Put Option Price",    color=col_put,      linewidth=2)
plot(in_rth and straddle_price > 0 ? straddle_price : na, title="Total Straddle (C+P)",color=col_straddle, linewidth=2)
plot(in_rth and i_show_net ? net_premium : na,            title="Net Premium (C-P)",   color=col_net,      linewidth=1, style=plot.style_line)
 
hline(0, "Zero Level", color=color.new(color.gray, 70))
 
// Dashboard HUD (Positioned Middle-Left)
var table tDash = table.new(position.middle_left, 2, 9, border_width=1)
if barstate.islast
    table.cell(tDash, 0, 0, str.format("{0} ({1})", i_underlying, active_expiry), text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 0, str.format("Strike: {0,number,#.0}", i_strike), text_color=col_text, bgcolor=col_bg, text_size=size.small)
 
    table.cell(tDash, 0, 1, "Call Premium", text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 1, c_px > 0 ? str.format("${0,number,#.00} ({1,number,+#.0;-#.0}%)", c_px, call_decay) : "No Data", text_color=col_call, bgcolor=col_bg, text_size=size.small)
 
    table.cell(tDash, 0, 2, "Put Premium", text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 2, p_px > 0 ? str.format("${0,number,#.00} ({1,number,+#.0;-#.0}%)", p_px, put_decay) : "No Data", text_color=col_put, bgcolor=col_bg, text_size=size.small)
 
    table.cell(tDash, 0, 3, "Total Straddle", text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 3, straddle_price > 0 ? str.format("${0,number,#.00} ({1,number,+#.0;-#.0}%)", straddle_price, straddle_decay) : "No Data", text_color=col_straddle, bgcolor=col_bg, text_size=size.small)
 
    table.cell(tDash, 0, 6, "Call / Put Ratio", text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 6, not na(call_put_ratio) ? str.format("{0,number,#.00}x", call_put_ratio) : "—", text_color=col_text, bgcolor=col_bg, text_size=size.small)
 
    table.cell(tDash, 0, 7, "Vol Regime", text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 7, vol_status, text_color=straddle_decay < -20.0 ? col_put : straddle_decay > 10.0 ? col_call : col_straddle, bgcolor=col_bg, text_size=size.small)
 
    table.cell(tDash, 0, 8, "MM Hedging Bias", text_color=col_text, bgcolor=col_bg, text_size=size.small)
    table.cell(tDash, 1, 8, mm_hedge_state, text_color=mm_color, bgcolor=col_bg, text_size=size.small)

← 返回列表