/*
   Generated by EX4-TO-MQ4 decompiler LITE V4.0.409.1c [-]
   Website: https://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2011, Global Profit System"
#property link      "http://www.metaquotes.net/"

//#include <stdlib.mqh>
#import "stdlib.ex4"
   string ErrorDescription(int a0);
/*
#import "TomsEA.dll"
   double BuysPipTargetExt(double a0, int a1);
   double BuySellLotsExt(double a0, int a1);
   double GetParam(int a0);
   int InitializeFunctionsExt();
   */
#import

extern string hd1 = "===RISK LEVEL (1-LOW, 2-MODERATE, 3-HIGH)===";
extern int iRiskLevel = 2;
bool gi_88 = FALSE;
double gd_92;
bool gi_100 = TRUE;
double gd_104;
int gi_112 = 30;
int gi_116 = 60;
string gs_120 = "---------------- MACD SETTINGS----------------------";
int gi_128 = 30;
int gi_132 = 78;
int gi_136 = 1;
double gd_140;
double gd_148;
double gd_156;
int gi_164 = PERIOD_M1;
int gi_168 = PERIOD_M30;
int gi_172 = PERIOD_H1;
string gs_176 = "------------SmaAngle Settings---------------";
bool gi_184 = FALSE;
double gd_188 = 50.0;
double gd_196 = 0.25;
double gd_204 = 99.0;
double gd_212 = 100.0;
int gi_220 = 6;
bool gi_224 = TRUE;
bool gi_228 = TRUE;
extern bool HedgingAllowed = TRUE;
bool gi_236 = TRUE;
double gd_240 = 48.0;
bool gi_248 = FALSE;
string gs_252 = "Martingale Settings";
int gi_260 = 20;
bool gi_264 = FALSE;
bool gi_268 = FALSE;
double gd_272;
double gd_280;
double gd_288;
string gs_296 = "StopLoss Settings";
bool gi_304 = FALSE;
int gi_308 = 20;
bool gi_312 = FALSE;
int gi_316 = 10;
string gs_320 = "StopLossMa";
double gd_328 = 33.0;
double gd_336 = 1.0;
string gs_344 = "///////////////////////////////";
bool gi_352 = TRUE;
bool gi_356 = TRUE;
int gi_360 = 400;
string gs_364 = "///////////////////////////////";
bool gi_372 = FALSE;
int gi_376 = 400;
int gi_380;
int gi_384;
int gi_388;
int gi_392;
bool gi_396;
double gda_400[25];
double gda_404[25];
double gd_408;
double gd_416;
double gd_424;
double gd_432;
double gd_440;
double gd_448;
double gd_456;
double gd_464;
int gi_472;
int gi_476;
int gi_480 = 0;
int gi_484 = -1;
bool gi_488 = FALSE;
int gi_492 = 100;
string gs_496 = "";
int gi_504 = 5;
int gi_508 = 0;
int gi_512;
int gi_516 = 9000;
int gi_520;
double gd_524;
double gd_540;
double gd_548;
double gd_556;
double gd_564;
double gd_572;
bool gi_580;
bool gi_584;
int gi_588;
bool gi_592 = FALSE;
string gs_596 = "SlowMa";
double gd_604 = 0.0;
double gd_612 = 0.0;
double gd_620 = 0.0;
double gd_628 = 0.0;
string gs_636 = "///////////////////////////";
string gs_644 = "MediumMa";
double gd_652 = 1.0;
double gd_660 = 0.0;
double gd_668 = 0.0;
double gd_676 = 0.0;
string gs_684 = "///////////////////////////";
string gs_692 = "FastMa";
double gd_700 = 1.0;
double gd_708 = 0.0;
double gd_716 = 0.0;
double gd_724 = 0.0;
string gs_732 = "///////////////////////////";
double gd_740 = 0.0;
double gd_748 = 0.0;
double gd_756 = 0.0;
double gd_788;
double gd_796;
double gd_804;
int gi_812 = 0;
bool gi_816 = FALSE;
int gi_820 = 3;
string gs_824 = "N/A";
string gs_832 = "v.21";
string gs_840 = "     ";
string gs_848 = "          ";
string gs_856 = "               ";
string gs_864 = "                    ";
string gs_872 = "                         ";
string gs_880 = "                              ";
string gs_888 = "                                   ";
string gs_896 = "                                        ";
string gs_904 = "                                             ";
string gs_912 = "                                                  ";
bool gi_920 = FALSE;
bool gi_924 = FALSE;
int gi_928;
int gi_932;
double gda_936[10];
double gda_940[10];
double gda_944[10] = {4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0};
int gi_948 = 100;
int gi_952 = 0;
int gi_956 = 5;
int gi_960 = 120;
bool gi_964 = FALSE;
int gi_968 = 0;
int gi_972 = 3;
double gd_980 = 0.02;
double gd_988 = 0.2;
int gi_996 = 0;
int gi_1000 = 34;
int gi_1004 = 0;
int gi_1008 = 0;
int gi_1012 = 0;
int gi_1016 = 0;
int gi_1020 = 9;
double gd_1024 = 0.3;
int gi_1032 = 0;
int gi_1036 = 5;
int gi_1040 = 1;
bool gi_1044 = FALSE;
string gs_1048 = "v3.1";
string gs_1056 = "OrderReliable fname unset";
int gi_1064 = 10;
double gd_1068 = 8.0;
double gd_1076 = 25.0;
int gi_1084 = 3;
bool gi_1088 = FALSE;
bool gi_1092 = FALSE;

void f0_37(int ai_0) {
   gi_820 = ai_0;
}

int f0_3(string as_0) {
   string ls_8 = StringSubstr(as_0, 0, 6);
   if (ls_8 == "AUDCAD") return (1);
   if (ls_8 == "AUDJPY") return (2);
   if (ls_8 == "AUDNZD") return (3);
   if (ls_8 == "AUDUSD") return (4);
   if (ls_8 == "CADJPY") return (5);
   if (ls_8 == "CHFJPY") return (6);
   if (ls_8 == "EURAUD") return (7);
   if (ls_8 == "EURCAD") return (8);
   if (ls_8 == "EURCHF") return (9);
   if (ls_8 == "EURGBP") return (10);
   if (ls_8 == "EURJPY") return (11);
   if (ls_8 == "EURUSD") return (12);
   if (ls_8 == "GBPCHF") return (13);
   if (ls_8 == "GBPJPY") return (14);
   if (ls_8 == "GBPUSD") return (15);
   if (ls_8 == "NZDJPY") return (16);
   if (ls_8 == "NZDUSD") return (17);
   if (ls_8 == "USDCAD") return (18);
   if (ls_8 == "USDCHF") return (19);
   if (ls_8 == "USDJPY") return (20);
   return (21);
}

int f0_36(int ai_0) {
   switch (ai_0) {
   case 1:
      return (1);
   case 5:
      return (2);
   case 15:
      return (3);
   case 30:
      return (4);
   case 60:
      return (5);
   case 240:
      return (6);
   case 1440:
      return (7);
   case 10080:
      return (8);
   case 43200:
      return (9);
   }
   return (0);
}

double f0_2() {
   double ld_0 = MarketInfo(Symbol(), MODE_TICKSIZE);
   return (f0_21(ld_0));
}

double f0_21(double ad_0) {
   if (ad_0 == 0.00001 || ad_0 == 0.001) ad_0 = 10.0 * ad_0;
   return (ad_0);
}

int f0_29(int ai_0, int ai_4) {
   gs_824 = "NumOpenPositionsThisPair";
   f0_13(ai_0);
   if (ai_4 == -1) return (gi_928 + gi_932);
   if (ai_4 == 0) return (gi_928);
   if (ai_4 == 1) return (gi_932);
   return (0);
}

int f0_13(int ai_0) {
   gs_824 = "CountOpenOrdersThisPair";
   gi_928 = 0;
   gi_932 = 0;
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != ai_0 && ai_0 != 0) continue;
      if (OrderType() == OP_BUY) gi_928++;
      else
         if (OrderType() == OP_SELL) gi_932++;
   }
   if (gi_928 == 0 && gi_932 == 0) return (-1);
   if (gi_928 > 0 && gi_932 == 0) return (0);
   if (gi_932 > 0 && gi_928 == 0) return (1);
   return (gi_928 + gi_932);
}

