/*
   Generated by EX4 TO MQ4 decompile Service 
   Website: http://www.ex4Tomq4.net 
   E-mail : info@ex4Tomq4.net 
*/
#property copyright "Copyright  2010, Andrea Salvatore"
#property link      "http://www.forexcrescendo.com"

#include <stdlib.mqh>

#import "kernel32.dll"
   int GetTimeZoneInformation(int& a0[]);

/*
#import "wininet.dll"
   int InternetOpenA(string a0, int a1, string a2, string a3, int a4);
   int InternetOpenUrlA(int a0, string a1, string a2, int a3, int a4, int a5);
   int InternetReadFile(int a0, string a1, int a2, int& a3[]);
   int InternetCloseHandle(int a0);
#import
*/

bool gi_unused_76 = FALSE;
string gs_dummy_80;
int gi_unused_88 = 0;
int gi_92 = 1;
string gs_dummy_96;
int gi_104 = 0;
int gi_108 = 0;
int g_count_112 = 0;
string gs_116;
int gia_124[1];
string gs_132 = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
int g_color_140 = Blue;
int g_color_144 = Red;
int gi_unused_148 = 16711935;
int gi_unused_152 = 8421504;
int gi_unused_156 = 0;
int gi_unused_160 = 1;
int gi_unused_164 = 1;
int gi_unused_168 = 1;
bool gi_172 = TRUE;
int gia_176[64] = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47};
string note0 = "AUTHENTICATION OPTIONS";
string username = "";
string password = "";
extern string note1 = "STRATEGY SETTINGS";
extern double initial_balance = 1000.0;
extern int order_step = 60;
extern double target_profit = 4.0;
extern string frequency_note = "0 - always; 1 - every hour; 2 - every 3 hours";
extern string frequency_noteb = "3 - every 6 hours; 4 - every 12 hours; 5 - every 24 hours";
extern int frequency = 4;
extern bool dd_based_frequency = FALSE;
extern double dd_step = 1.0;
extern bool use_mtf_trend = TRUE;
extern int trend_threshold = 3;
extern int trend_period_1 = 60;
extern int trend_period_2 = 240;
extern int trend_period_3 = 1440;
extern int trend_period_4 = 10080;
extern int trend_period_5 = 43200;
extern bool close_on_counter_trend = FALSE;
extern int counter_trend_threshold = 5;
extern bool manual_trend = FALSE;
extern string trendnote = "1 - long; -1 short";
extern int trend_direction = 0;
extern bool trend_following = FALSE;
extern bool use_stop_loss = FALSE;
extern int stop_loss = 500;
extern string note2 = "MONEY MANAGEMENT";
extern double lot_size = 0.01;
extern double risk_percent = 0.0;
extern double max_total_DD = 30.0;
extern double slippage = 5.0;
extern bool linear_martingale = FALSE;
extern bool fibonacci_martingale = FALSE;
extern bool exponential_martingale = FALSE;
extern double exponential_factor = 2.0;
extern string note3 = "EA SETTINGS";
extern int GMT_time_shift = 0;
extern int GBPUSD_Magic_Number = 130907;
extern int GBPJPY_Magic_Number = 300610;
extern bool randomize_entry_minute = FALSE;
extern string comment = "Crescendo v1.3";
extern bool hide_comment = FALSE;
bool gi_unused_424 = TRUE;
bool gi_unused_428 = TRUE;
int g_digits_432;
double gd_436;
double gd_unused_444;
double gd_540;
bool gi_unused_548 = FALSE;
bool gi_unused_552 = FALSE;
bool gi_556;
bool gi_560 = FALSE;
bool gi_564 = FALSE;
double gd_568;
double gd_576;
bool gi_584;
bool gi_588;
bool gi_592;
bool gi_596;
int gia_unused_600[1];
string gs_604;
bool gi_612 = true;
int gi_616;
bool gi_620 = false;
int g_str2int_624;
int gi_628;
double gd_632;
double gd_640;
double gd_648;
double gd_656;
double gd_664;
int gi_unused_672 = 10;
double gda_unused_676[200];
int gi_680 = 0;

int init() {
   //HideTestIndicators(TRUE);
   g_digits_432 = Digits;
   if (g_digits_432 == 3 || g_digits_432 == 5) {
      gd_436 = 10.0 * Point;
      gd_unused_444 = 10;
   } else {
      gd_436 = Point;
      gd_unused_444 = 1;
   }
   ObjectsDeleteAll();
   plotNewOpenTrades();
   plotNewClosedTrades();
   MathSrand(TimeLocal());
   if (randomize_entry_minute == TRUE) gi_616 = MathRand() % 10;
   return (0);
}

int deinit() {
   Comment("");
   return (0);
}

