/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Prospector"
#property link      "Copyright © 2009, Forex-Goldmine.com"

string gs_76 = "Prospector 1.04 ";
extern string OWN = "Copyright © 2009, Forex-Goldmine.com";
bool gi_unused_92 = TRUE;
extern string _Comment = "Prospector 1.04 ";
string gs_104 = "Put Unlock Code Here ";
extern bool DoTrades = TRUE;
bool gi_unused_116 = FALSE;
extern int TradeRange = 110;
extern int MaxLevels = 10;
extern double FixedCenter = 0.0;
extern int ProfitLock = 0;
bool gi_140 = TRUE;
double gd_144 = 1.429;
int gi_152 = 40;
int gi_156 = 0;
int gi_160 = 0;
int gi_164 = 0;
int gi_unused_168 = 1;
int gi_unused_172 = 1;
int gi_176 = 0;
bool gi_180 = FALSE;
bool gi_unused_184 = TRUE;
int gi_unused_188 = 89;
int gi_unused_192 = 0;
bool gi_unused_196 = TRUE;
bool gi_200 = FALSE;
bool gi_204 = FALSE;
int gi_208 = 0;
int gi_unused_212 = 0;
double gd_216 = 20.0;
int gi_224 = 10000;
bool gi_228 = FALSE;
string gs_unused_232 = "Leave as 0 to use actual account balance.";
double gd_240 = 0.0;
double gd_248 = 0.1;
bool gi_256 = FALSE;
extern double BalanceFactor = 3500.0;
int gi_268 = 0;
int gi_272 = 5;
extern string instr = "Leave Lots 0.00 for Auto-Calculation";
extern double TradeLots = 0.0;
double gd_292 = 0.01;
extern string magicnumber = "--Magic Number--";
extern int MagicNumber = 793001;
extern int LookBackMonths = 60;
string gs_unused_316 = " ";
double gd_unused_324 = 0.0;
double g_ihigh_332 = 0.0;
double g_ilow_340 = 0.0;
double gd_348 = 0.0;
double gd_356 = 0.0;
double gd_unused_364 = 0.0;
int g_ord_total_372 = 0;
int g_error_376 = 0/* NO_ERROR */;
double g_ord_open_price_380 = 0.0;
double g_ord_open_price_388 = 0.0;
int gi_396 = 0;
bool gi_400 = FALSE;
int gi_404 = 0;
int gi_408 = 0;
string g_comment_412 = " ";
int gi_420 = 3;
int g_color_424 = CLR_NONE;
int g_color_428 = CLR_NONE;
bool gi_432 = FALSE;
double gd_436 = 0.0;
double gd_444 = 0.0;
int gi_unused_452 = 0;
string gs_unused_456 = " ";
int gi_unused_464 = 0;
double gd_unused_468 = 5.0;
int gi_476 = 0;
double gda_480[501];
double gda_484[501];
double gda_488[501];
double gda_492[501];
double gda_496[501];
int g_count_500 = 0;
int g_index_504 = 0;
double gd_508 = 0.0;
int gi_516 = 0;
int gi_520 = 0;
int g_str2int_524 = 0;
double gd_528 = 0.0;
int gi_536 = 0;
int gi_540 = 0;
int gi_544 = 0;
int gi_548 = 0;
int gi_552 = 0;
int gi_556 = 0;

int init() {
   gi_552 = LookupLiveAccountNumbers();
   if (!IsDemo() && gi_552 == 0) {
      Comment("The Prospector Cannot Trade in this live account. It can be used only on a demo account.");
      return (0);
   }
   if (MarketInfo(Symbol(), MODE_MINLOT) < 1.0) gi_268 = 1;
   if (MarketInfo(Symbol(), MODE_MINLOT) < 0.1) gi_268 = 2;
   if (MarketInfo(Symbol(), MODE_MINLOT) < 0.01) gi_268 = 3;
   if (MarketInfo(Symbol(), MODE_MINLOT) < 0.001) gi_268 = 4;
   if (MarketInfo(Symbol(), MODE_MINLOT) < 0.0001) gi_268 = 5;
   if (MaxLevels > 14) MaxLevels = 14;
   if (Digits == 5 || Digits == 3) {
      TradeRange = 10 * TradeRange;
      gi_536 = 10 * gi_536;
   }
   PreSet();
   gi_536 = TradeRange;
   gi_540 = gi_152;
   gd_unused_468 = 5;
   if (Digits == 5 || Digits == 3) {
      gi_156 = 10 * gi_156;
      gd_unused_468 = 50;
      gi_164 = 10 * gi_164;
      gd_216 = 10.0 * gd_216;
      gi_176 = 10 * gi_176;
   }
   ArrayInitialize(gda_480, 0.0);
   ArrayInitialize(gda_492, 0.0);
   ArrayInitialize(gda_496, 0.0);
   ArrayInitialize(gda_484, 0.0);
   ArrayInitialize(gda_488, 0.0);
   gd_292 = CalculateLots();
   gd_356 = gd_292;
   CalculateHighAndLow();
   gi_544 = GetFibLevel();
   BuildGrid(gi_544);
   GetCurrentGrid();
   DisplayComments();
   return (0);
}