int f0_18(string as_0, int ai_8, double ad_12, double ad_20, int ai_28, double ad_32, double ad_40, string as_48, int ai_56, int ai_60 = 0, color ai_64 = -1) {
   int li_80;
   double ld_84;
   double ld_92;
   double ld_100;
   double ld_108;
   double ld_116;
   int li_124;
   double ld_148;
   double ld_156;
   int li_68 = -1;
   if (!gi_1092) {
      if (IsOptimization() || IsTesting()) {
         li_68 = OrderSend(as_0, ai_8, ad_12, ad_20, ai_28, ad_32, ad_40, as_48, ai_56, ai_60, Black);
         return (li_68);
      }
   }
   gs_1056 = "OrderSendReliable";
   f0_8("");
   f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
   f0_8("Attempted " + f0_40(ai_8) + " " + ad_12 + " lots @" + ad_20 + " sl:" + ad_32 + " tp:" + ad_40);
   double ld_72 = MarketInfo(as_0, MODE_POINT);
   f0_10(0, as_0, ai_8, li_80, ld_84, ld_108, ld_116, ld_92, ld_100, 0);
   ad_20 = NormalizeDouble(ad_20, li_80);
   ad_32 = NormalizeDouble(ad_32, li_80);
   ad_40 = NormalizeDouble(ad_40, li_80);
   f0_44(as_0, ai_8, ad_20, ad_32, ad_40);
   int li_128 = GetLastError();
   li_128 = 0;
   bool li_132 = FALSE;
   bool li_136 = FALSE;
   bool li_140 = FALSE;
   if (ai_8 == OP_BUYSTOP || ai_8 == OP_SELLSTOP || ai_8 == OP_BUYLIMIT || ai_8 == OP_SELLLIMIT) {
      li_124 = 0;
      while (!li_132) {
         li_68 = OrderSend(as_0, ai_8, ad_12, ad_20, ai_28, ad_32, ad_40, as_48, ai_56, ai_60, ai_64);
         li_128 = GetLastError();
         switch (li_128) {
         case 0/* NO_ERROR */:
            li_132 = TRUE;
            break;
         case 4/* SERVER_BUSY */:
         case 6/* NO_CONNECTION */:
         case 136/* OFF_QUOTES */:
         case 137/* BROKER_BUSY */:
         case 146/* TRADE_CONTEXT_BUSY */:
         case 128/* TRADE_TIMEOUT */:
            li_124++;
            break;
         case 135/* PRICE_CHANGED */:
         case 138/* REQUOTE */:
            RefreshRates();
            continue;
            break;
         case 129/* INVALID_PRICE */:
         case 130/* INVALID_STOPS */:
            li_124++;
            ld_148 = MarketInfo(as_0, MODE_STOPLEVEL) * ld_72;
            if (ai_8 == OP_BUYSTOP || ai_8 == OP_BUYLIMIT) {
               if (MathAbs(ld_100 - ad_20) > ld_148) break;
               if (gi_1088) {
                  li_136 = TRUE;
                  li_132 = TRUE;
                  break;
               }
               if (li_140) {
                  if (ai_8 == OP_BUYSTOP) {
                     ad_20 += ld_84;
                     if (ad_32 > 0.0) ad_32 += ld_84;
                     if (ad_40 > 0.0) ad_40 += ld_84;
                     f0_8("Pending BuyStop Order still has ERR_INVALID_STOPS, adding 1 pip; new price = " + DoubleToStr(ad_20, li_80));
                     if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                  } else {
                     if (ai_8 == OP_BUYLIMIT) {
                        ad_20 -= ld_84;
                        if (ad_32 > 0.0) ad_32 -= ld_84;
                        if (ad_40 > 0.0) ad_40 -= ld_84;
                        f0_8("Pending BuyLimit Order still has ERR_INVALID_STOPS, subtracting 1 pip; new price = " + DoubleToStr(ad_20, li_80));
                        if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                     }
                  }
               } else {
                  if (ai_8 == OP_BUYLIMIT) {
                     ld_156 = ad_20;
                     ad_20 = ld_100 - ld_148;
                     if (ad_32 > 0.0) ad_32 += ad_20 - ld_156;
                     if (ad_40 > 0.0) ad_40 += ad_20 - ld_156;
                     f0_8("Pending BuyLimit has ERR_INVALID_STOPS; new price = " + DoubleToStr(ad_20, li_80));
                     if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                  } else {
                     if (ai_8 == OP_BUYSTOP) {
                        ld_156 = ad_20;
                        ad_20 = ld_100 + ld_148;
                        if (ad_32 > 0.0) ad_32 += ad_20 - ld_156;
                        if (ad_40 > 0.0) ad_40 += ad_20 - ld_156;
                        f0_8("Pending BuyStop has ERR_INVALID_STOPS; new price = " + DoubleToStr(ad_20, li_80));
                        if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                     }
                  }
                  li_140 = TRUE;
               }
               f0_44(as_0, ai_8, ad_20, ad_32, ad_40);
            } else {
               if (!(ai_8 == OP_SELLSTOP || ai_8 == OP_SELLLIMIT)) break;
               if (MathAbs(ld_92 - ad_20) > ld_148) break;
               if (gi_1088) {
                  li_136 = TRUE;
                  li_132 = TRUE;
               } else {
                  if (li_140) {
                     if (ai_8 == OP_SELLSTOP) {
                        ad_20 -= ld_84;
                        if (ad_32 > 0.0) ad_32 -= ld_84;
                        if (ad_40 > 0.0) ad_40 -= ld_84;
                        f0_8("Pending SellStop Order still has ERR_INVALID_STOPS, subtracting 1 pip; new price = " + DoubleToStr(ad_20, li_80));
                        if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                     } else {
                        if (ai_8 == OP_SELLLIMIT) {
                           ad_20 += ld_84;
                           if (ad_32 > 0.0) ad_32 += ld_84;
                           if (ad_40 > 0.0) ad_40 += ld_84;
                           f0_8("Pending SellLimit Order still has ERR_INVALID_STOPS, adding 1 pip; new price = " + DoubleToStr(ad_20, li_80));
                           if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                        }
                     }
                  } else {
                     if (ai_8 == OP_SELLSTOP) {
                        ld_156 = ad_20;
                        ad_20 = ld_92 - ld_148;
                        if (ad_32 > 0.0) ad_32 -= ld_156 - ad_20;
                        if (ad_40 > 0.0) ad_40 -= ld_156 - ad_20;
                        f0_8("Pending SellStop has ERR_INVALID_STOPS; new price = " + DoubleToStr(ad_20, li_80));
                        if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                     } else {
                        if (ai_8 == OP_SELLLIMIT) {
                           ld_156 = ad_20;
                           ad_20 = ld_92 + ld_148;
                           if (ad_32 > 0.0) ad_32 -= ld_156 - ad_20;
                           if (ad_40 > 0.0) ad_40 -= ld_156 - ad_20;
                           f0_8("Pending SellLimit has ERR_INVALID_STOPS; new price = " + DoubleToStr(ad_20, li_80));
                           if (ad_32 > 0.0 || ad_40 > 0.0) f0_8("NOTE: SL (now " + DoubleToStr(ad_32, li_80) + ") & TP (now " + DoubleToStr(ad_40, li_80) + ") were adjusted proportionately");
                        }
                     }
                     li_140 = TRUE;
                  }
                  f0_44(as_0, ai_8, ad_20, ad_32, ad_40);
               }
            }
            break;
         case 3/* INVALID_TRADE_PARAMETERS */:
         default:
            li_132 = TRUE;
         }
         if (li_124 > gi_1064) li_132 = TRUE;
         if (li_132) {
            if (li_128 != 0/* NO_ERROR */ && li_128 != 1/* NO_RESULT */) f0_8("Non-retryable error: " + f0_12(li_128));
            if (li_124 > gi_1064) f0_8("Retry attempts maxed at " + gi_1064);
         } else {
            f0_8("Result of attempt " + li_124 + " of " + gi_1064 + ": Retryable error: " + f0_12(li_128));
            f0_8("~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~");
            f0_32(gd_1068, gd_1076);
            RefreshRates();
         }
      }
      if (li_128 == 0/* NO_ERROR */ || li_128 == 1/* NO_RESULT */) {
         f0_8("Ticket #" + li_68 + ": Successful " + f0_40(ai_8) + " order placed, details follow.");
         OrderSelect(li_68, SELECT_BY_TICKET, MODE_TRADES);
         OrderPrint();
         f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
         f0_8("");
         return (li_68);
      }
      if (!li_136) {
         f0_8("Failed to execute stop or limit order after " + gi_1064 + " retries");
         f0_8("Failed trade: " + f0_40(ai_8) + " " + DoubleToStr(ad_12, 2) + " lots  " + as_0 + "@" + ad_20 + " tp@" + ad_40 + " sl@" + ad_32);
         f0_8("Last error: " + f0_12(li_128));
         f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
         f0_8("");
         return (-1);
      }
   }
   if (li_136) {
      f0_8("Going from stop/limit order to market order because market is too close.");
      if (ai_8 == OP_BUYSTOP || ai_8 == OP_BUYLIMIT) {
         ai_8 = 0;
         ad_20 = ld_100;
      } else {
         if (ai_8 == OP_SELLSTOP || ai_8 == OP_SELLLIMIT) {
            ai_8 = 1;
            ad_20 = ld_92;
         }
      }
   }
   li_128 = GetLastError();
   li_128 = 0;
   li_68 = -1;
   li_132 = FALSE;
   if (ai_8 == OP_BUY || ai_8 == OP_SELL) {
      li_124 = 0;
      while (!li_132) {
         li_68 = OrderSend(as_0, ai_8, ad_12, ad_20, ai_28, ad_32, ad_40, as_48, ai_56, ai_60, ai_64);
         li_128 = GetLastError();
         switch (li_128) {
         case 0/* NO_ERROR */:
            li_132 = TRUE;
            break;
         case 4/* SERVER_BUSY */:
         case 6/* NO_CONNECTION */:
         case 129/* INVALID_PRICE */:
         case 136/* OFF_QUOTES */:
         case 137/* BROKER_BUSY */:
         case 146/* TRADE_CONTEXT_BUSY */:
         case 128/* TRADE_TIMEOUT */:
            li_124++;
            break;
         case 135/* PRICE_CHANGED */:
         case 138/* REQUOTE */:
            RefreshRates();
            continue;
            break;
         default:
            li_132 = TRUE;
         }
         if (li_124 > gi_1064) li_132 = TRUE;
         if (!li_132) {
            f0_8("Result of attempt " + li_124 + " of " + gi_1064 + ": Retryable error: " + f0_12(li_128));
            f0_8("~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~");
            f0_32(gd_1068, gd_1076);
            RefreshRates();
         } else {
            if (li_128 != 0/* NO_ERROR */ && li_128 != 1/* NO_RESULT */) f0_8("Non-retryable error: " + f0_12(li_128));
            if (li_124 > gi_1064) f0_8("Retry attempts maxed at " + gi_1064);
         }
      }
      if (li_128 == 0/* NO_ERROR */ || li_128 == 1/* NO_RESULT */) {
         f0_8("Ticket #" + li_68 + ": Successful " + f0_40(ai_8) + " order placed, details follow.");
         OrderSelect(li_68, SELECT_BY_TICKET, MODE_TRADES);
         OrderPrint();
         f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
         f0_8("");
         return (li_68);
      }
      f0_8("Failed to execute OP_BUY/OP_SELL, after " + gi_1064 + " retries");
      f0_8("Failed trade: " + f0_40(ai_8) + " " + DoubleToStr(ad_12, 2) + " lots  " + as_0 + "@" + ad_20 + " tp@" + ad_40 + " sl@" + ad_32);
      f0_8("Last error: " + f0_12(li_128));
      f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
      f0_8("");
      return (-1);
   }
   return (0);
}

