/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/

#property indicator_separate_window
#property indicator_minimum -1.3
#property indicator_maximum 1.3
#property indicator_buffers 1
#property indicator_color1 Lime
#property indicator_level1 0.9
#property indicator_level2 -0.9
#property indicator_level3 0.75
#property indicator_level4 -0.75

extern int TotalBars = 700;
extern int SnakeRange = 14;
extern int FilterPeriod = 21;
extern double MartFiltr = 2.0;
extern int PriceConst = 6;

extern bool Email = true;
extern double uroven1 = 0.75;
extern double uroven2 = -0.75;
extern int IntervalEmailMinut = 120;
extern int TochnostPeresech = 0.05;

double g_ibuf_100[];
double gda_104[];
double g_ibuf_108[];
int gi_112 = 14;
double gda_116[];
double gd_120;
int gia_128[];
int gia_132[];
int trade;int tm;
void MartAxis(int ai_0) {
   int li_4;
   int l_count_8;
   int li_16;
   double ld_24;
   gda_104[ai_0] = iMA(NULL, 0, SnakeRange + 1, 0, MODE_LWMA, PriceConst, ai_0);
   for (int li_20 = ai_0 + SnakeRange + 2; li_20 > ai_0; li_20--) {
      ld_24 = 0.0;
      li_4 = 0;
      l_count_8 = 0;
      for (int li_12 = li_20 + SnakeRange; li_12 >= li_20; li_12--) {
         l_count_8++;
         ld_24 += l_count_8 * SnakePrice(li_12);
         li_4 += l_count_8;
      }
      li_16 = li_20 - SnakeRange;
      if (li_16 < ai_0) li_16 = ai_0;
      while (li_12 >= li_16) {
         l_count_8--;
         ld_24 += l_count_8 * SnakePrice(li_12);
         li_4 += l_count_8;
         li_12--;
      }
      gda_104[li_20] = ld_24 / li_4;
   }
}

double SnakePrice(int ai_0) {
   switch (PriceConst) {
   case PRICE_CLOSE:
      return (Close[ai_0]);
   case PRICE_OPEN:
      return (Open[ai_0]);
   case PRICE_HIGH:
      return (High[ai_0]);
   case PRICE_LOW:
      return (Low[ai_0]);
   case PRICE_MEDIAN:
      return ((High[ai_0] + Low[ai_0]) / 2.0);
   case PRICE_TYPICAL:
      return ((Close[ai_0] + High[ai_0] + Low[ai_0]) / 3.0);
   case PRICE_WEIGHTED:
      return ((2.0 * Close[ai_0] + High[ai_0] + Low[ai_0]) / 4.0);
   }
   return (Close[ai_0]);
}

void SmoothOverMart(int ai_0) {
   double ld_4 = gda_104[ArrayMaximum(gda_104, FilterPeriod, ai_0)];
   double ld_12 = gda_104[ArrayMinimum(gda_104, FilterPeriod, ai_0)];
   g_ibuf_108[ai_0] = (2.0 * (MartFiltr + 2.0) * gda_104[ai_0] - (ld_4 + ld_12)) / 2.0 / (MartFiltr + 1.0);
}

double SpearmanRankCorrelation(double ada_0[], int ai_4) {
   double ld_16;
   for (int l_index_24 = 0; l_index_24 < ai_4; l_index_24++) ld_16 += MathPow(ada_0[l_index_24] - l_index_24 - 1.0, 2);
   double ld_ret_8 = 1 - 6.0 * ld_16 / (MathPow(ai_4, 3) - ai_4);
   return (ld_ret_8);
}

