/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2010.01.24, SwingMan"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Lime
#property indicator_color2 Lime
#property indicator_color3 Silver
#property indicator_color4 Silver
#property indicator_color5 Green
#property indicator_color6 Black
#property indicator_color7 Red
#property indicator_color8 Black

string gs_76 = "PAMA GannGrid_indv4.3";
bool gi_84 = TRUE;
extern string ____GridBegin____ = "";
extern bool UseBeginDate = FALSE;
extern string BeginDate = "2010.01.01 00:00";
extern int nBarsBack = 120;
extern string prices = "0=close, 4=median, 5=typical";
extern int Price_Mode = 0;
extern bool Show_GridMatrix = FALSE;
extern bool Show_GannGrid = FALSE;
extern bool Show_HiloArrows = TRUE;
extern bool Show_PriceArrows = FALSE;
extern bool Show_Comments = FALSE;
extern string ____MainGrid____ = "";
extern color MainGrid_Color = Silver;
extern int MainGrid_Style = 2;
extern int MinMaxGrid_Style = 0;
extern int MainGrid_Width = 1;
extern int fontSize = 8;
extern bool Draw_AllGrids = FALSE;
extern bool Draw_AdditionalGrids = FALSE;
extern string ____GannGrid____ = "";
extern color GannGrid_Color = Silver;
extern int GannGrid_Style = 2;
extern int GannGrid_Width = 1;
extern string ____Default_GridParameters____ = "Recomanded GridInterval 35 or 36";
extern int MainGrid_Intervals = 48;
extern double GannGrid_Interval = 8.0;
extern int Text_Shift = 50;
double g_ibuf_224[];
double g_ibuf_228[];
double g_ibuf_232[];
double g_ibuf_236[];
double g_ibuf_240[];
double g_ibuf_244[];
double g_ibuf_248[];
double g_ibuf_252[];
int g_window_256;
string gs_260;
int g_str2time_268;
datetime g_time_272;
datetime g_time_276;
bool gi_280 = FALSE;
int gi_284;
int gi_288;
int gi_292;
int gi_296;
int gi_300;
double gd_304;
double gd_312;
double gd_320;
bool g_bool_328;
int gi_332;

int init() {
   string ls_8;
/*   SetIndexBuffer(0, g_ibuf_224);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "PAMA Price");
   SetIndexBuffer(1, g_ibuf_228);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 159);
   SetIndexLabel(1, NULL);
   SetIndexBuffer(2, g_ibuf_232);
   SetIndexStyle(2, DRAW_LINE, MinMaxGrid_Style, MainGrid_Width, MainGrid_Color);
   SetIndexLabel(2, "maxPrice");
   SetIndexBuffer(3, g_ibuf_236);
   SetIndexStyle(3, DRAW_LINE, MinMaxGrid_Style, MainGrid_Width, MainGrid_Color);
   SetIndexLabel(3, "minPrice");*/
   SetIndexBuffer(4, g_ibuf_240);
   SetIndexStyle(4, DRAW_ARROW);
   SetIndexArrow(4, SYMBOL_ARROWUP);
   SetIndexLabel(4, NULL);
   SetIndexBuffer(5, g_ibuf_244);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexLabel(5, "upLine");
   SetIndexBuffer(6, g_ibuf_248);
   SetIndexStyle(6, DRAW_ARROW);
   SetIndexArrow(6, SYMBOL_ARROWDOWN);
   SetIndexLabel(6, NULL);
   SetIndexBuffer(7, g_ibuf_252);
   SetIndexStyle(7, DRAW_LINE);
   SetIndexLabel(7, "dnLine");
   SetIndexEmptyValue(7, EMPTY_VALUE);
   
   

      
   string ls_0 = gs_76 + "  ";
   if (Price_Mode == 0) ls_8 = "(ClosePrices) ";
   else {
      if (Price_Mode == 4) ls_8 = "(MedianPrices) ";
      else
         if (Price_Mode == 5) ls_8 = "(TypicalPrices) ";
   }
   gs_260 = ls_0 + ls_8;
   IndicatorShortName(gs_260);
   IndicatorDigits(Digits);
   if (UseBeginDate == TRUE) {
      g_str2time_268 = StrToTime(BeginDate + " 00:00");
      nBarsBack = 0;
   } else g_str2time_268 = 0;
   gi_284 = MainGrid_Intervals;
   gi_288 = GannGrid_Interval;
   gi_292 = gi_288 / 2;
   g_bool_328 = UseBeginDate;
   gi_280 = FALSE;
   gi_332 = MarketInfo(Symbol(), MODE_DIGITS);
   if (gi_332 == 5 || gi_332 == 3) gi_332--;
   if (gi_84) g_window_256 = 0;
   else g_window_256 = WindowFind(gs_260);
   

   return (0);
}