int f0_30(string as_0, int ai_8, double ad_12, double ad_20, int ai_28, double ad_32, double ad_40, string as_48, int ai_56, int ai_60 = 0, color ai_64 = -1) {
   int li_72;
   double ld_76;
   double ld_84;
   double ld_92;
   double ld_100;
   double ld_108;
   int li_116;
   double ld_128;
   int li_136;
   int li_68 = -1;
   if (IsOptimization() || IsTesting()) {
      li_68 = OrderSend(as_0, ai_8, ad_12, ad_20, ai_28, ad_32, ad_40, as_48, ai_56, ai_60, Black);
      return (li_68);
   }
   if (ai_8 > OP_SELL) {
      li_68 = f0_18(as_0, ai_8, ad_12, ad_20, ai_28, 0, 0, as_48, ai_56, ai_60, ai_64);
      return (li_68);
   }
   gs_1056 = "OrderSendReliableMKT";
   f0_8("");
   f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
   f0_8("Attempted " + f0_40(ai_8) + " " + ad_12 + " lots @" + ad_20 + " sl:" + ad_32 + " tp:" + ad_40);
   f0_10(0, as_0, ai_8, li_72, ld_76, ld_100, ld_108, ld_84, ld_92, 0);
   ad_20 = NormalizeDouble(ad_20, li_72);
   ad_32 = NormalizeDouble(ad_32, li_72);
   ad_40 = NormalizeDouble(ad_40, li_72);
   f0_44(as_0, ai_8, ad_20, ad_32, ad_40);
   int li_120 = GetLastError();
   li_120 = 0;
   bool li_124 = FALSE;
   if (ai_8 == OP_BUY || ai_8 == OP_SELL) {
      li_116 = 0;
      while (!li_124) {
         ld_128 = ad_20;
         li_136 = ai_28;
         if (ai_8 == OP_BUY) {
            ld_128 = NormalizeDouble(MarketInfo(as_0, MODE_ASK), MarketInfo(as_0, MODE_DIGITS));
            if (ld_128 > ad_20) li_136 = ai_28 - (ld_128 - ad_20) / ld_76;
         } else {
            if (ai_8 == OP_SELL) {
               ld_128 = NormalizeDouble(MarketInfo(as_0, MODE_BID), MarketInfo(as_0, MODE_DIGITS));
               if (ld_128 < ad_20) li_136 = ai_28 - (ad_20 - ld_128) / ld_76;
            }
         }
         if (li_136 > ai_28) li_136 = ai_28;
         if (li_136 >= 0) {
            li_68 = OrderSend(as_0, ai_8, ad_12, ld_128, li_136, ad_32, ad_40, as_48, ai_56, ai_60, ai_64);
            li_120 = GetLastError();
         } else li_120 = 129;
         switch (li_120) {
         case 135/* PRICE_CHANGED */: continue;
         case 138/* REQUOTE */: continue;
         case 0/* NO_ERROR */:
            li_124 = TRUE;
            break;
         case 4/* SERVER_BUSY */:
         case 6/* NO_CONNECTION */:
         case 129/* INVALID_PRICE */:
         case 136/* OFF_QUOTES */:
         case 137/* BROKER_BUSY */:
         case 146/* TRADE_CONTEXT_BUSY */:
         case 128/* TRADE_TIMEOUT */:
            li_116++;
            break;
         default:
            li_124 = TRUE;
         }
         if (li_116 > gi_1064) li_124 = TRUE;
         if (!li_124) {
            f0_8("Result of attempt " + li_116 + " of " + gi_1064 + ": Retryable error: " + f0_12(li_120));
            f0_8("~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~");
            f0_32(gd_1068, gd_1076);
         } else {
            if (li_120 != 0/* NO_ERROR */ && li_120 != 1/* NO_RESULT */) f0_8("Non-retryable error: " + f0_12(li_120));
            if (li_116 > gi_1064) f0_8("Retry attempts maxed at " + gi_1064);
         }
      }
      if (li_120 == 0/* NO_ERROR */ || li_120 == 1/* NO_RESULT */) {
         f0_8("Ticket #" + li_68 + ": Successful " + f0_40(ai_8) + " order placed, details follow.");
         OrderSelect(li_68, SELECT_BY_TICKET, MODE_TRADES);
         OrderPrint();
         f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
         f0_8("");
         return (li_68);
      }
      f0_8("Failed to execute OP_BUY/OP_SELL, after " + gi_1064 + " retries");
      f0_8("Failed trade: " + f0_40(ai_8) + " " + DoubleToStr(ad_12, 2) + " lots  " + as_0 + "@" + ad_20 + " tp@" + ad_40 + " sl@" + ad_32);
      f0_8("Last error: " + f0_12(li_120));
      f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
      f0_8("");
      return (-1);
   }
   return (0);
}

int f0_7(int ai_0, double ad_4, double ad_12, double ad_20, int ai_28, color ai_32 = -1) {
   int li_52;
   int li_56;
   double ld_60;
   double ld_68;
   double ld_76;
   double ld_84;
   double ld_92;
   bool li_36 = FALSE;
   bool li_40 = FALSE;
   if (!gi_1092) {
      if (IsOptimization() || IsTesting()) {
         li_36 = OrderModify(ai_0, ad_4, ad_12, ad_20, ai_28, Black);
         return (li_36);
      }
   }
   gs_1056 = "OrderModifyReliable";
   f0_8("");
   f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
   f0_8("Attempted modify of #" + ai_0 + " price:" + ad_4 + " sl:" + ad_12 + " tp:" + ad_20);
   string ls_44 = "ALLOCATE";
   f0_10(ai_0, ls_44, li_52, li_56, ld_60, ld_84, ld_92, ld_68, ld_76);
   double ld_100 = ad_4;
   if (li_52 == OP_BUY) ld_100 = ld_68;
   else
      if (li_52 == OP_SELL) ld_100 = ld_76;
   ld_100 = NormalizeDouble(ld_100, li_56);
   ad_4 = NormalizeDouble(ad_4, li_56);
   ad_12 = NormalizeDouble(ad_12, li_56);
   ad_20 = NormalizeDouble(ad_20, li_56);
   double ld_108 = ad_12;
   double ld_116 = ad_20;
   if (ad_12 == ld_84) ld_108 = 0;
   if (ad_20 == ld_92) ld_116 = 0;
   f0_44(ls_44, li_52, ld_100, ld_108, ld_116, 0);
   if (ad_12 != ld_84) ad_12 = ld_108;
   if (ad_20 != ld_92) ad_20 = ld_116;
   int li_124 = 0;
   int li_128 = GetLastError();
   li_128 = 0;
   bool li_132 = FALSE;
   while (!li_132) {
      li_36 = OrderModify(ai_0, ad_4, ad_12, ad_20, ai_28, ai_32);
      li_128 = GetLastError();
      if (li_36 == TRUE) li_132 = TRUE;
      else {
         switch (li_128) {
         case 0/* NO_ERROR */:
            li_132 = TRUE;
            f0_8("ERR_NO_ERROR received, but OrderClose() returned false; exiting");
            break;
         case 1/* NO_RESULT */:
            li_132 = TRUE;
            break;
         case 130/* INVALID_STOPS */:
            f0_8("OrderModifyReliable, ERR_INVALID_STOPS, should not happen; stops already adjusted");
         case 2/* COMMON_ERROR */:
         case 4/* SERVER_BUSY */:
         case 6/* NO_CONNECTION */:
         case 8/* TOO_FREQUENT_REQUESTS */:
         case 128/* TRADE_TIMEOUT */:
         case 129/* INVALID_PRICE */:
         case 136/* OFF_QUOTES */:
         case 137/* BROKER_BUSY */:
         case 141/* TOO_MANY_REQUESTS */:
         case 146/* TRADE_CONTEXT_BUSY */:
            li_124++;
            break;
         case 145/* TRADE_MODIFY_DENIED */: break;
         case 135/* PRICE_CHANGED */:
         case 138/* REQUOTE */:
            RefreshRates();
            continue;
            break;
         default:
            li_132 = TRUE;
            li_40 = TRUE;
         }
      }
      if (li_124 > gi_1064) li_132 = TRUE;
      if (!li_132) {
         f0_8("Result of attempt " + li_124 + " of " + gi_1064 + ": Retryable error: " + f0_12(li_128));
         f0_8("~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~  ~");
         f0_32(gd_1068, gd_1076);
         RefreshRates();
      } else {
         if (li_124 > gi_1064) f0_8("Retry attempts maxed at " + gi_1064);
         else
            if (li_40) f0_8("Non-retryable error: " + f0_12(li_128));
      }
   }
   if (li_128 == 1/* NO_RESULT */) {
      f0_8("Server reported modify order did not actually change parameters.");
      f0_8("Redundant modification: " + ai_0 + " " + ls_44 + "@" + ad_4 + " tp@" + ad_20 + " sl@" + ad_12);
      f0_8("Suggest modifying code logic to avoid.");
   }
   if (li_36) {
      f0_8("Ticket #" + ai_0 + ": Modification successful, updated trade details follow.");
      OrderSelect(ai_0, SELECT_BY_TICKET, MODE_TRADES);
      OrderPrint();
   } else {
      f0_8("Failed to execute modify after " + gi_1064 + " retries");
      f0_8("Failed modification: " + ai_0 + " " + ls_44 + "@" + ad_4 + " tp@" + ad_20 + " sl@" + ad_12);
      f0_8("Last error: " + f0_12(li_128));
   }
   f0_8("•  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •");
   f0_8("");
   return (li_36);
}

