/*
  Magic Indicator 2009
*/

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 Goldenrod
#property indicator_color4 Blue
#property indicator_color5 Red
#property indicator_width1 2
#property indicator_width2 2

double g_ibuf_76[];
double g_ibuf_80[];
double g_ibuf_84[];
double g_ibuf_88[];
double g_ibuf_92[];
extern int Long_Trend = 14;
extern int Short_Trend = 5;
extern int Short_MA = 5;
extern int Long_MA = 14;
extern bool Zero_Cross_Alert=True;
extern int   LineSize1=3;
extern int   LineSize2=3;
bool gi_104 = FALSE;
bool gi_108 = FALSE;
string gs_unused_120 = "SMA- 0, EMA - 1, SMMA - 2, LWMA- 3";
string gs_unused_128 = "settings TypeMA1=0, TypeMA2=3";
int g_ma_method_136 = MODE_SMA;
int g_ma_method_140 = MODE_LWMA;
extern double CCI_Long = 25.0;
extern double CCI_Turbo = 0.0;
double gd_unused_160 = 0.0;
double gd_168 = 0.0;
double gd_176 = 0.0;
double gd_184 = 0.0;
int gi_unused_192 = 100;
int gi_unused_196 = 100;

int init() {
   SetIndexStyle(0, DRAW_HISTOGRAM, 0,LineSize1, Lime);
   SetIndexBuffer(0, g_ibuf_76);
   SetIndexStyle(1, DRAW_HISTOGRAM, 0,LineSize1, Red);
   SetIndexBuffer(1, g_ibuf_80);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexLabel(2, "line");
   SetIndexBuffer(2, g_ibuf_84);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexLabel(3, "MA1 " + Short_MA);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexLabel(4, "MA2 " + Long_MA);
   SetIndexBuffer(3, g_ibuf_88);
   SetIndexBuffer(4, g_ibuf_92);
   return (0);
}

  int deinit() {
   for (int l_count_0 = 0; l_count_0 < Bars; l_count_0++) {
      ObjectDelete("SELL SIGNAL: " + DoubleToStr(l_count_0, 0));
      ObjectDelete("BUY SIGNAL: " + DoubleToStr(l_count_0, 0));
      ObjectDelete("EXIT: " + DoubleToStr(l_count_0, 0));
   }
   return (0);
}

int start() {
   double l_price_12;
   double ld_unused_20;
   int li_0 = IndicatorCounted();
   static datetime prevtime = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_44 = 1.2;
   if (li_0 > 0) li_0--;
   int li_8 = Bars - li_0;
   if (gi_104) li_8 = 100;
   for (int li_4 = 0; li_4 < li_8; li_4++) {
      ld_36 = High[iHighest(NULL, 0, MODE_CLOSE, Long_Trend, li_4)];
      ld_28 = Low[iLowest(NULL, 0, MODE_CLOSE, Long_Trend, li_4)];
      switch (Short_Trend) {
      case 1:
         l_price_12 = Open[li_4];
         break;
      case 2:
         l_price_12 = Close[li_4];
         break;
      case 3:
         l_price_12 = High[li_4];
         break;
      case 4:
         l_price_12 = Low[li_4];
         break;
      case 5:
         l_price_12 = (High[li_4] + Low[li_4] + Close[li_4]) / 3.0;
         break;
      case 6:
         l_price_12 = (Open[li_4] + High[li_4] + Low[li_4] + Close[li_4]) / 4.0;
         break;
      case 7:
         l_price_12 = (Open[li_4] + Close[li_4]) / 2.0;
         break;
      default:
         l_price_12 = (High[li_4] + Low[li_4]) / 2.0;
      }
      CCI_Long = 0.66 * ((l_price_12 - ld_28) / (ld_36 - ld_28) - 0.5) + 0.67 * CCI_Turbo;
      CCI_Long = MathMin(MathMax(CCI_Long, -0.999), 0.999);
      gd_168 = MathLog((CCI_Long + 1.0) / (1 - CCI_Long)) / 2.0 + gd_176 / 2.0;
      g_ibuf_76[li_4] = 0;
      g_ibuf_80[li_4] = 0;
      if (gd_168 < 0.0 && gd_176 > 0.0) {
         if (gi_108) {
            ObjectCreate("EXIT: " + DoubleToStr(li_4, 0), OBJ_TEXT, 0, Time[li_4], l_price_12);
            ObjectSetText("EXIT: " + DoubleToStr(li_4, 0), "EXIT AT " + DoubleToStr(l_price_12, 4), 10, "Arial Bold", Yellow);
         }
         gi_unused_192 = 0;
      }
      if (gd_168 > 0.0 && gd_176 < 0.0) {
         if (gi_108) {
            ObjectCreate("EXIT: " + DoubleToStr(li_4, 0), OBJ_TEXT, 0, Time[li_4], l_price_12);
            ObjectSetText("EXIT: " + DoubleToStr(li_4, 0), "EXIT AT " + DoubleToStr(l_price_12, 4), 10, "Arial Bold", Yellow);
         }
         gi_unused_196 = 0;
      }
      if (gd_168 >= 0.0) {
         g_ibuf_76[li_4] = gd_168;
         g_ibuf_84[li_4] = gd_168;
      } else {
         g_ibuf_80[li_4] = gd_168;
         g_ibuf_84[li_4] = gd_168;
      }
      ld_unused_20 = li_4;
      if (gd_168 < (-ld_44) && gd_168 > gd_176 && gd_176 <= gd_184) {
         if (gi_108) {
            ObjectCreate("SELL SIGNAL: " + DoubleToStr(li_4, 0), OBJ_TEXT, 0, Time[li_4], l_price_12);
            ObjectSetText("SELL SIGNAL: " + DoubleToStr(li_4, 0), "SELL AT " + DoubleToStr(l_price_12, 4), 10, "Arial Bold", Red);
       
         }
        // Alert("MagicInd UP"," on ",Symbol(), " TF "+Period());
         gi_unused_196 = 1;
      }
      if (Zero_Cross_Alert == true) 
      if (prevtime == Time[0]) 
         return(0);
         
      if (gd_168 > ld_44 && gd_168 < gd_176 && gd_176 >= gd_184) {
         if (gi_108) {
            ObjectCreate("BUY SIGNAL: " + DoubleToStr(li_4, 0), OBJ_TEXT, 0, Time[li_4], l_price_12);
            ObjectSetText("BUY SIGNAL: " + DoubleToStr(li_4, 0), "BUY AT " + DoubleToStr(l_price_12, 4), 10, "Arial Bold", Lime);
        
         }
         //Alert("MagicInd DOWN"," on ",Symbol(), " TF "+Period());
         gi_unused_192 = 1;
      }
      CCI_Turbo = CCI_Long;
      gd_184 = gd_176;
      gd_176 = gd_168;
   }
   for (li_4 = 0; li_4 < li_8; li_4++) g_ibuf_88[li_4] = iMAOnArray(g_ibuf_84, Bars, Short_MA, 0, g_ma_method_136, li_4);
   for (li_4 = 0; li_4 < li_8; li_4++) g_ibuf_92[li_4] = iMAOnArray(g_ibuf_88, Bars, Long_MA, 0, g_ma_method_140, li_4);
   return (0);
}