/*
   G e n e r a t e d  by ex4-to-mq4 decompiler FREEWARE 4.0.509.5
   Website:  h t tP: / /w ww .MeTAq U oTe s .n eT
   E-mail :  s Upp OrT @ m E t AQu O T es . net
*/
#property copyright "RFKing2"
#property link      ""
#property strict

extern string Remark1 = "== Main Settings ==";
extern bool ReOpt = FALSE;
extern int MagicNumber = 22222;
extern bool SignalsOnly = FALSE;
extern bool Alerts = TRUE;
extern bool SignalMail = FALSE;
extern bool PlaySounds = TRUE;
extern bool ECNBroker = FALSE;
extern int SleepTime = 100;
extern bool EachTickMode = TRUE;
extern bool AnimateOptimization = TRUE;
extern int MaxSimultaneousTrades = 4;
extern double Lots = 0.01;
extern bool MoneyManagement = TRUE;
extern int Split = 60;
extern int PercentRisked = 0;
extern int Slippage = 5;
extern bool UseStopLoss = TRUE;
extern int StopLoss = 1250;
extern bool UseTakeProfit = TRUE;
extern int TakeProfit = 200;
extern string Remark2 = "";
extern string Remark3 = "== Breakout Settings ==";
extern int BarsToOptimize = 0;
extern int InitialRange = 1;
extern int MaximumBarShift = 144000;
extern double MinimumWinRate = 80.0;
extern double MinimumRiskReward = 0.26;
extern double MinimumSuccessScore = 0.0;
extern int MinimumSampleSize = 10;
extern bool ReverseTrades = FALSE;
extern string Remark4 = "== Optimize Based On ==";
extern bool HighestProfit = FALSE;
extern bool HighestWinRate = FALSE;
extern bool HighestRiskReward = FALSE;
extern bool HighestSuccessScore = TRUE;
int Gi_unused_252 = 1;
string Gs_dummy_256;
int Gi_unused_264 = 0;
int Gi_unused_268 = 0;
int Gi_unused_272 = 0;
string Gs_dummy_276;
int Gia_unused_284[1];
string Gs_dummy_296;
string Gs_dummy_304;
string Gs_unused_328 = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
int Gi_unused_336 = 0;
int Gi_unused_340 = -1;
bool Gi_unused_344 = FALSE;
int G_count_352;
int G_bars_356;
int G_bars_360;
int G_bars_364;
int G_bars_368;
int G_bars_372;
int G_bars_376;
int G_bars_380;
int G_bars_384;
string Gs_accept_388 = "Accept";
string Gs_396 = "4-Digit Broker";
double Gd_unused_404 = 1.0;
int Gi_412;
bool Gi_416 = FALSE;
int G_count_420 = 0;
int G_count_424 = 0;
int Gi_428;
int G_day_of_year_432;
int Gi_436;
string G_time2str_440;
int G_str2int_448;
int G_str2int_452;
int G_str2int_456;
int Gi_460;
int Gi_unused_464;
double G_str2dbl_468;
double G_str2dbl_476;
double G_str2dbl_484;
string Gs_492;
int G_str2int_500;
int G_str2int_504;
string Gs_508 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_516 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_524 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_532 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_540 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_548 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_556 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_564 = "RRRRRR             RRRRRRRR  RRRRRRR    RRRRRRRRRRRRR..... ";
string Gs_572 = "RRRRRR   RRRRR    RRRRRR  RRRRRRR   RRRRRRRRRRR.....       ";
string Gs_580 = "RRRRRR   RRRRRR  RRRRRR  RRRRRRR   RRRRRRRRRR.....         ";
string Gs_588 = "RRRRRR   RRRRRR  RRRRRR  RRRRRR   RRRRRRRRRRR.....         ";
string Gs_596 = "RRRRRR   RRRRR  RRRRRRR  RRRRR   RRRRRRRRRRRR.....         ";
string Gs_604 = "RRRRRR             RRRRRRRR           RRRRRRRRRRRRRR.....  ";
string Gs_612 = "RRRRRR   RRRRR  RRRRRRR  RRRRR   RRRRRRRRRRRR.....         ";
string Gs_620 = "RRRRRR   RRRRR    RRRRRR  RRRRRR   RRRRRRRRRRR.....        ";
string Gs_628 = "RRRRRR   RRRRRR    RRRRR  RRRRRR   RRRRRRRRRRR.....        ";
string Gs_636 = "RRRRRR   RRRRRR    RRRRR  RRRRRRR     RRRRRRRRR.....       ";
string Gs_644 = "RRRRRR   RRRRRRR    RRRR  RRRRRRR     RRRRRRRRR.....       ";
string Gs_652 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_660 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_668 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_676 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_684 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_692 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";
string Gs_700 = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR.....              ";

int init() {
   double lotstep_0 = MarketInfo(Symbol(), MODE_LOTSTEP);
   if (lotstep_0 == 0.01) Gi_428 = 1;
   if (lotstep_0 == 0.1) Gi_428 = 2;
   if (lotstep_0 == 1.0) Gi_428 = 0;
   if (MagicNumber == 0) MagicNumber = AccountNumber();
   if (TimeMinute(TimeCurrent()) == 1 || TimeMinute(TimeCurrent()) == 30 && TimeSeconds(TimeCurrent()) == 1) G_count_352 = 0;
   G_bars_356 = Bars;
   G_bars_360 = Bars;
   G_bars_364 = Bars;
   G_bars_368 = Bars;
   G_bars_372 = Bars;
   G_bars_376 = Bars;
   G_bars_372 = Bars;
   G_bars_376 = Bars;
   if (Digits == 3 || Digits == 5) {
      Gs_396 = "5-Digit Broker";
      Gd_unused_404 = 10;
   }
   if (EachTickMode) Gi_412 = 0;
   else Gi_412 = 1;
   if ((!IsTesting()) && (!IsOptimization()) && Gs_accept_388 == "Accept") MasterFunction();
   return (0);
}