string f0_12(int ai_0) {
   return (ai_0 + "  ::  " + ErrorDescription(ai_0));
}

void f0_8(string as_0) {
   if (gi_1084 >= 99 || (!(IsTesting() || IsOptimization())))
      if (gi_1084 > 0) Print(gs_1056 + " " + gs_1048 + ":     " + as_0);
}

string f0_40(int ai_0) {
   if (ai_0 == 0) return ("BUY");
   if (ai_0 == 1) return ("SELL");
   if (ai_0 == 4) return ("BUY STOP");
   if (ai_0 == 5) return ("SELL STOP");
   if (ai_0 == 2) return ("BUY LIMIT");
   if (ai_0 == 3) return ("SELL LIMIT");
   return ("None (" + ai_0 + ")");
}

void f0_44(string as_0, int ai_8, double ad_12, double &ad_20, double &ad_28, bool ai_36 = TRUE) {
   double ld_72;
   double ld_80;
   string ls_40 = gs_1056;
   gs_1056 = "EnsureValidStops";
   double ld_48 = MarketInfo(as_0, MODE_POINT);
   double ld_56 = ad_20;
   double ld_64 = ad_28;
   int li_88 = MarketInfo(as_0, MODE_STOPLEVEL);
   double ld_92 = li_88 * ld_48;
   double ld_100 = MarketInfo(as_0, MODE_ASK) - MarketInfo(as_0, MODE_BID);
   if (ad_20 != 0.0) {
      if (ai_8 % 2 == 0) {
         ld_72 = ad_12 - ld_92;
         if (ai_8 == 0 && ai_36) ld_72 -= ld_100;
         ad_20 = MathMin(ad_20, ld_72);
      } else {
         ld_72 = ad_12 + ld_92;
         if (ai_8 == 1 && ai_36) ld_72 += ld_100;
         ad_20 = MathMax(ad_20, ld_72);
      }
      ad_20 = NormalizeDouble(ad_20, MarketInfo(as_0, MODE_DIGITS));
   }
   if (ad_28 != 0.0) {
      if (MathAbs(ad_12 - ad_28) <= ld_92) {
         if (ai_8 % 2 == 0) {
            ld_80 = ad_12 + ld_92;
            ad_28 = MathMax(ad_28, ld_80);
         } else {
            ld_80 = ad_12 - ld_92;
            ad_28 = MathMin(ad_28, ld_80);
         }
         ad_28 = NormalizeDouble(ad_28, MarketInfo(as_0, MODE_DIGITS));
      }
   }
   if (ad_20 != ld_56) f0_8("SL was too close to brokers min distance (" + li_88 + "); moved SL to: " + ad_20);
   if (ad_28 != ld_64) f0_8("TP was too close to brokers min distance (" + li_88 + "); moved TP to: " + ad_28);
   gs_1056 = ls_40;
}

void f0_32(double ad_0, double ad_8) {
   double ld_16;
   int li_24;
   double ld_28;
   if (IsTesting() == FALSE) {
      ld_16 = MathCeil(ad_0 / 0.1);
      if (ld_16 > 0.0) {
         li_24 = MathRound(ad_8 / 0.1);
         ld_28 = 1.0 - 1.0 / ld_16;
         Sleep(20);
         for (int li_36 = 0; li_36 < li_24; li_36++) {
            if (MathRand() > 32768.0 * ld_28) break;
            Sleep(20);
         }
      }
   }
}

int f0_10(int ai_0, string &as_4, int &ai_12, int &ai_16, double &ad_20, double &ad_28, double &ad_36, double &ad_44, double &ad_52, bool ai_60 = TRUE) {
   int li_64;
   string ls_68;
   if (ai_60) {
      li_64 = OrderTicket();
      if (!OrderSelect(ai_0, SELECT_BY_TICKET, MODE_TRADES)) {
         f0_8("OrderSelect() error: " + ErrorDescription(GetLastError()));
         return (0);
      }
      as_4 = OrderSymbol();
      ai_12 = OrderType();
      ad_36 = OrderTakeProfit();
      ad_28 = OrderStopLoss();
      if (li_64 >= 0) OrderSelect(li_64, SELECT_BY_TICKET, MODE_TRADES);
   }
   ad_44 = NormalizeDouble(MarketInfo(as_4, MODE_BID), MarketInfo(as_4, MODE_DIGITS));
   ad_52 = NormalizeDouble(MarketInfo(as_4, MODE_ASK), MarketInfo(as_4, MODE_DIGITS));
   ad_20 = MarketInfo(as_4, MODE_POINT);
   if (ad_20 == 0.001 || ad_20 == 0.00001) ad_20 = 10.0 * ad_20;
   ai_16 = MarketInfo(as_4, MODE_DIGITS);
   if (ai_16 == 0) {
      ls_68 = gs_1056;
      gs_1056 = "GetDigits";
      f0_8("error: MarketInfo(symbol, MODE_DIGITS) == 0");
      gs_1056 = ls_68;
      return (0);
   }
   if (ai_60) {
      ad_36 = NormalizeDouble(ad_36, ai_16);
      ad_28 = NormalizeDouble(ad_28, ai_16);
      ad_44 = NormalizeDouble(ad_44, ai_16);
      ad_52 = NormalizeDouble(ad_52, ai_16);
   }
   return (1);
}

int init() {
   Comment("  Tom\'s EA 1.88 waiting for Authentication...");
   gi_812 = 0;
   gi_816 = FALSE;
   if (IsDllsAllowed() == FALSE) {
      MessageBox("DLL calls are not enabled. Tom\'s EA will not be running. Please enable DLL calls and rerun EA.", "Warning...");
      Print("DLL calls disabled. Tom\'s EA suspended.");
      return (0);
   }
   if (Period() != PERIOD_M1) {
      MessageBox("Tom\'s EA is designed to run on 1 Minute period only. Execution on another periods is not allowed. Please change back to the 1 Minute period", "Warning...");
      Print("Wrong Period loaded. Tom\'s EA suspended.");
      return (0);
   }
   gd_104 = 20;
   if (iRiskLevel == 1) gd_104 = 10;
   if (iRiskLevel == 2) gd_104 = 20;
   if (iRiskLevel == 3) gd_104 = 30;
   if (iRiskLevel < 1) gd_104 = 10;
   if (iRiskLevel > 3) gd_104 = 30;
   while (gi_812 == 1 || gi_812 == 0) {
      Print("waiting for init...");
      gi_812 = InitializeFunctionsExt();
      Sleep(5000);
   }
   if (gi_812 > 1000) gi_816 = TRUE;
   if (gi_816 == TRUE) Comment("  Tom\'s EA 1.88 is Authenticated.");
   else Comment("  Tom\'s EA 1.88 is Not Authenticated.");
   if (gi_816 == FALSE) return (0);
   gd_92 = GetParam(1);
   gd_140 = GetParam(3);
   gd_148 = GetParam(4);
   gd_156 = GetParam(5);
   gd_272 = GetParam(6);
   gd_280 = GetParam(7);
   gd_288 = GetParam(8);
   if (Digits == 3) {
      gd_140 = 10.0 * gd_140;
      gd_148 = 10.0 * gd_148;
      gd_156 = 10.0 * gd_156;
   }
   gi_380 = gi_112;
   gi_384 = gi_116;
   gi_388 = gi_112;
   gi_392 = gi_116;
   if (gi_100) gd_92 = AccountEquity() * gd_104 / 10000000.0;
   double ld_0 = MarketInfo(Symbol(), MODE_MINLOT);
   if (ld_0 > 0.1) ld_0 = 0.1;
   gd_524 = 2;
   if (ld_0 >= 0.1) gd_524 = 1;
   if (ld_0 < 0.1) gd_524 = 2;
   if (ld_0 < 0.01) gd_524 = 3;
   if (gd_524 == 1.0) {
      gi_88 = TRUE;
      Print("Mini account detected.");
   }
   if (gi_88) gd_92 = 10.0 * gd_92;
   if (gd_92 < ld_0) gd_92 = ld_0;
   gd_796 = gd_92;
   gd_804 = gd_92;
   Print("Toms EA started.");
   PlaySound("Expert restart.wav");
   ObjectDelete("BuysBreakevenLine");
   ObjectDelete("SellsBreakevenLine");
   gi_512 = gi_516 + 100 * f0_3(Symbol()) + f0_36(Period());
   gi_520 = 0;
   f0_37(gi_508);
   gi_588 = 1;
   if (gi_592) gi_588 = 0;
   GlobalVariableSet(Symbol() + "_" + Period() + "_" + "MacdFastEma", gi_128);
   GlobalVariableSet(Symbol() + "_" + Period() + "_" + "MacdSlowEma", gi_132);
   GlobalVariableSet(Symbol() + "_" + Period() + "_" + "MacdSignalSma", gi_136);
   GlobalVariableSet(Symbol() + "_" + Period() + "_" + "CounterTradingLevel", gd_156);
   GlobalVariableSet(Symbol() + "_" + Period() + "_" + "MinimumLevel", gd_140);
   GlobalVariableSet(Symbol() + "_" + Period() + "_" + "MaximumLevel", gd_148);
   return (0);
}

