#property copyright "Copyright © 2004, by wellx"
#property link      "http://www.metaquotes.net"

extern int Equals = 4;
extern int nLeftUp = 1;
extern int nRightUp = 1;
extern int nLeftDown = 1;
extern int nRightDown = 1;

double FractalsUp[];
double FractalsDown[];

int pos = 0, cntup = 0, cntdown = 0, cnt = 0;
int r = 0, l = 0, e = 0;
int fup = 0, fdown = 0;

extern int       EMA1=5;
extern int       Shift1=0;
extern int       EMA2=8;
extern int       Shift2=0;
extern int       EMA3=13;
extern int       Shift3=0;

/*extern int       FastEMA=21;
extern int       FastShift=0;
extern int       SlowEMA=34;
extern int       SlowShift=0;*/

extern int       FastEMA=8;
extern int       FastShift=0;
extern int       SlowEMA=13;
extern int       SlowShift=0;


extern int dDay;
extern string tTime;

int init()
  {
   cntup = nLeftUp + nRightUp + Equals + 1;
   cntdown = nLeftDown + Equals + 1;
   if(cntup >= cntdown) 
       cnt = cntup;
   if(cntup < cntdown)  
       cnt = cntdown; 

   return(0);
  }

int deinit()
  {
   return(0);
  }

int start()
  {
   int i=0, j=0;
   int cbars = 10;
   
   bool iBUY=false;
   bool iSELL=false;
   double mBUY = 0;
   double mSELL = 0;
   int total,cnt;
   double prof;

   if(cbars < 0) 
       return(-1);

   if(cbars > 0) 
       cbars--;
   pos = 0;

   if(cbars > (Bars - cnt - 1)) 
       pos =(Bars - cnt - 1);
   else 
       pos = Bars - (cbars + nRightUp);

   while(pos >= nRightUp)
     {
       FractalsUp[pos] = NULL;
       FractalsDown[pos] = NULL;     
       //фРАКТАЛ ВВЕРХ
       r = nRightUp; //проверяем правую сторону фрактала
       //----
       for(i = 1; i <= r; i++)
         {
           if(High[pos] <= High[pos-i]) 
               break;
         }
       //если справа все ОК то i должно быть равно r+1
       if(i == r + 1) //FractalsUp[pos]=High[pos];
         {
           l = nLeftUp;  //проверяем левую сторону фрактала
           e = Equals;
           //----
           for(j = 1; j <= l + Equals; j++)
             {     
               if(High[pos] < High[pos+j]) 
                   break;
               //----
               if(High[pos] > High[pos+j]) 
                   l--;
               //----
               if(High[pos] == High[pos+j])
                   e--;
               //----
               if(l == 0) 
                 {
                   FractalsUp[pos] = High[pos];
                   iBUY = true;
                   mBUY = pos;
                   break;
                 }
               //----
               if(e < 0) 
                   break;
             }
         }
       //ФРАКТАЛ ВНИЗ
       r = nRightDown; //проверяем правую сторону фрактала
       //----
       for(i = 1; i <= r; i++)
         {
           if(Low[pos] >= Low[pos-i]) 
               break;
         }     
       //----
       if(i == r + 1) //FractalsUp[pos]=High[pos];
         {
           l = nLeftDown;  //проверяем левую сторону фрактала
           e = Equals;
           //----
           for(j = 1; j <= l + Equals; j++)
             {      
               if(Low[pos] > Low[pos+j]) 
                   break;
               //----
               if(Low[pos] < Low[pos+j]) 
                   l--;
               //----
               if(Low[pos] == Low[pos+j])
                   e--;
               //----
               if(l == 0) 
                 {
                   FractalsDown[pos] = Low[pos];
                   iSELL = true;
                   mSELL = pos;
                   break;
                 }
               if(e < 0) 
                   break;
             }
         }
       pos--;
     }

     double sl = 0.0015;      
     double dPrice = 0.0015;
     
     //if((Hour() >= 9) && (Hour() <= 19))
     
     double RSI;
     RSI = iRSI(NULL,0,7,PRICE_CLOSE,0);
     
     //if((RSI <= 30) || (RSI >= 70))
     if((Hour() >= 8) && (Hour() <= 18) && (dDay != Day()))
     {
         if(OrdersTotal() <= 1) 
         {
             if(iBUY && iSELL)
             {
                 if(mBUY > mSELL) // BUY
                 {
                     total = OrdersTotal();
                     for(cnt=0;cnt<total;cnt++)
                     {  
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
                        if(OrderType()==OP_SELL)
                        {
                           OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);   
                        }
                     }
                      OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+sl,"",2,0,Blue);
                      dDay = Day();
                 }
      
                 if(mBUY < mSELL) // SELL
                 {
                     total = OrdersTotal();
                     for(cnt=0;cnt<total;cnt++)
                     {  
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
                        if(OrderType()==OP_BUY)
                        {
                           OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
                        }
                     }
                      OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-sl,"",1,0,Red);
                      dDay = Day();
                 }    
             }
             if(iBUY && !iSELL)
             {
                     total = OrdersTotal();
                     for(cnt=0;cnt<total;cnt++)
                     {  
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
                        if(OrderType()==OP_SELL)
                        {
                           OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);   
                        }
                     }
                OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+sl,"",2,0,Blue);
                dDay = Day();
             }
             if(!iBUY && iSELL)
             {
                     total = OrdersTotal();
                     for(cnt=0;cnt<total;cnt++)
                     {  
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
                        if(OrderType()==OP_BUY)
                        {
                           OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
                        }
                     }
                OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-sl,"",1,0,Red);
                dDay = Day();
             }
         }
     }

      /*if(OrdersTotal() != 0)
      {
         total = OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
         {  
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
            prof = OrderProfit() / 10;
            if(prof >= 4)
            {
               if(OrderType()==OP_BUY)
               {
                  OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
               }
               if(OrderType()==OP_SELL)
               {
                  OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
               }
            }
         }
      }

      /*if(OrdersTotal() == 1)
      {
         total = OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
         {  
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);                     
            if(OrderType()==OP_BUYSTOP)
            {
               OrderDelete(OrderTicket());
            }
            if(OrderType()==OP_SELLSTOP)
            {
               OrderDelete(OrderTicket());
            }
         }
      }*/

     return(0);
  }