int start() {
   string ls_0;
   string ls_8;
   string ls_16;
   bool li_24;
   double ld_28;
   int li_36;
   double ld_40;
   double ld_48;
   int li_56;
   int li_60;
   double ld_64;
   int li_72;
   double ld_76;
   double ld_84;
   int li_92;
   int li_96;
   string ls_100;
   string ls_108;
   int li_120;
   int li_124;
   int li_128;
   string ls_unused_132;
   string ls_140;
   string ls_148;
   double ld_156;
   double ld_164;
   
  /* if (gi_620 == TRUE && g_str2int_624 == 0) {
      gs_604 = "\nCrescendo EA v1.3\n authenticating...";
      Comment(gs_604);
      g_str2int_624 = StrToInteger(authentication(username, password));
      if (g_str2int_624 == 1) gi_612 = TRUE;
      else {
         gs_604 = "\nCrescendo EA v1.3\n Authentication FAILED\n\n";
         gs_604 = gs_604 + " Send an email to:\n";
         gs_604 = gs_604 + "   support@forexcrescendo.com\n";
         gi_612 = FALSE;
         Comment(gs_604);
      }
      gi_620 = FALSE;
   }
   */
   
   if (gi_612 == TRUE) {
      gi_628 = TimeGMT();
      ls_0 = comment;
      if (hide_comment == TRUE) ls_0 = 0;
      if (StringFind(Symbol(), "GBPUSD", 0) != -1) {
         ls_8 = Symbol();
         ls_16 = "GBPJPY" + StringSubstr(Symbol(), 6, 0);
      }
      if (StringFind(Symbol(), "GBPJPY", 0) != -1) {
         ls_16 = Symbol();
         ls_8 = "GBPUSD" + StringSubstr(Symbol(), 6, 0);
      }
      li_24 = FALSE;
      if (ls_8 == Symbol() || ls_16 == Symbol()) li_24 = TRUE;
      if (li_24 != TRUE) {
         gs_604 = "\nCrescendo EA v1.3\n\n ERROR: Wrong Currency Pair.\n\n";
         gs_604 = gs_604 + " Crescendo EA can only be applied to\n";
         gs_604 = gs_604 + " GBPUSD and GBPJPY charts.\n";
         Comment(gs_604);
         return (0);
      }
      if (lot_size < MarketInfo(Symbol(), MODE_MINLOT) && risk_percent == 0.0) {
         gs_604 = "\nCrescendo EA v1.3\n\n ERROR: Wrong Minimum Lot Size.\n\n";
         gs_604 = gs_604 + " Your broker allows a minimum lot size of " + DoubleToStr(MarketInfo(Symbol(), MODE_MINLOT), 2) + "lots,\n";
         gs_604 = gs_604 + " while you set " + DoubleToStr(lot_size, 2) + " lots.\n";
         gs_604 = gs_604 + " Please check!\n";
         Comment(gs_604);
         return (0);
      }
      if (Symbol() == ls_8) {
         gi_584 = FALSE;
         if (use_stop_loss == TRUE) gi_584 = stop_loss;
         gi_592 = FALSE;
         gd_632 = closed_profit(ls_8, GBPUSD_Magic_Number) + closed_profit(ls_16, GBPJPY_Magic_Number);
         gd_640 = initial_balance + gd_632;
         ld_28 = gd_568;
         if (dd_based_frequency == TRUE) {
            frequency = MathAbs(NormalizeDouble(MathFloor(100.0 * (ld_28 / gd_640) / dd_step), 0));
            if (frequency < 0) frequency = 0;
            if (frequency > 5) frequency = 5;
         }
         gi_592 = check_for_open(frequency);
         gd_568 = profit(ls_8, GBPUSD_Magic_Number);
         if (gd_568 >= target_profit || (gi_592 == TRUE && close_on_counter_trend == TRUE && last_order_direction(ls_8, GBPUSD_Magic_Number) == 1 && trend(ls_8, GBPUSD_Magic_Number) <= (-1 * counter_trend_threshold)) ||
            (gi_592 == TRUE && close_on_counter_trend == TRUE && last_order_direction(ls_8, GBPUSD_Magic_Number) == -1 && trend(ls_8, GBPUSD_Magic_Number) >= counter_trend_threshold)) {
            while (count(ls_8, GBPUSD_Magic_Number) > 0) {
               RefreshRates();
               close(ls_8, GBPUSD_Magic_Number);
            }
            gi_592 = FALSE;
            if (trend_following == TRUE) gi_560 = TRUE;
         }
         li_36 = count(ls_8, GBPUSD_Magic_Number);
         if (gi_592 == TRUE && li_36 > 0) {
            ld_40 = last_order_size(ls_8, GBPUSD_Magic_Number);
            if (linear_martingale == TRUE) ld_40 = lot_size * (li_36 + 1);
            gi_556 = TRUE;
            if (fibonacci_martingale == TRUE) {
               gi_556 = nextFibo(NormalizeDouble(last_order_size(ls_8, GBPUSD_Magic_Number) / lot_size, 0));
               ld_40 = lot_size * gi_556;
            }
            if (exponential_martingale == TRUE) ld_40 *= exponential_factor;
            ld_48 = distance(ls_8, GBPUSD_Magic_Number);
            if (ld_48 >= order_step) {
               li_56 = last_order_direction(ls_8, GBPUSD_Magic_Number);
               RefreshRates();
               if (li_56 == 1) buy(ld_40, gi_584, 0, GBPUSD_Magic_Number, ls_0);
               if (li_56 == -1) sell(ld_40, gi_584, 0, GBPUSD_Magic_Number, ls_0);
               gi_592 = FALSE;
            }
         }
         if ((gi_592 == TRUE && li_36 == 0) || (trend_following == TRUE && gi_560 == TRUE)) {
            li_60 = trend(ls_8, GBPUSD_Magic_Number);
            if (manual_trend == TRUE) li_60 = trend_direction;
            if (li_36 == 0 && risk_percent > 0.0) lot_size = calculate_lots(risk_percent);
            RefreshRates();
            if (li_60 >= trend_threshold) buy(lot_size, gi_584, 0, GBPUSD_Magic_Number, ls_0);
            if (li_60 <= -1 * trend_threshold) sell(lot_size, gi_584, 0, GBPUSD_Magic_Number, ls_0);
            gi_560 = FALSE;
         }
      }
      if (Symbol() == ls_16) {
         gi_588 = FALSE;
         if (use_stop_loss == TRUE) gi_588 = stop_loss;
         gi_596 = FALSE;
         gd_632 = closed_profit(ls_8, GBPUSD_Magic_Number) + closed_profit(ls_16, GBPJPY_Magic_Number);
         gd_640 = initial_balance + gd_632;
         ld_64 = gd_576;
         if (dd_based_frequency == TRUE) {
            frequency = MathAbs(NormalizeDouble(MathFloor(100.0 * (ld_64 / gd_640) / dd_step), 0));
            if (frequency < 0) frequency = 0;
            if (frequency > 5) frequency = 5;
         }
         gi_596 = check_for_open(frequency);
         gd_576 = profit(ls_16, GBPJPY_Magic_Number);
         if (gd_576 >= target_profit || (gi_596 == TRUE && close_on_counter_trend == TRUE && last_order_direction(ls_16, GBPJPY_Magic_Number) == 1 && trend(ls_16, GBPJPY_Magic_Number) <= (-1 * counter_trend_threshold)) ||
            (gi_596 == TRUE && close_on_counter_trend == TRUE && last_order_direction(ls_16, GBPJPY_Magic_Number) == -1 && trend(ls_16, GBPJPY_Magic_Number) >= counter_trend_threshold)) {
            while (count(ls_16, GBPJPY_Magic_Number) > 0) {
               RefreshRates();
               close(ls_16, GBPJPY_Magic_Number);
            }
            gi_596 = FALSE;
            if (trend_following == TRUE) gi_564 = TRUE;
         }
         li_72 = count(ls_16, GBPJPY_Magic_Number);
         if (gi_596 == TRUE && li_72 > 0) {
            ld_76 = last_order_size(ls_16, GBPJPY_Magic_Number);
            if (linear_martingale == TRUE) ld_76 = lot_size * (li_72 + 1);
            gi_556 = TRUE;
            if (fibonacci_martingale == TRUE) {
               gi_556 = nextFibo(NormalizeDouble(last_order_size(ls_16, GBPJPY_Magic_Number) / lot_size, 0));
               ld_76 = lot_size * gi_556;
            }
            if (exponential_martingale == TRUE) ld_76 *= exponential_factor;
            ld_84 = distance(ls_16, GBPJPY_Magic_Number);
            if (ld_84 >= order_step) {
               li_92 = last_order_direction(ls_16, GBPJPY_Magic_Number);
               RefreshRates();
               if (li_92 == 1) buy(ld_76, gi_588, 0, GBPJPY_Magic_Number, ls_0);
               if (li_92 == -1) sell(ld_76, gi_588, 0, GBPJPY_Magic_Number, ls_0);
               gi_596 = FALSE;
            }
         }
         if ((gi_596 == TRUE && li_72 == 0) || (trend_following == TRUE && gi_564 == TRUE)) {
            li_96 = trend(ls_16, GBPJPY_Magic_Number);
            if (manual_trend == TRUE) li_96 = trend_direction;
            if (li_72 == 0 && risk_percent > 0.0) lot_size = calculate_lots(risk_percent);
            RefreshRates();
            if (li_96 >= trend_threshold) buy(lot_size, gi_588, 0, GBPJPY_Magic_Number, ls_0);
            if (li_96 <= -1 * trend_threshold) sell(lot_size, gi_588, 0, GBPJPY_Magic_Number, ls_0);
            gi_564 = FALSE;
         }
      }
      gd_632 = closed_profit(ls_8, GBPUSD_Magic_Number) + closed_profit(ls_16, GBPJPY_Magic_Number);
      gd_640 = initial_balance + gd_632;
      gd_648 = -1.0 * (gd_640 * max_total_DD / 100.0);
      gd_656 = profit(ls_8, GBPUSD_Magic_Number) + profit(ls_16, GBPJPY_Magic_Number);
      if (gd_656 < gd_664) gd_664 = gd_656;
      if (gd_656 <= gd_648) {
         while (count(ls_8, GBPUSD_Magic_Number) > 0 && Symbol() == ls_8) {
            RefreshRates();
            close(ls_8, GBPUSD_Magic_Number);
         }
         while (count(ls_16, GBPJPY_Magic_Number) > 0 && Symbol() == ls_16) {
            RefreshRates();
            close(ls_16, GBPJPY_Magic_Number);
         }
         gi_592 = FALSE;
         gi_596 = FALSE;
      }
      ls_100 = "========================\n";
      ls_100 = ls_100 + "               Crescendo EA v1.3\n";
      ls_100 = ls_100 + "          www.forexcrescendo.com\n";
      ls_100 = ls_100 + "========================\n";
      ls_100 = ls_100 + "  Authentication: OK\n";
      for (int count_116 = 0; count_116 <= MathMod(Seconds(), 5); count_116++) ls_108 = ls_108 + ".";
      ls_100 = ls_100 + "  Status: RUNNING " + ls_108 
      + "\n";
      ls_100 = ls_100 + "========================\n";
      ls_100 = ls_100 + "  Initial balance: $ " + DoubleToStr(initial_balance, 2) 
      + "\n";
      ls_100 = ls_100 + "  Closed Orders Profit: $ " + DoubleToStr(gd_632, 2) + " (" + DoubleToStr(100.0 * (gd_632 / initial_balance), 2) + "%)\n";
      ls_100 = ls_100 + "  EA Balance: $ " + DoubleToStr(gd_640, 2) 
      + "\n";
      ls_100 = ls_100 + "  Max DD: " + DoubleToStr(max_total_DD, 2) + "% ($ " + DoubleToStr(MathAbs(gd_648), 2) + ")\n";
      ls_100 = ls_100 + "========================\n";
      if (Symbol() == ls_8) {
         ls_100 = ls_100 + "  GBPUSD Orders: " + count(ls_8, GBPUSD_Magic_Number) 
         + "\n";
      }
      if (Symbol() == ls_16) {
         ls_100 = ls_100 + "  GBPJPY Orders: " + count(ls_16, GBPJPY_Magic_Number) 
         + "\n";
      }
      if (Symbol() == ls_8) {
         ls_100 = ls_100 + "  GBPUSD Target: $ " + DoubleToStr(target_profit, 2) 
         + "\n";
      }
      if (Symbol() == ls_16) {
         ls_100 = ls_100 + "  GBPJPY Target: $ " + DoubleToStr(target_profit, 2) 
         + "\n";
      }
      if (Symbol() == ls_8) {
         ls_100 = ls_100 + "  GBPUSD Orders Value: $ " + DoubleToStr(profit(ls_8, GBPUSD_Magic_Number), 2) 
         + "\n";
      }
      if (Symbol() == ls_16) {
         ls_100 = ls_100 + "  GBPJPY Orders Value: $ " + DoubleToStr(profit(ls_16, GBPJPY_Magic_Number), 2) 
         + "\n";
      }
      ls_100 = ls_100 + "  TOTAL Open Orders Value: $ " + DoubleToStr(gd_656, 2) 
      + "\n";
      ls_100 = ls_100 + "  MAX Floating DD: $ " + DoubleToStr(gd_664, 2) 
      + "\n";
      if (Symbol() == ls_8) {
         ls_100 = ls_100 + "  Breakeven Level @ " + DoubleToStr(getAveragePositionPrice(ls_8, GBPUSD_Magic_Number), Digits) 
         + "\n";
      }
      if (Symbol() == ls_16) {
         ls_100 = ls_100 + "  Breakeven Level @ " + DoubleToStr(getAveragePositionPrice(ls_16, GBPJPY_Magic_Number), Digits) 
         + "\n";
      }
      ls_100 = ls_100 + "========================\n";
      if (risk_percent > 0.0) {
         ls_100 = ls_100 + "  Risk %: " + risk_percent + "%\n";
         ls_100 = ls_100 + "  Lot Size: " + DoubleToStr(calculate_lots(risk_percent), 2) + " lots\n";
      } else ls_100 = ls_100 + "  Lot Size: " + DoubleToStr(lot_size, 2) + " lots\n";
      ls_100 = ls_100 + "  Slippage: " + DoubleToStr(slippage, 2) + " pips\n";
      ls_100 = ls_100 + "========================\n";
      if (manual_trend == FALSE) ls_100 = ls_100 + "  Trend: AUTOMATIC\n";
      else {
         if (trend_direction == 1) ls_100 = ls_100 + "  Trend: MANUAL - LONG\n";
         if (trend_direction == -1) ls_100 = ls_100 + "  Trend: MANUAL - SHORT\n";
      }
      if (Symbol() == ls_8) {
         ls_100 = ls_100 + "  Trend Score: " + trend(ls_8, GBPUSD_Magic_Number) 
         + "\n";
      }
      if (Symbol() == ls_16) {
         ls_100 = ls_100 + "  Trend Score: " + trend(ls_16, GBPJPY_Magic_Number) 
         + "\n";
      }
      ls_100 = ls_100 + "========================\n";
      if (frequency == 1) li_128 = 1;
      if (frequency == 2) li_128 = 3;
      if (frequency == 3) li_128 = 6;
      if (frequency == 4) li_128 = 12;
      if (frequency == 5) li_128 = 24;
      li_124 = 60 - TimeMinute(gi_628);
      if (frequency > 0 && frequency < 6) li_120 = li_128 - MathMod(TimeHour(gi_628) + GMT_time_shift, li_128) - 1.0;
      if (li_124 == 60) {
         li_124 = 0;
         li_120++;
      }
      ls_140 = li_120;
      if (li_120 < 10) ls_140 = "0" + li_120;
      ls_148 = li_124;
      if (li_124 < 10) ls_148 = "0" + li_124;
      ls_100 = ls_100 + "  Frequency: " + frequency + " (" + li_128 + "H)\n";
      ls_100 = ls_100 + "  GMT time: " + TimeToStr(gi_628, TIME_SECONDS) 
      + "\n";
      ls_100 = ls_100 + "  Shift: " + GMT_time_shift + " hours\n";
      if (frequency > 0 && frequency < 6) ls_100 = ls_100 + "  " + ls_140 + ":" + ls_148 + " to next market check\n";
      ls_100 = ls_100 + "========================\n";
      Comment(ls_100);
      if (Symbol() == ls_8) {
         ld_156 = getAveragePositionPrice(ls_8, GBPUSD_Magic_Number);
         horizLine("Breakeven", ld_156, Green, "Breakeven Line");
      }
      if (Symbol() == ls_16) {
         ld_164 = getAveragePositionPrice(ls_16, GBPJPY_Magic_Number);
         horizLine("Breakeven", ld_164, Green, "Breakeven Line");
      }
   }
   return (0);
}

