//+------------------------------------------------------------------+
//|                                               Stoch-MTF-True.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"

#property indicator_separate_window
#property indicator_minimum -15.0
#property indicator_maximum 115.0
#property indicator_buffers 5
#property indicator_color1 DimGray
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Black
#property indicator_color5 Black

extern int eintTF = 60;
extern int eintStochK = 14;
extern int eintStochSlow = 3;
double G_ibuf_88[];
double G_ibuf_92[];
double G_ibuf_96[];
double G_ibuf_100[];
double G_ibuf_104[];
int G_price_field_108 = 1;

int init() {
   string Ls_0 = getTfForDisplay(eintTF);
   string Ls_8 = "Stoch-MTF-True(" + Ls_0 + "," + eintStochK + "," + eintStochSlow + "," + getPriceForDisplay(G_price_field_108) + ")";
   IndicatorShortName(Ls_8);
   SetIndexBuffer(0, G_ibuf_88);
   SetIndexLabel(0, Ls_0 + " Step Curr");
   SetIndexBuffer(1, G_ibuf_92);
   SetIndexLabel(1, NULL);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 159);
   SetIndexBuffer(2, G_ibuf_96);
   SetIndexLabel(2, NULL);
   SetIndexStyle(2, DRAW_ARROW);
   SetIndexArrow(2, 159);
   SetIndexBuffer(3, G_ibuf_100);
   SetIndexLabel(3, Ls_0 + " Step Prev");
   SetIndexStyle(3, DRAW_NONE);
   SetIndexBuffer(4, G_ibuf_104);
   SetIndexLabel(4, Ls_0 + " Real Curr");
   SetIndexStyle(4, DRAW_NONE);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int Lia_12[];
   int ind_counted_0 = IndicatorCounted();
   int shift_8 = 0;
   ArraySetAsSeries(Lia_12, TRUE);
   if (ind_counted_0 < 0) return (-1);
   int Li_16 = MathMax(Bars - ind_counted_0, eintTF / Period());
   ArrayCopySeries(Lia_12, 5, Symbol(), eintTF);
   shift_8 = iBarShift(Symbol(), eintTF, Time[0]);
   for (int index_4 = 0; index_4 < Li_16; index_4++) {
      if (Time[index_4] < Lia_12[shift_8]) shift_8++;
      G_ibuf_88[index_4] = iStochastic(Symbol(), eintTF, eintStochK, 3, eintStochSlow, MODE_SMA, G_price_field_108, MODE_MAIN, shift_8);
      G_ibuf_100[index_4] = iStochastic(Symbol(), eintTF, eintStochK, 3, eintStochSlow, MODE_SMA, G_price_field_108, MODE_MAIN, shift_8 + 1);
   }
   for (index_4 = Li_16; index_4 >= 0; index_4--) {
      G_ibuf_104[index_4] = calcStoch(index_4);
      if (G_ibuf_104[index_4] > G_ibuf_100[index_4]) {
         G_ibuf_92[index_4] = G_ibuf_104[index_4];
         G_ibuf_96[index_4] = EMPTY_VALUE;
      } else {
         if (G_ibuf_104[index_4] < G_ibuf_100[index_4]) {
            G_ibuf_92[index_4] = EMPTY_VALUE;
            G_ibuf_96[index_4] = G_ibuf_104[index_4];
         } else {
            G_ibuf_92[index_4] = G_ibuf_92[index_4 + 1];
            G_ibuf_96[index_4] = G_ibuf_96[index_4 + 1];
         }
      }
   }
   return (0);
}

string getPriceForDisplay(int Ai_0) {
   string Ls_ret_4 = "Unknown";
   switch (Ai_0) {
   case 0:
      Ls_ret_4 = "Low/High";
      break;
   case 1:
      Ls_ret_4 = "Close/Close";
   }
   return (Ls_ret_4);
}

string getTfForDisplay(int Ai_0) {
   string Ls_ret_4 = "M" + Ai_0;
   switch (Ai_0) {
   case 1:
      Ls_ret_4 = "M1";
      break;
   case 5:
      Ls_ret_4 = "M5";
      break;
   case 15:
      Ls_ret_4 = "M15";
      break;
   case 30:
      Ls_ret_4 = "M30";
      break;
   case 60:
      Ls_ret_4 = "H1";
      break;
   case 240:
      Ls_ret_4 = "H4";
      break;
   case 1440:
      Ls_ret_4 = "D1";
      break;
   case 10080:
      Ls_ret_4 = "W1";
      break;
   case 43200:
      Ls_ret_4 = "MN1";
   }
   return (Ls_ret_4);
}

double calcStoch(int Ai_0) {
   double Lda_12[];
   double Lda_16[];
   double Lda_20[];
   double Lda_24[];
   double Lda_28[];
   double Ld_ret_4 = 0.0;
   double Ld_32 = 0.0;
   double Ld_40 = 0.0;
   int shift_48 = iBarShift(Symbol(), eintTF, Time[Ai_0]);
   ArrayResize(Lda_12, eintStochSlow);
   ArrayResize(Lda_16, eintStochSlow);
   ArrayResize(Lda_20, eintStochSlow);
   ArrayResize(Lda_28, eintStochK + eintStochSlow);
   ArrayCopySeries(Lda_24, 3, Symbol(), eintTF);
   ArrayCopy(Lda_28, Lda_24, 0, shift_48, eintStochK + eintStochSlow);
   Lda_28[0] = Close[Ai_0];
   for (int Li_52 = eintStochSlow - 1; Li_52 >= 0; Li_52--) {
      Lda_20[Li_52] = Lda_28[Li_52];
      Lda_12[Li_52] = Lda_28[ArrayMinimum(Lda_28, eintStochK, Li_52)];
      Lda_16[Li_52] = Lda_28[ArrayMaximum(Lda_28, eintStochK, Li_52)];
      Ld_40 += Lda_20[Li_52] - Lda_12[Li_52];
      Ld_32 += Lda_16[Li_52] - Lda_12[Li_52];
   }
   if (Ld_32 == 0.0) Ld_ret_4 = 100.0;
   else Ld_ret_4 = 100.0 * Ld_40 / Ld_32;
   return (Ld_ret_4);
}
