本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("SPX IC | strikes, IV/RV and exit", overlay = true)
shortPut = input.float(0.0, "Short put strike", minval = 0, group = "Position")
shortCall = input.float(0.0, "Short call strike", minval = 0, group = "Position")
wingWidth = input.float(10.0, "Wing width (index points)", minval = 0.01, group = "Position")
credit = input.float(2.5, "Actual opening credit (index points)", minval = 0, group = "Position")
multiplier = input.int(100, "Contract multiplier", minval = 1, group = "Position")
ivPct = input.float(0.0, "Expiry IV, annualized %", minval = 0, group = "Volatility")
forecastRvPct = input.float(0.0, "Forecast RV to exit, annualized % (optional)", minval = 0, group = "Volatility")
rvLookback = input.int(10, "Historical RV lookback (completed days)", minval = 3, maxval = 252, group = "Volatility")
exitAt = input.time(timestamp("24 Sep 2026 15:55 -0400"), "Mandatory exit (set chart timezone to New York)", group = "Risk")
histRvPct = request.security(syminfo.tickerid, "D",
ta.stdev(math.log(close / close[1]), rvLookback)[1] * math.sqrt(252.0) * 100.0,
lookahead = barmerge.lookahead_on)
nowMs = barstate.isrealtime ? timenow : time_close
yearsRemaining = na(nowMs) ? na : math.max(exitAt - nowMs, 0) / (365.0 * 24.0 * 60.0 * 60.0 * 1000.0)
em = close * ivPct / 100.0 * math.sqrt(yearsRemaining)
configured = timeframe.isintraday and not na(nowMs) and shortPut > 0 and shortCall > shortPut and wingWidth > 0 and credit > 0 and credit < wingWidth and ivPct > 0
active = configured and nowMs < exitAt
putGap = close - shortPut
callGap = shortCall - close
nearGap = math.min(putGap, callGap)
gapInEm = active and em > 0 ? nearGap / em : na
tpDebit = 0.40 * credit
slDebit = 2.00 * credit
maxExpiryLoss = wingWidth - credit
plot(configured ? shortPut : na, "Short put", color = color.orange, linewidth = 2)
plot(configured ? shortCall : na, "Short call", color = color.orange, linewidth = 2)
plot(active ? close - em : na, "Lower 1 sigma to exit", color = color.new(color.blue, 15))
plot(active ? close + em : na, "Upper 1 sigma to exit", color = color.new(color.blue, 15))
panelBg = color.rgb(24, 27, 34)
var table dashboard = table.new(position.top_right, 2, 9, bgcolor = panelBg, border_width = 1)
if barstate.islast
table.cell(dashboard, 0, 0, "SPX Iron Condor", text_color = color.white, bgcolor = color.navy)
table.cell(dashboard, 1, 0, not configured ? "SET INPUTS / 1-5m" : active ? "ACTIVE" : "EXIT TIME PASSED", text_color = color.white, bgcolor = not configured ? color.gray : active ? color.teal : color.red)
table.cell(dashboard, 0, 1, "Short P / C", text_color = color.white)
table.cell(dashboard, 1, 1, configured ? str.tostring(shortPut, "#.##") + " / " + str.tostring(shortCall, "#.##") : "—", text_color = color.white)
table.cell(dashboard, 0, 2, "Gap P / C (pts)", text_color = color.white)
table.cell(dashboard, 1, 2, configured ? str.tostring(putGap, "#.##") + " / " + str.tostring(callGap, "#.##") : "—", text_color = nearGap <= 0 and configured ? color.red : color.white)
table.cell(dashboard, 0, 3, "IV / forecast RV (%)", text_color = color.white)
table.cell(dashboard, 1, 3, ivPct > 0 ? str.tostring(ivPct, "#.##") + " / " + (forecastRvPct > 0 ? str.tostring(forecastRvPct, "#.##") + " (" + str.tostring(ivPct / forecastRvPct, "#.##") + "x)" : "set forecast") : "set IV", text_color = color.white)
table.cell(dashboard, 0, 4, "Historic RV (%)", text_color = color.white)
table.cell(dashboard, 1, 4, na(histRvPct) ? "—" : str.tostring(histRvPct, "#.##"), text_color = color.white)
table.cell(dashboard, 0, 5, "1σ move / nearest gap", text_color = color.white)
table.cell(dashboard, 1, 5, active ? str.tostring(em, "#.##") + " / " + str.tostring(gapInEm, "#.##") + "σ" : "—", text_color = color.white)
table.cell(dashboard, 0, 6, "Credit / wing", text_color = color.white)
table.cell(dashboard, 1, 6, configured ? str.tostring(100.0 * credit / wingWidth, "#.##") + "%" : "—", text_color = color.white)
table.cell(dashboard, 0, 7, "TP / SL close debit", text_color = color.white)
table.cell(dashboard, 1, 7, configured ? str.tostring(tpDebit, "#.##") + " / " + str.tostring(slDebit, "#.##") : "—", text_color = color.white)
table.cell(dashboard, 0, 8, "Max expiry loss / 1 lot", text_color = color.white)
table.cell(dashboard, 1, 8, configured ? "$" + str.tostring(maxExpiryLoss * multiplier, "#.##") : "—", text_color = color.white)
putCross = ta.crossunder(close, shortPut)
callCross = ta.crossover(close, shortCall)
alertcondition(active and putCross, "SPX crosses short put", "SPX crossed below the IC short put. Check the actual four-leg close debit.")
alertcondition(active and callCross, "SPX crosses short call", "SPX crossed above the IC short call. Check the actual four-leg close debit.")
alertcondition(configured and barstate.isrealtime and timenow >= exitAt, "Mandatory exit time", "Iron Condor mandatory exit time reached. Check the position and broker quotes.")