#property copyright "joinme"
#property link      "joinme@yandex.ru"


extern int dDay = 0;
extern int TF = PERIOD_H4;

int init()
{
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   double Al1,Al2,Al3;
   double Kuj;
   double Ten;
   double SpanA,SpanB;
   double SAR;

   Kuj = iIchimoku(NULL, TF, 12, 24, 120, MODE_KIJUNSEN, 0);
   Ten = iIchimoku(NULL, TF, 12, 24, 120, MODE_TENKANSEN, 0);
   SpanA = iIchimoku(NULL, TF, 12, 24, 120, MODE_SENKOUSPANA, 0);
   SpanB = iIchimoku(NULL, TF, 12, 24, 120, MODE_SENKOUSPANB, 0);

   Al1 = iAlligator(NULL,TF,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
   Al2 = iAlligator(NULL,TF,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
   Al3 = iAlligator(NULL,TF,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
   
   SAR = iSAR(NULL, TF, 0.02, 0.2,0);


   if((Al1 > Al2) && (Al2 > Al3) && (Kuj < Ten) && (SAR < Close[0]))
   {
      MTBuy();
   }
   else if((Al1 < Al2) && (Al2 < Al3) && (Kuj > Ten) && (SAR > Close[0]))
   {
      MTSell();
   }

   int total,cnt;
   
   if(SAR < Close[0])
   {
      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);
         }
      }      
   }

   if(SAR > Close[0])
   {
      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);
         }
      }      
   }

   return(0);
}

int MTBuy()
{
   int total,cnt;

   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);
      }
   }
   
   if((OrdersTotal() == 0) && (dDay != Day()))
   {
      OrderSend(Symbol(),OP_BUY,1,Ask,3,0,0,"",4444,0,Blue);
      dDay = Day();
   }
   
      //if((SpanA < SpanB) && (MathAbs(SpanA - SpanB) > 0.0010))
         

   return(0);
}

int MTSell()
{
   int total,cnt;

   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);            
      }
   }
   
   if((OrdersTotal() == 0) && (dDay != Day()))
   {
      OrderSend(Symbol(),OP_SELL,1,Bid,3,0,0,"",4444,0,Red);
      dDay = Day();
   }

      //if((SpanA > SpanB) && (MathAbs(SpanA - SpanB) > 0.0010))

   return(0);
}