int deinit() {
   DeleteAllObjects();
   return (0);
}

int start() {
   gi_552 = LookupLiveAccountNumbers();
   if (!IsDemo() && gi_552 == 0) {
      Comment("The Prospector Cannot Trade in this live account. It can be used only on a demo account.");
      return (0);
   }
   gd_528 = CalculateLots();
   gd_356 = gd_528;
   gi_544 = GetFibLevel();
   if (gi_156 != 0 && gi_556 != gi_544) {
      BuildGrid(gi_544);
      gi_556 = gi_544;
   }
   GetLotsi();
   CalculateHighAndLow();
   if (DoTrades == TRUE) CheckToOpen();
   if (DoTrades == TRUE) {
      if (ProfitLock > 0) {
         for (int l_pos_0 = 0; l_pos_0 <= OrdersTotal(); l_pos_0++) {
            OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
               if (OrderType() == OP_BUY) {
                  if (Bid - OrderOpenPrice() > Point * ProfitLock) {
                     if (OrderStopLoss() < Bid - Point * ProfitLock || OrderStopLoss() == 0.0) {
                        OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * ProfitLock, OrderTakeProfit(), 0, CLR_NONE);
                        continue;
                     }
                  }
               }
               if (OrderType() == OP_SELL) {
                  if (OrderOpenPrice() - Ask > Point * ProfitLock)
                     if (OrderStopLoss() > Ask + Point * ProfitLock || OrderStopLoss() == 0.0) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * ProfitLock, OrderTakeProfit(), 0, CLR_NONE);
               }
            }
         }
      }
   }
   gi_unused_116 = FALSE;
   DisplayComments();
   return (0);
}

void CheckToOpen() {
   double ld_0;
   gi_unused_464 = 0;
   if (Ask < gd_348) {
      if (search_for(1) == 0.0) {
         ld_0 = search_for(2);
         if (ld_0 == 0.0) {
            gi_544 = GetFibLevel();
            if (searchprices("A", "O") == 1) {
               GetLotsi();
               gi_548 = GetAddedPips();
               open_buy_market(gd_292, MagicNumber);
               if (search_for(2) != 0.0) {
                  if (gi_200 == TRUE) gd_436 = gda_488[gi_476] + MarketInfo(Symbol(), MODE_SPREAD) * Point + gi_548 * Point;
                  if (gi_200 == FALSE) gd_436 = gda_488[gi_476] + gi_548 * Point;
                  gd_444 = 0.0;
                  if (gi_544 < MaxLevels) gd_444 = gda_484[gi_476 - 1];
                  gi_unused_464 = 0;
                  OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(gd_444, Digits), NormalizeDouble(gd_436, Digits), 0, CLR_NONE);
               }
            }
         }
      }
   }
   if (Bid > gd_348) {
      if (search_for(2) == 0.0) {
         ld_0 = search_for(1);
         if (ld_0 == 0.0) {
            gi_544 = GetFibLevel();
            if (searchprices("B", "O") == 1) {
               GetLotsi();
               gi_548 = GetAddedPips();
               open_sell_market(gd_292, MagicNumber);
               if (search_for(1) != 0.0) {
                  if (gi_200 == TRUE) gd_436 = gda_496[gi_476] - MarketInfo(Symbol(), MODE_SPREAD) * Point - gi_548 * Point;
                  if (gi_200 == FALSE) gd_436 = gda_496[gi_476] - gi_548 * Point;
                  gd_444 = 0.0;
                  if (gi_544 < MaxLevels) gd_444 = gda_492[gi_476 + 1];
                  gi_unused_464 = 0;
                  OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(gd_444, Digits), NormalizeDouble(gd_436, Digits), 0, CLR_NONE);
               }
            }
         }
      }
   }
}

