//+------------------------------------------------------------------+
//|                                                     N0_NO_N0.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                                                  |
//|                                                     Modyfied by: |
//|                                                        Leonid533 |
//|                                                           NoName |
//+------------------------------------------------------------------+

#property copyright "Copyright й 2006"
#property link      "http://reshetov.xnet.uz/"
//---- input parameters
extern int     Stochastic_period=6;
extern int     slowing=3;
extern int    x0 = 88;
extern int    x1 = 128;
extern int    x2 = 102;
extern int    x3 = 40;
extern int    x4 = 98;
extern int    x5 = 106;
// StopLoss level
extern double sl = 85;

//extern double lots = 0.1;
extern int    MagicNumber = 188;
static int prevtime = 0;
static int spread = 3;
//---координаты точек------------
extern int      P_0 =0;
extern int      P_1 =7;
extern int      P_2 =23;
extern int      P_3 =14;
extern int      P_4 =16;
extern int      P_5 =21;

//-- Подключаемые модули --
#include  <b-Lots.mqh>  //Переменный лот


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  { 
  Comment(perceptron());
   if(Time[0] == prevtime) 
       return(0);
   prevtime = Time[0];
//----
   if(IsTradeAllowed()) 
     {
       spread = MarketInfo(Symbol(), MODE_SPREAD);
     } 
   else 
     {
       prevtime = Time[1];
       return(0);
     }
   int ticket = -1;
   // check for opened position
   int total = OrdersTotal();   
   for(int i = 0; i < total; i++) 
     {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES); 
       // check for symbol & magic number
       if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) 
         {
           int prevticket = OrderTicket();
           // long position is opened
           if(OrderType() == OP_BUY) 
             {
               // check profit 
               if(Bid > (OrderStopLoss() + (sl  + spread) * Point)) 
                 {               
                   if(perceptron() < 0) 
                     { 
                       // reverse
                       if (!OrderClose (prevticket, OrderLots(), Bid, 2, Blue) ) 
                       {Print("Ошибка закрытия ордера #", GetLastError());}
                       else {
                       Lots=GetSizeLot();
                       ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 
                                          Ask + sl * Point, 0, "AI", MagicNumber, 
                                          0, Red); 
                       }
                       
                      /*                    
                       Sleep(30000);
                       if(ticket < 0) 
                         {
                           prevtime = Time[1];
                         } 
                       else 
                         {
                           OrderCloseBy(ticket, prevticket, Blue);   
                         }
                       */
                     } 
                   else 
                     { 
                       // trailing stop
                       if(!OrderModify(OrderTicket(), OrderOpenPrice(), 
                          Bid - sl * Point, 0, 0, Blue)) 
                         {
                           Sleep(30000);
                           prevtime = Time[1];
                         }
                     }
                 }  
               // short position is opened
             } 
           else 
             {
               // check profit 
               if(Ask < (OrderStopLoss() - (sl + spread) * Point)) 
                 {
                   if(perceptron() > 0) 
                     { 
                       // reverse
                       if (!OrderClose (prevticket, OrderLots(), Ask, 2, Blue) ) 
                       {Print("Ошибка закрытия ордера #", GetLastError());}
                       else {
                       Lots=GetSizeLot();
                       ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 
                                          Bid - sl * Point, 0, "AI", MagicNumber,
                                          0, Blue);
                       }                   
                       /*                    
                       Sleep(30000);
                       if(ticket < 0) 
                         {
                           prevtime = Time[1];
                         } 
                       else 
                         {
                           OrderCloseBy(ticket, prevticket, Blue);   
                         }
                        */
                     } 
                   else 
                     { 
                       // trailing stop
                       if(!OrderModify(OrderTicket(), OrderOpenPrice(), 
                          Ask + sl * Point, 0, 0, Blue)) 
                         {
                           Sleep(30000);
                           prevtime = Time[1];
                         }  
                     }
                 }  
             }
           // exit
           return(0);
         }
     }
   // check for long or short position possibility
   if(perceptron() > 0) 
     { 
       //long
       Lots=GetSizeLot();
       ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Bid - sl * Point, 0, 
                      "ST", MagicNumber, 0, Blue); 
       if(ticket < 0) 
         {
           Sleep(30000);
           prevtime = Time[1];
         }
     } 
   else 
     { 
       // short
       Lots=GetSizeLot();
       ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Ask + sl * Point, 0, 
                      "ST", MagicNumber, 0, Red); 
       if(ticket < 0) 
         {
           Sleep(30000);
           prevtime = Time[1];
         }
     }
//--- exit
   return(0);
  }
//+------------------------------------------------------------------+
//|  The PERCEPRRON - a perceiving and recognizing function          |
//+------------------------------------------------------------------+
double perceptron() 
  {
     double w0 = x0 - 100;
   double w1 = x1 - 100;
   double w2 = x2 - 100;
   double w3 = x3 - 100;
   double w4 = x4 - 100;
   double w5 = x5 - 100;
    double a0 =  iStochastic(NULL,0,  Stochastic_period, 3,  slowing, MODE_SMA, 0, MODE_MAIN, P_0)- 50;
   double a1 =  iStochastic(NULL,0,  Stochastic_period, 3,  slowing, MODE_SMA, 0, MODE_MAIN, P_1)- 50;
   double a2 =  iStochastic(NULL,0,  Stochastic_period, 3, slowing, MODE_SMA, 0, MODE_MAIN, P_2)- 50;
   double a3 =  iStochastic(NULL,0,  Stochastic_period, 3, slowing, MODE_SMA, 0, MODE_MAIN, P_3) - 50;
   double a4 =  iStochastic(NULL,0,  Stochastic_period, 3,  slowing, MODE_SMA, 0, MODE_MAIN, P_4) - 50;
   double a5 =  iStochastic(NULL,0,  Stochastic_period, 3,  slowing, MODE_SMA, 0, MODE_MAIN, P_5) - 50;
   return(w0 * a0+ w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4 + w5 * a5);
  }


//+------------------------------------------------------------------+}------------------+