# ==========================================
# MTFL - Universal Architect Scan Logic
# ==========================================
input midBufferPercent = 1.0; #label: IPIN % Threshold
# 1. PRIMARY BAR DATA
def CurH = high;
def CurL = low;
def CurC = close;
def PCH = CompoundValue(1, high[1], high);
def PCL = CompoundValue(1, low[1], low);
def PCM = (PCH + PCL) / 2;
def MidBuffer = PCM * midBufferPercent / 100;
# 2. UNIVERSAL TIME CHANGE DETECTION
def isNew30 = (SecondsFromTime(0000) % 1800) == 0;
def isNew60 = (SecondsFromTime(0000) % 3600) == 0;
def isNewD = GetDay() != GetDay()[1];
def isNewW = GetWeek() != GetWeek()[1];
def isNewM = GetMonth() != GetMonth()[1];
def isNewQ = isNewM and (GetMonth()==1 or GetMonth()==4 or GetMonth()==7 or GetMonth()==10);
def isNewY = GetYear() != GetYear()[1];
# 3. THE MULTI-TIMEFRAME ENGINE (STABILIZED)
def rH30 = CompoundValue(1, if isNew30 then high else Max(rH30[1], high), high);
def rL30 = CompoundValue(1, if isNew30 then low else Min(rL30[1], low), low);
def h30 = CompoundValue(1, if isNew30 then rH30[1] else h30[1], high);
def l30 = CompoundValue(1, if isNew30 then rL30[1] else l30[1], low);
def rH60 = CompoundValue(1, if isNew60 then high else Max(rH60[1], high), high);
def rL60 = CompoundValue(1, if isNew60 then low else Min(rL60[1], low), low);
def h60 = CompoundValue(1, if isNew60 then rH60[1] else h60[1], high);
def l60 = CompoundValue(1, if isNew60 then rL60[1] else l60[1], low);
def rHD = CompoundValue(1, if isNewD then high else Max(rHD[1], high), high);
def rLD = CompoundValue(1, if isNewD then low else Min(rLD[1], low), low);
def hD = CompoundValue(1, if isNewD then rHD[1] else hD[1], high);
def lD = CompoundValue(1, if isNewD then rLD[1] else lD[1], low);
def rHW = CompoundValue(1, if isNewW then high else Max(rHW[1], high), high);
def rLW = CompoundValue(1, if isNewW then low else Min(rLW[1], low), low);
def hW = CompoundValue(1, if isNewW then rHW[1] else hW[1], high);
def lW = CompoundValue(1, if isNewW then rLW[1] else lW[1], low);
def rHM = CompoundValue(1, if isNewM then high else Max(rHM[1], high), high);
def rLM = CompoundValue(1, if isNewM then low else Min(rLM[1], low), low);
def hM = CompoundValue(1, if isNewM then rHM[1] else hM[1], high);
def lM = CompoundValue(1, if isNewM then rLM[1] else lM[1], low);
def rHQ = CompoundValue(1, if isNewQ then high else Max(rHQ[1], high), high);
def rLQ = CompoundValue(1, if isNewQ then low else Min(rLQ[1], low), low);
def hQ = CompoundValue(1, if isNewQ then rHQ[1] else hQ[1], high);
def lQ = CompoundValue(1, if isNewQ then rLQ[1] else lQ[1], low);
def rHY = CompoundValue(1, if isNewY then high else Max(rHY[1], high), high);
def rLY = CompoundValue(1, if isNewY then low else Min(rLY[1], low), low);
def hY = CompoundValue(1, if isNewY then rHY[1] else hY[1], high);
def lY = CompoundValue(1, if isNewY then rLY[1] else lY[1], low);
# 4. CANDLE STRUCTURE HELPERS
def isInside = CurH < PCH and CurL > PCL;
def isBothSides = CurH > PCH and CurL < PCL;
def isSweepLow = CurL < PCL and CurH <= PCH;
def isSweepHigh = CurH > PCH and CurL >= PCL;
# ==========================================
# TREND SIGNALS (Sweep one side, no Mid interaction)
# ==========================================
plot TU = CurH > PCH and CurL >= PCL and CurC > PCH;
plot TD = CurL < PCL and CurH <= PCH and CurC < PCL;
# ==========================================
# FAILED TREND SIGNALS (Sweep one side, never reached Mid)
# ==========================================
plot FTU = CurH > PCH and CurC < PCH and CurC > PCM and CurL > PCM;
plot FTD = CurL < PCL and CurC > PCL and CurC < PCM and CurH < PCM;
# ==========================================
# INSIDE CANDLE VARIATIONS
# ==========================================
plot IBULL = isInside and CurL >= PCM;
plot IBEAR = isInside and CurH <= PCM;
plot IBAL = isInside and CurH > PCM and CurL < PCM;
plot IPIN = isInside and AbsValue(CurH - PCM) <= MidBuffer and AbsValue(CurL - PCM) <= MidBuffer;
# ==========================================
# OUTSIDE CANDLE VARIATIONS (Both sides swept)
# ==========================================
plot OBULL = isBothSides and CurC > PCH;
plot OBEAR = isBothSides and CurC < PCL;
plot BITDU = isBothSides and CurC >= PCL and CurC < PCM;
plot BITDD = isBothSides and CurC <= PCH and CurC > PCM;
# ==========================================
# OTHER SIDE SIGNALS (One side swept, Mid interaction)
# ==========================================
plot OSBULL_Active = isSweepLow and CurC > PCM;
plot OSBULL_Potential = isSweepLow and CurH >= PCM and CurC < PCM;
plot OSBEAR_Active = isSweepHigh and CurC < PCM;
plot OSBEAR_Potential = isSweepHigh and CurL <= PCM and CurC > PCM;
# ==========================================
# STRUCTURAL MTFL BREAKS (TU / TD)
# ==========================================
plot T30_TU = CurH > h30 and CurC > h30;
plot T30_TD = CurL < l30 and CurC < l30;
plot T60_TU = CurH > h60 and CurC > h60;
plot T60_TD = CurL < l60 and CurC < l60;
plot Day_TU = CurH > hD and CurC > hD;
plot Day_TD = CurL < lD and CurC < lD;
plot Wk_TU = CurH > hW and CurC > hW;
plot Wk_TD = CurL < lW and CurC < lW;
plot Mo_TU = CurH > hM and CurC > hM;
plot Mo_TD = CurL < lM and CurC < lM;
plot Qtr_TU = CurH > hQ and CurC > hQ;
plot Qtr_TD = CurL < lQ and CurC < lQ;
plot Yr_TU = CurH > hY and CurC > hY;
plot Yr_TD = CurL < lY and CurC < lY;