I was attempting to get that to chart for SPX and any other ticker symbol (XOM for instance) but I can not seem to get it to work.
# Gamma Exposure Profile Indicator for ThinkOrSwim (XOM)
# Define Inputs
input fromStrike = 0;
input toStrike = 200;
# Define Variables
def todayDate = GetYYYYMMDD();
def fromStrikePrice = fromStrike;
def toStrikePrice = toStrike;
# Calculate Gamma Exposure
def calcGammaEx(S, K, vol, T, r, q, optType, OI) {
if (T == 0 || vol == 0) {
return 0;
}
def dp = (Log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * Sqrt(T));
def dm = dp - vol * Sqrt(T);
def gamma;
if (optType == 'call') {
gamma = Exp(-q * T) * normdist(dp, 0, 1) / (S * vol * Sqrt(T));
} else {
gamma = K * Exp(-r * T) * normdist(dm, 0, 1) / (S * S * vol * Sqrt(T));
}
return OI * 100 * S * S * 0.01 * gamma;
}
# Chart: Gamma Exposure Profile for XOM
def levels = CompoundValue(1, close, 0);
def totalGamma = 0;
def totalGammaExNext = 0;
def totalGammaExFri = 0;
# Loop through strike levels
for level in levels {
# Calculate gamma exposure at each strike level
def callGammaEx = calcGammaEx(level, GetStrike(), GetVolatility(), GetDTE(), 0, 0, "call", GetOpenInterest());
def putGammaEx = calcGammaEx(level, GetStrike(), GetVolatility(), GetDTE(), 0, 0, "put", GetOpenInterest());
# Aggregate gamma exposure
totalGamma += callGammaEx - putGammaEx;
# Excluding next expiry
if (GetExpDate() != GetNextExpDate()) {
totalGammaExNext += callGammaEx - putGammaEx;
}
# Excluding next monthly expiry
if (!IsMonthlyExp()) {
totalGammaExFri += callGammaEx - putGammaEx;
}
}
# Find Gamma Flip Point
def zeroCrossIdx = fold i = 1 to TotalItems(levels) with zeroCross = 0 do if Sign(totalGamma) != Sign(totalGamma[i - 1]) then i else zeroCross;
def negGamma = totalGamma[zeroCrossIdx];
def posGamma = totalGamma[zeroCrossIdx + 1];
def negStrike = levels[zeroCrossIdx];
def posStrike = levels[zeroCrossIdx + 1];
def zeroGamma = posStrike - ((posStrike - negStrike) * posGamma / (posGamma - negGamma));
# Plot
plot ChartGamma = totalGamma / 1000000; # Gamma Exposure in millions for XOM
ChartGamma.SetPaintingStrategy(PaintingStrategy.LINE);
ChartGamma.SetLineWeight(2);
ChartGamma.SetDefaultColor(Color.CYAN);
AddVerticalLine(close, "XOM Spot", Color.RED);
# End of Script