/*
   Generated by EX4-TO-MQ4 decompiler V4.0.451.2 [-]
   Website: https://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2012, Budai Ati"
#property link      "http://www.currencyrobot.net"

#property indicator_chart_window

extern color TrendUpLineColor = DodgerBlue;
extern color TrendDnLineColor = Magenta;
extern int TrendLineStyle = 0;
extern int TrendLineWidth = 2;

extern color UpLevelColor = Green;
extern color DnLevelColor = Maroon;
extern int LevelStyle = 0;
extern int LevelWidth = 1;

string BotVersion = "v2.1.2";
string BotName = "CurrencyRobot ";
extern string Labell5 = "Normal Settings";
extern int Entry_pip = 2;
extern bool reverse = FALSE;
extern bool KillAll = FALSE;
extern bool AutomatTrendline = True;
extern string Buy_line_name = "buy";
extern string Sell_line_name = "sell";
extern string Money_Managemant = "MM";
extern color DT_Color = Orange;
extern color DT_Color_Loss = Red;

int g_count_304;
int gi_312;
int Divisor4;
int gi_320;
int g_count_324;
int g_count_328;
int gi_332;
int g_leverage_336;
int g_ticket_340;
bool g_bool_344;
int gi_348;
int g_highest_352;
int gi_356;
int g_lowest_360;
int gi_364;
int g_count_372;
int g_ticket_376;
int gi_384;
int Digits4;
double Point4;
double g_price_400;
double gd_408;
double gd_416;
double gd_424;
double gd_432;
double gd_440;
double gd_448;
double gd_456;
double gd_464;
double gd_480;
double gd_496;
double gd_504;
double gd_512;
double gd_520;
double g_ihigh_536;
double g_ihigh_544;
double g_ilow_552;
double g_ilow_560;
double g_order_open_price_568;
double g_price_576;
double g_price_584;
double gd_600;
double gd_608;
double gd_616;
bool gi_632;
bool gi_636;
string gs_648;
string g_str_concat_656;
int gi_664;
int g_datetime_668;

int init() {
   g_count_304 = 1;
   g_count_372 = 0;
   gi_312 = 20;
   gi_384 = DigitsTransfer(Digits, Symbol());
   g_str_concat_656 = StringConcatenate(BotName, " ", BotVersion);
   g_price_400 = 0.0;
   g_ticket_376 = 0;
   start();
   return (0);
}

int deinit() {
   string name_8;
   int li_0 = ObjectsTotal();
   for (int objs_total_4 = li_0; objs_total_4 >= 0; objs_total_4--) {
      name_8 = ObjectName(objs_total_4);
      if (name_8 != "" || name_8 != " ") ObjectDelete(name_8);
   }
   return (0);
}

int start() {
   int datetime_28;
   int datetime_32;
   bool li_40;
   bool li_44;
   int lowest_52;
   gi_320++;
   g_count_304 = 0;
   g_count_324 = 0;
   g_count_328 = 0;
   gd_408 = 0;
   gd_416 = 0;
   gd_424 = 0.0;
   gd_432 = 0.0;
   gi_332 = 0;

   g_datetime_668 = datetime_32;
   gi_332 = g_count_324 + g_count_328;

   if (gd_424 != 0.0) gd_408 /= gd_424;
   if (gd_432 != 0.0) gd_416 /= gd_432;
   if (gi_332 > 0) {
      if (gi_332 > 1) gd_504 = (gd_408 + gd_416) / 2.0;
   } else {
      g_price_400 = 0.0;
      gd_408 = 0.0;
      gd_416 = 0.0;
      g_highest_352 = 0;
      gi_356 = 0;
      g_lowest_360 = 0;
      gi_364 = 0;
      g_ihigh_536 = 0;
      g_ihigh_544 = 0;
      g_ilow_552 = 0;
      g_ilow_560 = 0;
   }
   if (AutomatTrendline) {
      g_count_372 = 0;
      if (gi_664 != Time[0] || ObjectFind(Buy_line_name) == -1 || ObjectFind(Sell_line_name) == -1) {
         gi_664 = Time[0];
         ObjectDelete(Buy_line_name);
         ObjectDelete(Sell_line_name);
         g_highest_352 = 0;
         gi_356 = 0;
         g_lowest_360 = 0;
         gi_364 = 0;
         g_ihigh_536 = 0;
         g_ihigh_544 = 0;
         g_ilow_552 = 0;
         g_ilow_560 = 0;
         li_40 = f0_6();
         li_44 = f0_8();

         if (li_40) SetTLine(Buy_line_name, Time[g_highest_352], g_ihigh_536, Time[gi_356], g_ihigh_544, TrendUpLineColor, TrendLineWidth, TrendLineStyle);
         if (li_44) SetTLine(Sell_line_name, Time[g_lowest_360], g_ilow_552, Time[gi_364], g_ilow_560, TrendDnLineColor, TrendLineWidth, TrendLineStyle);
      }
   } else {
      if (gi_332 > 0) {
         ObjectDelete("Bl");
         ObjectDelete("Sl");
         ObjectDelete(Buy_line_name);
         ObjectDelete(Sell_line_name);
      }
   }
   
   int hour_48 = TimeHour(TimeCurrent());

   f0_10();
   return (0);
}

void f0_10() {
   gd_512 = 0.0;
   gd_520 = 0.0;
   gi_632 = FALSE;
   gi_636 = FALSE;
//void SetHLine(string HLineName, double HLinePrice, color HLineColor, int HLineStyle, int HLineWidth) 
/*
extern color TrendUpLineColor = Blue;
extern color TrendDnLineColor = Red;
extern int TrendLineStyle = 0;
extern int TrendLineWidth = 2;

extern color UpLevelColor = Blue;
extern color DnLevelColor = Red;
extern int TrendLineStyle = 0;
extern int TrendLineWidth = 1;
*/
   if (ObjectFind(Buy_line_name) != -1) {
      gd_512 = f0_4(Buy_line_name);
      SetHLine("Bl", gd_512, UpLevelColor, LevelStyle, LevelWidth);
   } else ObjectDelete("Bl");
   if (ObjectFind(Sell_line_name) != -1) {
      gd_520 = f0_4(Sell_line_name);
      SetHLine("Sl", gd_520, DnLevelColor, LevelStyle, LevelWidth);
   } else ObjectDelete("Sl");
   
   
   if (gd_512 == 0.0 && gd_520 == 0.0) return;
   if (gd_512 + Entry_pip * Point4 < Bid && gd_512 > 0.0 && gd_600 != gd_512) {
      gd_600 = gd_512;
      gi_632 = TRUE;
      gi_636 = FALSE;
   }
   if (gd_520 - Entry_pip * Point4 > Ask && gd_520 > 0.0 && gd_608 != gd_520) {
      gd_608 = gd_520;
      gi_632 = FALSE;
      gi_636 = TRUE;
   }
   if (reverse) {
      if (gi_632 && (!gi_636)) {
         gi_636 = TRUE;
         gi_632 = FALSE;
      }
      if (gi_636 && (!gi_632)) {
         gi_636 = FALSE;
         gi_632 = TRUE;
      }
   }
}