int deinit() {
   Delete_GannObjects();
   if (Show_Comments) Comment("");
   for (int l_index_0 = 0; l_index_0 < nBarsBack + 20; l_index_0++) {
      g_ibuf_240[l_index_0] = EMPTY_VALUE;
      g_ibuf_248[l_index_0] = EMPTY_VALUE;
      g_ibuf_244[l_index_0] = EMPTY_VALUE;
      g_ibuf_252[l_index_0] = EMPTY_VALUE;
      g_ibuf_232[l_index_0] = EMPTY_VALUE;
      g_ibuf_236[l_index_0] = EMPTY_VALUE;
   }
   return (0);
}

int start() {

   double ld_0;
   int l_shift_16;
   double ld_24;
   string ls_32;
   string ls_40;
   string ls_48;
   string ls_56;
   double ld_64;
   double ld_72;
   string ls_80;
   string ls_88;
   int li_96;
   int li_104;
   int li_108;
   datetime l_time_112;
   int li_116;
   double ld_120;
   double ld_128;
   if (gi_84 == FALSE)
      if (g_window_256 < 1) g_window_256 = WindowFind(gs_260);
   int li_8 = IndicatorCounted();
   if (li_8 < 0) return (-1);
   if (li_8 > 0) li_8--;
   int li_12 = Bars - li_8;
   g_time_272 = Time[0];
   if (g_time_272 != g_time_276) {
      g_time_276 = g_time_272;
      Delete_GannObjects();
      if ((UseBeginDate == FALSE && g_str2time_268 != 0) || (UseBeginDate == TRUE && li_12 < 4)) {
         nBarsBack = 0;
         gi_280 = FALSE;
         UseBeginDate = TRUE;
         l_shift_16 = iBarShift(Symbol(), Period(), g_str2time_268, FALSE);
         li_12 = l_shift_16;
      }
   }
   if (gi_280 == FALSE) {
      ld_0 = CurrentPrice(Price_Mode, 0);
      gd_304 = ld_0;
      gi_296 = Time[0];
      gd_312 = ld_0;
      gi_300 = Time[0];
   }
   
   
   
   
   for (int li_20 = 0; li_20 < Bars; li_20++) {
   
   
   
   
   
   
      ld_0 = CurrentPrice(Price_Mode, li_20);
      g_ibuf_224[li_20] = ld_0;
      g_ibuf_228[li_20] = ld_0;
      if (gi_280 == FALSE) {
         if (UseBeginDate == TRUE) {
            if (Time[li_20] > g_str2time_268) {
               if (ld_0 > gd_304) {
                  gd_304 = ld_0;
                  gi_296 = Time[li_20];
               }
               if (ld_0 < gd_312) {
                  gd_312 = ld_0;
                  gi_300 = Time[li_20];
               }
               nBarsBack++;
            }
         } else {
            if (UseBeginDate == FALSE) {
               if (li_20 < nBarsBack) {
                  if (ld_0 > gd_304) {
                     gd_304 = ld_0;
                     gi_296 = Time[li_20];
                  }
                  if (ld_0 < gd_312) {
                     gd_312 = ld_0;
                     gi_300 = Time[li_20];
                  }
                  g_str2time_268 = Time[li_20];
               }
            }
         }
      }
   }
   if (gi_280 == FALSE) {
      ld_24 = gd_304 - gd_312;
      gd_320 = NormalizeDouble(ld_24 / gi_284, Digits);
   }
   for (li_20 = 0; li_20 < li_12; li_20++) {
      g_ibuf_232[li_20] = gd_304;
      g_ibuf_236[li_20] = gd_312;
      Write_PriceValue(gd_304);
      Write_PriceValue(gd_312);
   }
   if (UseBeginDate == TRUE) nBarsBack++;
   if (gi_280 == FALSE) {
      ls_32 = ls_32 + "** " + gs_76 + " ** " 
      + "\n";
      if (g_bool_328) ls_40 = "  (Date)";
      else ls_40 = "  (nBars)";
      ls_32 = ls_32 + "GridBegin: " + TimeToStr(g_str2time_268, TIME_DATE|TIME_MINUTES) + ls_40 
      + "\n";
      ls_32 = ls_32 + "nBarsBack= " + nBarsBack 
      + "\n";
      ls_48 = "  (" + TimeToStr(gi_296) + ")";
      ls_56 = "  (" + TimeToStr(gi_300) + ")";
      ls_32 = ls_32 + "maxPrice= " + DoubleToStr(gd_304, Digits) + ls_48 
      + "\n";
      ls_32 = ls_32 + "minPrice= " + DoubleToStr(gd_312, Digits) + ls_56 
      + "\n";
      ld_64 = 100.0 * (gd_304 - gd_312) / gd_312;
      ld_72 = ld_64 / gi_284;
      ls_80 = " ( " + DoubleToStr(ld_72, 2) + " % )";
      ls_32 = ls_32 + "Percent= " + DoubleToStr(ld_64, 2) + " %\n";
      ls_32 = ls_32 + "GridStep= " + DoubleToStr(gd_320, Digits) + ls_80 
      + "\n";
      ls_88 = " (" + gi_292 + " GridSteps)";
      ls_32 = ls_32 + "mainStep= " + DoubleToStr(gd_320 * gi_292, Digits) + ls_88 
      + "\n";
      if (Show_Comments) Comment(ls_32);
      if (MainGrid_Intervals == 36) li_96 = gi_284;
      else li_96 = gi_284 + 1;
      if (Show_GridMatrix == TRUE) {
         for (li_20 = 1; li_20 < li_96; li_20++) {
            if (Draw_AllGrids == TRUE) Draw_HorizontalLine(gd_304 - li_20 * gd_320);
            else
               if (MathMod(li_20, gi_292) == 0.0) Draw_HorizontalLine(gd_304 - li_20 * gd_320);
            if (MathMod(li_20, gi_292) == 0.0) Write_PriceValue(gd_304 - li_20 * gd_320);
         }
      }
      if (Show_GridMatrix == TRUE) {
         if (Draw_AdditionalGrids) {
            for (int li_100 = 1; li_100 <= gi_292; li_100++) Draw_HorizontalLine(gd_304 + li_100 * gd_320);
            for (li_100 = 1; li_100 <= gi_292; li_100++) Draw_HorizontalLine(gd_312 - li_100 * gd_320);
         }
      }
      if (Show_GridMatrix == TRUE) {
         for (li_20 = 0; li_20 < nBarsBack; li_20++) {
            if (Draw_AllGrids == TRUE) li_104 = li_20;
            else li_104 = nBarsBack - 1 - li_20 * gi_292;
            if (li_104 > 0) li_108 = Time[li_104];
            else li_108 = Time[0];
            Draw_VerticalLine(li_108, gd_312, gd_304);
         }
      }
      Draw_TradingLines();
      if (Show_GannGrid) {
         l_time_112 = Time[nBarsBack - 1];
         li_116 = Time[nBarsBack - gi_292 - 1];
         ld_120 = gd_304;
         ld_128 = gd_304 - gi_292 * gd_320;
         Draw_GannGrid(l_time_112, li_116, ld_120, ld_128);
      }
   }
   gi_280 = TRUE;
   return (0);
}

