
#property copyright "Jaguar"
#property link      "Jaguarfxx@gmail.com"

#property indicator_separate_window
#property indicator_minimum -2.25
#property indicator_maximum 3.25
#property indicator_levelcolor Yellow
#property indicator_levelwidth 5
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 1

string gs_0_76 = "0";
string gs_84 = "";
extern double Yag_Period = 20.0;
extern bool Arrows = TRUE;
extern int ots = 5;
string gs_warning_104 = "Warning";
extern color ArrUp = Lime;//65280;
extern color ArrDn = Red;//255;
bool gi_120 = TRUE;
bool gi_124 = TRUE;
bool gi_128 = TRUE;
bool gi_132 = TRUE;
bool gi_136 = FALSE;
double g_ibuf_140[];
double g_ibuf_144[];
double g_ibuf_148[];
double g_ibuf_152[];
double g_ibuf_156[];
string gs_160;
bool g_bool_168;
bool g_bool_172;
int g_timeframe_176;
string gs_nothing_180 = "nothing";
datetime g_time_188;
string gsa_192[] = {"", "", "", "", "", "", "", "", ""};
int gia_196[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};

int init() {
   IndicatorBuffers(5);
   SetIndexBuffer(0, g_ibuf_152);
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, 159);
   SetIndexBuffer(1, g_ibuf_148);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 159);
   SetIndexBuffer(2, g_ibuf_140);
   SetIndexBuffer(3, g_ibuf_144);
   SetIndexBuffer(4, g_ibuf_156);
   gs_160 = WindowExpertName();
   g_bool_168 = gs_0_76 == "CalculateValue";
   if (g_bool_168) return (0);
   g_bool_172 = gs_0_76 == "returnBars";
   if (g_bool_172) return (0);
   g_timeframe_176 = f0_2(gs_0_76);
   if (StringTrimLeft(StringTrimRight(gs_84)) == "") gs_84 = Symbol();
   IndicatorShortName(gs_84 + " " + f0_6(g_timeframe_176) + " JAGUAR (" + DoubleToStr(Yag_Period, 2) + ")");
   return (0);
}

int deinit() {
   if ((!g_bool_168) && Arrows) f0_8();
   return (0);
}

int start() {
   double ld_0;
   double ld_8;
   int shift_16;
   int li_20 = 1;
   int li_24 = 6;
   int li_28 = 2030;
   if ((TimeDay(TimeCurrent()) > li_20 && TimeMonth(TimeCurrent()) >= li_24 && TimeYear(TimeCurrent()) >= li_28) || (TimeMonth(TimeCurrent()) > li_24 && TimeYear(TimeCurrent()) >= li_28) ||
      TimeYear(TimeCurrent()) > li_28) return(0);
   int li_32 = IndicatorCounted();
   if (li_32 < 0) return (-1);
   if (li_32 > 0) li_32--;
   int li_36 = MathMin(Bars - li_32, Bars - 1);
   if (g_bool_172) {
      g_ibuf_152[0] = li_36 + 1;
      return (0);
   }
   if (g_bool_168 || (gs_84 == Symbol() && g_timeframe_176 == Period())) {
      ld_0 = 1;
      if (Digits == 3 || Digits == 5) ld_0 = 10;
      for (int li_40 = li_36; li_40 >= 0; li_40--) {
         if (li_40 >= Bars - 2) {
            g_ibuf_140[li_40 + 1] = Close[li_40];
            g_ibuf_144[li_40 + 1] = Close[li_40];
         } else {
            ld_8 = Close[li_40];
            g_ibuf_140[li_40] = g_ibuf_140[li_40 + 1];
            g_ibuf_144[li_40] = g_ibuf_144[li_40 + 1];
            g_ibuf_148[li_40] = g_ibuf_148[li_40 + 1];
            g_ibuf_152[li_40] = g_ibuf_152[li_40 + 1];
            g_ibuf_156[li_40] = g_ibuf_156[li_40 + 1];
            if (ld_8 > g_ibuf_140[li_40] + Yag_Period * ld_0 * Point) {
               g_ibuf_140[li_40] = ld_8;
               g_ibuf_144[li_40] = ld_8 - Yag_Period * ld_0 * Point;
               g_ibuf_152[li_40] += 1.0;
               g_ibuf_148[li_40] = 0;
            }
            if (ld_8 < g_ibuf_144[li_40] - Yag_Period * ld_0 * Point) {
               g_ibuf_144[li_40] = ld_8;
               g_ibuf_140[li_40] = ld_8 + Yag_Period * ld_0 * Point;
               g_ibuf_148[li_40] = g_ibuf_148[li_40] - 1.0;
               g_ibuf_152[li_40] = 0;
            }
            if (g_ibuf_152[li_40] > 0.0) g_ibuf_156[li_40] = 1;
            if (g_ibuf_148[li_40] < 0.0) g_ibuf_156[li_40] = -1;
            f0_4(li_40);
         }
      }
      f0_1();
      return (0);
   }
   li_36 = MathMax(li_36, MathMin(Bars, iCustom(gs_84, g_timeframe_176, gs_160, "returnBars", "", 0, 0) * g_timeframe_176 / Period()));
   for (li_40 = li_36; li_40 >= 0; li_40--) {
      shift_16 = iBarShift(gs_84, g_timeframe_176, Time[li_40]);
      g_ibuf_152[li_40] = iCustom(gs_84, g_timeframe_176, gs_160, "CalculateValue", "", Yag_Period, 0, shift_16);
      g_ibuf_148[li_40] = iCustom(gs_84, g_timeframe_176, gs_160, "CalculateValue", "", Yag_Period, 1, shift_16);
      g_ibuf_156[li_40] = iCustom(gs_84, g_timeframe_176, gs_160, "CalculateValue", "", Yag_Period, 4, shift_16);
      f0_4(li_40);
   }
   f0_1();
   return (0);
}