void CheckForClose() {
   double ld_12;
   int li_unused_0 = 0;
   double ld_4 = search_for(2);
   if (ld_4 != 0.0) {
      if (Bid >= gd_436 && gd_436 != 0.0) {
         CloseMagic_Loss();
         ld_12 = ld_4 / gd_356;
         g_str2int_524 = StrToInteger(DoubleToStr(ld_12, 0));
         gi_520 -= g_str2int_524;
         if (gi_180 == FALSE && gi_520 < 0) gi_520 = 0;
         gs_unused_456 = "Win";
      }
      if (Bid <= gd_444 && gd_444 != 0.0 && gi_544 < MaxLevels) {
         CloseMagic_Loss();
         ld_12 = ld_4 / gd_356;
         g_str2int_524 = StrToInteger(DoubleToStr(ld_12, 0));
         gi_520 += g_str2int_524;
         if (gi_180 == FALSE && gi_520 < 0) gi_520 = 0;
         gs_unused_456 = "Loss";
      }
   }
   ld_4 = search_for(1);
   if (ld_4 != 0.0) {
      if (Ask <= gd_436 && gd_436 != 0.0) {
         CloseMagic_Loss();
         ld_12 = ld_4 / gd_356;
         g_str2int_524 = StrToInteger(DoubleToStr(ld_12, 0));
         gi_520 -= g_str2int_524;
         if (gi_180 == FALSE && gi_520 < 0) gi_520 = 0;
         gs_unused_456 = "Win";
      }
      if (Bid >= gd_444 && gd_444 != 0.0 && gi_544 < MaxLevels) {
         CloseMagic_Loss();
         ld_12 = ld_4 / gd_356;
         g_str2int_524 = StrToInteger(DoubleToStr(ld_12, 0));
         gi_520 += g_str2int_524;
         if (gi_180 == FALSE && gi_520 < 0) gi_520 = 0;
         gs_unused_456 = "Loss";
      }
   }
}

void DeleteAllObjects() {
   string l_name_4;
   int l_objs_total_0 = ObjectsTotal();
   for (int li_12 = 0; li_12 < l_objs_total_0; li_12++) {
      l_name_4 = ObjectName(li_12);
      if (l_name_4 != "") ObjectDelete(l_name_4);
   }
   ObjectDelete("FLP_txt");
   ObjectDelete("P_txt");
}

void ClosePair() {
   int l_cmd_12;
   int l_ticket_16;
   double l_ord_lots_20;
   string l_symbol_28;
   int l_ord_total_0 = OrdersTotal();
   for (int l_count_4 = 0; l_count_4 < 5; l_count_4++) {
      for (int l_pos_8 = 0; l_pos_8 < l_ord_total_0; l_pos_8++) {
         while (!IsTradeAllowed()) {
         }
         OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
         l_cmd_12 = OrderType();
         l_ticket_16 = OrderTicket();
         l_ord_lots_20 = OrderLots();
         l_symbol_28 = OrderSymbol();
         if (l_symbol_28 == Symbol() && l_cmd_12 == OP_BUY) OrderClose(l_ticket_16, l_ord_lots_20, MarketInfo(l_symbol_28, MODE_BID), 3, CLR_NONE);
         if (l_symbol_28 == Symbol() && l_cmd_12 == OP_SELL) OrderClose(l_ticket_16, l_ord_lots_20, MarketInfo(l_symbol_28, MODE_ASK), 3, CLR_NONE);
      }
   }
}

void CloseMagic_Win() {
   bool l_ord_close_8;
   int l_cmd_12;
   int l_ord_total_0 = OrdersTotal();
   int l_slippage_4 = 100;
   double l_lots_16 = gd_356;
   l_lots_16 = NormalizeDouble(l_lots_16, gi_268);
   for (int l_pos_24 = l_ord_total_0 - 1; l_pos_24 >= 0; l_pos_24--) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         l_cmd_12 = OrderType();
         l_ord_close_8 = FALSE;
         if (l_cmd_12 == OP_BUY) l_ord_close_8 = OrderClose(OrderTicket(), l_lots_16, MarketInfo(OrderSymbol(), MODE_BID), l_slippage_4, Red);
         else
            if (l_cmd_12 == OP_SELL) l_ord_close_8 = OrderClose(OrderTicket(), l_lots_16, MarketInfo(OrderSymbol(), MODE_ASK), l_slippage_4, Red);
      }
   }
}

void CloseMagic_Loss() {
   bool l_ord_close_8;
   int l_cmd_12;
   int l_ord_total_0 = OrdersTotal();
   int l_slippage_4 = 100;
   for (int l_pos_16 = l_ord_total_0 - 1; l_pos_16 >= 0; l_pos_16--) {
      OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         l_cmd_12 = OrderType();
         l_ord_close_8 = FALSE;
         if (l_cmd_12 == OP_BUY) l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), l_slippage_4, Red);
         else
            if (l_cmd_12 == OP_SELL) l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), l_slippage_4, Red);
      }
   }
}

