
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Black

extern string TimeFrame = "D1";
extern int RsiPeriod = 9;
extern int MaType = 1;
extern int MaPeriod = 2;
extern bool Interpolate = TRUE;
extern string arrowsIdentifier = "Bams-bung";
extern color arrowsUpColor = Lime;
extern color arrowsDnColor = Red;
extern bool alertsOn = FALSE;
extern bool alertsOnCurrent = FALSE;
extern bool alertsMessage = FALSE;
extern bool alertsSound = FALSE;
extern bool alertsEmail = FALSE;
double Gda_136[];
double Gda_140[];
double Gda_144[];
string Gs_148;
bool Gi_156 = FALSE;
bool Gi_160 = FALSE;
int Gi_164;
string Gs_nothing_168 = "nothing";
datetime Gt_176;

// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
   IndicatorBuffers(3);
   SetIndexBuffer(0, Gda_136);
   SetIndexLabel(0, "");
   SetIndexBuffer(1, Gda_140);
   SetIndexLabel(1, "");
   SetIndexBuffer(2, Gda_144);
   SetIndexLabel(2, "");
   if (TimeFrame == "calculate") {
      Gi_156 = TRUE;
      return (0);
   }
   if (TimeFrame == "returnBars") {
      Gi_160 = TRUE;
      return (0);
   }
   Gi_164 = f0_0(TimeFrame);
   Gs_148 = WindowExpertName();
   IndicatorShortName("Bams-bung " + f0_3());
   return (0);
}

// 52D46093050F38C27267BCE42543EF60
int deinit() {
   f0_2();
   return (0);
}

// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   int Li_0;
   int Li_4;
   double Ld_8;
   int Li_16;
   string Ls_20 = "2014.03.25";
   int Li_28 = StrToTime(Ls_20);
   if (TimeCurrent() >= Li_28) {
      Alert(" Expired ... Please contact Bams-bung.com ");
      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 (Gi_160) {
      Gda_136[0] = Li_36 + 1;
      return (0);
   }
   if (Gi_164 > Period()) Li_36 = MathMax(Li_36, MathMin(Bars - 1, iCustom(NULL, Gi_164, Gs_148, "returnBars", 0, 0) * Gi_164 / Period()));
   if (Gi_156) {
      for (int Li_40 = 0; Li_40 < Li_36; Li_40++) Gda_136[Li_40] = iRSI(NULL, 0, RsiPeriod, PRICE_CLOSE, Li_40);
      for (Li_40 = 0; Li_40 < Li_36; Li_40++) Gda_140[Li_40] = iMAOnArray(Gda_136, 0, MaPeriod, 0, MaType, Li_40);
      return (0);
   }
   for (Li_40 = Li_36; Li_40 >= 0; Li_40--) {
      Li_0 = iBarShift(NULL, Gi_164, Time[Li_40]);
      Gda_136[Li_40] = iCustom(NULL, Gi_164, Gs_148, "calculate", RsiPeriod, MaType, MaPeriod, 0, Li_0);
      Gda_140[Li_40] = iCustom(NULL, Gi_164, Gs_148, "calculate", RsiPeriod, MaType, MaPeriod, 1, Li_0);
      Gda_144[Li_40] = Gda_144[Li_40 + 1];
      if (Gda_136[Li_40] > Gda_140[Li_40]) Gda_144[Li_40] = 1;
      if (Gda_136[Li_40] < Gda_140[Li_40]) Gda_144[Li_40] = -1;
      f0_4(Time[Li_40]);
      if (Gda_144[Li_40] != Gda_144[Li_40 + 1]) {
         if (Gda_144[Li_40] == 1.0) f0_1(Li_40, arrowsUpColor, 150, 0);
         if (Gda_144[Li_40] == -1.0) f0_1(Li_40, arrowsDnColor, 153, 1);
      }
      if (Gi_164 <= Period() || Li_0 == iBarShift(NULL, Gi_164, Time[Li_40 - 1])) continue;
      if (Interpolate) {
         Li_4 = iTime(NULL, Gi_164, Li_0);
         for (int Li_44 = 1; Li_40 + Li_44 < Bars && Time[Li_40 + Li_44] >= Li_4; Li_44++) {
         }
         Ld_8 = 1.0 / Li_44;
         for (int Li_48 = 1; Li_48 < Li_44; Li_48++) {
            Gda_136[Li_40 + Li_48] = Li_48 * Ld_8 * (Gda_136[Li_40 + Li_44]) + (1.0 - Li_48 * Ld_8) * Gda_136[Li_40];
            Gda_140[Li_40 + Li_48] = Li_48 * Ld_8 * (Gda_140[Li_40 + Li_44]) + (1.0 - Li_48 * Ld_8) * Gda_140[Li_40];
         }
      }
   }
   if (alertsOn) {
      if (alertsOnCurrent) Li_16 = 0;
      else Li_16 = 1;
      if (Gda_144[Li_16] != Gda_144[Li_16 + 1]) {
         if (Gda_144[Li_16] == 1.0) f0_5("Trend UP");
         if (Gda_144[Li_16] == -1.0) f0_5("Trend DOWN");
      }
   }
   return (0);
}

