/*
   Generated by EX4-TO-MQ4 decompiler V4.0.220.2c []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2007, Yury V. Reshetov ICQ:282715499  http://reshetov.xnet.uz/"
#property link      "http://reshetov.xnet.uz/"

extern int x1 = 120;
extern int x2 = 172;
extern int x3 = 39;
extern int x4 = 172;
extern double tp = 50.0;
extern double sl = 50.0;
extern double lots = 0.1;
extern bool moneymanagement = FALSE;
extern int MagicNumber = 888;
int gi_124 = 0;
int gi_unused_128 = 3;

int init() {
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   double ld_12;
   if (Time[0] == gi_124) return (0);
   gi_124 = Time[0];
   if (IsTradeAllowed()) {
      RefreshRates();
      gi_unused_128 = MarketInfo(Symbol(), MODE_SPREAD);
   } else {
      gi_124 = Time[1];
      return (0);
   }
   int l_ticket_0 = -1;
   int l_ord_total_4 = OrdersTotal();
   for (int l_pos_8 = l_ord_total_4 - 1; l_pos_8 >= 0; l_pos_8--) {
      OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) return (0);
   }
   lots = getlots();
   if (lots < 0.01) {
      ld_12 = MarketInfo(Symbol(), MODE_SPREAD);
      OrderSend(Symbol(), OP_BUYSTOP, 1, Ask + 1000.0 * (Point * ld_12), 3, 0, 0, "NeuroFilter", MagicNumber, Time[0] - 1 + 60 * Period(), Blue);
   }
   RefreshRates();
   if (perceptron() > 0.0) {
      l_ticket_0 = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - sl * Point, Bid + tp * Point, "NeuroFilter", MagicNumber, 0, Blue);
      if (l_ticket_0 < 0) {
         Sleep(30000);
         gi_124 = Time[1];
      }
   } else {
      l_ticket_0 = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + sl * Point, Ask - tp * Point, "NeuroFilter", MagicNumber, 0, Red);
      if (l_ticket_0 < 0) {
         Sleep(30000);
         gi_124 = Time[1];
      }
   }
   return (0);
}

double getlots() {
   double ld_0 = 1;
   if (IsOptimization()) return (0.1);
   if (!moneymanagement) return (lots);
   int l_hist_total_8 = OrdersHistoryTotal();
   double ld_12 = 0;
   for (int l_pos_20 = l_hist_total_8 - 1; l_pos_20 >= 0; l_pos_20--) {
      OrderSelect(l_pos_20, SELECT_BY_POS, MODE_HISTORY);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY || OrderType() == OP_SELL) ld_12 += OrderProfit() / OrderLots();
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUYSTOP) break;
   }
   double ld_ret_24 = NormalizeDouble(MathFloor(ld_12 / 200.0) / 10.0, 1) * ld_0 + 0.5;
   double l_maxlot_32 = MarketInfo(Symbol(), MODE_MAXLOT);
   if (ld_ret_24 > l_maxlot_32) ld_ret_24 = l_maxlot_32;
   return (ld_ret_24);
}

double perceptron() {
   double ld_0 = x1 - 100.0;
   double ld_8 = x2 - 100.0;
   double ld_16 = x3 - 100.0;
   double ld_24 = x4 - 100.0;
   double ld_32 = Close[0] - Open[7];
   double ld_40 = Open[7] - Open[14];
   double ld_48 = Open[14] - Open[21];
   double ld_56 = Open[21] - Open[28];
   return (ld_0 * ld_32 + ld_8 * ld_40 + ld_16 * ld_48 + ld_24 * ld_56);
}
