# Gamma Edge Execution System
# Dollar-Normalized Version
# Rewritten from original framework - antwerks
# Normalized so extension / fade zones can be defined in dollars
# Works across futures, ETFs, and stocks by changing pointValue
declare upper;
# ================================
# === USER INPUTS ================
# ================================
input showLabel = yes;
input gammaMode = 1; # 1 = Short Gamma, -1 = Long Gamma
# Instrument normalization
input pointValue = 50.0; # ES=50, NQ=20, RTY=50, CL=1000, Stocks=1
# Core level
input reclaimLevel = 640.0;
# Fade zones in DOLLARS away from reclaim
input fadeZone1DollarLow = 150.0;
input fadeZone1DollarHigh = 250.0;
input fadeZone2DollarLow = 400.0;
input fadeZone2DollarHigh = 500.0;
# ORB
input orbStartTime = 0930;
input orbEndTime = 1000;
# Extension logic in DOLLARS
input extensionThresholdDollars = 250.0;
# Volume scoring
input volLength = 20;
input highVolMultiplier = 1.2;
# ================================
# === DERIVED PRICE ZONES ========
# ================================
def fadeZone1Low = reclaimLevel + (fadeZone1DollarLow / pointValue);
def fadeZone1High = reclaimLevel + (fadeZone1DollarHigh / pointValue);
def fadeZone2Low = reclaimLevel + (fadeZone2DollarLow / pointValue);
def fadeZone2High = reclaimLevel + (fadeZone2DollarHigh / pointValue);
# ================================
# === YESTERDAY LEVELS ===========
# ================================
def yHigh = high(period = "DAY")[1];
def yLow = low(period = "DAY")[1];
plot YH = yHigh;
YH.SetDefaultColor(Color.GREEN);
YH.SetStyle(Curve.SHORT_DASH);
plot YL = yLow;
YL.SetDefaultColor(Color.RED);
YL.SetStyle(Curve.SHORT_DASH);
# ================================
# === RECLAIM LEVEL ==============
# ================================
plot Reclaim = reclaimLevel;
Reclaim.SetDefaultColor(Color.WHITE);
Reclaim.SetLineWeight(2);
Reclaim.SetStyle(Curve.SHORT_DASH);
# ================================
# === GAMMA MODE COLORS ==========
# ================================
def isShortGamma = gammaMode == 1;
DefineGlobalColor("FZ1", if isShortGamma then Color.RED else Color.LIGHT_GRAY);
DefineGlobalColor("FZ2", if isShortGamma then Color.ORANGE else Color.GRAY);
# ================================
# === FADE ZONES ================
# ================================
plot FZ1L = fadeZone1Low;
plot FZ1H = fadeZone1High;
plot FZ2L = fadeZone2Low;
plot FZ2H = fadeZone2High;
FZ1L.SetDefaultColor(GlobalColor("FZ1"));
FZ1H.SetDefaultColor(GlobalColor("FZ1"));
FZ2L.SetDefaultColor(GlobalColor("FZ2"));
FZ2H.SetDefaultColor(GlobalColor("FZ2"));
AddCloud(FZ1L, FZ1H, GlobalColor("FZ1"), GlobalColor("FZ1"));
AddCloud(FZ2L, FZ2H, GlobalColor("FZ2"), GlobalColor("FZ2"));
# ================================
# === FIRST TOUCH DETECTION ======
# ================================
def inFZ1 = close >= fadeZone1Low and close <= fadeZone1High;
def inFZ2 = close >= fadeZone2Low and close <= fadeZone2High;
def firstTouchFZ1 = inFZ1 and !inFZ1[1];
def firstTouchFZ2 = inFZ2 and !inFZ2[1];
AddChartBubble(firstTouchFZ1, high, "FZ1", Color.YELLOW, yes);
AddChartBubble(firstTouchFZ2, high, "FZ2", Color.ORANGE, yes);
# ================================
# === TRAP DETECTION ============
# ================================
def aboveReclaim = close > reclaimLevel;
def belowReclaim = close < reclaimLevel;
def wasAbove = aboveReclaim[1];
def rejection = high > reclaimLevel and close < reclaimLevel;
def strongReject = close < open;
def trapShort = wasAbove and rejection and strongReject;
AddChartBubble(trapShort and !trapShort[1], high, "TRAP DN", Color.RED, yes);
# ================================
# === VWAP ======================
# ================================
plot VWAPLine = VWAP();
VWAPLine.SetDefaultColor(Color.CYAN);
VWAPLine.SetLineWeight(2);
def aboveVWAP = close > VWAPLine;
def belowVWAP = close < VWAPLine;
# ================================
# === ORB =======================
# ================================
def newDay = GetYYYYMMDD() <> GetYYYYMMDD()[1];
def inORB = SecondsFromTime(orbStartTime) >= 0 and SecondsTillTime(orbEndTime) > 0;
def orbFinished = SecondsTillTime(orbEndTime) <= 0;
rec orbHigh =
if newDay then Double.NaN
else if inORB and IsNaN(orbHigh[1]) then high
else if inORB then Max(high, orbHigh[1])
else orbHigh[1];
rec orbLow =
if newDay then Double.NaN
else if inORB and IsNaN(orbLow[1]) then low
else if inORB then Min(low, orbLow[1])
else orbLow[1];
plot ORB_High_Line = if orbFinished then orbHigh else Double.NaN;
ORB_High_Line.SetDefaultColor(Color.GREEN);
ORB_High_Line.SetStyle(Curve.SHORT_DASH);
ORB_High_Line.SetLineWeight(2);
plot ORB_Low_Line = if orbFinished then orbLow else Double.NaN;
ORB_Low_Line.SetDefaultColor(Color.RED);
ORB_Low_Line.SetStyle(Curve.SHORT_DASH);
ORB_Low_Line.SetLineWeight(2);
def breakAboveORB = orbFinished and close > orbHigh;
def breakBelowORB = orbFinished and close < orbLow;
AddChartBubble(breakAboveORB and !breakAboveORB[1], low, "ORB UP", Color.GREEN, no);
AddChartBubble(breakBelowORB and !breakBelowORB[1], high, "ORB DN", Color.RED, yes);
# ================================
# === CONFIRMATION LOGIC =========
# ================================
def trendConfirmedShort = belowVWAP and breakBelowORB;
def trendConfirmedLong = aboveVWAP and breakAboveORB;
# ================================
# === SCORING ====================
# ================================
def avgVol = Average(volume, volLength);
def highVol = volume > avgVol * highVolMultiplier;
def bull = close > open;
def bear = close < open;
def bullScore =
(if highVol then 2 else 1) +
(if bull then 2 else 0);
def bearScore =
(if highVol then 2 else 1) +
(if bear then 2 else 0);
def netScore = bullScore - bearScore;
# ================================
# === DOLLAR-NORMALIZED EXTENSION
# ================================
def extensionPoints = AbsValue(close - reclaimLevel);
def extensionDollars = extensionPoints * pointValue;
def isExtended = extensionDollars > extensionThresholdDollars;
# ================================
# === MARKET STATE LABEL =========
# ================================
def noTrade = AbsValue(netScore) <= 2;
def strongTrend = (trendConfirmedShort or trendConfirmedLong) and AbsValue(netScore) >= 4;
def weakEnv = AbsValue(netScore) <= 2 and !trendConfirmedShort and !trendConfirmedLong;
def lateTrend = isExtended and AbsValue(netScore) <= 2 and (trendConfirmedShort[10] or trendConfirmedLong[10]);
# ================================
# === LABELS =====================
# ================================
AddLabel(yes,
if gammaMode == 1 then "Gamma: SHORT (Trend Mode)"
else "Gamma: LONG (Mean Reversion)",
if gammaMode == 1 then Color.CYAN else Color.ORANGE
);
AddLabel(showLabel,
"Score: " + netScore +
if netScore >= 4 then " (LONG)"
else if netScore <= -4 then " (SHORT)"
else " (NO TRADE)",
if netScore >= 2 then Color.GREEN
else if netScore <= -2 then Color.RED
else Color.GRAY
);
AddLabel(yes,
if noTrade then "NO TRADE"
else "ACTIVE",
if noTrade then Color.GRAY else Color.WHITE
);
AddLabel(yes,
if trendConfirmedShort then "CONFIRMED SHORT (ORB + VWAP)"
else if trendConfirmedLong then "CONFIRMED LONG (ORB + VWAP)"
else "NO CONFIRMATION",
if trendConfirmedShort then Color.RED
else if trendConfirmedLong then Color.GREEN
else Color.GRAY
);
AddLabel(yes,
if netScore <= -4 then "Bias: SHORT (Sell Rips)"
else if netScore >= 4 then "Bias: LONG (Buy Dips)"
else "Bias: NEUTRAL",
if netScore <= -4 then Color.RED
else if netScore >= 4 then Color.GREEN
else Color.GRAY
);
AddLabel(yes,
"Reclaim: " + Round(reclaimLevel, 2) +
" | FZ1: $" + Round(fadeZone1DollarLow, 0) + "-$" + Round(fadeZone1DollarHigh, 0) +
" | FZ2: $" + Round(fadeZone2DollarLow, 0) + "-$" + Round(fadeZone2DollarHigh, 0),
Color.WHITE
);
AddLabel(yes,
"Extension: $" + Round(extensionDollars, 0),
if isExtended then Color.YELLOW else Color.GRAY
);
AddLabel(yes,
if strongTrend then "TREND CONFIRMED (HIGH EDGE)"
else if lateTrend then "LATE TREND / LOW TRADING EDGE"
else if weakEnv then "CHOP / LOW CONVICTION"
else "TRANSITION",
if strongTrend then Color.GREEN
else if lateTrend then Color.YELLOW
else if weakEnv then Color.GRAY
else Color.DARK_ORANGE
);
# ================================
# === ALERTS =====================
# ================================
Alert(trapShort, "Trap Detected -> Short Setup", Alert.BAR, Sound.Bell);