#Gamma Edge Execution System
#Developed by antwerks (Enhanced Version)
#03/29/2026
#=================================
#================================
#=== USER INPUTS ===============
#================================
input showLabel = yes;
input gammaMode = 1; # 1 = Short Gamma, -1 = Long Gamma
input reclaimLevel = 640;
input fadeZone1Low = 643;
input fadeZone1High = 645;
input fadeZone2Low = 648;
input fadeZone2High = 650;
input orbStartTime = 0930;
input orbEndTime = 1000;
input extensionThreshold = 5.0;
#================================
#=== 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 =================
#================================
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 (IMPROVED) ==
#================================
def aboveReclaim = close > reclaimLevel;
def belowReclaim = close < reclaimLevel;
def wasAbove = aboveReclaim[1];
def wasBelow = belowReclaim[1];
#Wick-based rejection
def upperWick = high - Max(open, close);
def lowerWick = Min(open, close) - low;
def body = AbsValue(close - open);
def strongRejectDown = upperWick > body * 1.5 and close < open;
def strongRejectUp = lowerWick > body * 1.5 and close > open;
def rejectionDown = high > reclaimLevel and close < reclaimLevel;
def rejectionUp = low < reclaimLevel and close > reclaimLevel;
def trapShort = wasAbove and rejectionDown and strongRejectDown;
def trapLong = wasBelow and rejectionUp and strongRejectUp;
AddChartBubble(trapShort and !trapShort[1], high, "TRAP DN", Color.RED, yes);
AddChartBubble(trapLong and !trapLong[1], low, "TRAP UP", Color.GREEN, no);
#================================
#=== 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;
plot ORB_Low_Line = if orbFinished then orbLow else Double.NaN;
ORB_High_Line.SetDefaultColor(Color.GREEN);
ORB_Low_Line.SetDefaultColor(Color.RED);
#First break only (fixed)
def breakAboveORB = orbFinished and close > orbHigh;
def breakBelowORB = orbFinished and close < orbLow;
def firstBreakAboveORB = breakAboveORB and !breakAboveORB[1];
def firstBreakBelowORB = breakBelowORB and !breakBelowORB[1];
AddChartBubble(firstBreakAboveORB, low, "ORB UP", Color.GREEN, no);
AddChartBubble(firstBreakBelowORB, high, "ORB DN", Color.RED, yes);
#================================
#=== CONFIRMATION ===============
#================================
def trendConfirmedShort = belowVWAP and firstBreakBelowORB;
def trendConfirmedLong = aboveVWAP and firstBreakAboveORB;
#================================
#=== SCORING (IMPROVED) =========
#================================
def avgVol = Average(volume, 20);
def highVol = volume > avgVol * 1.2;
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);
#Location factor
def locationScore =
if close > reclaimLevel then 1
else if close < reclaimLevel then -1
else 0;
def netScore = (bullScore - bearScore) + locationScore;
def noTrade = AbsValue(netScore) <= 2;
#================================
#=== MARKET STATE ===============
#================================
def extension = AbsValue(close - reclaimLevel);
def isExtended = extension > extensionThreshold;
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 strongTrend then "TREND CONFIRMED (HIGH EDGE)"
else if lateTrend then "LATE TREND"
else if weakEnv then "CHOP"
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 Short", Alert.BAR, Sound.Bell);
Alert(trapLong, "Trap Long", Alert.BAR, Sound.Bell);