int count(string a_symbol_0, int a_magic_8) {
   int count_12 = 0;
   int ord_total_16 = OrdersTotal();
   if (ord_total_16 > 0) {
      for (int pos_20 = 0; pos_20 < ord_total_16; pos_20++) {
         OrderSelect(pos_20, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) count_12++;
      }
      return (count_12);
   }
   return (0);
}

double profit(string a_symbol_0, int a_magic_8) {
   double ld_ret_12 = 0;
   int ord_total_20 = OrdersTotal();
   if (ord_total_20 > 0) {
      for (int pos_24 = 0; pos_24 < ord_total_20; pos_24++) {
         OrderSelect(pos_24, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) ld_ret_12 += OrderProfit() + OrderSwap() + OrderCommission();
      }
      return (ld_ret_12);
   }
   return (0.0);
}

double closed_profit(string a_symbol_0, int a_magic_8) {
   double ld_ret_12 = 0;
   int hist_total_20 = OrdersHistoryTotal();
   if (hist_total_20 > 0) {
      for (int pos_24 = 0; pos_24 < hist_total_20; pos_24++) {
         OrderSelect(pos_24, SELECT_BY_POS, MODE_HISTORY);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) ld_ret_12 += OrderProfit() + OrderSwap() + OrderCommission();
      }
      return (ld_ret_12);
   }
   return (0.0);
}