void Draw_TradingLines() {
   int li_0;
   double ld_4;
   int li_12 = nBarsBack - 1;
   double ld_16 = g_ibuf_224[li_12];
   double ld_24 = g_ibuf_224[li_12] - (g_ibuf_224[li_12 + 1]);
   if (ld_24 >= 0.0) {
      li_0 = 1;
      ld_4 = ld_16 - gi_288 * gd_320;
      Set_UpLineValue(li_12, ld_4);
   } else {
      li_0 = -1;
      ld_4 = ld_16 + gi_288 * gd_320;
      Set_DnLineValue(li_12, ld_4);
   }
   for (int li_32 = nBarsBack - 2; li_32 >= 0; li_32--) {
      if (li_0 == 1) {
         if (g_ibuf_224[li_32] - gi_288 * gd_320 > ld_4 + gd_320) ld_4 = g_ibuf_224[li_32] - gi_288 * gd_320;
         else ld_4 += gd_320;
         Set_UpLineValue(li_32, ld_4);
         if (g_ibuf_224[li_32] < ld_4) {
            li_0 = -1;
            ld_4 = g_ibuf_224[li_32] + gi_288 * gd_320;
            Set_DnLineValue(li_32, ld_4);
            if (Show_PriceArrows) g_ibuf_248[li_32] = g_ibuf_224[li_32];
            if (Show_HiloArrows) Show_SignalArrows(li_32, li_0);
         }
      } else {
         if (li_0 == -1) {
            if (g_ibuf_224[li_32] + gi_288 * gd_320 < ld_4 - gd_320) ld_4 = g_ibuf_224[li_32] + gi_288 * gd_320;
            else ld_4 -= gd_320;
            Set_DnLineValue(li_32, ld_4);
            if (g_ibuf_224[li_32] > ld_4) {
               li_0 = 1;
               ld_4 = g_ibuf_224[li_32] - gi_288 * gd_320;
               Set_UpLineValue(li_32, ld_4);
               if (Show_PriceArrows) g_ibuf_240[li_32] = g_ibuf_224[li_32];
               if (Show_HiloArrows) Show_SignalArrows(li_32, li_0);
            }
         }
      }
   }
}