double CalculateLots() {
   double ld_16;
   double ld_24;
   double l_lotsize_32;
   double ld_40;
   bool li_48;
   double ld_52;
   double ld_60;
   double l_minlot_0 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_8 = 0.0;
   if (TradeLots > 0.0) ld_8 = TradeLots;
   if (ld_8 > 0.0) {
      if (ld_8 < l_minlot_0) ld_8 = l_minlot_0;
      ld_8 = NormalizeDouble(ld_8, gi_268);
      return (ld_8);
   }
   if (gi_256 == TRUE) {
      if (TradeLots > 0.0) ld_8 = TradeLots;
      if (ld_8 > 0.0) {
         if (ld_8 < l_minlot_0) ld_8 = l_minlot_0;
         ld_8 = NormalizeDouble(ld_8, gi_268);
         return (ld_8);
      }
      if (gi_228 == TRUE) ld_16 = AccountEquity();
      if (gi_228 == FALSE) ld_16 = AccountBalance();
      if (gd_240 != 0.0) ld_16 = gd_240;
      ld_24 = gd_248 / 100.0 * ld_16;
      l_lotsize_32 = MarketInfo(Symbol(), MODE_LOTSIZE);
      ld_40 = ld_24 / (l_lotsize_32 / AccountLeverage());
      ld_8 = ld_40;
      if (ld_8 < l_minlot_0) ld_8 = l_minlot_0;
      ld_8 = NormalizeDouble(ld_8, gi_268);
      return (ld_8);
   }
   if (gi_256 == FALSE) {
      l_minlot_0 = MarketInfo(Symbol(), MODE_MINLOT);
      ld_8 = 0.0;
      li_48 = FALSE;
      l_lotsize_32 = MarketInfo(Symbol(), MODE_LOTSIZE);
      ld_52 = AccountBalance();
      if (gi_228 == TRUE) ld_52 = AccountEquity();
      if (gd_240 != 0.0) ld_52 = gd_240;
      ld_60 = BalanceFactor;
      ld_60 /= 100000 / l_lotsize_32;
      li_48 = ld_52 / ld_60;
      ld_8 = li_48 / 100.0;
      ld_8 = NormalizeDouble(ld_8, gi_268);
      if (ld_8 < l_minlot_0) ld_8 = l_minlot_0;
      ld_8 = NormalizeDouble(ld_8, gi_268);
      return (ld_8);
   }
   return (0.0);
}

void ClosePairType(int a_cmd_0) {
   int l_cmd_16;
   int l_ticket_20;
   double l_ord_lots_24;
   string l_symbol_32;
   int l_ord_total_4 = OrdersTotal();
   for (int l_count_8 = 0; l_count_8 < 5; l_count_8++) {
      for (int l_pos_12 = 0; l_pos_12 < l_ord_total_4; l_pos_12++) {
         while (!IsTradeAllowed()) {
         }
         OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
         l_cmd_16 = OrderType();
         l_ticket_20 = OrderTicket();
         l_ord_lots_24 = OrderLots();
         l_symbol_32 = OrderSymbol();
         if (l_symbol_32 == Symbol() && l_cmd_16 == OP_BUY && l_cmd_16 == a_cmd_0 && OrderMagicNumber() == MagicNumber) OrderClose(l_ticket_20, l_ord_lots_24, MarketInfo(l_symbol_32, MODE_BID), 3, CLR_NONE);
         if (l_symbol_32 == Symbol() && l_cmd_16 == OP_SELL && l_cmd_16 == a_cmd_0 && OrderMagicNumber() == MagicNumber) OrderClose(l_ticket_20, l_ord_lots_24, MarketInfo(l_symbol_32, MODE_ASK), 3, CLR_NONE);
      }
   }
}

double search_for(int ai_0) {
   double l_ord_lots_4 = 0.0;
   g_ord_total_372 = OrdersTotal();
   for (int l_pos_12 = g_ord_total_372 - 1; l_pos_12 >= 0; l_pos_12--) {
      if (!OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES)) {
         g_error_376 = GetLastError();
         Print("OrderSelect( ", l_pos_12, ", SELECT_BY_POS ) - Error #", g_error_376);
      } else {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == MagicNumber) {
               if (OrderCloseTime() == 0) {
                  if (ai_0 == 1 && OrderType() == OP_BUY) continue;
                  if (ai_0 == 2 && OrderType() == OP_SELL) continue;
                  if (OrderLots() >= l_ord_lots_4) {
                     l_ord_lots_4 = OrderLots();
                     g_ord_open_price_380 = OrderOpenPrice();
                     return (l_ord_lots_4);
                  }
               }
            }
         }
      }
   }
   return (l_ord_lots_4);
}

double search_for_8() {
   double ld_12;
   double ld_unused_0 = 0.0;
   g_ord_total_372 = OrdersTotal();
   for (int l_pos_8 = g_ord_total_372 - 1; l_pos_8 >= 0; l_pos_8--) {
      if (!OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES)) {
         g_error_376 = GetLastError();
         Print("OrderSelect( ", l_pos_8, ", SELECT_BY_POS ) - Error #", g_error_376);
      } else {
         if (OrderSymbol() == Symbol()) {
            if (OrderCloseTime() == 0) {
               if (OrderLots() >= gd_356 * gi_224 && OrderTakeProfit() == 0.0) {
                  ld_12 = 0.0;
                  if (OrderType() == OP_BUY) ld_12 = OrderOpenPrice() + 10.0 * ((OrderLots() - gd_356) / gd_356) * Point;
                  if (OrderType() == OP_SELL) ld_12 = OrderOpenPrice() - 10.0 * ((OrderLots() - gd_356) / gd_356) * Point;
                  OrderModify(OrderTicket(), OrderOpenPrice(), 0.0, NormalizeDouble(ld_12, Digits), 0, CLR_NONE);
               }
            }
         }
      }
   }
   return (0.0);
}

