#SqueezePlay Strategy
#Hint: Displays EMAs (8,21,34,55,89) and labels when EMAs are stacked, price is in the buy zone, and a squeeze is active.
#@Gavstah 10/2021 - Amended 4/2024 by karenjoy511
#Ref: https://usethinkscript.com/threads/squeeze-play-strategy-for-thinkorswim.8664/
input price = close;
DefineGlobalColor("BullLabel", Color.DARK_GREEN);
DefineGlobalColor("BearLabel", Color.MAGENTA);
DefineGlobalColor("BuyZoneLabel", Color.DARK_ORANGE);
DefineGlobalColor(“BullSqueezeLabel”, Color.DARK_GREEN);
DefineGlobalColor(“BearSqueezeLabel”, Color.MAGENTA);
# define EMAs
def ema8_ = ExpAverage(price, 8);
def ema21_ = ExpAverage(price, 21);
#def ema34_ = ExpAverage(price, 34);
#def ema55_ = ExpAverage(price, 55);
#def ema89_ = ExpAverage(price, 89);
# plot EMAs
plot EMA8 = ExpAverage(price, 8);
plot EMA21 = ExpAverage(price, 21);
plot EMA34 = ExpAverage (price, 34);
plot EMA55 = ExpAverage (price, 55);
plot EMA89 = ExpAverage (price, 89);
# is the price in the buy zone?
def buyZone = price < ema8_ and price > ema21_;
def sellZone = price > ema8_ and price < ema21_;
# now, let's see what we have
# 1) is momentum > 0 with an upward trend on the TTM_Squeeze() histogram?
def bullMomentum = TTM_Squeeze().Histogram > 0 && TTM_Squeeze().Histogram > TTM_Squeeze().Histogram[1];
def bearMomentum = TTM_Squeeze().Histogram < 0 && TTM_Squeeze().Histogram < TTM_Squeeze().Histogram[1];
# 2) Has the price closed over the 21 EMA?
def bullcloseOver21 = price > EMA21;
def bearcloseOver21 = price < EMA21;
# 3) Are EMAs stacked in ascending order ("The Royal Setup")?
def bullstackedEMAS = EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89;
def bearstackedEMAS = EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89;
# 4) are we in a TTM_squeeze (true if squeezealert has not fired)?
def squeeze = !TTM_Squeeze().SqueezeAlert;
# do we have a squeeze play?
def bullsqueezePlay = squeeze && bullstackedEMAS && bullMomentum && bullcloseOver21;
def bearsqueezePlay = squeeze && bearstackedEMAS && bearMomentum && bearcloseOver21;
# set the stacked label
AddLabel(if bullstackedEMAS then yes else no, ”Bull Stacked EMAs”, GlobalColor("BullLabel"));
AddLabel(if bearstackedEMAS then yes else no, ”Bear Stacked EMAs”, GlobalColor("BearLabel"));
# set Squeeze Play label if we're in a squeeze play.
AddLabel(if bullsqueezePlay then yes else no, " Squeeze Play ", GlobalColor("BullSqueezeLabel"));
AddLabel(if bearsqueezePlay then yes else no, " Squeeze Play ", GlobalColor("BearSqueezeLabel"));
# Add buy zone label if we're in the buy zone
AddLabel(if buyZone then yes else no, "BUY ZONE", GlobalColor("BuyZoneLabel"));
AddLabel(if sellZone then yes else no, "BUY ZONE", GlobalColor("BuyZoneLabel"));
# End