void close(string a_symbol_0, int a_magic_8) {
   if (OrdersTotal() > 0) {
      for (int ord_total_12 = OrdersTotal(); ord_total_12 >= 0; ord_total_12--) {
         OrderSelect(ord_total_12, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) {
            if (OrderType() == OP_BUY) orderCloseReliable(OrderTicket(), OrderLots(), Bid, slippage, Blue);
            if (OrderType() == OP_SELL) orderCloseReliable(OrderTicket(), OrderLots(), Ask, slippage, Red);
         }
      }
   }
}

int check_for_open(int ai_0) {
   bool li_ret_4 = FALSE;
   if (GMT_time_shift > 11) GMT_time_shift = 11;
   if (GMT_time_shift < -11) GMT_time_shift = -11;
   int li_8 = (TimeHour(gi_628) + GMT_time_shift) % 24;
   switch (ai_0) {
   case 0:
      li_ret_4 = TRUE;
      break;
   case 1:
      if (TimeMinute(gi_628) >= gi_616 && TimeMinute(gi_628) <= gi_616 + 1) li_ret_4 = TRUE;
      break;
   case 2:
      if (li_8 == 0 || li_8 == 3 || li_8 == 6 || li_8 == 9 || li_8 == 12 || li_8 == 15 || li_8 == 18 || li_8 == 21 && (TimeMinute(gi_628) >= gi_616 && TimeMinute(gi_628) <= gi_616 +
         1)) li_ret_4 = TRUE;
      break;
   case 3:
      if (li_8 == 0 || li_8 == 6 || li_8 == 12 || li_8 == 18 && (TimeMinute(gi_628) >= gi_616 && TimeMinute(gi_628) <= gi_616 + 1)) li_ret_4 = TRUE;
      break;
   case 4:
      if (li_8 == 0 || li_8 == 12 && (TimeMinute(gi_628) >= gi_616 && TimeMinute(gi_628) <= gi_616 + 1)) li_ret_4 = TRUE;
      break;
   case 5:
      if (li_8 == 0 && (TimeMinute(gi_628) >= gi_616 && TimeMinute(gi_628) <= gi_616 + 1)) li_ret_4 = TRUE;
      break;
   default:
      li_ret_4 = FALSE;
   }
   return (li_ret_4);
}

double calculate_lots(double ad_0) {
   RefreshRates();
   double ld_8 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_16 = MarketInfo(Symbol(), MODE_MAXLOT);
   double lotstep_24 = MarketInfo(Symbol(), MODE_LOTSTEP);
   if (lotstep_24 == 0.01) gd_540 = NormalizeDouble(initial_balance * ad_0 / 100000.0, 2);
   if (lotstep_24 == 0.1) gd_540 = NormalizeDouble(initial_balance * ad_0 / 100000.0, 1);
   if (gd_540 < ld_8) gd_540 = ld_8;
   if (gd_540 > ld_16) gd_540 = ld_16;
   return (gd_540);
}