double find_profit(int ai_0) {
   double ld_ret_4 = 0.0;
   g_ord_total_372 = OrdersTotal();
   for (int l_pos_12 = g_ord_total_372 - 1; l_pos_12 >= 0; l_pos_12--) {
      if (!OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES)) {
         g_error_376 = GetLastError();
         Print("OrderSelect( ", l_pos_12, ", SELECT_BY_POS ) - Error #", g_error_376);
      } else {
         if (OrderSymbol() == Symbol()) {
            if (OrderCloseTime() == 0) {
               if (ai_0 == 1 && OrderType() == OP_BUY) continue;
               if (ai_0 == 2 && OrderType() == OP_SELL) continue;
               ld_ret_4 += OrderProfit();
            }
         }
      }
   }
   return (ld_ret_4);
}

double search_for2(int ai_0) {
   double l_ord_lots_4 = 10000.0;
   g_ord_total_372 = OrdersTotal();
   int li_unused_12 = 0;
   for (int l_pos_16 = g_ord_total_372 - 1; l_pos_16 >= 0; l_pos_16--) {
      if (!OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         g_error_376 = GetLastError();
         Print("OrderSelect( ", l_pos_16, ", SELECT_BY_POS ) - Error #", g_error_376);
      } else {
         if (OrderSymbol() == Symbol()) {
            if (OrderCloseTime() == 0) {
               if (ai_0 == 1 && OrderType() == OP_BUY) continue;
               if (ai_0 == 2 && OrderType() == OP_SELL) continue;
               if (OrderLots() <= l_ord_lots_4) {
                  l_ord_lots_4 = OrderLots();
                  g_ord_open_price_388 = OrderOpenPrice();
               }
            }
         }
      }
   }
   if (l_ord_lots_4 == 10000.0) l_ord_lots_4 = 0.0;
   return (l_ord_lots_4);
}

int open_buy_market(double a_lots_0, int a_magic_8) {
   int l_slippage_44;
   int l_count_48;
   int l_ticket_52;
   double l_price_56;
   double l_maxlot_12 = MarketInfo(Symbol(), MODE_MAXLOT);
   double l_minlot_20 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_28 = a_lots_0;
   double l_price_36 = 0.0;
   if (gi_396 != 0.0) {
      l_price_36 = Ask - gi_396 * Point;
      l_price_36 = NormalizeDouble(l_price_36, Digits);
   }
   while (ld_28 > 0.0) {
      a_lots_0 = ld_28;
      if (a_lots_0 > l_maxlot_12) a_lots_0 = l_maxlot_12;
      if (a_lots_0 < l_minlot_20) a_lots_0 = l_minlot_20;
      a_lots_0 = NormalizeDouble(a_lots_0, gi_268);
      ld_28 -= a_lots_0;
      l_slippage_44 = gi_272;
      l_count_48 = 0;
      l_ticket_52 = -1;
      l_price_56 = 0.0;
      if (gi_400 == TRUE && gi_404 != 0) {
         l_price_56 = Ask + gi_404 * Point;
         l_price_56 = NormalizeDouble(l_price_56, Digits);
      }
      if (gi_400 == FALSE && gi_408 != 0 && gi_404 != 0) {
         l_price_56 = Ask + (gi_404 + gi_408) * Point;
         l_price_56 = NormalizeDouble(l_price_56, Digits);
      }
      g_comment_412 = _Comment + " " + DoubleToStr(a_magic_8, 0);
      while (l_ticket_52 == -1 && l_count_48 <= gi_420) {
         l_ticket_52 = OrderSend(Symbol(), OP_BUY, a_lots_0, Ask, l_slippage_44, l_price_36, l_price_56, g_comment_412, a_magic_8, 0, g_color_424);
         l_count_48++;
         if (l_ticket_52 >= 0) break;
         if (l_count_48 > gi_420) break;
         Sleep(1000);
      }
      if (l_ticket_52 != 0) {
         g_error_376 = GetLastError();
         if (gi_432 == TRUE) Alert("Error OrderSend # ", g_error_376);
      }
      if (ld_28 > 0.0) continue;
   }
   return (0);
}

