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 ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถเข้าใจและตรวจสอบได้ ต้องขอบคุณผู้เขียน! ที่ให้คุณใช้ได้ฟรี แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้

ต้องการที่จะใช้สคริปต์นี้บนชาร์ตใช่ไหม?

คำจำกัดสิทธิ์ความรับผิดชอบ