double distance(string a_symbol_0, int ai_8) {
   double ld_ret_12;
   double ld_20 = last_order_entry_level(a_symbol_0, ai_8);
   if (last_order_direction(a_symbol_0, ai_8) == 1) ld_ret_12 = (ld_20 - iClose(a_symbol_0, 0, 0)) / gd_436;
   if (last_order_direction(a_symbol_0, ai_8) == -1) ld_ret_12 = (iClose(a_symbol_0, 0, 0) - ld_20) / gd_436;
   return (ld_ret_12);
}

int last_order_direction(string a_symbol_0, int a_magic_8) {
   int li_ret_12;
   if (OrdersTotal() > 0) {
      for (int pos_16 = 0; pos_16 < OrdersTotal(); pos_16++) {
         OrderSelect(pos_16, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) {
            if (OrderType() == OP_BUY) li_ret_12 = 1;
            if (OrderType() == OP_SELL) li_ret_12 = -1;
         }
      }
   }
   return (li_ret_12);
}

double last_order_entry_level(string a_symbol_0, int a_magic_8) {
   double ord_open_price_12;
   if (OrdersTotal() > 0) {
      for (int pos_20 = 0; pos_20 < OrdersTotal(); pos_20++) {
         OrderSelect(pos_20, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) ord_open_price_12 = OrderOpenPrice();
      }
   }
   return (ord_open_price_12);
}

double last_order_size(string a_symbol_0, int a_magic_8) {
   double ord_lots_12;
   if (OrdersTotal() > 0) {
      for (int pos_20 = 0; pos_20 < OrdersTotal(); pos_20++) {
         OrderSelect(pos_20, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == a_symbol_0 && OrderMagicNumber() == a_magic_8) ord_lots_12 = OrderLots();
      }
   }
   return (ord_lots_12);
}

int trend(string as_unused_0, int ai_unused_8) {
   int li_ret_12;
   if (iCustom(Symbol(), trend_period_5, "HullMA", 10, 0, 3, 0, 0) >= iCustom(Symbol(), trend_period_5, "HullMA", 10, 0, 3, 0, 1)) li_ret_12++;
   if (iCustom(Symbol(), trend_period_5, "HullMA", 10, 0, 3, 0, 0) < iCustom(Symbol(), trend_period_5, "HullMA", 10, 0, 3, 0, 1)) li_ret_12 += -1;
   if (iCustom(Symbol(), trend_period_4, "HullMA", 10, 0, 3, 0, 0) > iCustom(Symbol(), trend_period_4, "HullMA", 10, 0, 3, 0, 1)) li_ret_12++;
   if (iCustom(Symbol(), trend_period_4, "HullMA", 10, 0, 3, 0, 0) <= iCustom(Symbol(), trend_period_4, "HullMA", 10, 0, 3, 0, 1)) li_ret_12 += -1;
   if (iCustom(Symbol(), trend_period_3, "HullMA", 10, 0, 3, 0, 0) >= iCustom(Symbol(), trend_period_3, "HullMA", 10, 0, 3, 0, 1)) li_ret_12++;
   if (iCustom(Symbol(), trend_period_3, "HullMA", 10, 0, 3, 0, 0) < iCustom(Symbol(), trend_period_3, "HullMA", 10, 0, 3, 0, 1)) li_ret_12 += -1;
   if (iCustom(Symbol(), trend_period_2, "HullMA", 10, 0, 3, 0, 0) > iCustom(Symbol(), trend_period_2, "HullMA", 10, 0, 3, 0, 1)) li_ret_12++;
   if (iCustom(Symbol(), trend_period_2, "HullMA", 10, 0, 3, 0, 0) <= iCustom(Symbol(), trend_period_2, "HullMA", 10, 0, 3, 0, 1)) li_ret_12 += -1;
   if (iCustom(Symbol(), trend_period_1, "HullMA", 10, 0, 3, 0, 0) >= iCustom(Symbol(), trend_period_1, "HullMA", 10, 0, 3, 0, 1)) li_ret_12++;
   if (iCustom(Symbol(), trend_period_1, "HullMA", 10, 0, 3, 0, 0) < iCustom(Symbol(), trend_period_1, "HullMA", 10, 0, 3, 0, 1)) li_ret_12 += -1;
   if (manual_trend == TRUE) li_ret_12 = trend_direction;
   return (li_ret_12);
}

/*
string ProcessURL(string as_0) {
   string ls_12;
   string ls_20;
   for (int li_8 = StringFind(as_0, " "); li_8 != -1; li_8 = StringFind(as_0, " ")) {
      ls_12 = StringTrimLeft(StringTrimRight(StringSubstr(as_0, 0, StringFind(as_0, " ", 0))));
      ls_20 = StringTrimLeft(StringTrimRight(StringSubstr(as_0, StringFind(as_0, " ", 0))));
      as_0 = ls_12 + "%20" + ls_20;
   }
   return (as_0);
}

string LoadURL(string as_0) {
   g_count_112 = 0;
   for (gi_104 = FALSE; g_count_112 < 3 && gi_104 == FALSE; g_count_112++) {
      if (gi_108 != 0) gi_104 = InternetOpenUrlA(gi_108, as_0, 0, 0, -2079850240, 0);
      if (gi_104 == FALSE) {
         InternetCloseHandle(gi_108);
         gi_108 = InternetOpenA("mymt4InetSession", gi_92, 0, 0, 0);
      }
   }
   gs_116 = "";
   gia_124[0] = 1;
   while (gia_124[0] > 0) {
      InternetReadFile(gi_104, gs_132, 200, gia_124);
      if (gia_124[0] > 0) gs_116 = gs_116 + StringSubstr(gs_132, 0, gia_124[0]);
   }
   InternetCloseHandle(gi_104);
   return (gs_116);
}

string authentication(string as_0, string as_8) {
   string ls_24;
   string ls_unused_32;
   string ls_16 = "mode=authenticate&username=" + as_0 + "&password=" + as_8;
   Base64Encode(ls_16, ls_24);
   return (LoadURL(ProcessURL("http://www.forexcrescendo.com/auth/crescendo_auth.php?encoded=" + ls_24)));
}
*/