int open_sell_market(double a_lots_0, int a_magic_8) {
   int l_slippage_44;
   int l_count_48;
   int l_ticket_52;
   double l_price_56;
   double l_maxlot_12 = MarketInfo(Symbol(), MODE_MAXLOT);
   double l_minlot_20 = MarketInfo(Symbol(), MODE_MINLOT);
   double ld_28 = a_lots_0;
   double l_price_36 = 0.0;
   if (gi_396 != 0.0) {
      l_price_36 = Bid + gi_396 * Point;
      l_price_36 = NormalizeDouble(l_price_36, Digits);
   }
   while (ld_28 > 0.0) {
      a_lots_0 = ld_28;
      if (a_lots_0 > l_maxlot_12) a_lots_0 = l_maxlot_12;
      if (a_lots_0 < l_minlot_20) a_lots_0 = l_minlot_20;
      a_lots_0 = NormalizeDouble(a_lots_0, gi_268);
      ld_28 -= a_lots_0;
      l_slippage_44 = gi_272;
      l_count_48 = 0;
      l_ticket_52 = -1;
      l_price_56 = 0.0;
      if (gi_400 == TRUE && gi_404 != 0) {
         l_price_56 = Bid - gi_404 * Point;
         l_price_56 = NormalizeDouble(l_price_56, Digits);
      }
      if (gi_400 == FALSE && gi_408 != 0 && gi_404 != 0) {
         l_price_56 = Bid - (gi_404 + gi_408) * Point;
         l_price_56 = NormalizeDouble(l_price_56, Digits);
      }
      g_comment_412 = _Comment + " " + DoubleToStr(a_magic_8, 0);
      while (l_ticket_52 == -1 && l_count_48 <= gi_420) {
         l_ticket_52 = OrderSend(Symbol(), OP_SELL, a_lots_0, Bid, l_slippage_44, l_price_36, l_price_56, g_comment_412, a_magic_8, 0, g_color_428);
         l_count_48++;
         if (l_ticket_52 >= 0) break;
         if (l_count_48 > gi_420) break;
         Sleep(1000);
      }
      if (l_ticket_52 != 0) {
         g_error_376 = GetLastError();
         if (gi_432 == TRUE) Alert("Error OrderSend # ", g_error_376);
      }
      if (ld_28 > 0.0) continue;
   }
   return (0);
}

void DisplayComments() {
   string ls_0 = "TRUE";
   if (DoTrades == FALSE) ls_0 = "FALSE";
   Comment("\n" 
      + gs_76, 
      "\n" 
      + "\n" 
      + LookBackMonths + " Months : " 
      + "\nHigh Price = " + DoubleToStr(g_ihigh_332, Digits) 
      + "\nLow Price = " + DoubleToStr(g_ilow_340, Digits) 
      + "\nCenter Price = " + DoubleToStr(gd_348, Digits) 
      + "\n\nTrade Range = " + gi_536 
      + "\nCurrent Grid = " + gi_476 
      + "\nCurrent Fibonacci Level = " + gi_544 
      + "\nLower Price = " + DoubleToStr(gda_484[gi_476], Digits) 
      + "\nUpper Price = " + DoubleToStr(gda_488[gi_476], Digits) 
      + "\n\nCurrent Server Time = " + TimeToStr(TimeCurrent(), TIME_SECONDS) 
      + "\n\nTradeWindow  = " + DoubleToStr(gd_508, Digits) 
      + "\nAccount Leverage =", AccountLeverage() 
      + "\nBalance Factor = " + DoubleToStr(BalanceFactor, 0) 
      + "\nBase Lots = " + DoubleToStr(gd_356, gi_268) 
      + "\nCalculated Lots Based on Fibonacci Level = " + DoubleToStr(gd_292, gi_268) 
   + "\n\nDoTrades = " + ls_0);
}

void GetLotsi() {
   if (gi_544 >= 14 && MaxLevels >= 14) {
      gd_292 = 610.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 14;
      return;
   }
   if (gi_544 >= 13 && MaxLevels >= 13) {
      gd_292 = 377.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 13;
      return;
   }
   if (gi_544 >= 12 && MaxLevels >= 12) {
      gd_292 = 233.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 12;
      return;
   }
   if (gi_544 >= 11 && MaxLevels >= 11) {
      gd_292 = 144.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 11;
      return;
   }
   if (gi_544 >= 10 && MaxLevels >= 10) {
      gd_292 = 89.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 10;
      return;
   }
   if (gi_544 >= 9 && MaxLevels >= 9) {
      gd_292 = 55.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 9;
      return;
   }
   if (gi_544 >= 8 && MaxLevels >= 8) {
      gd_292 = 34.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 8;
      return;
   }
   if (gi_544 >= 7 && MaxLevels >= 7) {
      gd_292 = 21.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 7;
      return;
   }
   if (gi_544 >= 6 && MaxLevels >= 6) {
      gd_292 = 13.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 6;
      return;
   }
   if (gi_544 >= 5 && MaxLevels >= 5) {
      gd_292 = 8.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 8;
      return;
   }
   if (gi_544 >= 4 && MaxLevels >= 4) {
      gd_292 = 5.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 4;
      return;
   }
   if (gi_544 >= 3 && MaxLevels >= 3) {
      gd_292 = 3.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 3;
      return;
   }
   if (gi_544 >= 2 && MaxLevels >= 2) {
      gd_292 = 2.0 * gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 2;
      return;
   }
   if (gi_544 >= 1 && MaxLevels >= 1) {
      gd_292 = gd_356;
      gd_292 = NormalizeDouble(gd_292, gi_268);
      gi_544 = 1;
      return;
   }
}