// 2569208C5E61CB15E209FFE323DB48B7
void f0_1(int Ai_0, color Ai_4, int Ai_8, bool Ai_12) {
   string Ls_16 = arrowsIdentifier + ":" + Time[Ai_0];
   double Ld_24 = 3.0 * iATR(NULL, 0, 20, Ai_0) / 4.0;
   ObjectCreate(Ls_16, OBJ_ARROW, 0, Time[Ai_0], 0);
   ObjectSet(Ls_16, OBJPROP_ARROWCODE, Ai_8);
   ObjectSet(Ls_16, OBJPROP_COLOR, Ai_4);
   if (Ai_12) {
      ObjectSet(Ls_16, OBJPROP_PRICE1, High[Ai_0] + Ld_24);
      return;
   }
   ObjectSet(Ls_16, OBJPROP_PRICE1, Low[Ai_0] - Ld_24);
}

// 6ABA3523C7A75AAEA41CC0DEC7953CC5
void f0_2() {
   string Ls_0;
   string Ls_8 = arrowsIdentifier + ":";
   int Li_16 = StringLen(Ls_8);
   for (int Li_20 = ObjectsTotal() - 1; Li_20 >= 0; Li_20--) {
      Ls_0 = ObjectName(Li_20);
      if (StringSubstr(Ls_0, 0, Li_16) == Ls_8) ObjectDelete(Ls_0);
   }
}

// 9B1AEE847CFB597942D106A4135D4FE6
void f0_4(int Ai_0) {
   string Ls_4 = arrowsIdentifier + ":" + Ai_0;
   ObjectDelete(Ls_4);
}

// A9B24A824F70CC1232D1C2BA27039E8D
void f0_5(string As_0) {
   string Ls_8;
   if (Gs_nothing_168 != As_0 || Gt_176 != Time[0]) {
      Gs_nothing_168 = As_0;
      Gt_176 = Time[0];
      Ls_8 = StringConcatenate(Symbol(), " at ", TimeToStr(TimeLocal(), TIME_SECONDS), " Signal Arrow ", As_0);
      if (alertsMessage) Alert(Ls_8);
      if (alertsEmail) SendMail(StringConcatenate(Symbol(), "Signal Arrow"), Ls_8);
      if (alertsSound) PlaySound("alert2.wav");
   }
}

// 09CBB5F5CE12C31A043D5C81BF20AA4A
int f0_0(string As_0) {
   int Li_8;
   for (int Li_12 = StringLen(As_0) - 1; Li_12 >= 0; Li_12--) {
      Li_8 = StringGetChar(As_0, Li_12);
      if ((Li_8 > '`' && Li_8 < '{') || (Li_8 > 'ß' && Li_8 < 256)) As_0 = StringSetChar(As_0, Li_12, Li_8 - 32);
      else
         if (Li_8 > -33 && Li_8 < 0) As_0 = StringSetChar(As_0, Li_12, Li_8 + 224);
   }
   int Li_16 = 0;
   if (As_0 == "M1" || As_0 == "1") Li_16 = 1;
   if (As_0 == "M5" || As_0 == "5") Li_16 = 5;
   if (As_0 == "M15" || As_0 == "15") Li_16 = 15;
   if (As_0 == "M30" || As_0 == "30") Li_16 = 30;
   if (As_0 == "H1" || As_0 == "60") Li_16 = 60;
   if (As_0 == "H4" || As_0 == "240") Li_16 = 240;
   if (As_0 == "D1" || As_0 == "1440") Li_16 = 1440;
   if (As_0 == "W1" || As_0 == "10080") Li_16 = 10080;
   if (As_0 == "MN" || As_0 == "43200") Li_16 = 43200;
   if (Li_16 == 0 || Li_16 < Period()) Li_16 = Period();
   return (Li_16);
}

// 945D754CB0DC06D04243FCBA25FC0802
string f0_3() {
   switch (Gi_164) {
   case PERIOD_M1:
      return ("M(1)");
   case PERIOD_M5:
      return ("M(5)");
   case PERIOD_M15:
      return ("M(15)");
   case PERIOD_M30:
      return ("M(30)");
   case PERIOD_H1:
      return ("H(1)");
   case PERIOD_H4:
      return ("H(4)");
   case PERIOD_D1:
      return ("D(1)");
   case PERIOD_W1:
      return ("W(1)");
   case PERIOD_MN1:
      return ("MN(1)");
   }
   return ("Unknown timeframe");
}