void Base64Encode(string as_0, string &as_8) {
   int li_28;
   int li_32;
   int li_36;
   int li_40;
   int li_44;
   int li_48;
   int li_52;
   int li_16 = 0;
   int li_20 = 0;
   int str_len_24 = StringLen(as_0);
   while (li_16 < str_len_24) {
      li_36 = StringGetChar(as_0, li_16);
      li_16++;
      if (li_16 >= str_len_24) {
         li_32 = 0;
         li_28 = 0;
         li_20 = 2;
      } else {
         li_32 = StringGetChar(as_0, li_16);
         li_16++;
         if (li_16 >= str_len_24) {
            li_28 = 0;
            li_20 = 1;
         } else {
            li_28 = StringGetChar(as_0, li_16);
            li_16++;
         }
      }
      li_40 = li_36 >> 2;
      li_44 = (li_36 & 3 * 16) | li_32 >> 4;
      li_48 = (li_32 & 15 * 4) | li_28 >> 6;
      li_52 = li_28 & 63;
      as_8 = as_8 + CharToStr(gia_176[li_40]);
      as_8 = as_8 + CharToStr(gia_176[li_44]);
      switch (li_20) {
      case 0:
         as_8 = as_8 + CharToStr(gia_176[li_48]);
         as_8 = as_8 + CharToStr(gia_176[li_52]);
         break;
      case 1:
         as_8 = as_8 + CharToStr(gia_176[li_48]);
         as_8 = as_8 + "=";
         break;
      case 2:
         as_8 = as_8 + "==";
      }
   }
}

int nextFibo(int ai_0) {
   int li_4;
   if (ai_0 == 0) return (1);
   int li_8 = 0;
   for (int li_ret_12 = 1; li_ret_12 <= ai_0; li_8 = li_4) {
      li_4 = li_ret_12;
      li_ret_12 += li_8;
   }
   return (li_ret_12);
}

double TimeZoneLocal() {
   int lia_0[43];
   switch (GetTimeZoneInformation(lia_0)) {
   case 0:
      return (lia_0[0] / (-60.0));
   case 1:
      return (lia_0[0] / (-60.0));
   case 2:
      return ((lia_0[0] + lia_0[42]) / (-60.0));
   }
   Print("Unkown return value from GetTimeZoneInformation in kernel32.dll. Returning 0");
   return (0);
}

double TimeZoneServer() {
   int li_0 = (TimeCurrent() - TimeLocal()) / 60;
   int li_4 = MathRound(li_0 / 30.0);
   li_0 = 30 * li_4;
   return (TimeZoneLocal() + li_0 / 60.0);
}

int TimeGMT() {
   if (!IsDllsAllowed()) {
      Alert(Symbol() + " SistemaUNODUE: DLLs are disabled.  To enable tick the checkbox in the Common Tab of EA\'\'s proterties");
      return;
   }
   int li_ret_0 = TimeLocal() - 3600.0 * TimeZoneLocal();
   int li_ret_4 = TimeCurrent() - 3600.0 * TimeZoneServer();
   if (li_ret_0 > li_ret_4 + 300) return (li_ret_0);
   return (li_ret_4);
}

int buy(double ad_0, double ad_8, double ad_16, int ai_24 = 42, string as_28 = "") {
   int li_40;
   RefreshRates();
   double ld_44 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_52 = MarketInfo(Symbol(), MODE_MAXLOT);
   double lotstep_60 = MarketInfo(Symbol(), MODE_LOTSTEP);
   if (NormalizeDouble(lotstep_60, 2) < 0.1) li_40 = 2;
   else li_40 = 1;
   ad_0 = NormalizeDouble(ad_0, li_40);
   if (ad_0 < ld_44) ad_0 = ld_44;
   if (ad_0 > ld_52) ad_0 = ld_52;
   if (ad_8 > 0.0) ad_8 = Ask - ad_8 * gd_436;
   else ad_8 = 0;
   if (ad_16 > 0.0) ad_16 = Ask + ad_16 * gd_436;
   else ad_16 = 0;
   if (!gi_172) return (orderSendReliable(Symbol(), OP_BUY, ad_0, Ask, 100, ad_8, ad_16, as_28, ai_24, 0, g_color_140));
   int li_ret_36 = orderSendReliable(Symbol(), OP_BUY, ad_0, Ask, 100, 0, 0, as_28, ai_24, 0, g_color_140);
   if (ad_8 + ad_16 > 0.0) orderModifyReliable(li_ret_36, 0, ad_8, ad_16, 0);
   return (li_ret_36);
}

int sell(double ad_0, double ad_8, double ad_16, int ai_24 = 42, string as_28 = "") {
   int li_40;
   RefreshRates();
   double ld_44 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_52 = MarketInfo(Symbol(), MODE_MAXLOT);
   double lotstep_60 = MarketInfo(Symbol(), MODE_LOTSTEP);
   if (NormalizeDouble(lotstep_60, 2) < 0.1) li_40 = 2;
   else li_40 = 1;
   ad_0 = NormalizeDouble(ad_0, li_40);
   if (ad_0 < ld_44) ad_0 = ld_44;
   if (ad_0 > ld_52) ad_0 = ld_52;
   if (ad_8 > 0.0) ad_8 = Bid + ad_8 * gd_436;
   else ad_8 = 0;
   if (ad_16 > 0.0) ad_16 = Bid - ad_16 * gd_436;
   else ad_16 = 0;
   if (!gi_172) return (orderSendReliable(Symbol(), OP_SELL, ad_0, Bid, 100, ad_8, ad_16, as_28, ai_24, 0, g_color_144));
   int li_ret_36 = orderSendReliable(Symbol(), OP_SELL, ad_0, Bid, 100, 0, 0, as_28, ai_24, 0, g_color_144);
   if (ad_8 + ad_16 > 0.0) orderModifyReliable(li_ret_36, 0, ad_8, ad_16, 0);
   return (li_ret_36);
}

double getLotsOnTable(int a_magic_0) {
   double ld_4 = 0;
   int ord_total_16 = OrdersTotal();
   for (int pos_12 = 0; pos_12 < ord_total_16; pos_12++) {
      OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == a_magic_0) {
         if (OrderType() == OP_BUY) ld_4 += OrderLots();
         if (OrderType() == OP_SELL) ld_4 -= OrderLots();
      }
   }
   ld_4 = MathAbs(ld_4);
   return (ld_4);
}

double getAveragePositionPrice(string a_symbol_0, int a_magic_8) {
   double ld_12 = getLotsOnTable(a_magic_8);
   double ld_ret_20 = 0;
   int ord_total_32 = OrdersTotal();
   for (int pos_28 = 0; pos_28 < ord_total_32; pos_28++) {
      OrderSelect(pos_28, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == a_magic_8 && OrderSymbol() == a_symbol_0) ld_ret_20 += OrderOpenPrice() * OrderLots() / ld_12;
   }
   return (ld_ret_20);
}