void Set_UpLineValue(int ai_0, double ad_4) {
   if (ad_4 >= g_ibuf_236[ai_0] - gd_320 && ad_4 <= g_ibuf_232[ai_0] + gd_320) {
      g_ibuf_244[ai_0] = ad_4;
      return;
   }
   g_ibuf_244[ai_0] = EMPTY_VALUE;
}

void Set_DnLineValue(int ai_0, double ad_4) {
   if (ad_4 >= g_ibuf_236[ai_0] - gd_320 && ad_4 <= g_ibuf_232[ai_0] + gd_320) {
      g_ibuf_252[ai_0] = ad_4;
      return;
   }
   g_ibuf_252[ai_0] = EMPTY_VALUE;
}

void Show_SignalArrows(int ai_0, int ai_4) {
   int li_8;
   color l_color_12;
   double l_price_40;
   double ld_48 = 0.4;
   int li_unused_56 = 2;
   //////////////////////////////////////////////////////////
   string l_name_60 = "Signal_" + TimeToStr(Time[ai_0]);
   /////////////////////////////////////////////////////////
   double l_iatr_16 = iATR(Symbol(), Period(), 34, ai_0);
   double ld_68 = l_iatr_16 * ld_48;
   double ld_76 = WindowPriceMax(0) - WindowPriceMin(0);
   double ld_32 = ld_76 / 100.0;
   double ld_24 = ld_32 + 2.0 * ld_68;
   if (ai_4 == 1) {
      li_8 = 233;
      l_color_12 = Blue;
      GlobalVariableSet(Symbol()+"  GANN  "+Period(),1);
      l_price_40 = Low[ai_0] - ld_32;
   } else {
      if (ai_4 == -1) {
         li_8 = 234;
         l_color_12 = Red;
         GlobalVariableSet(Symbol()+"  GANN  "+Period(),2);
         l_price_40 = High[ai_0] + ld_24;
      }
   }
   l_price_40 = NormalizeDouble(l_price_40, Digits);
   ObjectCreate(l_name_60, OBJ_ARROW, 0, Time[ai_0], l_price_40);
   
   ObjectSet(l_name_60, OBJPROP_COLOR, l_color_12);
   ObjectSet(l_name_60, OBJPROP_ARROWCODE, li_8);
}

