//-----------------------------------------------------------------------------
//                                                 Mn Fractal Store           |
//-----------------------------------------------------------------------------

#property copyright "Mn"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#property indicator_width1  0
#property indicator_width2  0

extern int mSizeL = 4,
           mSizeR = 2;
extern int mHist  = 50;
extern bool mComm = true,
            mAlert = false;

double mBars;
double mPeak[], mTrough[], mPkStore[100][2], mTrStore[100][2];
static int mTime;

//-----------------------------------------------------------------------------
void init()
 {
  SetIndexBuffer(0, mPeak);
  SetIndexBuffer(1, mTrough);
  
  SetIndexEmptyValue(0, 0);
  SetIndexEmptyValue(1, 0);
  
   SetIndexArrow(0, 158); 
   SetIndexStyle(0, DRAW_ARROW, STYLE_DOT);      

   SetIndexArrow(1, 158); 
   SetIndexStyle(1, DRAW_ARROW, STYLE_DOT);

 }

//-----------------------------------------------------------------------------
void start() 
{
   static int mTimer;
  int mCounted = IndicatorCounted();
  if(mCounted < 0) return; 
  if(mCounted > 0) mCounted--;
  int mLimit = MathMin(Bars, mHist) - mCounted;
  
  for (int i = mHist; i >= 0; i--) 
  {
    if(High[i] == High[iHighest(NULL, 0, MODE_HIGH, mSizeL, i)] && 
       iHighest(NULL, 0, MODE_HIGH, mSizeL, i) >= mSizeR &&
       High[i] == High[iHighest(NULL, 0, MODE_HIGH, mSizeR, i - mSizeR +1)]) 
      {
        mPeak[i] = High[i];
        MovePk();
        mPkStore[0][0] = High[i];
        mPkStore[0][1] = Time[i];
      }
    if(Low[i] == Low[iLowest(NULL, 0, MODE_LOW, mSizeL, i)] && 
       iLowest(NULL, 0, MODE_LOW, mSizeL, i) >= mSizeR &&
       Low[i] == Low[iLowest(NULL, 0, MODE_LOW, mSizeR, i - mSizeR +1)]) 
      {
        mTrough[i] = Low[i];
        MoveTr();
        mTrStore[0][0] = Low[i];
        mTrStore[0][1] = Time[i];
      }
      
    if(mPeak[i] == 0)
      mPeak[i] = mPeak[i+1];
    for(int z = 0; z <= mSizeR; z++)
      mPeak[z] = mPkStore[0][0];
    if(mTrough[i] == 0)
      mTrough[i] = mTrough[i+1];
    for(z = 0; z <= mSizeR; z++)
      mTrough[z] = mTrStore[0][0];
   }
   ChartRedraw();
   
   if(mAlert && High[1] < mPkStore[0][0] && Close[0] >= mPkStore[0][0] && Time[0] > mTimer)
     {  
       Alert(Symbol(), " BUY FS ", mPkStore[0][0]);
       mTimer = Time[0];
     }
   if(mAlert && Low[1] > mTrStore[0][0] && Close[0] <= mTrStore[0][0] && Time[0] > mTimer)
     {  
       Alert(Symbol(), " SELL FS ", mTrStore[0][0]);
       mTimer = Time[0];
     }
   
   if(mComm)
     Comment(DoubleToStr(mPkStore[0][0], Digits), "  ", mPkStore[1][0], "  ", mPkStore[2][0], "\n",
           DoubleToStr(mTrStore[0][0], Digits), "  ", mTrStore[1][0], "  ", mTrStore[2][0]);
}

//-----------------------------------------------------------------------------
void deinit()
 {
   Comment(" ");
 }

//-----------------------------------------------------------------------------
void MovePk()
 {
   for(int m = 99; m >0; m--)
     {
       mPkStore[m][0] = mPkStore[m-1][0];
       mPkStore[m][1] = mPkStore[m-1][1];
     }
 
  return;
 }
 
//-----------------------------------------------------------------------------
void MoveTr()
 {
   for(int m = 99; m >0; m--)
     {
       mTrStore[m][0] = mTrStore[m-1][0];
       mTrStore[m][1] = mTrStore[m-1][1];
     }
 
  return;
 }

//-----------------------------------------------------------------------------
