
#property copyright "Copyright © 2009, Vova Internet."
#property link      ""

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1 CLR_NONE
#property indicator_color2 CLR_NONE
#property indicator_color3 CLR_NONE
#property indicator_color4 Red
#property indicator_color5 Yellow
#property indicator_color6 Red
#property indicator_color7 Yellow

extern int period = 27;
extern int MA_Period = 14;
double g_ibuf_84[];
double g_ibuf_88[];
double g_ibuf_92[];
double g_ibuf_96[];
double g_ibuf_100[];
double g_ibuf_104[];
double g_ibuf_108[];
string gs_112 = "";
string gs_120 = "MPG End Trend V1.2   Copyright © 2009, Mehdi Maktoubian.";

int init() {
   int li_0 = xalitazi();
   if (li_0 == 0) return (-1);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexStyle(5, DRAW_HISTOGRAM);
   SetIndexStyle(6, DRAW_HISTOGRAM);
   IndicatorDigits(Digits + 1);
   SetIndexBuffer(0, g_ibuf_84);
   SetIndexBuffer(1, g_ibuf_88);
   SetIndexBuffer(2, g_ibuf_92);
   SetIndexBuffer(3, g_ibuf_96);
   SetIndexBuffer(4, g_ibuf_100);
   SetIndexBuffer(5, g_ibuf_104);
   SetIndexBuffer(6, g_ibuf_108);
   IndicatorShortName(gs_120);
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL);
   return (0);
}

int start() {
   double ld_0;
   double ld_8;
   double ld_16;
   int li_24 = IndicatorCounted();
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_unused_44 = 0;
   double ld_unused_52 = 0;
   double ld_60 = 0;
   double ld_unused_68 = 0;
   double l_low_76 = 0;
   double l_high_84 = 0;
   creataalltext();
   RefreshRates();
   int li_92 = 16777215;
   if (li_24 > 0) li_24--;
   int l_bars_96 = Bars;
   for (int li_100 = 0; li_100 < l_bars_96; li_100++) {
      l_high_84 = High[iHighest(NULL, 0, MODE_HIGH, period, li_100)];
      l_low_76 = Low[iLowest(NULL, 0, MODE_LOW, period, li_100)];
      ld_16 = (High[li_100] + Low[li_100]) / 2.0;
      ld_28 = 0.66 * ((ld_16 - l_low_76) / (l_high_84 - l_low_76) - 0.5) + 0.67 * ld_36;
      ld_28 = MathMin(MathMax(ld_28, -0.999), 0.999);
      g_ibuf_84[li_100] = MathLog((ld_28 + 1.0) / (1 - ld_28)) / 2.0 + ld_60 / 2.0;
      ld_36 = ld_28;
      ld_60 = g_ibuf_84[li_100];
   }
   bool li_104 = TRUE;
   for (li_100 = l_bars_96 - 2; li_100 >= 0; li_100--) {
      ld_8 = g_ibuf_84[li_100];
      ld_0 = g_ibuf_84[li_100 + 1];
      if ((ld_8 < 0.0 && ld_0 > 0.0) || ld_8 < 0.0) li_104 = FALSE;
      if ((ld_8 > 0.0 && ld_0 < 0.0) || ld_8 > 0.0) li_104 = TRUE;
      if (!li_104) {
         g_ibuf_92[li_100] = ld_8;
         g_ibuf_88[li_100] = 0.0;
         gs_112 = "Stil SHORT";
         li_92 = 65535;
      } else {
         g_ibuf_88[li_100] = ld_8;
         g_ibuf_92[li_100] = 0.0;
         gs_112 = "Stil LONG";
         li_92 = 65280;
      }
   }
   for (li_100 = Bars; li_100 >= 0; li_100--) g_ibuf_96[li_100] = g_ibuf_92[li_100] + g_ibuf_88[li_100];
   for (int li_108 = 0; li_108 < Bars; li_108++) g_ibuf_100[li_108] = iMAOnArray(g_ibuf_96, 0, MA_Period, 0, MODE_EMA, li_108);
   if (g_ibuf_100[0] < g_ibuf_96[0]) {
      g_ibuf_104[0] = -0.2;
      g_ibuf_108[0] = 0;
   } else {
      if (g_ibuf_100[0] > g_ibuf_96[0]) {
         g_ibuf_104[0] = 0;
         g_ibuf_108[0] = 0.2;
      }
   }
   settext("xboxforex_ind", gs_112, 12, li_92, 10, 15);
   return (0);
}

int xalitazi() {
   bool li_ret_0;
   int li_4 = 2010;
   int li_8 = 11;
   int l_year_12 = TimeYear(TimeCurrent());
   int l_month_16 = TimeMonth(TimeCurrent());
   if (l_year_12 <= li_4 && l_month_16 <= li_8) li_ret_0 = TRUE;
   else li_ret_0 = TRUE;
   return (li_ret_0);
}

void creataalltext() {
   createtext("xboxforex_ind");
   settext("xboxforex_ind", "", 12, White, 10, 15);
}

void createtext(string a_name_0) {
   ObjectCreate(a_name_0, OBJ_LABEL, WindowFind(gs_120), 0, 0);
}

void settext(string a_name_0, string a_text_8, int a_fontsize_16, color a_color_20, int a_x_24, int a_y_28) {
   ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_24);
   ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_28);
   ObjectSetText(a_name_0, a_text_8, a_fontsize_16, "Arial", a_color_20);
}