int deinit() {
   return (0);
}

int start()
 {
   if (Gs_accept_388 == "Accept")
   
    Comment(StartFunction(Symbol()));
   else 
    Comment("I know you didn\'t read the source code. Otherwise, the EA would be optimizing itself by now.");
   if (MathMod(1440, Split) != 0.0) {
      Print("Error this is an uneven block size.");
      return (0);
   }
   return (0);
}

void MasterFunction() {
   int Li_8;
   int Li_12;
   int Li_16;
   int Li_20;
   int Li_24;
   string Ls_28;
   int Li_36;
   string Ls_48;
   string Ls_60;
   string Ls_72;
   string Ls_84;
   int count_0 = 0;
   int count_4 = 0;
   string Ls_40 = "None";
   while (true) {
      Ls_48 = "";
      switch (TimeDayOfWeek(Li_12)) {
      case 0:
         Ls_48 = "Sunday";
         break;
      case 1:
         Ls_48 = "Monday";
         break;
      case 2:
         Ls_48 = "Tuesday";
         break;
      case 3:
         Ls_48 = "Wednesday";
         break;
      case 4:
         Ls_48 = "Thursday";
         break;
      case 5:
         Ls_48 = "Friday";
         break;
      case 6:
         Ls_48 = "Saturday";
      }
      Ls_60 = "";
      switch (TimeDayOfWeek(Li_8)) {
      case 0:
         Ls_60 = "Sunday";
         break;
      case 1:
         Ls_60 = "Monday";
         break;
      case 2:
         Ls_60 = "Tuesday";
         break;
      case 3:
         Ls_60 = "Wednesday";
         break;
      case 4:
         Ls_60 = "Thursday";
         break;
      case 5:
         Ls_60 = "Friday";
         break;
      case 6:
         Ls_60 = "Saturday";
      }
      Ls_72 = "";
      switch (TimeDayOfWeek(Li_20)) {
      case 0:
         Ls_72 = "Sunday";
         break;
      case 1:
         Ls_72 = "Monday";
         break;
      case 2:
         Ls_72 = "Tuesday";
         break;
      case 3:
         Ls_72 = "Wednesday";
         break;
      case 4:
         Ls_72 = "Thursday";
         break;
      case 5:
         Ls_72 = "Friday";
         break;
      case 6:
         Ls_72 = "Saturday";
      }
      Ls_84 = "";
      switch (TimeDayOfWeek(Li_16)) {
      case 0:
         Ls_84 = "Sunday";
         break;
      case 1:
         Ls_84 = "Monday";
         break;
      case 2:
         Ls_84 = "Tuesday";
         break;
      case 3:
         Ls_84 = "Wednesday";
         break;
      case 4:
         Ls_84 = "Thursday";
         break;
      case 5:
         Ls_84 = "Friday";
         break;
      case 6:
         Ls_84 = "Saturday";
      }
      Comment("Last Server Start: " + Ls_48 + " ", TimeToStr(Li_12, TIME_DATE|TIME_SECONDS), 
         "\n", "Last Computer Start: " + Ls_60 + " ", TimeToStr(Li_8, TIME_DATE|TIME_SECONDS), 
         "\n", "Last Server Stop: " + Ls_72 + " ", TimeToStr(Li_20, TIME_DATE|TIME_SECONDS), 
         "\n", "Last Computer Stop: " + Ls_84 + " ", TimeToStr(Li_16, TIME_DATE|TIME_SECONDS), 
         "\n", "Last Calculation took ", Li_36, " miliseconds. \n", "Refresh Rates: ", Ls_40, 
         "\n", "Last Tick: ", count_4, 
         "\n", "Cycle Count: ", count_0, 
      "\n", Ls_28);
      if (RefreshRates()) {
         Li_8 = TimeLocal();
         Li_12 = TimeCurrent();
         Li_24 = GetTickCount();
         if (Gs_accept_388 == "Accept") Ls_28 = StartFunction(Symbol());
         Li_36 = GetTickCount() - Li_24;
         Ls_40 = "True";
         count_4 = 0;
      } else {
         count_0++;
         count_4++;
         Ls_40 = "False";
         Sleep(SleepTime);
      }
      Li_16 = TimeLocal();
      Li_20 = TimeCurrent();
   }
}