double CurrentPrice(int ai_0, int ai_4) {
   double ld_ret_8;
   switch (ai_0) {
   case 0:
      ld_ret_8 = Close[ai_4];
      break;
   case 1:
      ld_ret_8 = Open[ai_4];
      break;
   case 4:
      ld_ret_8 = (High[ai_4] + Low[ai_4]) / 2.0;
      ld_ret_8 = NormalizeDouble(ld_ret_8, Digits);
      break;
   case 5:
      ld_ret_8 = (High[ai_4] + Low[ai_4] + Close[ai_4]) / 3.0;
      ld_ret_8 = NormalizeDouble(ld_ret_8, Digits);
   }
   return (ld_ret_8);
}

void Draw_HorizontalLine(double a_price_0) {
   a_price_0 = NormalizeDouble(a_price_0, Digits);
   string l_name_8 = "HLine_" + DoubleToStr(a_price_0, Digits);
   ObjectCreate(l_name_8, OBJ_TREND, g_window_256, g_str2time_268, a_price_0, Time[0], a_price_0);
   ObjectSet(l_name_8, OBJPROP_COLOR, MainGrid_Color);
   ObjectSet(l_name_8, OBJPROP_STYLE, MainGrid_Style);
   ObjectSet(l_name_8, OBJPROP_WIDTH, MainGrid_Width);
   ObjectSet(l_name_8, OBJPROP_RAY, FALSE);
   ObjectSet(l_name_8, OBJPROP_BACK, FALSE);
}

void Draw_VerticalLine(int ai_0, double a_price_4, double a_price_12) {
   string l_name_20 = "VLine_" + TimeToStr(ai_0);
   ObjectCreate(l_name_20, OBJ_TREND, g_window_256, ai_0, a_price_4, ai_0, a_price_12);
   ObjectSet(l_name_20, OBJPROP_COLOR, MainGrid_Color);
   ObjectSet(l_name_20, OBJPROP_STYLE, MainGrid_Style);
   ObjectSet(l_name_20, OBJPROP_WIDTH, MainGrid_Width);
   ObjectSet(l_name_20, OBJPROP_RAY, FALSE);
   ObjectSet(l_name_20, OBJPROP_BACK, FALSE);
}

void Draw_GannGrid(int a_datetime_0, int a_datetime_4, double a_price_8, double a_price_16) {
   string l_name_24 = "Gann_" + a_price_8;
   double ld_32 = (-gd_320) * MathPow(10, Digits);
   ObjectCreate(l_name_24, OBJ_GANNGRID, g_window_256, a_datetime_0, a_price_8, a_datetime_4, a_price_16);
   ObjectSet(l_name_24, OBJPROP_COLOR, GannGrid_Color);
   ObjectSet(l_name_24, OBJPROP_STYLE, GannGrid_Style);
   ObjectSet(l_name_24, OBJPROP_WIDTH, GannGrid_Width);
   ObjectSet(l_name_24, OBJPROP_RAY, TRUE);
   ObjectSet(l_name_24, OBJPROP_SCALE, ld_32);
   ObjectSet(l_name_24, OBJPROP_BACK, TRUE);
}

void Write_PriceValue(double a_price_0) {
   string ls_8;
   for (int li_16 = 1; li_16 <= Text_Shift; li_16++) ls_8 = ls_8 + " ";
   string l_text_20 = DoubleToStr(a_price_0, Digits);
   string l_name_28 = "Price_" + l_text_20;
   l_text_20 = ls_8 + l_text_20;
   ObjectCreate(l_name_28, OBJ_TEXT, g_window_256, Time[0], a_price_0);
   ObjectSetText(l_name_28, l_text_20, fontSize);
}

void Delete_GannObjects() {
   Delete_ObjectNames("Gann_");
   Delete_ObjectNames("HLine_");
   Delete_ObjectNames("VLine_");
   Delete_ObjectNames("Price_");
   Delete_ObjectNames("Signal_");
}

void Delete_ObjectNames(string as_0) {
   string l_name_16;
   int l_objs_total_8 = ObjectsTotal();
   for (int l_objs_total_12 = l_objs_total_8; l_objs_total_12 >= 0; l_objs_total_12--) {
      l_name_16 = ObjectName(l_objs_total_12);
      if (StringFind(l_name_16, as_0, 0) != -1) ObjectDelete(l_name_16);
   }
}