//-----------------------------------------------------------------------------
//                                                 Mn Receidivist             |
//-----------------------------------------------------------------------------

#property copyright "Mn"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Aqua
#property indicator_color4 White
#property indicator_color5 Aqua
#property indicator_color6 White
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  1
#property indicator_width4  1
#property indicator_width5  4
#property indicator_width6  4
//extern int mBarsToCheck = 5000;      // bars to check for previous level
extern int nlines=20;                // Max lines to draw
extern color SupportColor = Orange;
extern color ResistanceColor = Blue;
extern bool Alertactive=true;
extern double mGap = 2;
//extern bool mRecidAlert = true;

double mHighR[], mLowS[], RecidLo[], RecidHi[], RecidCl[], RecidOp[] , Support[] , Resistance[];
double mNewLvl, mLastH, mLastL;
double Reslines[0],Suplines[0];
int mTime = 0;
bool mResistReset = false, mSupportReset = false;

//-----------------------------------------------------------------------------
void init() {
  SetIndexBuffer(0, mHighR);
  SetIndexBuffer(1, mLowS);
  SetIndexBuffer(2, RecidLo);
  SetIndexBuffer(3, RecidHi);
  SetIndexBuffer(4, RecidCl);
  SetIndexBuffer(5, RecidOp);
  SetIndexBuffer(6, Support);
  SetIndexBuffer(7, Resistance);
    
  SetIndexEmptyValue(0, 0);
  SetIndexEmptyValue(1, 0);
  SetIndexEmptyValue(2, 0);
  SetIndexEmptyValue(3, 0);
  SetIndexEmptyValue(4, 0);
  SetIndexEmptyValue(5, 0);
  SetIndexEmptyValue(6, 0);
  SetIndexEmptyValue(7, 0);
  
  SetIndexStyle(0, DRAW_ARROW);
  SetIndexArrow(0, 234);

  SetIndexStyle(1, DRAW_ARROW);
  SetIndexArrow(1, 233);  
  SetIndexStyle(2, DRAW_HISTOGRAM);
  SetIndexStyle(3, DRAW_HISTOGRAM);
  SetIndexStyle(4, DRAW_HISTOGRAM);
  SetIndexStyle(5, DRAW_HISTOGRAM);

  /*ObjectCreate("mRecidLineR", OBJ_HLINE, 0, 0, 0, 0, 0);
  ObjectSet("mRecidLineR", OBJPROP_STYLE, STYLE_DOT);
  ObjectSet("mRecidLineR", OBJPROP_COLOR, Blue);
  ObjectCreate("mRecidLineS", OBJ_HLINE, 0, 0, 0, 0, 0);
  ObjectSet("mRecidLineS", OBJPROP_STYLE, STYLE_DOT);
  ObjectSet("mRecidLineS", OBJPROP_COLOR, Red);

  ObjectCreate("mRecidDets", OBJ_LABEL, 0, 0, 0, 0, 0);
  ObjectSet("mRecidDets", OBJPROP_CORNER, 1);
  ObjectSet("mRecidDets", OBJPROP_XDISTANCE, 2);
  ObjectSet("mRecidDets", OBJPROP_YDISTANCE, 2);
  ObjectCreate("mRecidDets2", OBJ_LABEL, 0, 0, 0, 0, 0);
  ObjectSet("mRecidDets2", OBJPROP_CORNER, 1);
  ObjectSet("mRecidDets2", OBJPROP_XDISTANCE, 2);
  ObjectSet("mRecidDets2", OBJPROP_YDISTANCE, 17);
*/
  mGap = mGap * Point;
  if(Digits == 3 || Digits == 5)
  mGap *= 10;

//  mLastH = Open[20]; mLastL = Open[20];
  
  return(0);  
}