void BuildGrid(int ai_0) {
   int li_4;
   gi_536 = TradeRange;
   if (ai_0 == 0) ai_0 = 1;
   if (gi_156 != 0) {
      if (ai_0 >= gi_160) {
         li_4 = ai_0 - gi_160;
         gi_536 = TradeRange + gi_156 * (li_4 + 1);
      }
   }
   g_count_500 = 0;
   g_index_504 = 0;
   double ld_8 = gi_540;
   while (g_index_504 <= ld_8) {
      g_index_504++;
      g_count_500++;
      gda_484[g_index_504] = gd_144 + (gi_536 * Point + gi_164 * Point) * (g_count_500 - 1);
      gda_488[g_index_504] = gda_484[g_index_504] + gi_536 * Point;
      gda_492[g_index_504] = gda_488[g_index_504];
      gda_496[g_index_504] = gda_484[g_index_504];
      gda_484[g_index_504] += gi_208 * Point;
      gda_488[g_index_504] += gi_208 * Point;
      gda_492[g_index_504] += gi_208 * Point;
      gda_496[g_index_504] += gi_208 * Point;
   }
   gd_508 = 5.0 * Point;
   if (Digits == 5 || Digits == 3) gd_508 = 50.0 * Point;
}

int searchprices(string as_0, string as_8) {
   double ld_20;
   bool li_28;
   bool li_ret_16 = FALSE;
   if (as_0 == "B" && as_8 == "O") {
      g_count_500 = 0;
      g_index_504 = 0;
      ld_20 = gi_540;
      while (g_index_504 <= ld_20) {
         g_index_504++;
         g_count_500++;
         li_28 = FALSE;
         if (gi_140 == FALSE && (Bid >= gda_492[g_index_504] && Bid <= gda_492[g_index_504] + gd_508)) li_28 = TRUE;
         if (gi_140 == TRUE && (Bid >= gda_492[g_index_504] - MarketInfo(Symbol(), MODE_SPREAD) * Point && Bid <= gda_492[g_index_504] + gd_508)) li_28 = TRUE;
         if (li_28 == TRUE) {
            gi_476 = g_index_504;
            li_ret_16 = TRUE;
         }
      }
   }
   if (as_0 == "B" && as_8 == "C") {
      g_count_500 = 0;
      g_index_504 = 0;
      ld_20 = gi_540;
      while (g_index_504 <= ld_20) {
         g_index_504++;
         g_count_500++;
         if (Bid >= gda_488[g_index_504] && Bid <= gda_488[g_index_504] + gd_508) {
            gi_476 = g_index_504;
            li_ret_16 = TRUE;
         }
      }
   }
   if (as_0 == "A" && as_8 == "O") {
      g_count_500 = 0;
      g_index_504 = 0;
      ld_20 = gi_540;
      while (g_index_504 <= ld_20) {
         g_index_504++;
         g_count_500++;
         li_28 = FALSE;
         if (gi_140 == FALSE && (Ask <= gda_484[g_index_504] && Ask >= gda_484[g_index_504] - gd_508)) li_28 = TRUE;
         if (gi_140 == TRUE && (Ask <= gda_484[g_index_504] + MarketInfo(Symbol(), MODE_SPREAD) * Point && Ask >= gda_484[g_index_504] - gd_508)) li_28 = TRUE;
         if (li_28 == TRUE) {
            gi_476 = g_index_504;
            li_ret_16 = TRUE;
         }
      }
   }
   if (as_0 == "A" && as_8 == "C") {
      g_count_500 = 0;
      g_index_504 = 0;
      ld_20 = gi_540;
      while (g_index_504 <= ld_20) {
         g_index_504++;
         g_count_500++;
         if (Ask <= gda_496[g_index_504] && Ask >= gda_496[g_index_504] - gd_508) {
            gi_476 = g_index_504;
            li_ret_16 = TRUE;
         }
      }
   }
   if (li_ret_16 == TRUE) {
      gi_544 = MathAbs(gi_476 - gi_516);
      gi_544++;
   }
   return (li_ret_16);
}

