//+------------------------------------------------------------------+
//|                                            mikko_auto_sr_MTF.mq4 |
//|            Coded by mikkom, contact: mikko.mattila@kirkas.com    |
//|            Modified for MTF, Color, and Width control            |
//+------------------------------------------------------------------+
#property copyright "mikkom"
#property indicator_chart_window
#property indicator_buffers 0

#define UP 1
#define DOWN 2

//--- Inputs
extern string _s1 = "--- MTF Settings ---";
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Select Timeframe

extern string _s2 = "--- ZigZag Settings ---";
extern int ExtDepth      = 20;
extern int ExtDeviation  = 10;
extern int ExtBackstep   = 3;

extern string _s3 = "--- Visual Settings ---";
extern int historyLines     = 5;
extern int lineCombinations = 1;
extern color HorizontalColor = clrDimGray;
extern int   HorizontalWidth = 1;
extern ENUM_LINE_STYLE HorizontalStyle = STYLE_DOT;

extern color TrendUpColor    = clrSkyBlue;
extern color TrendDownColor  = clrTomato;
extern int   TrendLineWidth  = 2;
extern bool  DrawRays        = false;

//--- Internal Variables
string prefix = "mikko_mtf_";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
   IndicatorShortName("Mikko S/R MTF");
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
   ObjectsDeleteAll(0, prefix);
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   
   // Clean up existing objects to redraw
   ObjectsDeleteAll(0, prefix);

   int last = 0;
   int lastHighIdx[11];
   int lastLowIdx[11];
   ArrayInitialize(lastHighIdx, -1);
   ArrayInitialize(lastLowIdx, -1);
   
   int lineCount = 0;
   
   // Variable Declarations (Fixed: declared once at the top)
   int i, j, k, prevIdx;
   double zz, high, low, pricePrev;
   datetime timeI, timePrev;
   string hline_name, trend_id;

   // Determine how many bars of the target timeframe to scan
   int limit = iBars(Symbol(), TimeFrame);
   if(limit > 2000) limit = 2000; 

   for(i = 0; i < limit && lineCount < historyLines * 2; i++) {
   
      // Get ZigZag values
      zz   = iCustom(Symbol(), TimeFrame, "ZigZag", ExtDepth, ExtDeviation, ExtBackstep, 0, i);
      high = iCustom(Symbol(), TimeFrame, "ZigZag", ExtDepth, ExtDeviation, ExtBackstep, 1, i);
      low  = iCustom(Symbol(), TimeFrame, "ZigZag", ExtDepth, ExtDeviation, ExtBackstep, 2, i);

      timeI = iTime(Symbol(), TimeFrame, i);

      //--- Logic for Highs (Resistance)
      if(high > 0 && last == DOWN) {
         lastHighIdx[0] = i;
         
         hline_name = prefix + "hline_h_" + (string)timeI;
         ObjectCreate(0, hline_name, OBJ_HLINE, 0, timeI, high);
         ObjectSetInteger(0, hline_name, OBJPROP_COLOR, HorizontalColor);
         ObjectSetInteger(0, hline_name, OBJPROP_WIDTH, HorizontalWidth);
         ObjectSetInteger(0, hline_name, OBJPROP_STYLE, HorizontalStyle);

         if(lastHighIdx[1] != -1) {
            for(j = 0; j < lineCombinations; j++) {
               if(lastHighIdx[1+j] != -1) {
                  prevIdx = lastHighIdx[1+j];
                  timePrev = iTime(Symbol(), TimeFrame, prevIdx);
                  pricePrev = iHigh(Symbol(), TimeFrame, prevIdx);
                  
                  trend_id = prefix + "up_" + (string)i + "_" + (string)j;
                  ObjectCreate(0, trend_id, OBJ_TREND, 0, timeI, high, timePrev, pricePrev);
                  ObjectSetInteger(0, trend_id, OBJPROP_COLOR, TrendUpColor);
                  ObjectSetInteger(0, trend_id, OBJPROP_WIDTH, TrendLineWidth);
                  ObjectSetInteger(0, trend_id, OBJPROP_RAY, DrawRays);
               }
            }
            lineCount++;         
         } 

         for(k = 10; k > 0; k--) lastHighIdx[k] = lastHighIdx[k-1];
      }
      
      //--- Logic for Lows (Support)
      if(low > 0 && last == UP) {
         lastLowIdx[0] = i;
         
         hline_name = prefix + "hline_l_" + (string)timeI;
         ObjectCreate(0, hline_name, OBJ_HLINE, 0, timeI, low);
         ObjectSetInteger(0, hline_name, OBJPROP_COLOR, HorizontalColor);
         ObjectSetInteger(0, hline_name, OBJPROP_WIDTH, HorizontalWidth);
         ObjectSetInteger(0, hline_name, OBJPROP_STYLE, HorizontalStyle);
        
         if(lastLowIdx[1] != -1) {
            for(j = 0; j < lineCombinations; j++) {
               if(lastLowIdx[1+j] != -1) {
                  prevIdx = lastLowIdx[1+j];
                  timePrev = iTime(Symbol(), TimeFrame, prevIdx);
                  pricePrev = iLow(Symbol(), TimeFrame, prevIdx);

                  trend_id = prefix + "down_" + (string)i + "_" + (string)j;
                  ObjectCreate(0, trend_id, OBJ_TREND, 0, timeI, low, timePrev, pricePrev);
                  ObjectSetInteger(0, trend_id, OBJPROP_COLOR, TrendDownColor);
                  ObjectSetInteger(0, trend_id, OBJPROP_WIDTH, TrendLineWidth);
                  ObjectSetInteger(0, trend_id, OBJPROP_RAY, DrawRays);
               }
            }
            lineCount++;         
         } 

         for(k = 10; k > 0; k--) lastLowIdx[k] = lastLowIdx[k-1];
      }

      if(high > 0) last = UP;
      if(low > 0)  last = DOWN;
   }
   
   return(0);
}