
#property copyright "CURRENCY METER"
#property link      "currencymeter.com"

bool gi_76 = TRUE;
bool gi_80 = TRUE;
int gi_84 = 125729;
string gs_88 = "2009.12.30";
string gs_96 = "2008.08.01";
extern string Remark1 = "== Main Settings ==";
extern int MagicNumber = 5000;
extern bool SignalsOnly = FALSE;
extern bool Alerts = FALSE;
extern bool SignalMail = FALSE;
extern bool PlaySounds = FALSE;
extern bool EachTickMode = TRUE;
extern bool ReverseSignals = FALSE;
extern double Lots = 0.1;
extern bool MoneyManagement = FALSE;
extern int Risk = 0;
extern int Slippage = 5;
extern bool UseStopLoss = TRUE;
extern int StopLoss = 50;
extern bool UseTakeProfit = TRUE;
extern int TakeProfit = 60;
extern bool UseTrailingStop = FALSE;
extern int TrailingStop = 30;
extern bool MoveStopOnce = TRUE;
extern int MoveStopWhenPrice = 40;
extern int MoveStopTo = 5;
int gi_196;
int gi_200;
string gs_204 = "4-Digit Broker";
double gd_212 = 1.0;
int gi_220;
bool gi_224 = FALSE;

int init() {
   gi_196 = Bars;
   gi_200 = Bars;
   if (Digits == 3 || Digits == 5) {
      gs_204 = "5-Digit Broker";
      gd_212 = 10;
   }
   if (EachTickMode) gi_220 = 0;
   else gi_220 = 1;
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int li_8;
   double ld_12;
   double ld_20;
   //if (CheckV()) return (0);
   int li_0 = 0;
   if (EachTickMode && Bars != gi_200) gi_224 = FALSE;
   int li_4 = OrdersTotal();
   li_0 = 0;
   if (MoneyManagement) {
      if (Risk < 1 || Risk > 100) {
         Comment("Invalid Risk Value.");
         return (0);
      }
      Lots = MathFloor(100.0 * (AccountFreeMargin() * AccountLeverage() * Risk * Point * gd_212) / (Ask * MarketInfo(Symbol(), MODE_LOTSIZE) * MarketInfo(Symbol(), MODE_MINLOT))) * MarketInfo(Symbol(), MODE_MINLOT);
   }
   double ld_28 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, gi_220 + 0);
   double ld_36 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, gi_220 + 1);
   double ld_44 = iMA(NULL, 0, 7, 0, MODE_EMA, PRICE_CLOSE, gi_220 + 0);
   double ld_52 = iMA(NULL, 0, 7, 0, MODE_EMA, PRICE_CLOSE, gi_220 + 1);
   double ld_60 = iMA(NULL, 0, 12, 0, MODE_EMA, PRICE_CLOSE, gi_220 + 0);
   double ld_68 = iMA(NULL, 0, 12, 0, MODE_EMA, PRICE_CLOSE, gi_220 + 1);
   string ls_76 = "None";
   if (ld_28 > ld_60 && ld_36 <= ld_68) ls_76 = "Open Long";
   if (ld_28 < ld_60 && ld_36 >= ld_68) ls_76 = "Open Short";
   if (ReverseSignals && ls_76 == "Open Long") ls_76 = "Open Short";
   if (ReverseSignals && ls_76 == "Open Short") ls_76 = "Open Long";
   string ls_84 = "None";
   if (ld_28 < ld_44) ls_84 = "Close Long";
   if (ld_28 > ld_60) ls_84 = "Close Short";
   if (ReverseSignals && ls_84 == "Close Long") ls_84 = "Close Short";
   if (ReverseSignals && ls_84 == "Close Short") ls_84 = "Close Long";
   bool li_92 = FALSE;
   for (int li_96 = 0; li_96 < li_4; li_96++) {
      OrderSelect(li_96, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         li_92 = TRUE;
         if (OrderType() == OP_BUY) {
            if (!UseTakeProfit && ls_84 == "Close Long") li_0 = 3;
            if (li_0 == 3 && (EachTickMode && !gi_224) || (!EachTickMode && Bars != gi_200)) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) gi_200 = Bars;
               li_92 = FALSE;
            } else {
               if (MoveStopOnce && MoveStopWhenPrice > 0) {
                  if (Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice) {
                     if (OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo) {
                        OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red);
                        if (!(!EachTickMode)) continue;
                        gi_200 = Bars;
                        continue;
                     }
                  }
               }
               if (UseTrailingStop && TrailingStop > 0) {
                  if (Bid - OrderOpenPrice() > Point * TrailingStop) {
                     if (OrderStopLoss() < Bid - Point * TrailingStop) {
                        OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                        if (!EachTickMode) gi_200 = Bars;
                     }
                  }
               }
            }
         } else {
            if (!UseTakeProfit && ls_84 == "Close Short") li_0 = 4;
            if (li_0 == 4 && (EachTickMode && !gi_224) || (!EachTickMode && Bars != gi_200)) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) gi_200 = Bars;
               li_92 = FALSE;
            } else {
               if (MoveStopOnce && MoveStopWhenPrice > 0) {
                  if (OrderOpenPrice() - Ask >= Point * MoveStopWhenPrice) {
                     if (OrderStopLoss() > OrderOpenPrice() - Point * MoveStopTo) {
                        OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopTo, OrderTakeProfit(), 0, Red);
                        if (!(!EachTickMode)) continue;
                        gi_200 = Bars;
                        continue;
                     }
                  }
               }
               if (UseTrailingStop && TrailingStop > 0) {
                  if (OrderOpenPrice() - Ask > Point * TrailingStop) {
                     if (OrderStopLoss() > Ask + Point * TrailingStop || OrderStopLoss() == 0.0) {
                        OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                        if (!EachTickMode) gi_200 = Bars;
                     }
                  }
               }
            }
         }
      }
   }
   if (ls_76 == "Open Long") li_0 = 1;
   if (ls_76 == "Open Short") li_0 = 2;
   if (li_0 == 1 && (EachTickMode && !gi_224) || (!EachTickMode && Bars != gi_196)) {
      if (SignalsOnly) {
         if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
         if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
         if (PlaySounds) PlaySound("alert.wav");
      }
      if (!li_92 && !SignalsOnly) {
         if (AccountFreeMargin() < 1000.0 * Lots) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return (0);
         }
         if (UseStopLoss) ld_12 = Ask - StopLoss * Point;
         else ld_12 = 0.0;
         if (UseTakeProfit) ld_20 = Ask + TakeProfit * Point;
         else ld_20 = 0.0;
         li_8 = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, ld_12, ld_20, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
         if (li_8 > 0) {
            if (OrderSelect(li_8, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("BUY order opened : ", OrderOpenPrice());
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
               if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + "Buy Signal");
               if (PlaySounds) PlaySound("alert.wav");
            } else Print("Error opening BUY order : ", GetLastError());
         }
         if (EachTickMode) gi_224 = TRUE;
         if (!EachTickMode) gi_196 = Bars;
         return (0);
      }
   }
   if (li_0 == 2 && (EachTickMode && !gi_224) || (!EachTickMode && Bars != gi_196)) {
      if (SignalsOnly) {
         if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
         if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
         if (PlaySounds) PlaySound("alert.wav");
      }
      if (!li_92 && !SignalsOnly) {
         if (AccountFreeMargin() < 1000.0 * Lots) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return (0);
         }
         if (UseStopLoss) ld_12 = Bid + StopLoss * Point;
         else ld_12 = 0.0;
         if (UseTakeProfit) ld_20 = Bid - TakeProfit * Point;
         else ld_20 = 0.0;
         li_8 = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, ld_12, ld_20, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
         if (li_8 > 0) {
            if (OrderSelect(li_8, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("SELL order opened : ", OrderOpenPrice());
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
               if (Alerts) Alert("[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + "Sell Signal");
               if (PlaySounds) PlaySound("alert.wav");
            } else Print("Error opening SELL order : ", GetLastError());
         }
         if (EachTickMode) gi_224 = TRUE;
         if (!EachTickMode) gi_196 = Bars;
         return (0);
      }
   }
   if (!EachTickMode) gi_200 = Bars;
   return (0);
}

/*bool CheckV() {
   int li_0 = TimeCurrent();
   int li_4 = StrToTime(gs_88);
   int li_8 = StrToTime(gs_96);
   if (li_0 >= li_4 || li_0 <= li_8) {
      Comment(" V5 Robot has been Expired. Please contact Currencymeter.com to renew.");
      ObjectsDeleteAll();
      return (TRUE);
   }
   if (!gi_76 && IsDemo() && gi_80 == TRUE) {
      Comment("Cannot run on demo account");
      return (TRUE);
   }
   if (!IsDemo() && AccountNumber() != gi_84) {
      Comment("F2");
      return (TRUE);
   }
   if (IsDemo() && AccountNumber() != gi_84 && gi_80 == TRUE) {
      Comment("Wrong account Number. Robot has been disabled");
      return (TRUE);
   }
   Comment("");
   return (FALSE);
}*/