int deinit() {
   ObjectDelete("BuysBreakevenLine");
   ObjectDelete("SellsBreakevenLine");
   return (0);
}

int f0_6() {
   int li_0 = 0;
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_BUY || !OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderTicket() > li_0) li_0 = OrderTicket();
   }
   return (li_0);
}

int f0_41() {
   int li_0 = 0;
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_SELL || !OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderTicket() > li_0) li_0 = OrderTicket();
   }
   return (li_0);
}

double f0_35() {
   double ld_0;
   double ld_8;
   double ld_16;
   double ld_24;
   double ld_32;
   int li_48 = f0_29(gi_512, 0);
   for (int li_52 = 0; li_52 <= OrdersTotal() - 1; li_52++) {
      OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_BUY || !OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES)) continue;
      ld_24 = OrderOpenPrice();
      ld_16 = OrderLots();
      ld_32 = ld_24 * ld_16;
      ld_0 += ld_16;
      ld_8 += ld_32;
   }
   if (ld_0 == 0.0) ld_0 = 1;
   gd_424 = ld_0;
   double ld_40 = ld_8 / ld_0;
   return (ld_40);
}

double f0_24() {
   double ld_0;
   double ld_8;
   double ld_16;
   double ld_24;
   double ld_32;
   int li_48 = f0_29(gi_512, 1);
   for (int li_52 = 0; li_52 <= OrdersTotal() - 1; li_52++) {
      OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_SELL || !OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES)) continue;
      ld_24 = OrderOpenPrice();
      ld_16 = OrderLots();
      ld_32 = ld_24 * ld_16;
      ld_0 += ld_16;
      ld_8 += ld_32;
   }
   if (ld_0 == 0.0) ld_0 = 1;
   gd_432 = ld_0;
   double ld_40 = ld_8 / ld_0;
   return (ld_40);
}

void f0_14(string as_0) {
   if (IsTesting() == FALSE) {
      if (gi_488) Alert(as_0);
      if (gi_396) SendMail(gs_496, as_0);
   }
}

double f0_1() {
   double ld_0 = BuysPipTargetExt(gd_456, gi_472);
   return (ld_0);
}

double f0_23() {
   double ld_0 = BuysPipTargetExt(gd_464, gi_476);
   return (ld_0);
}

void f0_20() {
   int li_0;
   double ld_4;
   int li_20;
   int li_24;
   double ld_28;
   double ld_36;
   int li_44;
   double ld_12 = iLow(NULL, 0, 1);
   double ld_48 = iMA(NULL, gd_756, gd_328, gd_748, gd_336, gd_740, 0);
   OrderSelect(f0_38(), SELECT_BY_TICKET, MODE_TRADES);
   if (gi_372) ld_4 = OrderOpenPrice() - gi_376 * f0_2();
   if (gi_312) ld_4 = ld_48 - gi_316 * f0_2();
   OrderSelect(f0_6(), SELECT_BY_TICKET, MODE_TRADES);
   if (gi_304 && Bid - OrderOpenPrice() > gi_308 * f0_2()) {
      ld_4 = OrderOpenPrice();
      li_0 = 1;
   }
   for (int li_56 = OrdersTotal() - 1; li_56 >= 0; li_56--) {
      OrderSelect(li_56, SELECT_BY_POS, MODE_TRADES);
      li_20 = OrderType();
      ld_28 = OrderOpenPrice();
      li_24 = OrderTicket();
      ld_36 = OrderStopLoss();
      li_44 = OrderMagicNumber();
      ld_4 = 0;
      if (li_20 != OP_BUY || li_44 != gi_512) continue;
      if (gi_584 && OrderComment() == "cyatrade") continue;
      if (NormalizeDouble(OrderTakeProfit(), Digits) == NormalizeDouble(gd_564, Digits) && NormalizeDouble(ld_36, Digits) == NormalizeDouble(ld_4, Digits)) continue;
      f0_7(li_24, ld_28, ld_4, gd_564, 0, CLR_NONE);
   }
   if (gd_564 <= Bid) f0_4();
}

void f0_0() {
   int li_0;
   double ld_4;
   int li_20;
   int li_24;
   double ld_28;
   double ld_36;
   int li_44;
   double ld_12 = iHigh(NULL, 0, 1);
   double ld_48 = iMA(NULL, gd_756, gd_328, gd_748, gd_336, gd_740, 0);
   OrderSelect(f0_33(), SELECT_BY_TICKET, MODE_TRADES);
   if (gi_372) ld_4 = OrderOpenPrice() + gi_376 * f0_2();
   if (gi_312) ld_4 = ld_48 + gi_316 * f0_2();
   OrderSelect(f0_41(), SELECT_BY_TICKET, MODE_TRADES);
   if (gi_304 && OrderOpenPrice() - Ask > gi_308 * f0_2()) {
      ld_4 = OrderOpenPrice();
      li_0 = 1;
   }
   for (int li_56 = OrdersTotal() - 1; li_56 >= 0; li_56--) {
      OrderSelect(li_56, SELECT_BY_POS, MODE_TRADES);
      li_20 = OrderType();
      ld_28 = OrderOpenPrice();
      li_24 = OrderTicket();
      ld_36 = OrderStopLoss();
      li_44 = OrderMagicNumber();
      ld_4 = 0;
      if (li_20 != OP_SELL || li_44 != gi_512) continue;
      if (gi_580 && OrderComment() == "cyatrade") continue;
      if (NormalizeDouble(OrderTakeProfit(), Digits) == NormalizeDouble(gd_572, Digits) && NormalizeDouble(OrderStopLoss(), Digits) == NormalizeDouble(ld_4, Digits)) continue;
      f0_7(li_24, ld_28, ld_4, gd_572, 0, CLR_NONE);
   }
   if (gd_572 >= Ask) f0_17();
}

void f0_11() {
   int li_0;
   int li_4;
   double ld_8;
   double ld_16;
   int li_24;
   double ld_36;
   double ld_44;
   double ld_64;
   int li_72;
   int li_76;
   int li_80;
   string ls_28 = "";
   double ld_52 = 600;
   int li_60 = 100;
   for (int li_84 = OrdersTotal() - 1; li_84 >= 0; li_84--) {
      OrderSelect(li_84, SELECT_BY_POS, MODE_TRADES);
      li_0 = OrderType();
      ld_8 = OrderOpenPrice();
      li_4 = OrderTicket();
      ld_16 = OrderStopLoss();
      li_24 = OrderMagicNumber();
      ls_28 = OrderSymbol();
      ld_36 = OrderLots();
      if (li_24 == gi_512) {
         if (ls_28 == Symbol()) {
            RefreshRates();
            if (li_0 == OP_BUY) {
               ld_44 = MarketInfo(ls_28, MODE_BID);
               if (ld_44 == 0.0) continue;
               if (ld_44 > ld_8 - ld_52 * f0_2()) continue;
               while (true) {
                  ld_64 = MarketInfo(ls_28, MODE_BID);
                  li_72 = OrderClose(li_4, ld_36, ld_64, li_60, Red);
                  if (li_72 != 1) {
                     li_76 = GetLastError();
                     Print("LastError = ", li_76);
                  } else li_76 = 0;
                  if (li_76 == 135/* PRICE_CHANGED */) {
                     Sleep(1000);
                     RefreshRates();
                     li_80++;
                  }
                  if (li_76 == 136/* OFF_QUOTES */) {
                     Sleep(1000);
                     RefreshRates();
                     li_80++;
                  }
                  if (li_76 == 137/* BROKER_BUSY */) {
                     Sleep(1000);
                     RefreshRates();
                     li_80++;
                  }
                  if (li_76 == 138/* REQUOTE */) {
                     Sleep(1000);
                     RefreshRates();
                     li_80++;
                  }
                  if (li_76 != 142) break;
                  Sleep(1000);
                  RefreshRates();
                  li_80++;
               }
            }
            ld_44 = MarketInfo(ls_28, MODE_ASK);
            if (ld_44 != 0.0) {
               if (ld_44 >= ld_8 + ld_52 * f0_2()) {
                  while (true) {
                     ld_64 = MarketInfo(ls_28, MODE_ASK);
                     li_72 = OrderClose(li_4, ld_36, ld_64, li_60, Red);
                     if (li_72 != 1) {
                        li_76 = GetLastError();
                        Print("LastError = ", li_76);
                     } else li_76 = 0;
                     if (li_76 == 135/* PRICE_CHANGED */) {
                        Sleep(1000);
                        RefreshRates();
                        li_80++;
                     }
                     if (li_76 == 136/* OFF_QUOTES */) {
                        Sleep(1000);
                        RefreshRates();
                        li_80++;
                     }
                     if (li_76 == 137/* BROKER_BUSY */) {
                        Sleep(1000);
                        RefreshRates();
                        li_80++;
                     }
                     if (li_76 == 138/* REQUOTE */) {
                        Sleep(1000);
                        RefreshRates();
                        li_80++;
                     }
                     if (li_76 != 142) break;
                     Sleep(1000);
                     RefreshRates();
                     li_80++;
                  }
               }
            }
         }
      }
   }
}