string StartFunction(string A_symbol_0) {
   int ticket_16;
   double price_20;
   double price_28;
   int file_92;
   int str2int_96;
   int str2int_100;
   int str2int_104;
   int Li_108;
   int str2int_112;
   double str2dbl_116;
   double str2dbl_124;
   double str2dbl_132;
   string Ls_140;
   int str2int_148;
   int str2int_152;
   double Ld_164;
   double maxlot_172;
   double minlot_180;
   double Ld_188;
   int Li_8 = 0;
   if (EachTickMode && Bars != G_bars_360) Gi_416 = FALSE;
   int order_total_12 = OrdersTotal();
   Li_8 = 0;
   if (TimeMinute(TimeCurrent()) == 1 || TimeMinute(TimeCurrent()) == 30 && TimeSeconds(TimeCurrent()) == 1) G_count_352 = 0;
   if (TimeDayOfYear(TimeCurrent()) != G_day_of_year_432) {
      if (ReOpt) Gi_436 = SelfOptimize(A_symbol_0);
      G_day_of_year_432 = TimeDayOfYear(TimeCurrent());
      G_time2str_440 = TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS);
   }
   int shift_60 = iBarShift(NULL, 0, StrToTime("00:00"), FALSE);
   int Li_64 = shift_60 - InitialRange;
   int highest_68 = iHighest(NULL, 0, MODE_HIGH, Li_64 - 1, 1);
   int lowest_72 = iLowest(NULL, 0, MODE_LOW, Li_64 - 1, 1);
   double ihigh_76 = iHigh(NULL, 0, highest_68);
   double ilow_84 = iLow(NULL, 0, lowest_72);
   if (G_str2int_456 != BlockID(TimeCurrent())) file_92 = FileOpen(WindowExpertName() + " " + A_symbol_0 + " Optimized Settings.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
   if (file_92 == -1) return (-1);
   if (file_92 > 0) {
      while (!FileIsEnding(file_92)) {
         str2int_96 = StrToInteger(FileReadString(file_92));
         str2int_100 = StrToInteger(FileReadString(file_92));
         str2int_104 = StrToInteger(FileReadString(file_92));
         Li_108 = StrToInteger(FileReadString(file_92));
         str2int_112 = StrToInteger(FileReadString(file_92));
         str2dbl_116 = StrToDouble(FileReadString(file_92));
         str2dbl_124 = StrToDouble(FileReadString(file_92));
         str2dbl_132 = StrToDouble(FileReadString(file_92));
         Ls_140 = FileReadString(file_92);
         str2int_148 = StrToInteger(FileReadString(file_92));
         str2int_152 = StrToInteger(FileReadString(file_92));
         if (str2int_96 == TimeMonth(TimeCurrent()) && str2int_100 == TimeDayOfWeek(TimeCurrent()) && str2int_104 == BlockID(TimeCurrent())) {
            G_str2int_448 = str2int_96;
            G_str2int_452 = str2int_100;
            G_str2int_456 = str2int_104;
            Gi_460 = Li_108;
            Gi_unused_464 = str2int_112;
            G_str2dbl_468 = str2dbl_116;
            G_str2dbl_476 = str2dbl_124;
            G_str2dbl_484 = str2dbl_132;
            Gs_492 = Ls_140;
            G_str2int_500 = str2int_148;
            G_str2int_504 = str2int_152;
            break;
         }
      }
   }
   if (file_92 > 0) FileClose(file_92);
   if (!ReverseTrades) TakeProfit = Gi_460;
   if (ReverseTrades) StopLoss = Gi_460;
   int count_156 = 0;
   for (int order_total_160 = OrdersTotal(); order_total_160 >= 0; order_total_160--) {
      OrderSelect(order_total_160, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == MagicNumber && OrderSymbol() == A_symbol_0 && OrderType() == OP_BUY || OrderType() == OP_SELL) count_156++;
   }
   if (MoneyManagement) {
      Ld_164 = 0.00001 * PercentRisked;
      maxlot_172 = NormalizeDouble(AccountBalance() * Ld_164, Gi_428);
      minlot_180 = MarketInfo(Symbol(), MODE_MINLOT);
      Ld_188 = MarketInfo(Symbol(), MODE_MAXLOT);
      if (maxlot_172 < minlot_180) maxlot_172 = 0.02;
      if (maxlot_172 > Ld_188) maxlot_172 = Ld_188;
   }
   string Ls_196 = "false";
   if (iHigh(NULL, 0, 0) == ihigh_76) Ls_196 = "true";
   if (Ls_196 == "true") {
      G_count_420++;
      G_count_424 = 0;
   }
   string Ls_204 = "false";
   if (iLow(NULL, 0, 0) == ilow_84) Ls_204 = "true";
   if (Ls_204 == "true") {
      G_count_424++;
      G_count_420 = 0;
   }
   string Ls_unused_212 = "false";
   if (iSAR(NULL, PERIOD_M15, 0.02, 0.2, 0) < iLow(NULL, 0, Gi_412 + 0)) Ls_unused_212 = "true";
   string Ls_unused_220 = "false";
   if (iSAR(NULL, PERIOD_M15, 0.02, 0.2, 0) > iHigh(NULL, 0, Gi_412 + 0)) Ls_unused_220 = "true";
   string Ls_228 = "None";
   if (count_156 < MaxSimultaneousTrades || MaxSimultaneousTrades == 0 && G_str2int_500 >= MinimumSampleSize && shift_60 > InitialRange && Gs_492 == "Breakout" && Close[Gi_412] > ihigh_76) Ls_228 = "Open Long";
   if (count_156 < MaxSimultaneousTrades || MaxSimultaneousTrades == 0 && G_str2int_500 >= MinimumSampleSize && shift_60 > InitialRange && Gs_492 == "Counter" && G_count_420 != 0) Ls_228 = "Open Short";
   if (count_156 < MaxSimultaneousTrades || MaxSimultaneousTrades == 0 && G_str2int_500 >= MinimumSampleSize && shift_60 > InitialRange && Gs_492 == "Breakout" && Close[Gi_412] < ilow_84) Ls_228 = "Open Short";
   if (count_156 < MaxSimultaneousTrades || MaxSimultaneousTrades == 0 && G_str2int_500 >= MinimumSampleSize && shift_60 > InitialRange && Gs_492 == "Counter" && G_count_424 != 0) Ls_228 = "Open Long";
   string Ls_236 = Ls_228;
   if (ReverseTrades && Ls_228 == "Open Long") Ls_236 = "Open Short";
   if (ReverseTrades && Ls_228 == "Open Short") Ls_236 = "Open Long";
   string str_concat_244 = StringConcatenate("Broker Type: ", Gs_396, 
      "\n", "Last Optimized: ", G_time2str_440, 
      "\n", "Bars Used: ", Gi_436, 
      "\n", "Current Month: ", G_str2int_448, 
      "\n", "Current Day: ", G_str2int_452, 
      "\n", "Block ID: ", BlockID(TimeCurrent()), 
      "\n", "Max Lot Size: ", maxlot_172, 
      "\n", "Max Trades: ", MaxSimultaneousTrades, 
      "\n", "Current TP: ", Gi_460, 
      "\n", "Current Win Rate: ", 100.0 * G_str2dbl_468, "% (", MinimumWinRate, ")\n", "Current Risk Reward: ", G_str2dbl_476, " (", MinimumRiskReward, ")\n", "Current Success Score: ", 100.0 * G_str2dbl_484, " (", MinimumSuccessScore, ")\n", "Array Win: ", G_str2int_500 - G_str2int_504 - 1, 
      "\n", "Array Lose: ", G_str2int_504 + 1, 
      "\n", "Total Array: ", G_str2int_500, 
      "\n", "Total Open Trades: ", count_156, 
      "\n", "Trade Style: ", Gs_492, 
      "\n", "High Shift: ", G_count_420, "   Low Shift: ", G_count_424, 
   "\n", "Trade Trigger: ", Ls_236);
   int Li_252 = 65535;
   int Li_256 = 65535;
   if (Gs_492 == "Breakout") {
      Li_252 = 65280;
      Li_256 = 255;
   }
   if (Gs_492 == "Counter") {
      Li_252 = 255;
      Li_256 = 65280;
   }
   ObjectMaker("HiLoTrader High", OBJ_HLINE, 0, 0, ihigh_76, Li_252, TRUE, STYLE_DOT);
   ObjectMaker("HiloTrader Low", OBJ_HLINE, 0, 0, ilow_84, Li_256, TRUE, STYLE_DOT);
   bool Li_260 = FALSE;
   for (int pos_264 = 0; pos_264 < order_total_12; pos_264++) {
      OrderSelect(pos_264, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         Li_260 = TRUE;
         if (OrderType() == OP_BUY) {
         }
      }
   }
   if (Ls_236 == "Open Long" && G_count_352 < 1) {
      Li_8 = 1;
      G_count_424 = 0;
      G_count_420 = 0;
   }
   if (Ls_236 == "Open Short" && G_count_352 < 1) {
      Li_8 = 2;
      G_count_424 = 0;
      G_count_420 = 0;
   }
   Li_260 = FALSE;
   if (Li_8 == 1 && (EachTickMode && (!Gi_416)) || ((!EachTickMode) && Bars != G_bars_356)) {
      if (SignalsOnly) {
         if (SignalMail && G_bars_364 != Bars) {
            SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
            G_bars_364 = Bars;
         }
         if (Alerts && G_bars_372 != Bars) {
            Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
            G_bars_372 = Bars;
         }
         if (PlaySounds && G_bars_380 != Bars) {
            PlaySound("alert.wav");
            G_bars_380 = Bars;
         }
      }
      if ((!Li_260) && (!SignalsOnly) && G_count_352 < 1) {
         if (UseStopLoss) price_20 = Ask - StopLoss * Point;
         else price_20 = 0.0;
         if (UseTakeProfit) price_28 = Ask + TakeProfit * Point;
         else price_28 = 0.0;
         if (ECNBroker) {
            ticket_16 = OrderModify(OrderSend(Symbol(), OP_BUY, maxlot_172, Ask, Slippage, 0, 0, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue), OrderOpenPrice(), price_20,
               price_28, 0, CLR_NONE);
         }
         if (!ECNBroker) ticket_16 = OrderSend(Symbol(), OP_BUY, maxlot_172, Ask, Slippage, price_20, price_28, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
         if (ticket_16 > 0) {
            if (OrderSelect(ticket_16, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("BUY order opened : ", OrderOpenPrice());
               if (SignalMail && G_bars_364 != Bars) {
                  SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
                  G_bars_364 = Bars;
               }
               if (Alerts && G_bars_372 != Bars) {
                  Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
                  G_bars_372 = Bars;
               }
               if (PlaySounds && G_bars_380 != Bars) {
                  PlaySound("alert.wav");
                  G_bars_380 = Bars;
               }
               G_count_352++;
            } else Print("Error opening BUY order : ", GetLastError());
         }
         if (EachTickMode) Gi_416 = TRUE;
         if (!(!EachTickMode)) return (str_concat_244);
         G_bars_356 = Bars;
         return (str_concat_244);
      }
   }
   if (Li_8 == 2 && (EachTickMode && (!Gi_416)) || ((!EachTickMode) && Bars != G_bars_356)) {
      if (SignalsOnly) {
         if (SignalMail && G_bars_368 != Bars) {
            SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
            G_bars_368 = Bars;
         }
         if (Alerts && G_bars_376 != Bars) {
            Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
            G_bars_376 = Bars;
         }
         if (PlaySounds && G_bars_384 != Bars) {
            PlaySound("alert.wav");
            G_bars_384 = Bars;
         }
      }
      if ((!Li_260) && (!SignalsOnly) && G_count_352 < 1) {
         if (AccountFreeMarginCheck(Symbol(), OP_SELL, maxlot_172) < 0.0) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return (str_concat_244);
         }
         if (UseStopLoss) price_20 = Bid + StopLoss * Point;
         else price_20 = 0.0;
         if (UseTakeProfit) price_28 = Bid - TakeProfit * Point;
         else price_28 = 0.0;
         if (ECNBroker) {
            ticket_16 = OrderModify(OrderSend(Symbol(), OP_SELL, maxlot_172, Bid, Slippage, 0, 0, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink), OrderOpenPrice(), price_20,
               price_28, 0, CLR_NONE);
         }
         if (!ECNBroker) ticket_16 = OrderSend(Symbol(), OP_SELL, maxlot_172, Bid, Slippage, price_20, price_28, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
         if (ticket_16 > 0) {
            if (OrderSelect(ticket_16, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("SELL order opened : ", OrderOpenPrice());
               if (SignalMail && G_bars_368 != Bars) {
                  SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
                  G_bars_368 = Bars;
               }
               if (Alerts && G_bars_376 != Bars) {
                  Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
                  G_bars_376 = Bars;
               }
               if (PlaySounds && G_bars_384 != Bars) {
                  PlaySound("alert.wav");
                  G_bars_384 = Bars;
               }
               G_count_352++;
            } else Print("Error opening SELL order : ", GetLastError());
         }
         if (EachTickMode) Gi_416 = TRUE;
         if (!(!EachTickMode)) return (str_concat_244);
         G_bars_356 = Bars;
         return (str_concat_244);
      }
   }
   if (!EachTickMode) G_bars_360 = Bars;
   return (str_concat_244);
}

int SelfOptimize(string A_symbol_0) {
   int month_8 = TimeMonth(TimeCurrent());
   int day_of_week_12 = TimeDayOfWeek(TimeCurrent());
   int Li_16 = BlockID(TimeCurrent());
   int minute_20 = TimeMinute(TimeCurrent());
   int Li_24 = 1440;
   int Li_unused_28 = (Li_24 - MathMod(Li_24, Split)) / Split;
   int Li_32 = Li_24 / Split;
   for (int count_40 = 0; count_40 <= Li_32; count_40++) {
   }
   DeleteFile(WindowExpertName() + " " + A_symbol_0 + " Master Copy.csv");
   DeleteFile(WindowExpertName() + " " + A_symbol_0 + " Optimized Settings.csv");
   DeleteFile(WindowExpertName() + " " + A_symbol_0 + " All Settings.csv");
   DeleteFile(WindowExpertName() + " " + A_symbol_0 + " All Permutation Settings.csv");
   int Li_ret_36 = BarsToOptimize;
   if (BarsToOptimize == 0) Li_ret_36 = iBars(A_symbol_0, 0);
   if (BarsToOptimize > iBars(A_symbol_0, 0)) {
      Alert("Error: Not enough bars to optimize for symbol: " + A_symbol_0 + ".");
      return (0);
   }
   int Li_48 = Li_ret_36;
   int Li_52 = Li_48;
   int Li_56 = Li_48;
   double ihigh_60 = 0;
   double ilow_68 = 0;
   double ihigh_76 = 0;
   double ilow_84 = 0;
   int highest_92 = 0;
   int lowest_96 = 0;
   int Li_100 = 0;
   int Li_104 = 0;
   double ihigh_108 = 0;
   double ilow_116 = 0;
   for (int Li_44 = Li_52; Li_44 > 1; Li_44--) {
      OptimizationComments(" Looking for trades on bar " + Li_44);
      if (TimeDayOfYear(Time[Li_44]) != TimeDayOfYear(Time[Li_44 + 1])) Li_52 = Li_44;
      if (Li_52 - Li_44 == InitialRange) {
         Li_56 = Li_44;
         highest_92 = iHighest(A_symbol_0, 0, MODE_HIGH, Li_52 - Li_56, Li_56);
         ihigh_60 = iHigh(A_symbol_0, 0, highest_92);
         lowest_96 = iLowest(A_symbol_0, 0, MODE_LOW, Li_52 - Li_56, Li_56);
         ilow_68 = iLow(A_symbol_0, 0, lowest_96);
      }
      if (Li_52 > Li_56) {
         if (iHigh(NULL, 0, Li_44) > ihigh_60) {
            ihigh_76 = ihigh_60;
            ihigh_60 = iHigh(NULL, 0, Li_44);
            Li_100 = MathMax(Li_44 - MaximumBarShift, TradeCloseShift(A_symbol_0, "Long", ihigh_76, Li_44) + 1);
            ihigh_108 = iHigh(A_symbol_0, 0, iHighest(A_symbol_0, 0, MODE_HIGH, Li_44 - Li_100, Li_100));
            FileWriter(A_symbol_0, TimeMonth(iTime(A_symbol_0, 0, Li_44)), TimeDayOfWeek(iTime(A_symbol_0, 0, Li_44)), BlockID(iTime(A_symbol_0, 0, Li_44)), "Breakout", (ihigh_108 - ihigh_76) / MarketInfo(A_symbol_0,
               MODE_POINT), Li_100 - 1, Li_44 - Li_100);
            Li_100 = MathMax(Li_44 - MaximumBarShift, TradeCloseShift(A_symbol_0, "Short", ihigh_76, Li_44) + 1);
            ilow_116 = iLow(A_symbol_0, 0, iLowest(A_symbol_0, 0, MODE_LOW, Li_44 - Li_100, Li_100));
            FileWriter(A_symbol_0, TimeMonth(iTime(A_symbol_0, 0, Li_44)), TimeDayOfWeek(iTime(A_symbol_0, 0, Li_44)), BlockID(iTime(A_symbol_0, 0, Li_44)), "Counter", (ihigh_76 - ilow_116) / MarketInfo(A_symbol_0,
               MODE_POINT), Li_100 - 1, Li_44 - Li_100);
         }
         if (iLow(NULL, 0, Li_44) < ilow_68) {
            ilow_84 = ilow_68;
            ilow_68 = iLow(NULL, 0, Li_44);
            Li_104 = MathMax(Li_44 - MaximumBarShift, TradeCloseShift(A_symbol_0, "Long", ilow_84, Li_44) + 1);
            ihigh_108 = iHigh(A_symbol_0, 0, iHighest(A_symbol_0, 0, MODE_HIGH, Li_44 - Li_104, Li_104));
            FileWriter(A_symbol_0, TimeMonth(iTime(A_symbol_0, 0, Li_44)), TimeDayOfWeek(iTime(A_symbol_0, 0, Li_44)), BlockID(iTime(A_symbol_0, 0, Li_44)), "Counter", (ihigh_108 - ilow_84) / MarketInfo(A_symbol_0,
               MODE_POINT), Li_104 - 1, Li_44 - Li_104);
            Li_104 = MathMax(Li_44 - MaximumBarShift, TradeCloseShift(A_symbol_0, "Short", ilow_84, Li_44) + 1);
            ilow_116 = iLow(A_symbol_0, 0, iLowest(A_symbol_0, 0, MODE_LOW, Li_44 - Li_104, Li_104));
            FileWriter(A_symbol_0, TimeMonth(iTime(A_symbol_0, 0, Li_44)), TimeDayOfWeek(iTime(A_symbol_0, 0, Li_44)), BlockID(iTime(A_symbol_0, 0, Li_44)), "Breakout", (ilow_84 - ilow_116) / MarketInfo(A_symbol_0,
               MODE_POINT), Li_104 - 1, Li_44 - Li_104);
         }
      }
   }
   for (int Li_124 = 1; Li_124 <= 12; Li_124++) {
      for (int count_128 = 0; count_128 <= 6; count_128++) {
         for (int count_132 = 0; count_132 <= Li_32; count_132++) {
            OptimizeTP(A_symbol_0, Li_124, count_128, count_132);
            FileDelete(WindowExpertName() + " " + A_symbol_0 + " " + Li_124 + " " + count_128 + " " + count_132 + ".csv");
         }
      }
   }
   return (Li_ret_36);
}

void OptimizeTP(string As_0, int Ai_8, int Ai_12, int Ai_16) {
   double Lda_20[];
   double Lda_24[];
   string Ls_32;
   string Ls_40;
   string Ls_48;
   string Ls_56;
   double Ld_64;
   double Ld_72;
   double Ld_80;
   double Ld_88;
   double Ld_96;
   double Ld_104;
   double Ld_116;
   double Ld_124;
   double Ld_132;
   double Ld_140;
   double Ld_148;
   double Ld_156;
   int file_164;
   double Ld_168;
   double Ld_176;
   double Ld_184;
   double Ld_192;
   double Ld_200;
   double Ld_208;
   double Ld_220;
   double Ld_228;
   double Ld_236;
   double Ld_244;
   double Ld_252;
   double Ld_260;
   int file_268;
   double Ld_272;
   double Ld_280;
   double Ld_288;
   double Ld_296;
   double Ld_304;
   string Ls_312;
   int arr_size_320;
   int Li_324;
   int file_328;
   int file_332;
   ArrayResize(Lda_20, 0);
   ArrayResize(Lda_24, 0);
   int file_28 = FileOpen(WindowExpertName() + " " + As_0 + " " + Ai_8 + " " + Ai_12 + " " + Ai_16 + ".csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
   if (file_28 != -1) {
      if (file_28 > 0) {
         while (!FileIsEnding(file_28)) {
            Ls_32 = FileReadString(file_28);
            Ls_40 = FileReadString(file_28);
            Ls_48 = FileReadString(file_28);
            Ls_56 = FileReadString(file_28);
            if (Ls_32 != "" && Ls_56 == "Breakout") Lda_20[ArrayResize(Lda_20, ArraySize(Lda_20) + 1) - 1] = StrToDouble(Ls_32);
            if (Ls_32 != "" && Ls_56 == "Counter") Lda_24[ArrayResize(Lda_24, ArraySize(Lda_24) + 1) - 1] = StrToDouble(Ls_32);
            OptimizationComments("Reading trade files for Month:  " + Ai_8 + " Day: " + Ai_12 + "   Block:  " + Ai_16);
         }
      }
      FileClose(file_28);
      if (ArraySize(Lda_20) != 0) ArraySort(Lda_20);
      if (ArraySize(Lda_24) != 0) ArraySort(Lda_24);
      for (int index_112 = 0; index_112 < ArraySize(Lda_20); index_112++) {
         Ld_116 = StopLoss * index_112;
         Ld_124 = Lda_20[index_112] * (ArraySize(Lda_20) - index_112);
         Ld_132 = Ld_124 - Ld_116;
         Ld_140 = 1.0 - 1.0 * (1.0 * (index_112 + 1) / ArraySize(Lda_20));
         Ld_148 = 1.0 * (1.0 * Lda_20[index_112] / StopLoss);
         Ld_156 = Ld_140 * Ld_148;
         file_164 = FileOpen(WindowExpertName() + " " + As_0 + " All Permutation Settings.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
         if (file_164 > 0) {
            FileSeek(file_164, 0, SEEK_END);
            FileWrite(file_164, Ai_8, Ai_12, Ai_16, index_112, "Breakout", Ld_116, Ld_124, Ld_132, Ld_140, Ld_148, Ld_156);
            FileFlush(file_164);
            FileClose(file_164);
         }
         if (Ld_140 >= MinimumWinRate / 100.0 && Ld_148 >= MinimumRiskReward && Ld_156 >= MinimumSuccessScore) {
            Ld_64 = Ld_132;
            Ld_72 = Lda_20[index_112];
            Ld_104 = index_112;
            Ld_80 = 1.0 * Ld_140;
            Ld_88 = Ld_148;
            Ld_96 = Ld_156;
         }
         OptimizationComments("Optimizing Breakout for Month:  " + Ai_8 + " Day: " + Ai_12 + "   Block:  " + Ai_16);
      }
      for (int index_216 = 0; index_216 < ArraySize(Lda_24); index_216++) {
         Ld_220 = StopLoss * index_216;
         Ld_228 = Lda_24[index_216] * (ArraySize(Lda_24) - index_216);
         Ld_236 = Ld_228 - Ld_220;
         Ld_244 = 1.0 - 1.0 * (1.0 * (index_216 + 1) / ArraySize(Lda_24));
         Ld_252 = 1.0 * (1.0 * Lda_24[index_216] / StopLoss);
         Ld_260 = Ld_244 * Ld_252;
         file_268 = FileOpen(WindowExpertName() + " " + As_0 + " All Permutation Settings.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
         if (file_268 > 0) {
            FileSeek(file_268, 0, SEEK_END);
            FileWrite(file_268, Ai_8, Ai_12, Ai_16, index_216, "Counter", Ld_220, Ld_228, Ld_236, Ld_244, Ld_252, Ld_260);
            FileFlush(file_268);
            FileClose(file_268);
         }
         if (Ld_244 >= MinimumWinRate / 100.0 && Ld_252 >= MinimumRiskReward && Ld_260 >= MinimumSuccessScore) {
            Ld_168 = Ld_236;
            Ld_176 = Lda_24[index_216];
            Ld_208 = index_216;
            Ld_184 = 1.0 * Ld_244;
            Ld_192 = Ld_252;
            Ld_200 = Ld_260;
         }
         OptimizationComments("Optimizing Counter for Month:  " + Ai_8 + " Day: " + Ai_12 + "   Block:  " + Ai_16);
      }
      Ld_272 = -1;
      Ld_280 = -1;
      Ld_288 = -1;
      Ld_296 = -1;
      Ld_304 = -1;
      Ls_312 = "None";
      arr_size_320 = -1;
      Li_324 = -1;
      if ((HighestProfit && Ld_64 > Ld_168) || (HighestWinRate && Ld_80 > Ld_184) || (HighestRiskReward && Ld_88 > Ld_192) || (HighestSuccessScore && Ld_96 > Ld_200)) {
         Ld_272 = Ld_72;
         Ld_280 = Ld_64;
         Ld_288 = 1.0 * Ld_80;
         Ld_296 = Ld_88;
         Ld_304 = Ld_96;
         Ls_312 = "Breakout";
         arr_size_320 = ArraySize(Lda_20);
         Li_324 = Ld_104;
      }
      if ((HighestProfit && Ld_168 > Ld_64) || (HighestWinRate && Ld_184 > Ld_80) || (HighestRiskReward && Ld_192 > Ld_88) || (HighestSuccessScore && Ld_200 > Ld_96)) {
         Ld_272 = Ld_176;
         Ld_280 = Ld_168;
         Ld_288 = 1.0 * Ld_184;
         Ld_296 = Ld_192;
         Ld_304 = Ld_200;
         Ls_312 = "Counter";
         arr_size_320 = ArraySize(Lda_24);
         Li_324 = Ld_208;
      }
      file_328 = FileOpen(WindowExpertName() + " " + As_0 + " Optimized Settings.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
      if (file_328 > 0) {
         FileSeek(file_328, 0, SEEK_END);
         FileWrite(file_328, Ai_8, Ai_12, Ai_16, Ld_272, Ld_280, Ld_288, Ld_296, Ld_304, Ls_312, arr_size_320, Li_324);
         FileFlush(file_328);
         FileClose(file_328);
      }
      file_332 = FileOpen(WindowExpertName() + " " + As_0 + " All Settings.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
      if (file_332 > 0) {
         FileSeek(file_332, 0, SEEK_END);
         FileWrite(file_332, Ai_8, Ai_12, Ai_16, Ld_72, Ld_64, Ld_80, Ld_88, Ld_96, "Breakout", ArraySize(Lda_20), Ld_104);
         FileFlush(file_332);
         FileClose(file_332);
      }
      file_332 = FileOpen(WindowExpertName() + " " + As_0 + " All Settings.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
      if (file_332 > 0) {
         FileSeek(file_332, 0, SEEK_END);
         FileWrite(file_332, Ai_8, Ai_12, Ai_16, Ld_176, Ld_168, Ld_184, Ld_192, Ld_200, "Counter", ArraySize(Lda_24), Ld_208);
         FileFlush(file_332);
         FileClose(file_332);
      }
   }
}

string FileWriter(string As_0, int Ai_8, int Ai_12, int Ai_16, string As_20, int Ai_28, int Ai_32, int Ai_36) {
   int file_40 = FileOpen(WindowExpertName() + " " + As_0 + " " + Ai_8 + " " + Ai_12 + " " + Ai_16 + ".csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
   if (file_40 > 0) {
      FileSeek(file_40, 0, SEEK_END);
      FileWrite(file_40, Ai_28, Ai_32, Ai_36, As_20);
      FileFlush(file_40);
      FileClose(file_40);
   }
   file_40 = FileOpen(WindowExpertName() + " " + As_0 + " Master Copy.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
   if (file_40 > 0) {
      FileSeek(file_40, 0, SEEK_END);
      FileWrite(file_40, Ai_8, Ai_12, Ai_16, As_20, Ai_28, Ai_32, Ai_36);
      FileFlush(file_40);
      FileClose(file_40);
   }
   return ("");
}

int TradeCloseShift(string A_symbol_0, string As_8, double Ad_16, int Ai_24) {
   double Ld_28 = 0;
   if (As_8 == "Long") {
      if (!ReverseTrades) Ld_28 = Ad_16 - StopLoss * MarketInfo(A_symbol_0, MODE_POINT);
      if (ReverseTrades) Ld_28 = Ad_16 - TakeProfit * MarketInfo(A_symbol_0, MODE_POINT);
   }
   if (As_8 == "Short") {
      if (!ReverseTrades) Ld_28 = Ad_16 + StopLoss * MarketInfo(A_symbol_0, MODE_POINT);
      if (ReverseTrades) Ld_28 = Ad_16 + TakeProfit * MarketInfo(A_symbol_0, MODE_POINT);
   }
   for (int Li_ret_36 = Ai_24; Li_ret_36 > 0; Li_ret_36--) {
      if (As_8 == "Long")
         if (iHigh(A_symbol_0, 0, Li_ret_36) >= Ld_28 && iLow(A_symbol_0, 0, Li_ret_36) <= Ld_28) return (Li_ret_36);
      if (As_8 == "Short")
         if (iHigh(A_symbol_0, 0, Li_ret_36) >= Ld_28 && iLow(A_symbol_0, 0, Li_ret_36) <= Ld_28) return (Li_ret_36);
   }
   return (0);
}

void DeleteFile(string A_name_0) {
   int file_8 = FileOpen(A_name_0, FILE_CSV|FILE_READ, ';');
   if (file_8 > 0) {
      FileClose(file_8);
      FileDelete(A_name_0);
   }
}

void OptimizationComments(string As_0) {
   if (AnimateOptimization) {
      Gs_508 = ShiftString(Gs_508);
      Gs_516 = ShiftString(Gs_516);
      Gs_524 = ShiftString(Gs_524);
      Gs_532 = ShiftString(Gs_532);
      Gs_540 = ShiftString(Gs_540);
      Gs_548 = ShiftString(Gs_548);
      Gs_556 = ShiftString(Gs_556);
      Gs_564 = ShiftString(Gs_564);
      Gs_572 = ShiftString(Gs_572);
      Gs_580 = ShiftString(Gs_580);
      Gs_588 = ShiftString(Gs_588);
      Gs_596 = ShiftString(Gs_596);
      Gs_604 = ShiftString(Gs_604);
      Gs_612 = ShiftString(Gs_612);
      Gs_620 = ShiftString(Gs_620);
      Gs_628 = ShiftString(Gs_628);
      Gs_636 = ShiftString(Gs_636);
      Gs_644 = ShiftString(Gs_644);
      Gs_652 = ShiftString(Gs_652);
      Gs_660 = ShiftString(Gs_660);
      Gs_668 = ShiftString(Gs_668);
      Gs_676 = ShiftString(Gs_676);
      Gs_684 = ShiftString(Gs_684);
      Gs_692 = ShiftString(Gs_692);
      Gs_700 = ShiftString(Gs_700);
   }
   Comment(Gs_508 
      + "\n", Gs_516 
      + "\n", Gs_524 
      + "\n", Gs_532 
      + "\n", Gs_540 
      + "\n", Gs_548 
      + "\n", Gs_556 
      + "\n", Gs_564 
      + "\n", Gs_572 
      + "\n", Gs_580 
      + "\n", Gs_588 
      + "\n", Gs_596 
      + "\n", Gs_604 
      + "\n", Gs_612 
      + "\n", Gs_620 
      + "\n", Gs_628 
      + "\n", Gs_636 
      + "\n", Gs_644 
      + "\n", Gs_652 
      + "\n", Gs_660 
      + "\n", Gs_668 
      + "\n", Gs_676 
      + "\n", Gs_684 
      + "\n", Gs_692 
      + "\n", Gs_700 
   + "\n", As_0);
}

string ShiftString(string As_0) {
   string Ls_8 = StringSubstr(As_0, 0, 1);
   string Ls_16 = StringSubstr(As_0, 1, StringLen(As_0) - 1);
   As_0 = Ls_16 + Ls_8;
   return (As_0);
}

void ObjectMaker(string A_name_0, int Ai_8, int A_window_12, int A_datetime_16, double A_price_20, color A_color_28, int A_bool_32, int A_style_36) {
   ObjectDelete(A_name_0);
   ObjectCreate(A_name_0, Ai_8, A_window_12, A_datetime_16, A_price_20);
   ObjectSet(A_name_0, OBJPROP_COLOR, A_color_28);
   ObjectSet(A_name_0, OBJPROP_STYLE, A_style_36);
   ObjectSet(A_name_0, OBJPROP_BACK, A_bool_32);
}

int BlockID(int Ai_0) {
   int hour_4 = TimeHour(Ai_0);
   int minute_8 = TimeMinute(Ai_0);
   int Li_12 = 60 * hour_4 + minute_8;
   int Li_ret_16 = (Li_12 - MathMod(Li_12, Split)) / Split;
   int Li_20 = Li_12 / Split;
   return (Li_ret_16);
}