void f0_1() {
   int li_0;
   if ((!g_bool_168) && gi_120) {
      if (gi_124) li_0 = 0;
      else li_0 = 1;
      li_0 = iBarShift(NULL, 0, iTime(NULL, g_timeframe_176, li_0));
      if (g_ibuf_156[li_0] != g_ibuf_156[li_0 + 1]) {
         if (g_ibuf_156[li_0] == 1.0) f0_3(li_0, "buy");
         if (g_ibuf_156[li_0] == -1.0) f0_3(li_0, "sell");
      }
   }
}

void f0_3(int ai_0, string as_4) {
   string str_concat_12;
   if (gs_nothing_180 != as_4 || g_time_188 != Time[ai_0]) {
      gs_nothing_180 = as_4;
      g_time_188 = Time[ai_0];
      str_concat_12 = StringConcatenate(gs_84 + " " + f0_6(g_timeframe_176) + " " + Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " Jaguar warning ", as_4);
      if (gi_128) Alert(str_concat_12);
      if (gi_136) SendMail(StringConcatenate(Symbol(), "JAGUAR "), str_concat_12);
      if (gi_132) PlaySound("alert2.wav");
   }
}

void f0_4(int ai_0) {
   if ((!g_bool_168) && Arrows && gs_84 == Symbol()) {
      f0_0(Time[ai_0]);
      if (g_ibuf_156[ai_0] != g_ibuf_156[ai_0 + 1]) {
         if (g_ibuf_156[ai_0] == 1.0) f0_7(ai_0, ArrUp, 233, 0);
         if (g_ibuf_156[ai_0] == -1.0) f0_7(ai_0, ArrDn, 234, 1);
      }
   }
}

void f0_7(int ai_0, color a_color_4, int ai_8, bool ai_12) {
   string name_16 = gs_warning_104 + ":" + Time[ai_0];
   double ld_24 = ots* iATR(NULL, 0, 20, ai_0) / 4.0;
   ObjectCreate(name_16, OBJ_ARROW, 0, Time[ai_0], 0);
   ObjectSet(name_16, OBJPROP_ARROWCODE, ai_8);
   ObjectSet(name_16, OBJPROP_COLOR, a_color_4);
   if (ai_12) {
      ObjectSet(name_16, OBJPROP_PRICE1, High[ai_0] + ld_24);
      return;
   }
   ObjectSet(name_16, OBJPROP_PRICE1, Low[ai_0] - ld_24);
}

void f0_8() {
   string name_0;
   string ls_8 = gs_warning_104 + ":";
   int str_len_16 = StringLen(ls_8);
   for (int li_20 = ObjectsTotal() - 1; li_20 >= 0; li_20--) {
      name_0 = ObjectName(li_20);
      if (StringSubstr(name_0, 0, str_len_16) == ls_8) ObjectDelete(name_0);
   }
}

void f0_0(int ai_0) {
   string name_4 = gs_warning_104 + ":" + ai_0;
   ObjectDelete(name_4);
}

int f0_2(string as_0) {
   as_0 = f0_5(as_0);
   for (int li_8 = ArraySize(gia_196) - 1; li_8 >= 0; li_8--)
      if (as_0 == gsa_192[li_8] || as_0 == "" + gia_196[li_8]) return (MathMax(gia_196[li_8], Period()));
   return (Period());
}

string f0_6(int ai_0) {
   for (int li_4 = ArraySize(gia_196) - 1; li_4 >= 0; li_4--)
      if (ai_0 == gia_196[li_4]) return (gsa_192[li_4]);
   return ("");
}

string f0_5(string as_0) {
   int li_8;
   string ls_ret_12 = as_0;
   for (int li_20 = StringLen(as_0) - 1; li_20 >= 0; li_20--) {
      li_8 = StringGetChar(ls_ret_12, li_20);
      if ((li_8 > '`' && li_8 < '{') || (li_8 > 'ß' && li_8 < 256)) ls_ret_12 = StringSetChar(ls_ret_12, li_20, li_8 - 32);
      else
         if (li_8 > -33 && li_8 < 0) ls_ret_12 = StringSetChar(ls_ret_12, li_20, li_8 + 224);
   }
   return (ls_ret_12);
}