//-----------------------------------------------------------------------------
void start() 
{
  int mCounted = IndicatorCounted();
  int y;
  if(mCounted < 0) return (-1); 
  if(mCounted > 0) mCounted--;
  int mLimit = Bars - mCounted;
  bool breaked=false;

ArraySort(Reslines,WHOLE_ARRAY,0,MODE_ASCEND);
ArraySort(Suplines,WHOLE_ARRAY,0,MODE_DESCEND);

for (y=0;y<=ArrayRange(Suplines,0);y++)
{
   if (Open[0]<Suplines[y])
      {
      if (Alertactive) Alert(StringConcatenate("Break Support At:",Suplines[y]));
      mLimit = Bars-1;
      breaked=true;
      }
}
if (breaked)
   {
   for(y=ObjectsTotal();y>=0;y--)
      {
      if(StringSubstr(ObjectName(y),0,13)=="ResitanceLine")ObjectDelete(ObjectName(y));
      if(StringSubstr(ObjectName(y),0,11)=="SupportLine")ObjectDelete(ObjectName(y));
      }
   ArrayInitialize(Reslines,0);
   ArrayInitialize(Suplines,0);
   ArrayResize(Reslines,0);
   ArrayResize(Suplines,0);
   }

      breaked=false;   
         
for (y=0;y<=ArrayRange(Reslines,0);y++)
{
   if ((Open[0]>Reslines[y])&&(Reslines[y]>0))
      {
      if (Alertactive) Alert(StringConcatenate("Break Resistance At:",Reslines[y]));
      mLimit = Bars-1;
      breaked=true;      
      }
}      
if (breaked)
   {
   for(y=ObjectsTotal();y>=0;y--)
   {
   if(StringSubstr(ObjectName(y),0,13)=="ResitanceLine")ObjectDelete(ObjectName(y));
   if(StringSubstr(ObjectName(y),0,11)=="SupportLine")ObjectDelete(ObjectName(y));
   }
   ArrayInitialize(Reslines,0);
   ArrayInitialize(Suplines,0);
   ArrayResize(Reslines,0);
   ArrayResize(Suplines,0);
   }      
  
      breaked=false;     
      
  for (int i = mLimit; i >= 0; i--) 
   {
    RecidLo[i] = 0; RecidHi[i] = 0; RecidCl[i] = 0; RecidOp[i] = 0; 
    if (Open[i+1] == High[i+1]) 
      {     // Down Bar
        mHighR[i+1] = High[i+1] + mGap;
//        mLastH = mHighR[i+1] - mGap;

         RecidLo[i+1] = High[i+1]; 
         RecidHi[i+1] = Low[i+1]; 
         RecidCl[i+1] = Open[i+1];
         RecidOp[i+1] = Close[i+1];
         Resistance[i+1] = Open[i+1];
/*         
        if(!mResistReset)
          {
           ObjectSet("mRecidLineR", OBJPROP_PRICE1, mLastH);
           ObjectSetText("mRecidDets", DoubleToStr(mLastH, Digits) + " Bar 1", 8, "Arial", Blue);
           mSupportReset = false;
          }
*/      }

    if (Open[i+1] == Low[i+1]) 
      {     // Up bar
        mLowS[i+1] = Low[i+1] - mGap; 
//        mLastL = mLowS[i+1] + mGap;
        RecidLo[i+1] = Open[i+1]; RecidHi[i+1] = High[i+1]; RecidCl[i+1] = Close[i+1];
       
         RecidLo[i+1] = Low[i+1]; 
         RecidHi[i+1] = High[i+1]; 
         RecidCl[i+1] = Open[i+1];
         RecidOp[i+1] = Close[i+1];
         Support[i+1] = Open[i+1];
         
/*        if(!mSupportReset)
          {
           ObjectSet("mRecidLineS", OBJPROP_PRICE1, mLastL);
           ObjectSetText("mRecidDets2", DoubleToStr(mLastL, Digits) + " Bar 1", 8, "Arial", Red);
           mResistReset = false;
         }
         
*/      }
    
    
/*    if(i == 0)
     {
      if((Close[1] > mLastH) && mLastH > 0)
       {
        mNewLvl = GetPrevLvl(1, Close[1]);
        mResistReset = true;
        mSupportReset = false;
        mTime = Time[0];
       }
      if(Close[1] < mLastL)
       {
        mNewLvl = GetPrevLvl(2, Close[1]);
        mResistReset = false;
        mSupportReset = true;
        mTime = Time[0];
       }
     }  // if(i == 0)
*/     
   }  // for i 

for(y=ObjectsTotal();y>=0;y--)
{
if(StringSubstr(ObjectName(y),0,13)=="ResitanceLine")ObjectDelete(ObjectName(y));
if(StringSubstr(ObjectName(y),0,11)=="SupportLine")ObjectDelete(ObjectName(y));
}
ArrayResize(Reslines,0);
ArrayResize(Suplines,0);

// Resistance Lines
   
  for (i = Bars; i >= 0; i--) 
   {
   if (Resistance[i+1]!=0)
      {
      breaked=false;
      for (int i2 = i; i2 >= 0; i2--)
         {
         if (Open[i2]>Resistance[i+1]) {breaked=true; break;}
         }
      if (!breaked)
         {
         ArrayResize(Reslines,ArrayRange(Reslines,0)+1);
         Reslines[ArrayRange(Reslines,0)-1]=Resistance[i+1];
         }  
      }   
   }
ArraySort(Reslines,WHOLE_ARRAY,0,MODE_ASCEND);
/*int c=0;
  for (i = 0; i < ArrayRange(Reslines,0); i++) 
   {
   if (High[0]>Reslines[i]){ Reslines[i]=0; c++;}
   }

ArraySort(Reslines,WHOLE_ARRAY,0,MODE_DESCEND);
ArrayResize(Reslines,ArrayRange(Reslines,0)-c);
ArraySort(Reslines,WHOLE_ARRAY,0,MODE_ASCEND);
*/

if (nlines<=ArrayRange(Reslines,0)) int lines=nlines;
else lines=ArrayRange(Reslines,0);


  for (i = 0; i < lines; i++) 
   {
   
   string name=StringConcatenate("ResitanceLine",i);
     ObjectCreate(name, OBJ_HLINE, 0, 0, 0, 0, 0);
     ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
     ObjectSet(name, OBJPROP_COLOR, ResistanceColor);
     ObjectSet(name, OBJPROP_PRICE1, Reslines[i]);

   }
 
// Support Lines
   
  for (i = Bars; i >= 0; i--) 
   {
   if (Support[i+1]!=0)
      {
      breaked=false;
      for (i2 = i; i2 >= 0; i2--)
         {
         if (Open[i2]<Support[i+1]) {breaked=true; break;}
         }
      if (!breaked)
         {
         ArrayResize(Suplines,ArrayRange(Suplines,0)+1);
         Suplines[ArrayRange(Suplines,0)-1]=Support[i+1];
         }  
      }   
   }

ArraySort(Suplines,WHOLE_ARRAY,0,MODE_DESCEND);
/*c=0;
  for (i = 0; i < ArrayRange(Suplines,0); i++) 
   {
   if (Low[0]<Suplines[i]){ Suplines[i]=0; c++;}
   }

ArraySort(Suplines,WHOLE_ARRAY,0,MODE_ASCEND);
ArrayResize(Suplines,ArrayRange(Suplines,0)-c);
ArraySort(Suplines,WHOLE_ARRAY,0,MODE_DESCEND);

*/
if (nlines<=ArrayRange(Suplines,0)) lines=nlines;
else lines=ArrayRange(Suplines,0);

  for (i = 0; i < lines; i++) 
   {
   name=StringConcatenate("SupportLine",i);
     ObjectCreate(name, OBJ_HLINE, 0, 0, 0, 0, 0);
     ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
     ObjectSet(name, OBJPROP_COLOR, SupportColor);
     ObjectSet(name, OBJPROP_PRICE1, Suplines[i]);

   }
    
  return(0);  
}

