//+------------------------------------------------------------------+
//|                                              NeuroMACDwithMM.mq4 |
//|                               Copyright © 2007, Yury V. Reshetov |
//|                                http://bigforex.biz/load/2-1-0-80 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Yury V. Reshetov"
#property link      "http://bigforex.biz/load/2-1-0-80"

//---- input parameters

extern int          x1 = 70;
extern int          x2 = 77;
extern int          x3 = 109;
extern int          x4 = 52;
extern double       tp = 20;
extern double       sl = 70;
extern double       MaximumRisk = 0.99;
extern int          mn = 888;
static int          prevtime = 0;
static double       minlot = 0.1;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   minlot = MarketInfo(Symbol(), MODE_MINLOT);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

   if (Time[0] == prevtime) return(0);
   prevtime = Time[0];

   int total = OrdersTotal();
   for (int i = 0; i < total; i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == mn) {
         return(0);
      }
   }
   
   double main = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);
   double signal = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_SIGNAL, 0);
   
   double lt = minlot;
   
   if (main > 0 && signal > main) {
      if (perceptron() > 0) lt = getLots();
      OrderSend(Symbol(), OP_SELL, lt, Bid, 3, Ask + sl * Point, Ask - tp * Point, "MACDSimple", mn, 0, Red); 
   }
   if (main < 0 && signal < main) {
      if (perceptron() < 0) lt = getLots();
      OrderSend(Symbol(), OP_BUY, lt, Ask, 3, Bid - sl * Point, Bid + tp * Point, "MACDSimple", mn, 0, Blue); 
   }
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double getLots() {
   int round = MathAbs(MathLog(minlot) / MathLog(10.0)) + 0.5;
   double lot = minlot;
//---- select lot size
   lot = NormalizeDouble(AccountFreeMargin() * MaximumRisk / 1000.0, round);
   if (AccountFreeMargin() < lot * MarketInfo(Symbol(), MODE_MARGINREQUIRED)) {
      lot = NormalizeDouble(AccountFreeMargin() / MarketInfo(Symbol(), MODE_MARGINREQUIRED), round);
   }
   if(lot < minlot) lot = minlot;
   double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
   if(lot > maxlot) lot = maxlot;
//---- return lot size
   return(lot);
}

double perceptron()   {
   double       w1 = x1 - 100;
   double       w2 = x2 - 100;
   double       w3 = x3 - 100;
   double       w4 = x4 - 100;
   double a1 = Close[0] - Open[7];
   double a2 = Open[7] - Open[14];
   double a3 = Open[14] - Open[21];
   double a4 = Open[21] - Open[28];
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
  }