double f0_4(string a_name_0) {
   double ld_8;
   if (ObjectFind(a_name_0) == -1) return (-1);
   ld_8 = ObjectGetValueByShift(a_name_0, 0);
   return (ld_8);
}


int f0_6() {
   double ihigh_48;
   double ld_56;
   double ld_64;
   int li_0 = 130;
   int li_4 = 8;
   int highest_8 = iHighest(Symbol(), 0, MODE_HIGH, li_0, 0);
   if (highest_8 == 0) return (0);
   g_highest_352 = highest_8;
   g_ihigh_536 = iHigh(Symbol(), 0, g_highest_352);
   double ld_12 = 0;
   gi_356 = 0;
   g_ihigh_544 = iHigh(Symbol(), 0, gi_356);
   double ihigh_20 = g_ihigh_544;
   double ld_28 = 0;
   double ld_36 = g_ihigh_536 - g_ihigh_544;
   for (int highest_44 = highest_8; highest_44 >= 0; highest_44--) {
      ihigh_48 = iHigh(Symbol(), 0, highest_44);
      ld_56 = ld_36 * highest_44 / highest_8;
      if (ihigh_48 > ihigh_20 + ld_56) {
         ld_64 = ihigh_48 - (ihigh_20 + ld_56);
         if (highest_44 > li_4 && ld_64 > ld_12) {
            g_ihigh_536 = ihigh_48;
            g_highest_352 = highest_44;
            ld_12 = ihigh_48 - (ihigh_20 + ld_56);
            continue;
         }
         if (highest_44 <= li_4 && ld_64 > ld_28) {
            g_ihigh_544 = ihigh_48;
            gi_356 = highest_44;
            ld_36 = g_ihigh_536 - g_ihigh_544;
            ld_28 = ihigh_48 - (ihigh_20 + ld_56);
         }
      }
   }
   return (1);
}