void plotOpenedTradeArrow(int a_ticket_0, bool ai_4 = FALSE) {
   string name_8;
   string name_16;
   color color_24;
   if (IsOptimization() == FALSE) {
      OrderSelect(a_ticket_0, SELECT_BY_TICKET);
      name_8 = "#" + a_ticket_0 + " ";
      if (OrderType() == OP_BUY) {
         name_8 = name_8 + "buy ";
         color_24 = g_color_140;
      }
      if (OrderType() == OP_SELL) {
         name_8 = name_8 + "sell ";
         color_24 = g_color_144;
      }
      name_8 = name_8 + DoubleToStr(OrderLots(), 2) + " ";
      name_8 = name_8 + OrderSymbol() + " ";
      name_16 = name_8 + "at " + DoubleToStr(0, MarketInfo(OrderSymbol(), MODE_DIGITS));
      name_8 = name_8 + "at " + DoubleToStr(OrderOpenPrice(), MarketInfo(OrderSymbol(), MODE_DIGITS));
      ObjectDelete(name_16);
      if (ai_4) ObjectDelete(name_8);
      else {
         ObjectCreate(name_8, OBJ_ARROW, 0, OrderOpenTime(), OrderOpenPrice());
         ObjectSet(name_8, OBJPROP_ARROWCODE, 1);
         ObjectSet(name_8, OBJPROP_COLOR, color_24);
         ObjectSetText(name_8, formatOrderArrowInfo());
      }
   }
}

void plotClosedTradeArrow(int a_ticket_0, bool ai_4 = FALSE) {
   string name_8;
   color color_16;
   if (IsOptimization() == FALSE) {
      OrderSelect(a_ticket_0, SELECT_BY_TICKET);
      name_8 = "#" + a_ticket_0 + " ";
      if (OrderType() == OP_BUY) {
         name_8 = name_8 + "buy ";
         color_16 = g_color_144;
      }
      if (OrderType() == OP_SELL) {
         name_8 = name_8 + "sell ";
         color_16 = g_color_140;
      }
      name_8 = name_8 + DoubleToStr(OrderLots(), 2) + " ";
      name_8 = name_8 + OrderSymbol() + " ";
      name_8 = name_8 + "at " + DoubleToStr(OrderOpenPrice(), MarketInfo(OrderSymbol(), MODE_DIGITS)) + " ";
      name_8 = name_8 + "close at " + DoubleToStr(OrderClosePrice(), MarketInfo(OrderSymbol(), MODE_DIGITS));
      if (ai_4) ObjectDelete(name_8);
      else {
         ObjectCreate(name_8, OBJ_ARROW, 0, OrderCloseTime(), OrderClosePrice());
         ObjectSet(name_8, OBJPROP_ARROWCODE, 3);
         ObjectSet(name_8, OBJPROP_COLOR, color_16);
         ObjectSetText(name_8, formatOrderArrowInfo());
      }
   }
}

void plotClosedTradeLine(int a_ticket_0, bool ai_4 = FALSE) {
   string name_8;
   color color_16;
   if (IsOptimization() == FALSE) {
      OrderSelect(a_ticket_0, SELECT_BY_TICKET);
      name_8 = "#" + a_ticket_0 + " ";
      if (OrderType() == OP_BUY) color_16 = g_color_140;
      if (OrderType() == OP_SELL) color_16 = g_color_144;
      name_8 = name_8 + DoubleToStr(OrderOpenPrice(), MarketInfo(OrderSymbol(), MODE_DIGITS));
      name_8 = name_8 + " -> ";
      name_8 = name_8 + DoubleToStr(OrderClosePrice(), MarketInfo(OrderSymbol(), MODE_DIGITS));
      if (ai_4) ObjectDelete(name_8);
      else {
         ObjectCreate(name_8, OBJ_TREND, 0, OrderOpenTime(), OrderOpenPrice(), OrderCloseTime(), OrderClosePrice());
         ObjectSet(name_8, OBJPROP_RAY, FALSE);
         ObjectSet(name_8, OBJPROP_STYLE, STYLE_DOT);
         ObjectSet(name_8, OBJPROP_COLOR, color_16);
      }
   }
}

string formatOrderArrowInfo() {
   return (StringConcatenate(OrderComment(), " (", OrderMagicNumber(), ")\nP/L: ", DoubleToStr(OrderProfit() + OrderSwap() + OrderCommission(), 2), " ", AccountCurrency(), " (", DoubleToStr(MathAbs(OrderOpenPrice() - OrderClosePrice()) / (MarketInfo(OrderSymbol(), MODE_POINT) * pointsPerPip()), 2), " pips)"));
}

void plotNewOpenTrades(int a_magic_0 = -1) {
   int ord_total_4;
   if (IsTesting() == FALSE) {
      ord_total_4 = OrdersTotal();
      for (int pos_8 = 0; pos_8 < ord_total_4; pos_8++) {
         OrderSelect(pos_8, SELECT_BY_POS, MODE_TRADES);
         if (a_magic_0 == -1 || OrderMagicNumber() == a_magic_0 && OrderSymbol() == Symbol())
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) plotOpenedTradeArrow(OrderTicket());
      }
   }
}

void plotNewClosedTrades(int a_magic_0 = -1) {
   bool bool_4;
   int li_8;
   int datetime_12;
   bool li_16;
   int hist_total_20;
   if (IsTesting() == FALSE) {
      bool_4 = GlobalVariableCheck("ARROW_MAX_AGE");
      li_8 = 60.0 * (60.0 * (24.0 * GlobalVariableGet("ARROW_MAX_AGE")));
      datetime_12 = TimeCurrent();
      hist_total_20 = OrdersHistoryTotal();
      OrderSelect(hist_total_20 - 1, SELECT_BY_POS, MODE_HISTORY);
      if (OrderTicket() + li_8 + bool_4 != gi_680) {
         gi_680 = OrderTicket() + li_8 + bool_4;
         for (int pos_24 = 0; pos_24 < hist_total_20; pos_24++) {
            OrderSelect(pos_24, SELECT_BY_POS, MODE_HISTORY);
            if (a_magic_0 == -1 || OrderMagicNumber() == a_magic_0 && OrderSymbol() == Symbol()) {
               if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
                  if (bool_4 && datetime_12 - OrderCloseTime() > li_8) li_16 = TRUE;
                  else li_16 = FALSE;
                  plotOpenedTradeArrow(OrderTicket(), li_16);
                  plotClosedTradeArrow(OrderTicket(), li_16);
                  plotClosedTradeLine(OrderTicket(), li_16);
               }
            }
         }
      }
   }
}

