本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// 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("Expected Move Zones [OPRA IV Straddle]_RRR", overlay = true, max_lines_count = 500, max_labels_count = 500)
// =========================================================================
// USER INPUTS & UI POLISH
// =========================================================================
grp_calc = "Calculation & UI Settings"
annualDaysInput = input.int(365, "Annualization Days", options = [252, 365], group = grp_calc)
strikeInterval = input.int(1, "Strike Interval Gap", tooltip = "Set to 1 for standard stocks (NVDA, AAPL). Set to 5 for indexes (SPY, QQQ) to ensure the script targets a valid ATM strike.", group = grp_calc)
sdMultInput = input.float(1.0, "Standard Deviation Multiplier", step = 0.5, tooltip = "1.0 = ~68% probability. 2.0 = ~95% probability.", group = grp_calc)
mult0dteInput = input.bool(false, "Apply 0.85x Multiplier to 0DTE", tooltip = "Narrows the 0DTE boundary to better approximate real-world ATM straddle pricing dynamics.", group = grp_calc)
showTable = input.bool(true, "Show Data Table", group = grp_calc)
labelOffset = input.int(8, "Label Projection Offset (Bars)", minval = 1, tooltip = "Adjusts how far into the right margin the labels are drawn.", group = grp_calc)
grp_sess = "Timezone & Session Settings"
sessionStr = input.session("0930-1600", "Regular Trading Hours (RTH)", group = grp_sess)
tzStr = input.string("America/New_York", "Timezone", group = grp_sess)
sessionHrs = input.float(6.5, "Total Session Hours (For 0DTE Math)", group = grp_sess)
grp_z1 = "Zone 1: 4-Week (28 DTE)"
showZ1 = input.bool(true, "Show Z1", group = grp_z1, inline = "z1")
colZ1Line = input.color(color.new(color.blue, 30), "Line", group = grp_z1, inline = "z1")
colZ1Fill = input.color(color.new(color.blue, 85), "Fill", group = grp_z1, inline = "z1")
grp_z2 = "Zone 2: Weekly Friday"
showZ2 = input.bool(true, "Show Z2", group = grp_z2, inline = "z2")
colZ2Line = input.color(color.new(color.purple, 30), "Line", group = grp_z2, inline = "z2")
colZ2Fill = input.color(color.new(color.purple, 85), "Fill", group = grp_z2, inline = "z2")
grp_z3 = "Zone 3: Nearest / 0DTE"
showZ3 = input.bool(true, "Show Z3", group = grp_z3, inline = "z3")
colZ3Line = input.color(color.new(color.orange, 30), "Line", group = grp_z3, inline = "z3")
colZ3Fill = input.color(color.new(color.orange, 85), "Fill", group = grp_z3, inline = "z3")
// =========================================================================
// HELPER FUNCTIONS
// =========================================================================
getFridayDTE() =>
int dayOfWeek = dayofweek(time)
int daysUntilFriday = 6 - dayOfWeek
if daysUntilFriday <= 0
daysUntilFriday += 7
daysUntilFriday
getRemainingSessionHours() =>
int currentHour = hour(time, tzStr)
int currentMinute = minute(time, tzStr)
float currentTime = currentHour + (currentMinute / 60.0)
float rthClose = 16.0
math.max(0, rthClose - currentTime)
// =========================================================================
// OPRA OPTION ID CONSTRUCTION & TRUE IV CALCULATION
// =========================================================================
getOpraIV(int dte) =>
float currentPrice = close
int atmStrike = math.round(currentPrice / strikeInterval) * strikeInterval
int targetTime = time + (dte * 86400000)
string yy = str.tostring(year(targetTime) % 100, "00")
string mm = str.tostring(month(targetTime), "00")
string dd = str.tostring(dayofmonth(targetTime), "00")
string strikeStr = str.tostring(atmStrike * 1000, "00000000")
string callID = "OPRA:" + syminfo.ticker + yy + mm + dd + "C" + strikeStr
string putID = "OPRA:" + syminfo.ticker + yy + mm + dd + "P" + strikeStr
// Only request data on live bar to preserve 40-symbol request limit
string safeCallID = barstate.islast ? callID : ""
string safePutID = barstate.islast ? putID : ""
float callPrice = request.security(safeCallID, "D", close, ignore_invalid_symbol = true)
float putPrice = request.security(safePutID, "D", close, ignore_invalid_symbol = true)
float rawStraddle = callPrice + putPrice
float t = math.max(1.0, dte) / annualDaysInput
float fallbackHV = ta.stdev(math.log(close / close[1]), 21) * math.sqrt(annualDaysInput)
float derivedIV = rawStraddle / (close * 0.79788 * math.sqrt(t))
bool isTheoretical = na(rawStraddle) or rawStraddle == 0
float safeIV = isTheoretical ? fallbackHV : derivedIV
float safeStraddle = isTheoretical ? (fallbackHV * close * 0.79788 * math.sqrt(t)) : rawStraddle
[safeIV, safeStraddle, isTheoretical]
// =========================================================================
// EXPECTED MOVE CALCULATION CORE
// =========================================================================
calcExpectedMove(float iv, float dte) =>
float timeFactor = 0.0
float multiplier = 1.0
if dte == 0
timeFactor := getRemainingSessionHours() / sessionHrs
if mult0dteInput
multiplier := 0.85
else
timeFactor := dte
close * iv * math.sqrt(timeFactor / annualDaysInput) * multiplier * sdMultInput
// =========================================================================
// CALCULATE ZONES
// =========================================================================
int z1_dte = 28
[z1_iv, z1_straddle, z1_theo] = getOpraIV(z1_dte)
float z1_em = calcExpectedMove(z1_iv, z1_dte)
float z1_upper = close + z1_em
float z1_lower = close - z1_em
int z2_dte = getFridayDTE()
[z2_iv, z2_straddle, z2_theo] = getOpraIV(z2_dte)
float z2_em = calcExpectedMove(z2_iv, z2_dte)
float z2_upper = close + z2_em
float z2_lower = close - z2_em
int z3_dte = 0
[z3_iv, z3_straddle, z3_theo] = getOpraIV(z3_dte)
float z3_em = calcExpectedMove(z3_iv, z3_dte)
float z3_upper = close + z3_em
float z3_lower = close - z3_em
// =========================================================================
// INITIALIZE DRAWING OBJECTS & TABLE
// =========================================================================
var line z1_line_u = na, var label z1_label_u = na
var line z1_line_l = na, var label z1_label_l = na
var line z2_line_u = na, var label z2_label_u = na
var line z2_line_l = na, var label z2_label_l = na
var line z3_line_u = na, var label z3_label_u = na
var line z3_line_l = na, var label z3_label_l = na
var table emTable = table.new(position.top_right, columns = 5, rows = 4, bgcolor = color.new(color.black, 70), border_width = 1, border_color = color.new(color.gray, 70))
// =========================================================================
// UPDATE DRAWINGS & TABLE ON LAST BAR
// =========================================================================
if barstate.islast
string sdTxt = sdMultInput != 1.0 ? " [" + str.tostring(sdMultInput) + "SD]" : ""
string z1_txt_u = "Exp " + str.tostring(z1_dte) + " DTE" + sdTxt + ": " + str.tostring(z1_upper, "#.##") + " (+" + str.tostring(z1_em, "#.##") + ")"
string z1_txt_l = "Exp " + str.tostring(z1_dte) + " DTE" + sdTxt + ": " + str.tostring(z1_lower, "#.##") + " (-" + str.tostring(z1_em, "#.##") + ")"
string z2_txt_u = "Exp " + str.tostring(z2_dte) + " DTE" + sdTxt + ": " + str.tostring(z2_upper, "#.##") + " (+" + str.tostring(z2_em, "#.##") + ")"
string z2_txt_l = "Exp " + str.tostring(z2_dte) + " DTE" + sdTxt + ": " + str.tostring(z2_lower, "#.##") + " (-" + str.tostring(z2_em, "#.##") + ")"
string z3_txt_u = "Exp " + str.tostring(z3_dte) + " DTE" + sdTxt + ": " + str.tostring(z3_upper, "#.##") + " (+" + str.tostring(z3_em, "#.##") + ")"
string z3_txt_l = "Exp " + str.tostring(z3_dte) + " DTE" + sdTxt + ": " + str.tostring(z3_lower, "#.##") + " (-" + str.tostring(z3_em, "#.##") + ")"
if showZ1
if na(z1_line_u)
z1_line_u := line.new(bar_index, z1_upper, bar_index + labelOffset, z1_upper, color=colZ1Line, style=line.style_dashed, extend=extend.right)
z1_label_u := label.new(bar_index + labelOffset, z1_upper, z1_txt_u, color=color.new(color.white, 100), textcolor=colZ1Line, style=label.style_label_left)
z1_line_l := line.new(bar_index, z1_lower, bar_index + labelOffset, z1_lower, color=colZ1Line, style=line.style_dashed, extend=extend.right)
z1_label_l := label.new(bar_index + labelOffset, z1_lower, z1_txt_l, color=color.new(color.white, 100), textcolor=colZ1Line, style=label.style_label_left)
linefill.new(z1_line_u, z1_line_l, color=colZ1Fill)
else
line.set_xy1(z1_line_u, bar_index, z1_upper), line.set_xy2(z1_line_u, bar_index + labelOffset, z1_upper)
label.set_xy(z1_label_u, bar_index + labelOffset, z1_upper), label.set_text(z1_label_u, z1_txt_u)
line.set_xy1(z1_line_l, bar_index, z1_lower), line.set_xy2(z1_line_l, bar_index + labelOffset, z1_lower)
label.set_xy(z1_label_l, bar_index + labelOffset, z1_lower), label.set_text(z1_label_l, z1_txt_l)
if showZ2
if na(z2_line_u)
z2_line_u := line.new(bar_index, z2_upper, bar_index + labelOffset, z2_upper, color=colZ2Line, style=line.style_dashed, extend=extend.right)
z2_label_u := label.new(bar_index + labelOffset, z2_upper, z2_txt_u, color=color.new(color.white, 100), textcolor=colZ2Line, style=label.style_label_left)
z2_line_l := line.new(bar_index, z2_lower, bar_index + labelOffset, z2_lower, color=colZ2Line, style=line.style_dashed, extend=extend.right)
z2_label_l := label.new(bar_index + labelOffset, z2_lower, z2_txt_l, color=color.new(color.white, 100), textcolor=colZ2Line, style=label.style_label_left)
linefill.new(z2_line_u, z2_line_l, color=colZ2Fill)
else
line.set_xy1(z2_line_u, bar_index, z2_upper), line.set_xy2(z2_line_u, bar_index + labelOffset, z2_upper)
label.set_xy(z2_label_u, bar_index + labelOffset, z2_upper), label.set_text(z2_label_u, z2_txt_u)
line.set_xy1(z2_line_l, bar_index, z2_lower), line.set_xy2(z2_line_l, bar_index + labelOffset, z2_lower)
label.set_xy(z2_label_l, bar_index + labelOffset, z2_lower), label.set_text(z2_label_l, z2_txt_l)
if showZ3
if na(z3_line_u)
z3_line_u := line.new(bar_index, z3_upper, bar_index + labelOffset, z3_upper, color=colZ3Line, style=line.style_dashed, extend=extend.right)
z3_label_u := label.new(bar_index + labelOffset, z3_upper, z3_txt_u, color=color.new(color.white, 100), textcolor=colZ3Line, style=label.style_label_left)
z3_line_l := line.new(bar_index, z3_lower, bar_index + labelOffset, z3_lower, color=colZ3Line, style=line.style_dashed, extend=extend.right)
z3_label_l := label.new(bar_index + labelOffset, z3_lower, z3_txt_l, color=color.new(color.white, 100), textcolor=colZ3Line, style=label.style_label_left)
linefill.new(z3_line_u, z3_line_l, color=colZ3Fill)
else
line.set_xy1(z3_line_u, bar_index, z3_upper), line.set_xy2(z3_line_u, bar_index + labelOffset, z3_upper)
label.set_xy(z3_label_u, bar_index + labelOffset, z3_upper), label.set_text(z3_label_u, z3_txt_u)
line.set_xy1(z3_line_l, bar_index, z3_lower), line.set_xy2(z3_line_l, bar_index + labelOffset, z3_lower)
label.set_xy(z3_label_l, bar_index + labelOffset, z3_lower), label.set_text(z3_label_l, z3_txt_l)
if showTable
// Clear previous runs to prevent ghosting when toggling zones
table.clear(emTable, 0, 1, 4, 3)
string tableMoveHeader = sdMultInput != 1.0 ? "+/- Move [" + str.tostring(sdMultInput) + "SD]" : "+/- Move"
table.cell(emTable, 0, 0, "Zone", text_color = color.white, text_size = size.small, bgcolor = color.new(color.gray, 80))
table.cell(emTable, 1, 0, "DTE", text_color = color.white, text_size = size.small, bgcolor = color.new(color.gray, 80))
table.cell(emTable, 2, 0, "Straddle", text_color = color.white, text_size = size.small, bgcolor = color.new(color.gray, 80))
table.cell(emTable, 3, 0, "IV", text_color = color.white, text_size = size.small, bgcolor = color.new(color.gray, 80))
table.cell(emTable, 4, 0, tableMoveHeader, text_color = color.white, text_size = size.small, bgcolor = color.new(color.gray, 80))
int rowIdx = 1 // Dynamic row counter
if showZ1
table.cell(emTable, 0, rowIdx, "4-Week", text_color = colZ1Line, text_size = size.small)
table.cell(emTable, 1, rowIdx, str.tostring(z1_dte), text_color = color.white, text_size = size.small)
table.cell(emTable, 2, rowIdx, "$" + str.tostring(z1_straddle, "#.##") + (z1_theo ? "*" : ""), text_color = z1_theo ? color.yellow : color.white, text_size = size.small)
table.cell(emTable, 3, rowIdx, str.tostring(z1_iv * 100, "#.##") + "%", text_color = z1_theo ? color.yellow : color.white, text_size = size.small)
table.cell(emTable, 4, rowIdx, "$" + str.tostring(z1_em, "#.##"), text_color = color.white, text_size = size.small)
rowIdx += 1
if showZ2
table.cell(emTable, 0, rowIdx, "Weekly", text_color = colZ2Line, text_size = size.small)
table.cell(emTable, 1, rowIdx, str.tostring(z2_dte), text_color = color.white, text_size = size.small)
table.cell(emTable, 2, rowIdx, "$" + str.tostring(z2_straddle, "#.##") + (z2_theo ? "*" : ""), text_color = z2_theo ? color.yellow : color.white, text_size = size.small)
table.cell(emTable, 3, rowIdx, str.tostring(z2_iv * 100, "#.##") + "%", text_color = z2_theo ? color.yellow : color.white, text_size = size.small)
table.cell(emTable, 4, rowIdx, "$" + str.tostring(z2_em, "#.##"), text_color = color.white, text_size = size.small)
rowIdx += 1
if showZ3
table.cell(emTable, 0, rowIdx, "0DTE / Near", text_color = colZ3Line, text_size = size.small)
table.cell(emTable, 1, rowIdx, str.tostring(z3_dte), text_color = color.white, text_size = size.small)
table.cell(emTable, 2, rowIdx, "$" + str.tostring(z3_straddle, "#.##") + (z3_theo ? "*" : ""), text_color = z3_theo ? color.yellow : color.white, text_size = size.small)
table.cell(emTable, 3, rowIdx, str.tostring(z3_iv * 100, "#.##") + "%", text_color = z3_theo ? color.yellow : color.white, text_size = size.small)
table.cell(emTable, 4, rowIdx, "$" + str.tostring(z3_em, "#.##"), text_color = color.white, text_size = size.small)