OPEN-SOURCE SCRIPT

Credit Spread Position Overlay

//version=5
indicator("SPX Credit Spread Overlay", overlay=true)

// --- User Inputs ---
buySide = input.string("Put", title="Buy Side (Put/Call)", options=["Call", "Put"])
sellSide = input.string("Put", title="Sell Side (Put/Call)", options=["Call", "Put"])
spreadWidth = input.int(50, title="Width of the Spread (Strike Difference)", minval=1)
optionIV = input.float(0.20, title="Implied Volatility (IV) Approx.", minval=0.01, maxval=5) // Placeholder for IV
numContracts = input.int(1, title="Number of Contracts", minval=1) // SPX options are cash-settled, so we can track number of contracts
expirationDate = input.string("2024-01-19", title="Expiration Date (yyyy-mm-dd)") // Placeholder for expiration date

// --- SPX Specific Adjustments ---
spxPrice = close // SPX spot price (current price)
spxMultiplier = 100 // SPX options multiplier for cash-settled contracts

// --- Calculating Strike Prices ---
buyStrike = buySide == "Call" ? spxPrice + spreadWidth : spxPrice - spreadWidth
sellStrike = sellSide == "Call" ? spxPrice + 2 * spreadWidth : spxPrice - 2 * spreadWidth

// --- Plotting Strike Prices ---
plot(buyStrike, color=color.green, linewidth=2, title="Buy Strike")
plot(sellStrike, color=color.red, linewidth=2, title="Sell Strike")

// --- Simplified Premium Calculation (Using IV and Price) ---
// Approximation for premiums based on IV
buyPremium = optionIV * 0.5 * buyStrike / 100 // Simplified calculation for buy option premium
sellPremium = optionIV * 0.5 * sellStrike / 100 // Simplified calculation for sell option premium

// --- Net Credit / Debit Calculation ---
netPremium = sellPremium - buyPremium
maxProfit = netPremium * spxMultiplier * numContracts // Maximum profit is the net premium times the SPX multiplier and contracts
maxLoss = (sellStrike - buyStrike) * spxMultiplier * numContracts - maxProfit // Maximum loss is the strike difference minus the premium received

// --- Profit/Loss Calculation for Various Underlying Prices ---
pl = 0.0
if (buySide == "Put" and spxPrice < buyStrike)
pl := maxProfit - (buyStrike - spxPrice) * spxMultiplier * numContracts
else if (sellSide == "Put" and spxPrice < sellStrike)
pl := maxProfit - (sellStrike - spxPrice) * spxMultiplier * numContracts
else
pl := maxProfit

// --- Plotting Profit/Loss ---
plot(pl, color=color.blue, title="Profit/Loss Curve", linewidth=2)
hline(0, "Break-even", color=color.gray, linestyle=hline.style_dashed)

// --- Max Profit / Max Loss Visualization ---
bgcolor(spxPrice >= buyStrike ? color.new(color.green, 90) : na, title="Max Profit Region")
bgcolor(spxPrice <= sellStrike ? color.new(color.red, 90) : na, title="Max Loss Region")

// --- Adding Labels for Strikes ---
label.new(bar_index, buyStrike, text="Buy Strike: " + str.tostring(buyStrike), color=color.green, style=label.style_label_left, yloc=yloc.abovebar)
label.new(bar_index, sellStrike, text="Sell Strike: " + str.tostring(sellStrike), color=color.red, style=label.style_label_left, yloc=yloc.belowbar)

// --- Expiration Date Visualization ---
label.new(bar_index, high, text="Exp: " + expirationDate, color=color.yellow, style=label.style_label_down, yloc=yloc.abovebar)

// --- Implied Volatility (IV) Plot ---
ivPlot = plot(optionIV, color=color.purple, title="Implied Volatility (IV)")

// --- Dynamic IV Adjustment Placeholder ---
// This part cannot fetch real IV from SPX options, but you can dynamically adjust the 'optionIV' input.
options

오픈 소스 스크립트

진정한 TradingView 정신에 따라, 이 스크립트의 저자는 트레이더들이 이해하고 검증할 수 있도록 오픈 소스로 공개했습니다. 저자에게 박수를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰에 의해 관리됩니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.

차트에 이 스크립트를 사용하시겠습니까?

면책사항