void GetCurrentGrid() {
   g_index_504 = 0;
   gi_476 = 0;
   gi_516 = 0;
   bool li_0 = FALSE;
   double ld_4 = gi_540;
   while (g_index_504 <= ld_4) {
      g_index_504++;
      if (Bid > gda_484[g_index_504] && Bid < gda_488[g_index_504]) gi_476 = g_index_504;
      if (gi_476 == 0 && Ask > gda_484[g_index_504] && Ask < gda_488[g_index_504]) gi_476 = g_index_504;
      if (li_0 == FALSE && gda_484[g_index_504] <= gd_348 && gda_488[g_index_504] >= gd_348) {
         gi_516 = g_index_504;
         li_0 = TRUE;
      }
   }
}

void CalculateHighAndLow() {
   g_ihigh_332 = iHigh(Symbol(), PERIOD_MN1, iHighest(Symbol(), PERIOD_MN1, MODE_HIGH, LookBackMonths, 0));
   g_ilow_340 = iLow(Symbol(), PERIOD_MN1, iLowest(Symbol(), PERIOD_MN1, MODE_LOW, LookBackMonths, 0));
   gd_348 = (g_ihigh_332 + g_ilow_340) / 2.0;
   if (FixedCenter != 0.0) gd_348 = FixedCenter;
   gd_348 = NormalizeDouble(gd_348, Digits);
}

int GetFibLevel() {
   int l_count_0 = 0;
   GetCurrentGrid();
   l_count_0 = MathAbs(gi_476 - gi_516);
   l_count_0++;
   return (l_count_0);
}

int GetAddedPips() {
   int li_ret_0 = 0;
   if (gi_204 == FALSE) return (gi_176);
   if (gi_204 == TRUE) {
      if (gi_544 == 1) li_ret_0 = gi_176 + 0;
      if (gi_544 == 2) li_ret_0 = gi_176 + 0;
      if (gi_544 == 3) li_ret_0 = gi_176 + 5;
      if (gi_544 == 4) li_ret_0 = gi_176 + 10;
      if (gi_544 == 5) li_ret_0 = gi_176 + 15;
      if (gi_544 == 6) li_ret_0 = gi_176 + 20;
      if (gi_544 == 7) li_ret_0 = gi_176 + 21;
      if (gi_544 == 8) li_ret_0 = gi_176 + 23;
      if (gi_544 == 9) li_ret_0 = gi_176 + 24;
      if (gi_544 == 10) li_ret_0 = gi_176 + 24;
      if (gi_544 >= 11) li_ret_0 = gi_176 + 25;
      return (li_ret_0);
   }
   return (li_ret_0);
}

void PreSet() {
   CalculateHighAndLow();
   int li_12 = TradeRange / 2;
   double ld_16 = gd_348 - li_12 * Point;
   gi_152 = MaxLevels * 2 + 1;
   gd_144 = ld_16 - MaxLevels * TradeRange * Point;
   gi_516 = MaxLevels + 1;
}

int Decoder() {
   bool li_ret_0 = FALSE;
   bool li_4 = FALSE;
   int l_acc_number_8 = AccountNumber();
   int li_12 = StrToInteger(StringSubstr(gs_104, 1, 2)) - 2;
   int li_16 = StrToInteger(StringSubstr(gs_104, 3, 2)) - 3;
   string l_dbl2str_20 = DoubleToStr(StrToInteger(StringSubstr(gs_104, 5, 2)) - 2, 0);
   string ls_28 = StringSubstr(gs_104, 7, 1);
   string ls_36 = StringSubstr(gs_104, 8, 1);
   string ls_44 = ls_36 + ls_28;
   ls_36 = DoubleToStr(StrToInteger(ls_44) - 2, 0);
   string ls_52 = l_dbl2str_20 + ls_36;
   int l_str2int_60 = StrToInteger(ls_52);
   if (Year() > l_str2int_60 || Month() > li_12 || (Day() > li_16 && Month() >= li_12)) {
      Comment("\n\nExpert Has Expired");
      li_4 = TRUE;
   }
   string ls_64 = StringSubstr(gs_104, 0, 1) + StringSubstr(gs_104, 9, StringLen(gs_104) - 9);
   int li_72 = StrToInteger(ls_64);
   li_72 += 177;
   li_72 /= 3;
   li_72 += 13;
   li_72 /= 2;
   if (!IsDemo() && li_72 == l_acc_number_8 && li_4 == FALSE) li_ret_0 = TRUE;
   if (IsDemo() && li_4 == FALSE) li_ret_0 = TRUE;
   return (li_ret_0);
}

int LookupLiveAccountNumbers() {
   int li_unused_0 = 0;
   if (AccountNumber() == 23454) gi_552 = 1;
   if (AccountNumber() == 28180) gi_552 = 1;
   if (AccountNumber() == 24613) gi_552 = 1;
   if (AccountNumber() == 66576) gi_552 = 1;
   if (AccountNumber() == 97457) gi_552 = 1;
   return (gi_552);
}