int start() {
   bool li_36;
   bool li_40;
   int li_44;
   int li_48;
   if (gi_816 == FALSE) return (0);
   if (AccountMargin() == 0.0) gd_540 = 1;
   else gd_540 = AccountMargin();
   gd_540 = NormalizeDouble(100.0 * (AccountEquity() / gd_540), 0);
   double ld_8 = AccountEquity();
   double ld_16 = AccountBalance() - AccountBalance() * (gd_204 / 100.0);
   if (ld_8 <= ld_16) {
      f0_17();
      f0_4();
   }
   bool li_24 = f0_15();
   string ls_28 = "start()";
   gi_472 = f0_29(gi_512, 0);
   gi_476 = f0_29(gi_512, 1);
   gi_584 = FALSE;
   gs_496 = "";
   if (gi_476 >= gi_260 && (!gi_580)) gi_584 = TRUE;
   gi_580 = FALSE;
   if (gi_472 >= gi_260 && (!gi_584)) gi_580 = TRUE;
   if (gi_472 <= 1) li_44 = gi_380;
   else li_44 = gi_384;
   if (gi_476 <= 1) li_48 = gi_388;
   else li_48 = gi_392;
   if (OrderSelect(f0_6(), SELECT_BY_TICKET, MODE_TRADES)) gd_440 = OrderOpenPrice();
   if (OrderSelect(f0_41(), SELECT_BY_TICKET, MODE_TRADES)) gd_448 = OrderOpenPrice();
   gd_408 = gd_796;
   gd_416 = gd_804;
   for (int li_52 = 1; li_52 < 21; li_52++) {
      gda_400[li_52] = BuySellLotsExt(gd_408, li_52);
      gda_404[li_52] = BuySellLotsExt(gd_416, li_52);
   }
   gd_456 = gd_796 * li_44;
   gd_464 = gd_804 * li_48;
   if (gi_472 == 0) gd_564 = 0;
   if (gi_476 == 0) gd_572 = 0;
   if (gi_472 > 0) gd_564 = f0_35() + f0_1() * f0_2() / f0_22();
   if (gi_476 > 0) gd_572 = f0_24() - f0_23() * f0_2() / f0_34();
   OrderSelect(f0_6(), SELECT_BY_TICKET, MODE_TRADES);
   if (gi_472 != gd_548) {
      gd_548 = gi_472;
      f0_20();
      if (gi_580) f0_27();
   }
   if (li_24 && gi_472 > 0 && OrderStopLoss() != OrderOpenPrice()) f0_20();
   if (gi_304 && Bid - OrderOpenPrice() > gi_308 * f0_2() && OrderStopLoss() != OrderOpenPrice()) f0_20();
   OrderSelect(f0_41(), SELECT_BY_TICKET, MODE_TRADES);
   if (gi_476 != gd_556) {
      gd_556 = gi_476;
      f0_0();
      if (gi_584) f0_26();
   }
   if (li_24 && gi_476 > 0 && OrderStopLoss() != OrderOpenPrice()) f0_0();
   if (gi_304 && OrderOpenPrice() - Ask > gi_308 * f0_2() && OrderStopLoss() != OrderOpenPrice()) f0_0();
   f0_11();
   if (li_24) {
      li_36 = f0_39();
      if (li_36) f0_26();
      li_40 = f0_19();
      if (li_40) f0_27();
   }
   gd_788 = iClose(NULL, PERIOD_D1, 1);
   if (Hour() == 0) ObjectDelete("Yesterdays close");
   ObjectCreate("Yesterdays close", OBJ_HLINE, 0, TimeCurrent(), gd_788);
   return (0);
}

int f0_26() {
   int li_0 = f0_9();
   if (li_0 >= 0) f0_14(TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + ": " + f0_40(0) + " " + DoubleToStr(OrderLots(), 2) + " " + Symbol() + " @ " + DoubleToStr(Ask, Digits));
   return (li_0);
}

int f0_27() {
   int li_0 = f0_31();
   if (li_0 >= 0) f0_14(TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) + ": " + f0_40(1) + " " + DoubleToStr(OrderLots(), 2) + " " + Symbol() + " @ " + DoubleToStr(Ask, Digits));
   return (li_0);
}

double f0_5() {
   double ld_8;
   double ld_0 = 0;
   if (OrderSelect(f0_38(), SELECT_BY_TICKET, MODE_TRADES)) ld_8 = OrderStopLoss();
   double ld_16 = iLow(NULL, 0, 1);
   if (gi_352 && gi_356) ld_0 = ld_16 - gi_360 * f0_2();
   if (gi_352 && (!gi_356) && gi_472 == 0) ld_0 = ld_16 - gi_360 * f0_2();
   if (gi_352 && (!gi_356) && gi_472 > 0) ld_0 = ld_8;
   return (ld_0);
}

double f0_43() {
   double ld_8;
   double ld_0 = 0;
   if (OrderSelect(f0_38(), SELECT_BY_TICKET, MODE_TRADES)) ld_8 = OrderStopLoss();
   double ld_16 = iHigh(NULL, 0, 1);
   if (gi_352 && gi_356) ld_0 = ld_16 + gi_360 * f0_2();
   if (gi_352 && (!gi_356) && gi_476 == 0) ld_0 = ld_16 + gi_360 * f0_2();
   if (gi_352 && (!gi_356) && gi_476 > 0) ld_0 = ld_8;
   return (ld_0);
}

int f0_9() {
   double ld_0;
   string ls_16;
   if (gi_816 == FALSE) return (-1);
   if (OrderSelect(f0_38(), SELECT_BY_TICKET, MODE_TRADES)) ld_0 = OrderStopLoss();
   double ld_8 = iLow(NULL, 0, 1);
   if (gi_584) ls_16 = "cyatrade";
   if (!gi_584) ls_16 = "";
   double ld_48 = 0;
   double ld_56 = 0;
   ld_48 = f0_5();
   int li_28 = 32768;
   double ld_64 = Ask;
   double ld_32 = NormalizeDouble(f0_25(), gd_524);
   Print("Calculated lots " + f0_25() + " normalized to " + ld_32);
   if (ld_32 > 150.0 * gd_796) return (-1);
   ls_16 = "Tom\'s EA 1.88";
   ld_48 = NormalizeDouble(ld_48, Digits);
   if (ld_48 < MarketInfo(Symbol(), MODE_STOPLEVEL)) ld_48 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   ld_48 = 0;
   int li_24 = f0_30(Symbol(), OP_BUY, ld_32, ld_64, gi_492, ld_48, ld_56, ls_16, gi_512, 0, li_28);
   if (li_24 > 0) return (li_24);
   return (0);
}

int f0_31() {
   double ld_0;
   string ls_16;
   if (gi_816 == FALSE) return (-1);
   if (OrderSelect(f0_33(), SELECT_BY_TICKET, MODE_TRADES)) ld_0 = OrderStopLoss();
   double ld_8 = iHigh(NULL, 0, 1);
   if (gi_580) ls_16 = "cyatrade";
   if (!gi_580) ls_16 = "";
   double ld_48 = 0;
   double ld_56 = 0;
   ld_48 = f0_43();
   int li_28 = 255;
   double ld_64 = Bid;
   double ld_32 = NormalizeDouble(f0_16(), gd_524);
   Print("Calculated lots " + f0_25() + " normalized to " + ld_32);
   if (ld_32 > 150.0 * gd_804) return (-1);
   ls_16 = "Tom\'s EA 1.88";
   ld_48 = NormalizeDouble(ld_48, Digits);
   ld_48 = 0;
   int li_24 = f0_30(Symbol(), OP_SELL, ld_32, ld_64, gi_492, ld_48, ld_56, ls_16, gi_512, 0, li_28);
   if (li_24 > 0) return (li_24);
   return (0);
}

