
extern double RefHour = 14;
extern double CloseHour = 9;
extern double TopPriceBuf = 27;
extern double BottomPriceBuf = 23;

extern int Tenkan=9;
extern int Kijun=26;
extern int Senkou=52;
extern int FlatSE=8;

extern int dDay = 0;

extern string isFlat = "EMPTY";
extern string PrevisFlat = "EMPTY";

int init()
{
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   int total,cnt;
   double SAR = iSAR(NULL,0,0.02,0.2,0);
   
   double FlatLevelUp=iCustom(NULL,PERIOD_H4,"Cronex Taichi",Tenkan,Kijun,Senkou,FlatSE,4,0);
   double FlatLevelDn=iCustom(NULL,PERIOD_H4,"Cronex Taichi",Tenkan,Kijun,Senkou,FlatSE,5,0);
   
   
   Print("begin: ", isFlat);
   
   if((FlatLevelUp != 0) && (FlatLevelDn != 0)) // FLAT
   {
      isFlat = "FLAT";
      
      if(PrevisFlat != isFlat)
      {
         total = OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
         {  
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
            if(OrderType()==OP_BUY)
            {
               OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
            }
            if(OrderType()==OP_SELL)
            {
               OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
            }
         }
      }

      if((TimeHour(Time[0]) == 14) && (TimeMinute(Time[0]) >= 55) && (dDay != Day()))
      {
         if((Close[1] > Open[1]) && (SAR > Close[0]))
         {
            if(OrdersTotal() == 0) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0020,"",1,0,Red);      
            dDay = Day();
         }
      
         if((Close[1] < Open[1]) && (SAR < Close[0]))
         {
            if(OrdersTotal() == 0) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0020,"",2,0,Blue);
            dDay = Day();
         }
      }
      
      PrevisFlat = isFlat;
   }
   else
   {
      isFlat = "NOT FLAT";
      
      if(PrevisFlat != isFlat)
      {
         total = OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
         {  
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
            if(OrderType()==OP_BUY)
            {
               OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
            }
            if(OrderType()==OP_SELL)
            {
               OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
            }
         }
      }

      if((TimeHour(Time[0]) == 14) && (TimeMinute(Time[0]) >= 55) && (dDay != Day()))
      {
         if((Close[1] > Open[1]) && (SAR > Close[0]))
         {
            if(OrdersTotal() == 0) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0020,"",2,0,Blue);
            dDay = Day();
         }
      
         if((Close[1] < Open[1]) && (SAR < Close[0]))
         {
            if(OrdersTotal() == 0) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0020,"",1,0,Red);      
            dDay = Day();
         }
      }
      
      PrevisFlat = isFlat;
   }
   
   
   Print("end: ", isFlat);
   
   return(0);



















   /*int BarsInADay=24*60/Period();
   int n=MathFloor(BarsInADay*0.7);

   string OpenPos = "EMPTY";

   for(int i=0;i<=100;i++)
   {
      if(TimeHour(Time[i])==RefHour && TimeMinute(Time[i]) >= 50)
      {
      
      Print("!!!!! " + TimeHour(Time[i]));

      int x=MathMax(i-n,0);
      string DateString=TimeDay(Time[i])+"/"+TimeMonth(Time[i]);
      double TrendLineBeginTime=Time[i];
      double TrendLineEndTime=Time[x];

      ObjectCreate(DateString+" RefPrice",OBJ_TREND,0,TrendLineBeginTime,High[i]+0.015,TrendLineBeginTime,High[i]+0.0010);
      ObjectSet(DateString+" RefPrice",OBJPROP_TIME1,TrendLineBeginTime);
      ObjectSet(DateString+" RefPrice",OBJPROP_TIME2,TrendLineBeginTime);
      ObjectSet(DateString+" RefPrice",OBJPROP_PRICE1,High[i]+0.015);
      ObjectSet(DateString+" RefPrice",OBJPROP_PRICE2,High[i]+0.0010);
      ObjectSet(DateString+" RefPrice",OBJPROP_COLOR,Red);
      ObjectSet(DateString+" RefPrice",OBJPROP_WIDTH,2);
      ObjectSet(DateString+" RefPrice",OBJPROP_RAY,0);

      ObjectCreate(DateString+" RefPrice1",OBJ_TREND,0,TrendLineBeginTime,Low[i]-0.015,TrendLineBeginTime,Low[i]-0.0010);
      ObjectSet(DateString+" RefPrice1",OBJPROP_TIME1,TrendLineBeginTime);
      ObjectSet(DateString+" RefPrice1",OBJPROP_TIME2,TrendLineBeginTime);
      ObjectSet(DateString+" RefPrice1",OBJPROP_PRICE1,Low[i]-0.015);
      ObjectSet(DateString+" RefPrice1",OBJPROP_PRICE2,Low[i]-0.0010);
      ObjectSet(DateString+" RefPrice1",OBJPROP_COLOR,Red);
      ObjectSet(DateString+" RefPrice1",OBJPROP_WIDTH,2);
      ObjectSet(DateString+" RefPrice1",OBJPROP_RAY,0);


      //if(dDay != Day())
      //{
         if(Close[i] > Open[i])
         {
            OpenPos = "BUY";
            //if(OrdersTotal() == 0) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0020,"",2,0,Blue);
            //dDay = Day();
         }

         if(Close[i] < Open[i])
         {
            OpenPos = "SELL";
            //if(OrdersTotal() == 0) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0020,"",1,0,Red);
            //dDay = Day();
         }
      //}
      }
   }

   
   Print(TimeHour(Time[0]));
   
   if((TimeHour(Time[0]) == RefHour) && (TimeMinute(Time[0]) >= 50))
   {
      Print(OpenPos + " " + TimeHour(Time[0]));
      
      if(dDay != Day())
      {
         if(OpenPos == "BUY")
         {
            if(OrdersTotal() == 0) OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+0.0020,"",2,0,Blue);
            dDay = Day();
         }

         if(OpenPos == "SELL")
         {
            if(OrdersTotal() == 0) OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-0.0020,"",1,0,Red);
            dDay = Day();
         }
      }
   }

   return(0);*/
}


