# ########################################
# The Strat
# Created by @rewardiaz
# Shows inside, directional, and outside bars
# ########################################
# Global inputs
input useAlerts = yes;
input alertType = Alert.BAR;
input buySound = Sound.DING;
input sellSound = Sound.DING;
input Show_Strat_Numbers = yes;
input Last_Bars = 10;
input Show_Twos = no;
# Basic conditions
def na = Double.NaN;
def o = open;
def c = close;
def l = low;
def h = high;
def en = (!IsNaN(c) and IsNaN(c[-Last_Bars]));
def IsUp = c > o;
def IsDown = c < o;
# Chart coloring
DefineGlobalColor("rising", CreateColor(0, 165, 0));
DefineGlobalColor("bear", CreateColor(225, 0, 0));
DefineGlobalColor("neutral", Color.dark_orange);
# ########################################
# Bar type identification for TheStrat
def highPrev = h[1];
def lowPrev = l[1];
# Inside Bar: Current high < previous high and current low > previous low
def isInside = (h <= highPrev and l >= lowPrev);
# Directional Bar Up: Current high > previous high and current low > previous low
def isDirectionalUp = h > highPrev and l > lowPrev;
# Directional Bar Down: Current high < previous high and current low < previous low
def isDirectionalDown = h < highPrev and l < lowPrev;
# Outside Bar: Current high > previous high and current low < previous low
def isOutside = h > highPrev and l < lowPrev;
# Simplified Candle Type Encoding (1=Inside, 2=Up, -2=Down, 3=Outside)
def currentCandleType =
if isInside then 1
else if isOutside then 3
else if isDirectionalUp then 2
else if isDirectionalDown then -2
else 0;
# ########################################
# Pattern Detection (Bullish and Bearish Reversal Patterns)
# 2-1-2 Continuation
def is212BullishContinuation = IsUp[3] && currentCandleType[2] == 2 and currentCandleType[1] == 1 and currentCandleType == 2;
def is212BearishContinuation = IsDown[3] && currentCandleType[2] == -2 and currentCandleType[1] == 1 and currentCandleType == -2;
# 2-1-2 Reversal
def is212BullishReversal = IsDown[3] && currentCandleType[2] == 2 and currentCandleType[1] == 1 and currentCandleType == 2;
def is212BearishReversal = IsUp[3] && currentCandleType[2] == -2 and currentCandleType[1] == 1 and currentCandleType == -2;
# 2-2 Continuation (Ensure it's exclusive from 2-1-2)
def is22BullishPattern = currentCandleType[1] == 2 and currentCandleType == 2;
def is22BearishPattern = currentCandleType[1] == -2 and currentCandleType == -2;
# 2-2 Reversals
def is22BullishReversal = IsDown[2] && currentCandleType[1] == -2 && currentCandleType == 2;
def is22BearishReversal = IsUp[2] && currentCandleType[1] == 2 && currentCandleType == -2;
# 1-3
def is13BullishReversal = currentCandleType[1] == 1 and currentCandleType == 3 && IsUp;
def is13BearishReversal = currentCandleType[1] == 1 and currentCandleType == 3 && IsDown;
# 3-1-2 Bullish / Bearish Reversal
def is312BullishPattern = IsDown[2] && currentCandleType[2] == 3 and currentCandleType[1] == 1 and currentCandleType == 2;
def is312BearishPattern = IsUp[2] && currentCandleType[2] == 3 and currentCandleType[1] == 1 and currentCandleType == -2;
# 2-1-1 Continuation (Directional, Inside, Inside)
def is211BullishContinuation = currentCandleType[2] == 2 and currentCandleType[1] == 1 and currentCandleType == 1;
def is211BearishContinuation = currentCandleType[2] == -2 and currentCandleType[1] == 1 and currentCandleType == 1;
# 2-1-1 Reversal (Directional followed by inside then opposite directional)
def is211BullishReversal = IsDown[3] && currentCandleType[2] == -2 and currentCandleType[1] == 1 and currentCandleType == 1;
def is211BearishReversal = IsUp[3] && currentCandleType[2] == 2 and currentCandleType[1] == 1 and currentCandleType == -2;
# 3-1-1 Reversal (Outside, Inside, then opposite directional)
def is311BullishReversal = IsDown[2] && currentCandleType[2] == 3 and currentCandleType[1] == 1 and currentCandleType == 2;
def is311BearishReversal = IsUp[2] && currentCandleType[2] == 3 and currentCandleType[1] == 1 and currentCandleType == -2;
# 3-2-1 Reversal (Outside, Directional, Inside)
def is321BullishReversal = IsUp[2] && currentCandleType[2] == 3 and currentCandleType[1] == 2 and currentCandleType == 1;
def is321BearishReversal = IsUp[2] && currentCandleType[2] == 3 and currentCandleType[1] == -2 and currentCandleType == 1;
# 1-2-1 Reversal (Inside, Directional, Inside)
def is121BullishReversal = currentCandleType[2] == 1 and currentCandleType[1] == 2 and currentCandleType == 1;
def is121BearishReversal = currentCandleType[2] == 1 and currentCandleType[1] == -2 and currentCandleType == 1;
# Match for other patterns (1-2-2, 3-2-2)
def is122BullishPattern = currentCandleType[2] == 1 and currentCandleType[1] == 2 and currentCandleType == 2;
def is122BearishPattern = currentCandleType[2] == 1 and currentCandleType[1] == -2 and currentCandleType == -2;
def is322BullishPattern = IsDown[2] && currentCandleType[2] == 3 and currentCandleType[1] == 2 and currentCandleType == 2;
def is322BearishPattern = IsUp[2] && currentCandleType[2] == 3 and currentCandleType[1] == -2 and currentCandleType == -2;
# ########################################
# Alerts based on detected patterns
def ActionPatterns =
if is211BullishContinuation then 1 else
if is211BearishContinuation then -1 else
if is211BullishReversal then 2 else
if is211BearishReversal then -2 else
if is322BullishPattern then 4 else
if is322BearishPattern then -4 else
if is122BullishPattern then 5 else
if is122BearishPattern then -5 else
if is212BullishContinuation then 11 else
if is212BearishContinuation then -11 else
if is312BullishPattern then 10 else
if is312BearishPattern then -10 else
if is311BullishReversal then 12 else
if is311BearishReversal then -12 else
if is321BullishReversal then 13 else
if is321BearishReversal then -13 else
if is121BullishReversal then 14 else
if is121BearishReversal then -14 else
if is13BullishReversal then 15 else
if is13BearishReversal then -15 else
if is22BullishReversal then 16 else
if is22BearishReversal then -16 else
if is212BullishReversal then 17 else
if is212BearishReversal then -17 else
0;
# Consolidated alert for both bullish and bearish patterns
Alert(useAlerts and ActionPatterns != 0, "Pattern Detected", alertType, if ActionPatterns > 0 then buySound else sellSound);
# ########################################
# Plot labels for the patterns
AddLabel(is211BullishContinuation, "2-1-1 Bullish Continuation", GlobalColor("rising"));
AddLabel(is211BearishContinuation, "2-1-1 Bearish Continuation", GlobalColor("bear"));
AddLabel(is211BullishReversal, "2-1-1 Bullish Reversal", GlobalColor("rising"));
AddLabel(is211BearishReversal, "2-1-1 Bearish Reversal", GlobalColor("bear"));
AddLabel(is212BullishContinuation, "2-1-2 Bullish Continuation", GlobalColor("rising"));
AddLabel(is212BearishContinuation, "2-1-2 Bearish Continuation", GlobalColor("bear"));
AddLabel(is212BullishReversal, "2-1-2 Bullish Reversal", GlobalColor("rising"));
AddLabel(is212BearishReversal, "2-1-2 Bearish Reversal", GlobalColor("bear"));
AddLabel(is312BullishPattern, "3-1-2 Bullish", GlobalColor("rising"));
AddLabel(is312BearishPattern, "3-1-2 Bearish", GlobalColor("bear"));
AddLabel(is311BullishReversal, "3-1-1 Bullish Reversal", GlobalColor("rising"));
AddLabel(is311BearishReversal, "3-1-1 Bearish Reversal", GlobalColor("bear"));
AddLabel(is321BullishReversal, "3-2-1 Bullish Reversal", GlobalColor("rising"));
AddLabel(is321BearishReversal, "3-2-1 Bearish Reversal", GlobalColor("bear"));
AddLabel(is121BullishReversal, "1-2-1 Bullish Reversal", GlobalColor("rising"));
AddLabel(is121BearishReversal, "1-2-1 Bearish Reversal", GlobalColor("bear"));
AddLabel(is322BullishPattern, "3-2-2 Bullish Reversal", GlobalColor("rising"));
AddLabel(is322BearishPattern, "3-2-2 Bearish Reversal", GlobalColor("bear"));
AddLabel(is122BullishPattern, "1-2-2 Bullish Reversal", GlobalColor("rising"));
AddLabel(is122BearishPattern, "1-2-2 Bearish Reversal", GlobalColor("bear"));
AddLabel(is13BullishReversal, "1-3 Bullish Reversal", GlobalColor("rising"));
AddLabel(is13BearishReversal, "1-3 Bearish Reversal", GlobalColor("bear"));
AddLabel(is22BullishReversal, "2-2 Bullish Reversal", GlobalColor("rising"));
AddLabel(is22BearishReversal, "2-2 Bearish Reversal", GlobalColor("bear"));
AddLabel(is22BullishPattern, "2-2 Bullish Continuation", GlobalColor("rising"));
AddLabel(is22BearishPattern, "2-2 Bearish Continuation", GlobalColor("bear"));
# Paint strat numbers only if enabled and valid candle type
plot barType =
if !en then na
else if !Show_Twos and AbsValue(currentCandleType) == 2 then na
else if Show_Strat_Numbers and currentCandleType != 0 then AbsValue(currentCandleType)
else na;
barType.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
barType.AssignValueColor(Color.BLACK);