double f0_25() {
   double ld_0;
   if (!gi_584) {
      if (gi_472 == 0) ld_0 = gda_400[1];
      if (gi_472 == 1) ld_0 = gda_400[2];
      if (gi_472 == 2) ld_0 = gda_400[3];
      if (gi_472 == 3) ld_0 = gda_400[4];
      if (gi_472 == 4) ld_0 = gda_400[5];
      if (gi_472 == 5) ld_0 = gda_400[6];
      if (gi_472 == 6) ld_0 = gda_400[7];
      if (gi_472 == 7) ld_0 = gda_400[8];
      if (gi_472 == 8) ld_0 = gda_400[9];
      if (gi_472 == 9) ld_0 = gda_400[10];
      if (gi_472 == 10) ld_0 = gda_400[11];
      if (gi_472 == 11) ld_0 = gda_400[12];
      if (gi_472 == 12) ld_0 = gda_400[13];
      if (gi_472 == 13) ld_0 = gda_400[14];
      if (gi_472 == 14) ld_0 = gda_400[15];
      if (gi_472 == 15) ld_0 = gda_400[16];
      if (gi_472 == 16) ld_0 = gda_400[17];
      if (gi_472 == 17) ld_0 = gda_400[18];
      if (gi_472 == 18) ld_0 = gda_400[19];
      if (gi_472 == 19) ld_0 = gda_400[20];
   }
   if (gi_584) {
      if (gi_476 == 1) ld_0 = gda_400[1];
      if (gi_476 == 2) ld_0 = gda_400[2];
      if (gi_476 == 3) ld_0 = gda_400[3];
      if (gi_476 == 4) ld_0 = gda_400[4];
      if (gi_476 == 5) ld_0 = gda_400[5];
      if (gi_476 == 6) ld_0 = gda_400[6];
      if (gi_476 == 7) ld_0 = gda_400[7];
      if (gi_476 == 8) ld_0 = gda_400[8];
      if (gi_476 == 9) ld_0 = gda_400[9];
      if (gi_476 == 10) ld_0 = gda_400[10];
      if (gi_476 == 11) ld_0 = gda_400[11];
      if (gi_476 == 12) ld_0 = gda_400[12];
      if (gi_476 == 13) ld_0 = gda_400[13];
      if (gi_476 == 14) ld_0 = gda_400[14];
      if (gi_476 == 15) ld_0 = gda_400[15];
      if (gi_476 == 16) ld_0 = gda_400[16];
      if (gi_476 == 17) ld_0 = gda_400[17];
      if (gi_476 == 18) ld_0 = gda_400[18];
      if (gi_476 == 19) ld_0 = gda_400[19];
      if (gi_476 == 20) ld_0 = gda_400[20];
   }
   return (ld_0);
}

double f0_16() {
   double ld_0;
   if (!gi_580) {
      if (gi_476 == 0) ld_0 = gda_404[1];
      if (gi_476 == 1) ld_0 = gda_404[2];
      if (gi_476 == 2) ld_0 = gda_404[3];
      if (gi_476 == 3) ld_0 = gda_404[4];
      if (gi_476 == 4) ld_0 = gda_404[5];
      if (gi_476 == 5) ld_0 = gda_404[6];
      if (gi_476 == 6) ld_0 = gda_404[7];
      if (gi_476 == 7) ld_0 = gda_404[8];
      if (gi_476 == 8) ld_0 = gda_404[9];
      if (gi_476 == 9) ld_0 = gda_404[10];
      if (gi_476 == 10) ld_0 = gda_404[11];
      if (gi_476 == 11) ld_0 = gda_404[12];
      if (gi_476 == 12) ld_0 = gda_404[13];
      if (gi_476 == 13) ld_0 = gda_404[14];
      if (gi_476 == 14) ld_0 = gda_404[15];
      if (gi_476 == 15) ld_0 = gda_404[16];
      if (gi_476 == 16) ld_0 = gda_404[17];
      if (gi_476 == 17) ld_0 = gda_404[18];
      if (gi_476 == 18) ld_0 = gda_404[19];
      if (gi_476 == 19) ld_0 = gda_404[20];
   }
   if (gi_580) {
      if (gi_472 == 1) ld_0 = gda_404[1];
      if (gi_472 == 2) ld_0 = gda_404[2];
      if (gi_472 == 3) ld_0 = gda_404[3];
      if (gi_472 == 4) ld_0 = gda_404[4];
      if (gi_472 == 5) ld_0 = gda_404[5];
      if (gi_472 == 6) ld_0 = gda_404[6];
      if (gi_472 == 7) ld_0 = gda_404[7];
      if (gi_472 == 8) ld_0 = gda_404[8];
      if (gi_472 == 9) ld_0 = gda_404[9];
      if (gi_472 == 10) ld_0 = gda_404[10];
      if (gi_472 == 11) ld_0 = gda_404[11];
      if (gi_472 == 12) ld_0 = gda_404[12];
      if (gi_472 == 13) ld_0 = gda_404[13];
      if (gi_472 == 14) ld_0 = gda_404[14];
      if (gi_472 == 15) ld_0 = gda_404[15];
      if (gi_472 == 16) ld_0 = gda_404[16];
      if (gi_472 == 17) ld_0 = gda_404[17];
      if (gi_472 == 18) ld_0 = gda_404[18];
      if (gi_472 == 19) ld_0 = gda_404[19];
      if (gi_472 == 20) ld_0 = gda_404[20];
   }
   return (ld_0);
}

double f0_22() {
   double ld_0;
   for (int li_8 = OrdersTotal() - 1; li_8 >= 0; li_8--) {
      if (!OrderSelect(li_8, SELECT_BY_POS, MODE_TRADES) || OrderType() != OP_BUY || OrderMagicNumber() != gi_512) continue;
      ld_0 += OrderLots();
   }
   if (ld_0 == 0.0) ld_0 = 1;
   return (ld_0);
}

double f0_34() {
   double ld_0;
   for (int li_8 = OrdersTotal() - 1; li_8 >= 0; li_8--) {
      if (!OrderSelect(li_8, SELECT_BY_POS, MODE_TRADES) || OrderType() != OP_SELL || OrderMagicNumber() != gi_512) continue;
      ld_0 += OrderLots();
   }
   if (ld_0 == 0.0) ld_0 = 1;
   return (ld_0);
}

void f0_4() {
   int li_4;
   int li_8;
   double ld_20;
   int li_28;
   int li_12 = 100;
   int li_16 = OrdersTotal() - 1;
   for (int li_0 = li_16; li_0 >= 0; li_0--) {
      if (OrderSelect(li_0, SELECT_BY_POS, MODE_TRADES)) {
         li_4 = OrderType();
         if (!(li_4 == OP_BUY && OrderMagicNumber() == gi_512)) continue;
      }
      ld_20 = MarketInfo(OrderSymbol(), MODE_BID);
      li_28 = OrderClose(OrderTicket(), OrderLots(), ld_20, li_12, Red);
      if (li_28 != 1) {
         li_8 = GetLastError();
         Print("LastError = ", li_8);
      } else li_8 = 0;
      if (li_8 == 135/* PRICE_CHANGED */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 == 136/* OFF_QUOTES */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 == 137/* BROKER_BUSY */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 == 138/* REQUOTE */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 != 142) continue;
      Sleep(1000);
      RefreshRates();
      li_0++;
   } // else Print("When selecting a trade, error ", GetLastError(), " occurred");
}

void f0_17() {
   int li_4;
   int li_8;
   double ld_20;
   int li_28;
   int li_12 = 100;
   int li_16 = OrdersTotal() - 1;
   for (int li_0 = li_16; li_0 >= 0; li_0--) {
      if (OrderSelect(li_0, SELECT_BY_POS, MODE_TRADES)) {
         li_4 = OrderType();
         if (!(li_4 == OP_SELL && OrderMagicNumber() == gi_512)) continue;
      }
      ld_20 = MarketInfo(OrderSymbol(), MODE_ASK);
      li_28 = OrderClose(OrderTicket(), OrderLots(), ld_20, li_12, Red);
      if (li_28 != 1) {
         li_8 = GetLastError();
         Print("LastError = ", li_8);
      } else li_8 = 0;
      if (li_8 == 135/* PRICE_CHANGED */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 == 136/* OFF_QUOTES */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 == 137/* BROKER_BUSY */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 == 138/* REQUOTE */) {
         Sleep(1000);
         RefreshRates();
         li_0++;
      }
      if (li_8 != 142) continue;
      Sleep(1000);
      RefreshRates();
      li_0++;
   } // else Print("When selecting a trade, error ", GetLastError(), " occurred");
}

int f0_15() {
   if (Bars == gi_484) return (0);
   gi_484 = Bars;
   return (1);
}

