/*
   Generated by EX4-TO-MQ4 decompiler V4.0.220.2 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2009, Pro-Fx-Experts"
#property link      ""

extern double Lots = 0.1;
extern double Max_Allowed_Lot = 25.0;
extern int TakeProfit = 25;
extern bool Use_StopLoss = FALSE;
extern int StopLoss = 100;
extern bool Use_Friday_Close = TRUE;
extern string Friday_Close_Time = "20:00";
extern bool Open_New_Orders = TRUE;
int g_slippage_124 = 3;
int g_magic_132;
double g_price_136;
double g_price_144;
double g_price_152;
double g_price_160;
string g_comment_168 = "Money-Grid™";
bool gi_176 = TRUE;

int init() {
   g_magic_132 = MakeMagicNumber(205110);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int li_4;
   int li_8;
   int li_12;
   int li_16;
   int l_price_20;
   int l_price_24;
   double ld_28;
   double ld_36;
   double ld_44;
   double ld_52;
   double ld_60;
   double ld_68;
   double l_spread_76;
   double ld_unused_84;
   double ld_92;
   double ld_100;
   int li_108;
   string l_time2str_112;
   int l_str2time_120;
   int l_ord_total_128;
   int li_0 = 39974;
   if (li_0 == AccountNumber() || IsDemo() || IsTesting()) {
      RefreshRates();
      li_4 = 0;
      li_8 = 0;
      li_12 = 0;
      li_16 = 0;
      l_price_20 = 0;
      l_price_24 = 0;
      ld_28 = 0;
      ld_36 = 0;
      ld_44 = 0;
      ld_52 = 0;
      ld_60 = 0;
      ld_68 = 0;
      l_spread_76 = 0;
      ld_unused_84 = 0;
      ld_92 = 0;
      ld_100 = 0;
      li_4 = CountBuys(g_magic_132);
      li_8 = CountSells(g_magic_132);
      li_12 = CountBuyLimit(g_magic_132);
      li_16 = CountSellLimit(g_magic_132);
      ld_28 = LastBuyLot(g_magic_132);
      ld_36 = LastSellLot(g_magic_132);
      ld_44 = LastBuyLimLot(g_magic_132);
      ld_52 = LastSellLimLot(g_magic_132);
      ld_60 = BuyProfit(g_magic_132);
      ld_68 = SellProfit(g_magic_132);
      l_spread_76 = MarketInfo(Symbol(), MODE_SPREAD);
      li_108 = TakeProfit + l_spread_76;
      l_time2str_112 = TimeToStr(TimeCurrent(), TIME_DATE);
      l_str2time_120 = StrToTime(l_time2str_112 + " " + Friday_Close_Time);
      if ((LastComm(g_magic_132) == "PMG-Sell[tp]" && ld_52 > 2.0 * Lots) || (LastComm(g_magic_132) == "PMG-Buy[tp]" && ld_44 > 2.0 * Lots) || (Use_Friday_Close == TRUE &&
         DayOfWeek() == 5 && TimeCurrent() >= l_str2time_120)) {
         for (int li_124 = 20; li_124 > 0; li_124--) {
            l_ord_total_128 = OrdersTotal();
            for (int l_pos_132 = l_ord_total_128 - 1; l_pos_132 >= 0; l_pos_132--) {
               OrderSelect(l_pos_132, SELECT_BY_POS);
               if (OrderMagicNumber() == g_magic_132 && OrderSymbol() == Symbol()) {
                  if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Yellow);
                  if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Yellow);
                  if (OrderType() == OP_SELLLIMIT) OrderDelete(OrderTicket());
                  if (OrderType() == OP_BUYLIMIT) OrderDelete(OrderTicket());
               }
            }
         }
      }
      if (Use_Friday_Close == TRUE && DayOfWeek() == 5 && TimeCurrent() >= l_str2time_120) Open_New_Orders = FALSE;
      else Open_New_Orders = TRUE;
      if (Open_New_Orders == TRUE) {
         if (Use_StopLoss == TRUE) g_price_136 = Ask - StopLoss * Point;
         else g_price_136 = 0;
         if (li_4 == 0)
            if (li_4 == 0 && !(2.0 * ld_28 > Max_Allowed_Lot)) OrderSend(Symbol(), OP_BUY, Lots, Ask, g_slippage_124, g_price_136, Ask + 50 * TakeProfit * Point, g_comment_168, g_magic_132, 0, Blue);
         if (Use_StopLoss == TRUE) g_price_144 = Bid + StopLoss * Point;
         else g_price_144 = 0;
         if (li_8 == 0 && !(2.0 * ld_36 > Max_Allowed_Lot))
            if (li_8 == 0) OrderSend(Symbol(), OP_SELL, Lots, Bid, g_slippage_124, g_price_144, Bid - 50 * TakeProfit * Point, g_comment_168, g_magic_132, 0, Red);
         ld_100 = ld_36 / (10.0 * Lots);
         l_price_24 = li_108 + NormalizeDouble(li_108 * ld_100, 0);
         ld_92 = ld_28 / (10.0 * Lots);
         l_price_20 = li_108 + NormalizeDouble(li_108 * ld_92, 0);
         if (Use_StopLoss == TRUE) g_price_160 = Bid + (l_price_24 + StopLoss) * Point;
         else g_price_160 = 0;
         if (li_16 == 0 && li_4 > 0 && li_8 > 0 && !(2.0 * ld_36 > Max_Allowed_Lot)) OrderSend(Symbol(), OP_SELLLIMIT, 2.0 * ld_36, Ask + l_price_24 * Point, g_slippage_124, g_price_160, Ask, "PMG-Sell", g_magic_132, 0, DodgerBlue);
         if (Use_StopLoss == TRUE) g_price_152 = Ask - (l_price_20 + StopLoss) * Point;
         else g_price_152 = 0;
         if (li_12 == 0 && li_4 > 0 && li_8 > 0 && !(2.0 * ld_28 > Max_Allowed_Lot)) OrderSend(Symbol(), OP_BUYLIMIT, 2.0 * ld_28, Bid - l_price_20 * Point, g_slippage_124, g_price_152, Bid, "PMG-Buy", g_magic_132, 0, Tomato);
      }
      if (gi_176) {
         if (li_0 == AccountNumber()) {
            Comment("\n                                        " + g_comment_168 + " MagicNumber: " + g_magic_132 + "    Day: " + DayOfWeek() + " CL Time: " + TimeToStr(l_str2time_120, TIME_DATE|TIME_SECONDS) 
               + "\n                                        ---------------------------------------------------------------" 
               + "\n                                        Current Server Time   : " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) 
               + "\n                                        Account Balance         : " + DoubleToStr(NormalizeDouble(AccountBalance(), 2), 2) 
               + "\n                                        Account Equity           : " + DoubleToStr(NormalizeDouble(AccountEquity(), 2), 2) 
               + "\n                                        ---------------------------------------------------------------" 
               + "\n                                        Buy Profit/Loss           : " + DoubleToStr(BuyProfit(g_magic_132), 2) 
               + "\n                                        Sell Profit/Loss            : " + DoubleToStr(SellProfit(g_magic_132), 2) 
               + "\n                                                                         ------------" 
               + "\n                                        Buy+Sell Profit/(Loss) : " + DoubleToStr(BuyProfit(g_magic_132) + SellProfit(g_magic_132), 2) 
               + "\n                                                                         =====" 
               + "\n                                        Open Buy orders        : " + li_4 
               + "\n                                        Open Sell orders         : " + li_8 
               + "\n                                        Startup TakeProfit       : " + TakeProfit 
               + "\n                                        Sell Grid size              : " + DoubleToStr(l_price_24 - l_spread_76, 0) + " pips" 
               + "\n                                        Buy Grid size             : " + DoubleToStr(l_price_20 - l_spread_76, 0) + " pips" 
            + "\n                                        ---------------------------------------------------------------");
         } else {
            Comment("\n                                " + g_comment_168 + "  MagicNumber " + g_magic_132 
               + "\n                                ---------------------------------------------------------------------------------------------" 
               + "\n                                Visit www.pro-fx-experts.com" 
               + "\n                                You are welcom to send this demo Expert Advisor to your friends." 
               + "\n                                Or order your own Live Account version from Pro-Fx-Experts today" 
               + "\n                                by sending us a email to support@pro-fx-experts.com." 
               + "\n                                Get a free quote on your manual trading system at Pro-Fx-Experts." 
            + "\n                                ---------------------------------------------------------------------------------------------");
         }
      }
      ObjectCreate("CC", OBJ_LABEL, 0, 0, 0);
      ObjectSet("CC", OBJPROP_CORNER, 3);
      ObjectSet("CC", OBJPROP_XDISTANCE, 5);
      ObjectSet("CC", OBJPROP_YDISTANCE, 5);
      ObjectSet("CC", OBJPROP_BACK, TRUE);
      ObjectSetText("CC", "Pro-Fx-Experts Copyright © " + Year() + ",  www.Pro-Fx-Experts.com", 8, "Arial", Silver);
      ObjectCreate("HB", OBJ_LABEL, 0, 0, 0);
      ObjectSet("HB", OBJPROP_CORNER, 3);
      ObjectSet("HB", OBJPROP_XDISTANCE, 5);
      ObjectSet("HB", OBJPROP_YDISTANCE, 20);
      ObjectSet("HB", OBJPROP_BACK, TRUE);
      ObjectSetText("HB", "Money-Grid™ V3", 10, "Tahoma Bold", Tomato);
   } else {
      if (li_0 != AccountNumber() || !IsDemo()) {
         Comment("\n " 
            + "\n " 
            + "\n  ==================================" 
            + "\n  Account No. " + AccountNumber() + " invalid." 
            + "\n  ==================================" 
            + "\n  Your Account no. is not valid for trading with this expert" 
            + "\n  advisor. Only a valid Live account or a Demo account is" 
            + "\n  allowed. Please contact Nico Roets at Pro-Fx-Experts.com" 
            + "\n  to purchase your own lifetime Live account licence." 
            + "\n  For more information e-mail: support@pro-fx-experts.com" 
         + "\n  ==================================");
         return (0);
      }
   }
   return (0);
}

int CountBuys(int a_magic_0) {
   int l_count_4 = 0;
   int l_ord_total_12 = OrdersTotal();
   for (int l_pos_8 = 0; l_pos_8 < l_ord_total_12; l_pos_8++) {
      OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != a_magic_0) continue;
      if (OrderType() == OP_BUY) l_count_4++;
   }
   return (l_count_4);
}

int CountSells(int a_magic_0) {
   int l_count_4 = 0;
   int l_ord_total_12 = OrdersTotal();
   for (int l_pos_8 = 0; l_pos_8 < l_ord_total_12; l_pos_8++) {
      OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != a_magic_0) continue;
      if (OrderType() == OP_SELL) l_count_4++;
   }
   return (l_count_4);
}

int CountBuyLimit(int a_magic_0) {
   int li_ret_4;
   for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
      OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderType() == OP_BUYLIMIT && OrderMagicNumber() == a_magic_0) li_ret_4++;
   }
   return (li_ret_4);
}

int CountSellLimit(int a_magic_0) {
   int li_ret_4;
   for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
      OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderType() == OP_SELLLIMIT && OrderMagicNumber() == a_magic_0) li_ret_4++;
   }
   return (li_ret_4);
}

int MakeMagicNumber(int ai_0) {
   int li_4 = 0;
   int li_ret_8 = 0;
   if (Symbol() == "AUDCAD" || Symbol() == "AUDCADm") li_4 = 1000;
   else {
      if (Symbol() == "AUDJPY" || Symbol() == "AUDJPYm") li_4 = 2000;
      else {
         if (Symbol() == "AUDNZD" || Symbol() == "AUDNZDm") li_4 = 3000;
         else {
            if (Symbol() == "AUDUSD" || Symbol() == "AUDUSDm") li_4 = 4000;
            else {
               if (Symbol() == "CHFJPY" || Symbol() == "CHFJPYm") li_4 = 5000;
               else {
                  if (Symbol() == "EURAUD" || Symbol() == "EURAUDm") li_4 = 6000;
                  else {
                     if (Symbol() == "EURCAD" || Symbol() == "EURCADm") li_4 = 7000;
                     else {
                        if (Symbol() == "EURCHF" || Symbol() == "EURCHFm") li_4 = 8000;
                        else {
                           if (Symbol() == "EURGBP" || Symbol() == "EURGBPm") li_4 = 9000;
                           else {
                              if (Symbol() == "EURJPY" || Symbol() == "EURJPYm") li_4 = 2100;
                              else {
                                 if (Symbol() == "EURUSD" || Symbol() == "EURUSDm") li_4 = 1100;
                                 else {
                                    if (Symbol() == "GBPCHF" || Symbol() == "GBPCHFm") li_4 = 1200;
                                    else {
                                       if (Symbol() == "GBPJPY" || Symbol() == "GBPJPYm") li_4 = 1300;
                                       else {
                                          if (Symbol() == "GBPUSD" || Symbol() == "GBPUSDm") li_4 = 1400;
                                          else {
                                             if (Symbol() == "NZDJPY" || Symbol() == "NZDJPYm") li_4 = 1500;
                                             else {
                                                if (Symbol() == "NZDUSD" || Symbol() == "NZDUSDm") li_4 = 1600;
                                                else {
                                                   if (Symbol() == "USDCAD" || Symbol() == "USDCADm") li_4 = 1700;
                                                   else {
                                                      if (Symbol() == "USDCHF" || Symbol() == "USDCHFm") li_4 = 1800;
                                                      else
                                                         if (Symbol() == "USDJPY" || Symbol() == "USDJPYm") li_4 = 1900;
                                                   }
                                                }
                                             }
                                          }
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
   li_ret_8 = ai_0 + li_4;
   return (li_ret_8);
}

double LastBuyLot(int a_magic_0) {
   int l_ord_total_4 = OrdersTotal();
   int l_datetime_8 = 0;
   int l_datetime_12 = 0;
   double l_ord_lots_16 = 0;
   for (int l_pos_24 = 0; l_pos_24 < l_ord_total_4; l_pos_24++) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      l_datetime_8 = OrderOpenTime();
      if (l_datetime_8 > l_datetime_12 && OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT && OrderMagicNumber() == a_magic_0) {
         l_datetime_12 = l_datetime_8;
         l_ord_lots_16 = OrderLots();
      }
   }
   return (l_ord_lots_16);
}

double LastSellLot(int a_magic_0) {
   int l_ord_total_4 = OrdersTotal();
   int l_datetime_8 = 0;
   int l_datetime_12 = 0;
   double l_ord_lots_16 = 0;
   for (int l_pos_24 = 0; l_pos_24 < l_ord_total_4; l_pos_24++) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      l_datetime_8 = OrderOpenTime();
      if (l_datetime_8 > l_datetime_12 && OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT && OrderMagicNumber() == a_magic_0) {
         l_datetime_12 = l_datetime_8;
         l_ord_lots_16 = OrderLots();
      }
   }
   return (l_ord_lots_16);
}

double LastBuyLimLot(int a_magic_0) {
   int l_ord_total_4 = OrdersTotal();
   int l_datetime_8 = 0;
   int l_datetime_12 = 0;
   double l_ord_lots_16 = 0;
   for (int l_pos_24 = 0; l_pos_24 < l_ord_total_4; l_pos_24++) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      l_datetime_8 = OrderOpenTime();
      if (l_datetime_8 > l_datetime_12 && OrderType() == OP_BUYLIMIT && OrderMagicNumber() == a_magic_0) {
         l_datetime_12 = l_datetime_8;
         l_ord_lots_16 = OrderLots();
      }
   }
   return (l_ord_lots_16);
}

double LastSellLimLot(int a_magic_0) {
   int l_ord_total_4 = OrdersTotal();
   int l_datetime_8 = 0;
   int l_datetime_12 = 0;
   double l_ord_lots_16 = 0;
   for (int l_pos_24 = 0; l_pos_24 < l_ord_total_4; l_pos_24++) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      l_datetime_8 = OrderOpenTime();
      if (l_datetime_8 > l_datetime_12 && OrderType() == OP_SELLLIMIT && OrderMagicNumber() == a_magic_0) {
         l_datetime_12 = l_datetime_8;
         l_ord_lots_16 = OrderLots();
      }
   }
   return (l_ord_lots_16);
}

string LastComm(int a_magic_0) {
   int l_hist_total_4 = OrdersHistoryTotal();
   int l_datetime_8 = 0;
   int l_datetime_12 = 0;
   string ls_ret_16 = "0";
   for (int l_pos_24 = 0; l_pos_24 < l_hist_total_4; l_pos_24++) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_HISTORY);
      l_datetime_8 = OrderCloseTime();
      if (l_datetime_8 > l_datetime_12 && OrderMagicNumber() == a_magic_0) {
         l_datetime_12 = l_datetime_8;
         ls_ret_16 = OrderComment();
      }
   }
   return (ls_ret_16);
}

double BuyProfit(int a_magic_0) {
   double ld_ret_4 = 0;
   for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == a_magic_0)
         if (OrderType() == OP_BUY) ld_ret_4 += OrderProfit() + OrderCommission() + OrderSwap();
   }
   return (ld_ret_4);
}

double SellProfit(int a_magic_0) {
   double ld_ret_4 = 0;
   for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == a_magic_0)
         if (OrderType() == OP_SELL) ld_ret_4 += OrderProfit() + OrderCommission() + OrderSwap();
   }
   return (ld_ret_4);
}