int f0_8() {
   double ilow_48;
   double ld_56;
   double ld_64;
   int li_0 = 130;
   int li_4 = 8;
   int lowest_8 = iLowest(Symbol(), 0, MODE_LOW, li_0, 0);
   if (lowest_8 == 0) return (0);
   g_lowest_360 = lowest_8;
   g_ilow_552 = iLow(Symbol(), 0, g_lowest_360);
   double ld_12 = 0;
   gi_364 = 0;
   g_ilow_560 = iLow(Symbol(), 0, gi_364);
   double ilow_20 = g_ilow_560;
   double ld_28 = 0;
   double ld_36 = g_ilow_560 - g_ilow_552;
   for (int lowest_44 = lowest_8; lowest_44 >= 0; lowest_44--) {
      ilow_48 = iLow(Symbol(), 0, lowest_44);
      ld_56 = ld_36 * lowest_44 / lowest_8;
      if (ilow_48 < ilow_20 - ld_56) {
         ld_64 = ilow_20 - ld_56 - ilow_48;
         if (lowest_44 > li_4 && ld_64 > ld_12) {
            g_ilow_552 = ilow_48;
            g_lowest_360 = lowest_44;
            ld_12 = ilow_20 - ld_56 - ilow_48;
            continue;
         }
         if (lowest_44 <= li_4 && ld_64 > ld_28) {
            g_ilow_560 = ilow_48;
            gi_364 = lowest_44;
            ld_28 = ilow_20 - ld_56 - ilow_48;
         }
      }
   }
   return (1);
}

void SetHLine(string HLineName, double HLinePrice, color HLineColor, int HLineStyle, int HLineWidth) 
{
   if (ObjectFind(HLineName) != 1) ObjectCreate(HLineName, OBJ_HLINE, 0, D'15.09.2000 00:30', 0.0005);
   ObjectSet(HLineName, OBJPROP_COLOR, HLineColor);
   ObjectSet(HLineName, OBJPROP_WIDTH, HLineWidth);
   ObjectSet(HLineName, OBJPROP_STYLE, HLineStyle);
   ObjectSet(HLineName, OBJPROP_PRICE1, HLinePrice);
   ObjectSet(HLineName, OBJPROP_TIMEFRAMES, NULL);
}

void SetTLine(string TLineName, int TLineTime, double TLinePrice, int TLineTime2, double TLinePrice2, color TLineColor, int TLineWidth, int TLineStyle) 
{
   ObjectCreate(TLineName, OBJ_TREND, 0, TLineTime, TLinePrice, TLineTime2, TLinePrice2, 0);
   ObjectSet(TLineName, OBJPROP_COLOR, TLineColor);
   ObjectSet(TLineName, OBJPROP_RAY, TRUE);
   ObjectSet(TLineName, OBJPROP_WIDTH, TLineWidth);
   ObjectSet(TLineName, OBJPROP_STYLE, TLineStyle);
   ObjectSet(TLineName, OBJPROP_TIMEFRAMES, NULL);
}

int DigitsTransfer(int digs, string symb) {
   int res = 0;
   Point4 = Point;
   if (digs == 4 || (digs == 2 && StringFind(symb, "JPY") != -1) || (digs == 1 && StringFind(symb, "XAU") != -1) || (digs == 2 && StringFind(symb, "XAG") != -1)) {
      Divisor4 = 1;
      Digits4 = Digits;
   }
   if (digs == 5 || (digs == 3 && StringFind(symb, "JPY") != -1) || (digs == 2 && StringFind(symb, "XAU") != -1) || (digs == 3 && StringFind(symb, "XAG") != -1)) {
      Point4 = 10.0 * Point4;
      Divisor4 = 10;
      Digits4 = Digits - 1;
   }
   if (StringFind(symb, "JPY") != -1) res = 1;
   if (StringFind(symb, "XAU") != -1) res = 2;
   if (StringFind(symb, "XAG") != -1) res = 3;
   return (res);
}