int f0_39() {
   if (gi_816 == FALSE) return (0);
   double ld_0 = iHigh(NULL, 0, 1);
   double ld_8 = iLow(NULL, 0, 1);
   double ld_16 = iClose(NULL, 0, 1);
   double ld_24 = iHigh(NULL, 0, 2);
   double ld_32 = iLow(NULL, 0, 2);
   double ld_40 = iClose(NULL, 0, 2);
   double ld_48 = (iHigh(NULL, 0, 1) + iLow(NULL, 0, 1)) / 2.0;
   gi_472 = f0_29(gi_512, 0);
   gi_476 = f0_29(gi_512, 1);
   int li_116 = gi_164;
   if (gi_472 > 2 && gi_472 < 5) li_116 = gi_168;
   if (gi_472 > 4) li_116 = gi_172;
   double ld_60 = iMACD(NULL, li_116, gi_128, gi_132, gi_136, PRICE_CLOSE, MODE_MAIN, gi_588);
   double ld_68 = iMACD(NULL, li_116, gi_128, gi_132, gi_136, PRICE_CLOSE, MODE_MAIN, gi_588 + 1);
   double ld_76 = iMACD(NULL, li_116, gi_128, gi_132, gi_136, PRICE_CLOSE, MODE_MAIN, gi_588 + 2);
   double ld_120 = iMA(NULL, gd_628, gd_272, gd_620, gd_604, gd_612, 0);
   double ld_128 = iMA(NULL, gd_676, gd_280, gd_668, gd_652, gd_660, 0);
   double ld_136 = iMA(NULL, gd_724, gd_288, gd_716, gd_700, gd_708, 0);
   bool li_144 = FALSE;
   if (DayOfWeek() < gi_220 && gd_540 > gd_212) {
      if (!gi_228) {
         if (gi_236)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 < (-gd_156) && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
         if (gi_236 && gi_472 > 0)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 > gd_140 && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
         if (!gi_236)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 < gd_148 && ld_68 > gd_140 && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
         if (!gi_236 && gi_472 > 0)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 < (-gd_140) && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
      }
      if (gi_228) {
         if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 < (-gd_156) && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
         if (gi_472 > 0)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 > gd_140 && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
         if (gi_472 == 0)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 < gd_148 && ld_68 > gd_140 && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
         if (gi_472 > 0)
            if (ld_60 > ld_68 && ld_68 <= ld_76 && ld_68 < (-gd_140) && ld_0 > ld_24 && ld_16 > ld_48) li_144 = TRUE;
      }
   }
   if (li_144 == TRUE) {
      if (gi_268 && gi_472 == 0) {
         if (ld_136 > ld_128 && ld_128 > ld_120) li_144 = TRUE;
         else li_144 = FALSE;
      }
   }
   if (li_144 == TRUE) {
      if (gi_264 && gi_472 == 0) {
         if (Bid > gd_788) li_144 = TRUE;
         else li_144 = FALSE;
      }
   }
   if (gi_472 > 0) {
      if (!gi_248) {
         if (li_144 == TRUE && gd_240 * f0_2() < f0_28() - Ask) li_144 = TRUE;
         else li_144 = FALSE;
      }
      if (gi_248) {
         if (li_144 == TRUE && gd_240 * f0_2() < MathAbs(gd_440 - Ask)) li_144 = TRUE;
         else li_144 = FALSE;
      }
   }
   if (!HedgingAllowed && gi_476 != 0) li_144 = FALSE;
   if (gi_472 == 0 && (!gi_224) && DayOfWeek() == 0) li_144 = FALSE;
   return (li_144);
}

int f0_19() {
   if (gi_816 == FALSE) return (0);
   double ld_0 = iHigh(NULL, 0, 1);
   double ld_8 = iLow(NULL, 0, 1);
   double ld_16 = iClose(NULL, 0, 1);
   double ld_24 = iHigh(NULL, 0, 2);
   double ld_32 = iLow(NULL, 0, 2);
   double ld_40 = iClose(NULL, 0, 2);
   double ld_48 = (iHigh(NULL, 0, 1) + iLow(NULL, 0, 1)) / 2.0;
   gi_472 = f0_29(gi_512, 0);
   gi_476 = f0_29(gi_512, 1);
   int li_116 = gi_164;
   if (gi_476 > 2 && gi_476 < 5) li_116 = gi_168;
   if (gi_476 > 4) li_116 = gi_172;
   double ld_60 = iMACD(NULL, li_116, gi_128, gi_132, gi_136, PRICE_CLOSE, MODE_MAIN, gi_588);
   double ld_68 = iMACD(NULL, li_116, gi_128, gi_132, gi_136, PRICE_CLOSE, MODE_MAIN, gi_588 + 1);
   double ld_76 = iMACD(NULL, li_116, gi_128, gi_132, gi_136, PRICE_CLOSE, MODE_MAIN, gi_588 + 2);
   double ld_120 = iMA(NULL, gd_628, gd_272, gd_620, gd_604, gd_612, 0);
   double ld_128 = iMA(NULL, gd_676, gd_280, gd_668, gd_652, gd_660, 0);
   double ld_136 = iMA(NULL, gd_724, gd_288, gd_716, gd_700, gd_708, 0);
   bool li_144 = FALSE;
   if (DayOfWeek() < gi_220 && gd_540 > gd_212) {
      if (!gi_228) {
         if (gi_236)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > gd_156 && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
         if (gi_236 && gi_476 > 0)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > (-gd_148) && ld_68 < (-gd_140) && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
         if (!gi_236)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > (-gd_148) && ld_68 < (-gd_140) && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
         if (!gi_236 && gi_476 > 0)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > gd_140 && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
      }
      if (gi_228) {
         if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > gd_156 && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
         if (gi_476 > 0)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > (-gd_148) && ld_68 < (-gd_140) && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
         if (gi_476 == 0)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > (-gd_148) && ld_68 < (-gd_140) && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
         if (gi_476 > 0)
            if (ld_60 < ld_68 && ld_68 >= ld_76 && ld_68 > gd_140 && ld_8 < ld_32 && ld_16 < ld_48) li_144 = TRUE;
      }
   }
   if (li_144 == TRUE) {
      if (gi_268 && gi_476 == 0) {
         if (ld_136 < ld_128 && ld_128 < ld_120) li_144 = TRUE;
         else li_144 = FALSE;
      }
   }
   if (li_144 == TRUE) {
      if (gi_264 && gi_476 == 0) {
         if (Bid < gd_788) li_144 = TRUE;
         else li_144 = FALSE;
      }
   }
   if (gi_476 > 0) {
      if (!gi_248) {
         if (li_144 == TRUE && gd_240 * f0_2() < Bid - f0_42()) li_144 = TRUE;
         else li_144 = FALSE;
      }
      if (gi_248) {
         if (li_144 == TRUE && gd_240 * f0_2() < MathAbs(gd_448 - Bid)) li_144 = TRUE;
         else li_144 = FALSE;
      }
   }
   if (!HedgingAllowed && gi_472 != 0) li_144 = FALSE;
   if (gi_476 == 0 && (!gi_224) && DayOfWeek() == 0) li_144 = FALSE;
   return (li_144);
}

int f0_38() {
   int li_0 = 0;
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_BUY || !OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES)) continue;
      if (li_0 == 0) li_0 = OrderTicket();
      if (OrderTicket() < li_0) li_0 = OrderTicket();
   }
   return (li_0);
}

int f0_33() {
   int li_0 = 0;
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_SELL || !OrderSelect(li_4, SELECT_BY_POS, MODE_TRADES)) continue;
      if (li_0 == 0) li_0 = OrderTicket();
      if (OrderTicket() < li_0) li_0 = OrderTicket();
   }
   return (li_0);
}

double f0_28() {
   double ld_0 = 0.0;
   for (int li_8 = OrdersTotal() - 1; li_8 >= 0; li_8--) {
      OrderSelect(li_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_BUY || !OrderSelect(li_8, SELECT_BY_POS, MODE_TRADES)) continue;
      if (ld_0 == 0.0) ld_0 = OrderOpenPrice();
      if (OrderOpenPrice() < ld_0) ld_0 = OrderOpenPrice();
   }
   return (ld_0);
}

double f0_42() {
   double ld_0 = 0.0;
   for (int li_8 = OrdersTotal() - 1; li_8 >= 0; li_8--) {
      OrderSelect(li_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != gi_512 || OrderType() != OP_SELL || !OrderSelect(li_8, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderOpenPrice() > ld_0) ld_0 = OrderOpenPrice();
   }
   return (ld_0);
}

//decompiled func
double GetParam(int a0) {
switch (a0)
{
   case 1:  return (0.01);
   case 2:  return (20.0);
   case 3:  return (0.0002);
   case 4:  return (0.0025);
   case 5:  return (0.004);
   case 6:  return (89.0);
   case 7:  return (21.0);
   case 8:  return (8.0);
   default: return (0.00);
}
   return(0);
}

int InitializeFunctionsExt() {
   return(1100);
}

double BuySellLotsExt(double a0, int a1) {
switch (a1)
   {
   case 0:  return (0);
   case 1:  return (a0*1.0);
   case 2:  return (a0*1.0);
   case 3:  return (a0*1.0);
   case 4:  return (a0*3.0);
   case 5:  return (a0*6.0);
   case 6:  return (a0*9.0);
   case 7:  return (a0*12.0);
   case 8:  return (a0*15.0);
   case 9:  return (a0*21.0);   
   case 10: return (a0*27.0);   
   case 11: return (a0*33.0);   
   case 12: return (a0*39.0);   
   case 13: return (a0*45.0);   
   case 14: return (a0*55.0);   
   case 15: return (a0*65.0);   
   case 16: return (a0*75.0);   
   case 17: return (a0*85.0);   
   case 18: return (a0*95.0);   
   case 19: return (a0*120.0);   
   case 20: return (a0*140.0);   
   }
   return(0);   
}

double BuysPipTargetExt(double a0, int a1) {
switch (a1)
   {
   case 0: return (a0);
   case 1: return (a0);
   case 2: return (a0);
   case 3: return (a0*2);
   case 4: return (a0*2);
   case 5: return (a0*3);
   case 6: return (a0*4);
   case 7: return (a0*4);
   case 8: return (a0*5);
   case 9: return (a0*5);
   case 10: return (a0*5);
   case 11: return (a0*5);
   case 12: return (a0*5);
   case 13: return (a0*5);
   case 14: return (a0*5);
   case 15: return (a0*5);
   case 16: return (a0*5);
   case 17: return (a0*5);
   case 18: return (a0*5);
   case 19: return (a0*5);
   case 20: return (a0*5);
   }
   return(0);
}