void RankPrices(int aia_0[]) {
   double ld_4;
   double ld_12;
   int l_index_24;
   int li_32;
   int li_36;
   int li_40;
   double lda_44[];
   ArrayResize(lda_44, gi_112);
   ArrayCopy(gia_132, aia_0);
   for (int l_index_20 = 0; l_index_20 < gi_112; l_index_20++) lda_44[l_index_20] = l_index_20 + 1;
   ArraySort(gia_132, WHOLE_ARRAY, 0, MODE_DESCEND);
   for (l_index_20 = 0; l_index_20 < gi_112 - 1; l_index_20++) {
      if (gia_132[l_index_20] == gia_132[l_index_20 + 1]) {
         li_32 = gia_132[l_index_20];
         l_index_24 = l_index_20 + 1;
         li_36 = 1;
         ld_12 = l_index_20 + 1;
         while (l_index_24 < gi_112) {
            if (gia_132[l_index_24] != li_32) break;
            li_36++;
            ld_12 += l_index_24 + 1;
            l_index_24++;
         }
         ld_4 = li_36;
         ld_12 /= ld_4;
         for (int li_28 = l_index_20; li_28 < l_index_24; li_28++) lda_44[li_28] = ld_12;
         l_index_20 = l_index_24;
      }
   }
   for (l_index_20 = 0; l_index_20 < gi_112; l_index_20++) {
      li_40 = aia_0[l_index_20];
      for (l_index_24 = 0; l_index_24 < gi_112; l_index_24++) {
         if (li_40 == gia_132[l_index_24]) {
            gda_116[l_index_20] = lda_44[l_index_24];
            break;
         }
      }
   }
}

int init() {
   IndicatorBuffers(3);
   SetIndexBuffer(0, g_ibuf_100);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(1, gda_104);
   SetIndexStyle(1, DRAW_NONE);
   SetIndexBuffer(2, g_ibuf_108);
   SetIndexStyle(2, DRAW_NONE);
   ArrayResize(gda_116, gi_112);
   ArrayResize(gia_128, gi_112);
   ArrayResize(gia_132, gi_112);
   if (gi_112 > 30) IndicatorShortName("Decrease rangeN input!");
   else IndicatorShortName("FXPROFITS( SR:" + SnakeRange + ", FP:" + FilterPeriod + " )");
   gd_120 = MathPow(10, Digits);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int li_8;
   int li_12;
   int li_16;
   int li_20 = TotalBars;
   if (gi_112 > 30) return (-1);
   if (li_20 == 0) {
      li_8 = TotalBars - (gi_112 + FilterPeriod + SnakeRange + 4);
      li_12 = TotalBars - (SnakeRange + 2);
      li_16 = TotalBars - (FilterPeriod + SnakeRange + 3);
   }
   if (li_20 > 0) {
      li_8 = TotalBars + 1;
      li_12 = li_8;
      li_16 = li_8;
   }
   for (int li_0 = li_12; li_0 >= 0; li_0--) MartAxis(li_0);
   for (li_0 = li_16; li_0 >= 0; li_0--) SmoothOverMart(li_0);
   for (li_0 = li_8; li_0 >= 0; li_0--) {
      for (int l_index_4 = 0; l_index_4 < gi_112; l_index_4++) gia_128[l_index_4] = (g_ibuf_108[li_0 + l_index_4]) * gd_120;
      RankPrices(gia_128);
      g_ibuf_100[li_0] = SpearmanRankCorrelation(gda_116, gi_112);
      if (g_ibuf_100[li_0] > 1.0) g_ibuf_100[li_0] = 1.0;
      if (g_ibuf_100[li_0] < -1.0) g_ibuf_100[li_0] = -1.0;
   }
   
   
 if(Email == true)
 {
 if(trade==0&&g_ibuf_100[0]>=uroven1)trade=1;
 if(trade==0&&g_ibuf_100[0]<=uroven2)trade=2; 
 if((trade==2||trade==0)&&(g_ibuf_100[0]>uroven1-TochnostPeresech)&&(g_ibuf_100[0]<uroven1+TochnostPeresech)&&(TimeCurrent()>tm+IntervalEmailMinut*60)){trade=1;tm=TimeCurrent();SendMail("i-Regr","Сработала верхняя граница.  "+g_ibuf_100[0]);}
 if((trade==1||trade==0)&&(g_ibuf_100[0]<uroven2+TochnostPeresech)&&(g_ibuf_100[0]>uroven2-TochnostPeresech)&&(TimeCurrent()>tm+IntervalEmailMinut*60)){trade=2;tm=TimeCurrent();SendMail("i-Regr","Сработала нижняя граница.  "+g_ibuf_100[0]);}
 }
   
   
   
   return (0);
}