//-----------------------------------------------------------------------------
void deinit()
 {
   ObjectDelete("mRecidLineR");
   ObjectDelete("mRecidLineS");
   ObjectDelete("mRecidDets");
   ObjectDelete("mRecidDets2");

for(int y=ObjectsTotal();y>=0;y--)
{
if(StringSubstr(ObjectName(y),0,13)=="ResitanceLine")ObjectDelete(ObjectName(y));
if(StringSubstr(ObjectName(y),0,11)=="SupportLine")ObjectDelete(ObjectName(y));
}

Comment("");

  return(0);  
 }

//-----------------------------------------------------------------------------
/*double GetPrevLvl(int mRorS, double mPrice)
 {
   for(int m = 1; m < mBarsToCheck; m++)
    {
      if(mRorS == 1)
        {
          if(mLastH != 0 && mHighR[m] != 0 && (mHighR[m] - mGap) > mPrice)
            {
             ObjectSet("mRecidLineR", OBJPROP_PRICE1, mHighR[m] - mGap);
             ObjectSetText("mRecidDets", DoubleToStr(mHighR[m] - mGap, Digits) + " Bar " + m, 8, "Arial", Blue);
             if(mRecidAlert && Time[0] > mTime)
                Alert(Symbol(), " OneWay crossed Resist");
             mLastH = mHighR[m] - mGap;
             return((mHighR[m] - mGap));
            }
        }
      else     //mRorS == 2
        {
          if(mLastL != 0 && mLowS[m] != 0 && (mLowS[m] + mGap) < mPrice)
            {
             ObjectSet("mRecidLineS", OBJPROP_PRICE1, mLowS[m] + mGap);
             ObjectSetText("mRecidDets2", DoubleToStr(mLowS[m] + mGap, Digits) + " Bar " + m, 8, "Arial", Red);
             if(mRecidAlert && Time[0] > mTime)
               Alert(Symbol(), " OneWay crossed Support");
             mLastL = mLowS[m] + mGap;
             return((mLowS[m] + mGap));
            }
        }
    }

  return(-1);  
 }

//-----------------------------------------------------------------------------
*/