string horizLine(string a_name_0, double a_price_8, color a_color_16 = 255, string a_text_20 = "") {
   if (!IsOptimization()) {
      if (a_name_0 == "") a_name_0 = "line_" + Time[0];
      if (ObjectFind(a_name_0) == -1) ObjectCreate(a_name_0, OBJ_HLINE, 0, 0, a_price_8);
      else ObjectSet(a_name_0, OBJPROP_PRICE1, a_price_8);
      ObjectSet(a_name_0, OBJPROP_COLOR, a_color_16);
      ObjectSetText(a_name_0, a_text_20);
   }
   return (a_name_0);
}

int orderModifyReliable(int a_ticket_0, double ad_4, double ad_12, double ad_20, int a_datetime_28, color a_color_32 = -1) {
   bool bool_36;
   int error_40;
   Print("OrderModify(" + a_ticket_0 + "," + ad_4 + "," + ad_12 + "," + ad_20 + "," + a_datetime_28 + "," + a_color_32 + ")");
   while (true) {
      while (IsTradeContextBusy()) {
         Print("OrderModify: Waiting for trade context.");
         Sleep(MathRand() / 10);
      }
      bool_36 = OrderModify(a_ticket_0, NormalizeDouble(ad_4, Digits), NormalizeDouble(ad_12, Digits), NormalizeDouble(ad_20, Digits), a_datetime_28, a_color_32);
      if (bool_36) {
         Print("OrderModify: Success!");
         return (1);
      }
      error_40 = GetLastError();
      if (isTemporaryError(error_40)) Print("OrderModify: Temporary Error: " + error_40 + " " + ErrorDescription(error_40) + ". waiting.");
      else {
         Print("OrderModify: Permanent Error: " + error_40 + " " + ErrorDescription(error_40) + ". giving up.");
         return (0);
      }
      Sleep(MathRand() / 10);
   }
   return /*(WARN)*/;
}

int orderSendReliable(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_price_20, int a_slippage_28, double ad_32, double ad_40, string a_comment_48 = "", int a_magic_56 = 0, int a_datetime_60 = 0, color a_color_64 = -1) {
   int ticket_68;
   int error_72;
   Print("OrderSend(" + a_symbol_0 + "," + a_cmd_8 + "," + a_lots_12 + "," + a_price_20 + "," + a_slippage_28 + "," + ad_32 + "," + ad_40 + "," + a_comment_48 + "," +
      a_magic_56 + "," + a_datetime_60 + "," + a_color_64 + ")");
   while (true) {
      if (IsStopped()) {
         Print("OrderSend: Trading is stopped!");
         return (-1);
      }
      RefreshRates();
      if (a_cmd_8 == OP_BUY) a_price_20 = Ask;
      if (a_cmd_8 == OP_SELL) a_price_20 = Bid;
      if (!IsTradeContextBusy()) {
         ticket_68 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, NormalizeDouble(a_price_20, MarketInfo(a_symbol_0, MODE_DIGITS)), a_slippage_28, NormalizeDouble(ad_32, MarketInfo(a_symbol_0, MODE_DIGITS)), NormalizeDouble(ad_40, MarketInfo(a_symbol_0, MODE_DIGITS)), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
         if (ticket_68 > 0) {
            Print("OrderSend: Success! Ticket: " + ticket_68);
            return (ticket_68);
         }
         error_72 = GetLastError();
         if (isTemporaryError(error_72)) Print("OrderSend: Temporary Error: " + error_72 + " " + ErrorDescription(error_72) + ". waiting.");
         else {
            Print("OrderSend: Permanent Error: " + error_72 + " " + ErrorDescription(error_72) + ". giving up.");
            return (-1);
         }
      } else Print("OrderSend: Must wait for trade context");
      Sleep(MathRand() / 10);
   }
   return /*(WARN)*/;
}

int orderCloseReliable(int a_ticket_0, double a_lots_4, double a_price_12, int a_slippage_20, color a_color_24 = -1) {
   bool ord_close_28;
   int error_32;
   Print("OrderClose");
   OrderSelect(a_ticket_0, SELECT_BY_TICKET, MODE_TRADES);
   while (true) {
      if (IsStopped()) {
         Print("OrderClose: Trading is stopped!");
         return (0);
      }
      RefreshRates();
      if (OrderType() == OP_BUY) a_price_12 = Bid;
      if (OrderType() == OP_SELL) a_price_12 = Ask;
      if (!IsTradeContextBusy()) {
         ord_close_28 = OrderClose(a_ticket_0, a_lots_4, NormalizeDouble(a_price_12, MarketInfo(OrderSymbol(), MODE_DIGITS)), a_slippage_20, a_color_24);
         if (ord_close_28) {
            Print("OrderClose: Success!");
            return (1);
         }
         error_32 = GetLastError();
         if (isTemporaryError(error_32)) Print("OrderClose: Temporary Error: " + error_32 + " " + ErrorDescription(error_32) + ". waiting.");
         else {
            Print("OrderClose: Permanent Error: " + error_32 + " " + ErrorDescription(error_32) + ". giving up.");
            return (0);
         }
      } else Print("OrderClose: Must wait for trade context");
      Sleep(MathRand() / 10);
   }
   return /*(WARN)*/;
}

bool isTemporaryError(int ai_0) {
   return (ai_0 == 0 || ai_0 == 2 || ai_0 == 4 || ai_0 == 6 || ai_0 == 132 || ai_0 == 135 || ai_0 == 129 || ai_0 == 136 || ai_0 == 137 || ai_0 == 138 || ai_0 == 128 ||
      ai_0 == 146);
}

double pointsPerPip() {
   int digits_4;
   string ls_16;
   double ld_ret_8 = 1;
   int file_24 = FileOpenHistory("symbols.raw", FILE_BIN|FILE_READ);
   int li_28 = FileSize(file_24) / 1936;
   for (int count_0 = 0; count_0 < li_28; count_0++) {
      ls_16 = FileReadString(file_24, 12);
      if (StringFind(ls_16, "EURUSD") != -1) {
         digits_4 = MarketInfo(ls_16, MODE_DIGITS);
         if (digits_4 == 4) {
            ld_ret_8 = 1;
            break;
         }
         ld_ret_8 = 10;
         break;
      }
      FileSeek(file_24, 1924, SEEK_CUR);
   }
   FileClose(file_24);
   return (ld_ret_8);
}