//+------------------------------------------------------------------+
//|                                                   ExCandles2.mq4 |
//|                           Copyright © 2006, Alex Sidd (Executer) |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Alex Sidd (Executer)"
#property link      "mailto:work_st@mail.ru"
//----

#property indicator_chart_window
#property indicator_buffers 1
#property  indicator_color1  Black
//---- input parameters
extern int ExPeriod = 24;
extern bool TrendFilter = true;
extern int how_bars = 0;
//---- buffers
double CCodeBuffer[];
//+------------------------------------------------------------------+
//| initialization function                                          |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   IndicatorBuffers(1);
   SetIndexStyle(0, DRAW_NONE);
   SetIndexBuffer(0, CCodeBuffer);
   SetIndexDrawBegin(0, ExPeriod);
   short_name = "ExCandles";
   IndicatorShortName(short_name);
   SetIndexLabel(0, short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectsDeleteAll(0, OBJ_ARROW);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+  
int Trend(int i)
  {
   double negative = 0;
   double positive = 0;
   if(i < Bars - ExPeriod - 1)
     {
       int k = i + ExPeriod;
       while(k > i)
         {
           if(Open[k] < Close[k]) 
               positive += (Close[k] - Open[k]);
           if(Open[k] > Close[k]) 
               negative += (Open[k] - Close[k]);
           k--;
         }
     }
   else 
       return(0);
   if(positive - negative > 0) 
       return(1);
   if(positive - negative < 0) 
       return(-1);
   if(positive == negative) 
       return(0);
  }
//+------------------------------------------------------------------+
//| Executer Candles Indicator                                           |
//+------------------------------------------------------------------+
bool IsViselnik(int i1)
  {
   double buf = Open[i1]-Close[i1];
   if(buf == 0) 
       buf = 1;
   if((TrendFilter == true && Trend(i1) > 0 && ((High[i1] - Open[i1])*100 / 
      buf < 20) && ((Close[i1] - Low[i1])*100 / buf > 180)) ||
      (TrendFilter == false && ((High[i1] - Open[i1])*100 / buf < 20) && 
      ((Close[i1] - Low[i1])*100 / buf > 180))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsHummer(int i1)
  {
   double buf = Close[i1]-Open[i1];
   if(buf == 0) 
       buf = 1;
   if((TrendFilter == true && Trend(i1) < 0 && ((High[i1] - Close[i1])*100 / 
      buf > 180) && ((Open[i1] - Low[i1])*100 / buf < 20))||
      (TrendFilter == false && ((High[i1] - Close[i1])*100 / buf > 180) && 
      ((Open[i1] - Low[i1])*100 / buf < 20))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsBear(int i1, int i2)
  {
   if((TrendFilter == true && Trend(i1) >0 && (Open[i1] - Close[i1] > 0) && 
      (Open[i1] > Close[i2]) && (Close[i1] < Open[i2]) && (Open[i2] - Close[i2] < 0)) ||
      (TrendFilter == false > 0 && (Open[i1] - Close[i1] > 0) && (Open[i1] > Close[i2]) &&
      (Close[i1] < Open[i2]) && (Open[i2] - Close[i2] < 0))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsBull(int i1, int i2)
  {
   if((TrendFilter == true&&Trend(i1) < 0 && (Open[i1] - Close[i1] < 0) && 
      (Open[i2] - Close[i2] > 0) && (Close[i1] > Open[i2]) && (Open[i1] < Close[i2])) ||
      (TrendFilter == false && (Open[i1] - Close[i1] < 0) && (Open[i2] - Close[i2] > 0) &&
      (Close[i1] > Open[i2]) && (Open[i1] < Close[i2]))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool Zavesa_black(int i1, int i2)
  {
   double x = (High[i2] - Low[i2]);
   if(x == 0) 
       x = 0.0001;
   if((TrendFilter == true && Trend(i1) > 0 &&
      (Open[i1] - Close[i1] > 0) &&
      (Open[i2] - Close[i2] < 0) && 
      ((Close[i2] - Open[i2]) / x > 0.6) && 
      (Open[i1] > High[i2]) && 
      (Close[i1] < (Open[i2] + (Close[i2] - Open[i2]) / 2))) ||
      (TrendFilter == false &&
      (Open[i1] - Close[i1] >0) &&
      (Open[i2] - Close[i2] < 0) && 
      ((Close[i2] - Open[i2]) / x > 0.6) && 
      (Open[i1] > High[i2]) && 
      (Close[i1] < (Open[i2] + (Close[i2] - Open[i2]) / 2)))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool Prosvet(int i1, int i2)
  {
   double x = (High[i2] - Low[i2]);
   if(x == 0) 
       x = 0.0001;
   if((TrendFilter == true && Trend(i1) < 0 &&
      (Open[i1] - Close[i1] < 0) &&
      (Open[i2] - Close[i2] > 0) && 
      ((Open[i2] - Close[i2]) / x > 0.6) && 
      (Open[i1] < Low[i2]) && 
      (Close[i1] > (Close[i2] + (Open[i2] - Close[i2]) / 2))) ||
      (TrendFilter == false &&
      (Open[i1] - Close[i1] < 0) &&
      (Open[i2] - Close[i2] > 0) && 
      ((Open[i2] - Close[i2]) / x > 0.6) && 
      (Open[i1] < Low[i2]) && 
      (Close[i1] > (Close[i2] + (Open[i2] - Close[i2]) / 2)))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool morning_star(int i1, int i2, int i3)
  { 
   if((TrendFilter == true && Trend(i1) < 0 &&
      (Open[i3] - Close[i3] > 0) && 
      (Open[i1] - Close[i1] < 0) && 
      (Close[i2] < Open[i3]) && 
      (Close[i2] < Open[i1]) && 
      (Close[i2] < Close[i1]) &&
      (Close[i2] < Close[i3]) &&
      (Open[i2] < Open[i1]) && 
      (Open[i2] < Open[i3]) && 
      (Open[i2] < Close[i3]) && 
      (Open[i2] < Close[i1])) ||
      (TrendFilter == false&&
      (Open[i3] - Close[i3] > 0) && 
      (Open[i1] - Close[i1] < 0) && 
      (Close[i2] < Open[i3]) && 
      (Close[i2] < Open[i1]) && 
      (Close[i2] < Close[i1]) &&
      (Close[i2] < Close[i3]) &&
      (Open[i2] < Open[i1]) && 
      (Open[i2] < Open[i3]) && 
      (Open[i2] < Close[i3]) && 
      (Open[i2] < Close[i1]))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool evening_star(int i1, int i2, int i3)
  {
   if((TrendFilter == true && Trend(i1) > 0 &&
      (Open[i3] - Close[i3]<0) && 
      (Open[i1] - Close[i1]>0) && 
      (Close[i2] > Open[i3]) && 
      (Close[i2] > Open[i1]) && 
      (Close[i2] > Close[i1]) &&
      (Close[i2] > Close[i3]) &&
      (Open[i2] > Open[i1]) && 
      (Open[i2] > Open[i3]) && 
      (Open[i2] > Close[i3]) && 
      (Open[i2] > Close[i1]))||
      (TrendFilter == false&&
      (Open[i3] - Close[i3] < 0) && 
      (Open[i1] - Close[i1] > 0) && 
      (Close[i2] > Open[i3]) && 
      (Close[i2] > Open[i1]) && 
      (Close[i2] > Close[i1]) &&
      (Close[i2] > Close[i3]) &&
      (Open[i2] > Open[i1]) && 
      (Open[i2] > Open[i3]) && 
      (Open[i2] > Close[i3]) && 
      (Open[i2] > Close[i1]))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsEveningDodgiStar(int i1, int i2, int i3)
  {
   if((TrendFilter == true && Trend(i1) > 0 &&
      (Open[i2] == Close[i2]) &&
      (Open[i3] - Close[i3]<0) && 
      (Open[i1] - Close[i1]>0) && 
      (Close[i2] > Open[i3]) && 
      (Close[i2] > Open[i1]) && 
      (Close[i2] > Close[i1]) &&
      (Close[i2] > Close[i3]) &&
      (Open[i2] > Open[i1]) && 
      (Open[i2] > Open[i3]) && 
      (Open[i2] > Close[i3]) && 
      (Open[i2] > Close[i1])) ||
      (TrendFilter == false &&
      (Open[i2] == Close[i2]) &&
      (Open[i3] - Close[i3]<0) && 
      (Open[i1] - Close[i1]>0) && 
      (Close[i2] > Open[i3]) && 
      (Close[i2] > Open[i1]) && 
      (Close[i2] > Close[i1]) &&
      (Close[i2] > Close[i3]) &&
      (Open[i2] > Open[i1]) && 
      (Open[i2] > Open[i3]) && 
      (Open[i2] > Close[i3]) && 
      (Open[i2] > Close[i1]))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsMorningDodgiStar(int i1, int i2, int i3)
  {
   if((TrendFilter == true && Trend(i1) < 0 &&
      (Open[i2] == Close[i2]) &&
      (Open[i3] - Close[i3] > 0) && 
      (Open[i1] - Close[i1] < 0) && 
      (Close[i2] < Open[i3]) && 
      (Close[i2] < Open[i1]) && 
      (Close[i2] < Close[i1]) &&
      (Close[i2] < Close[i3]) &&
      (Open[i2] < Open[i1]) && 
      (Open[i2] < Open[i3]) && 
      (Open[i2] < Close[i3]) && 
      (Open[i2] < Close[i1])) ||
      (TrendFilter == false &&
      (Open[i2] == Close[i2]) &&
      (Open[i3] - Close[i3] > 0) && 
      (Open[i1] - Close[i1] < 0) && 
      (Close[i2] < Open[i3]) && 
      (Close[i2] < Open[i1]) && 
      (Close[i2] < Close[i1]) &&
      (Close[i2] < Close[i3]) &&
      (Open[i2] < Open[i1]) && 
      (Open[i2] < Open[i3]) && 
      (Open[i2] < Close[i3]) && 
      (Open[i2] < Close[i1]))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsFallStar(int i1)
  {
   double buf = Open[i1] - Close[i1];
   if(buf == 0) 
       buf = 1;
   if((TrendFilter == true && Trend(i1) > 0 && ((High[i1] - Open[i1])*100 / 
      buf > 180) && ((Close[i1] - Low[i1])*100 / buf < 20)) ||
      (TrendFilter == false && ((High[i1] - Open[i1])*100 / buf > 180) && 
      ((Close[i1] - Low[i1])*100 / buf < 20))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool IsTurnedHummer(int i1)
  {
   double buf = Close[i1] - Open[i1];
   if(buf == 0) 
       buf = 1;
   if((TrendFilter == true && Trend(i1) < 0 && ((High[i1] - Close[i1])*100 / 
      buf < 20) && ((Open[i1] - Low[i1])*100 / buf > 180))||
      (TrendFilter == false && ((High[i1] - Close[i1])*100 / buf < 20) && 
      ((Open[i1] - Low[i1])*100 / buf > 180))) 
       return(true);
   else 
       return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars = IndicatorCounted();
   double negative = 0, positive = 0;
//----
   if(Bars <= ExPeriod) 
       return(0);
   if(counted_bars < 1)
       for(i = 1; i <= ExPeriod; i++) 
           CCodeBuffer[Bars-i] = 0.0;
//----
   if(how_bars <= 0 || how_bars > Bars) 
       how_bars = Bars;
   i = how_bars - ExPeriod - 1;
   if(counted_bars >= ExPeriod) 
       i = how_bars - counted_bars - 1;
   while(i >= 0)
     {
       if(IsFallStar(i + 1) == true)
         {
           ObjectCreate("Падающая звезда " + i, OBJ_ARROW, 0, Time[i+1], High[i+1] + 
                        20*Point);
           ObjectSet("Падающая звезда " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
           ObjectSet("Падающая звезда " + i, OBJPROP_COLOR, Red);
           CCodeBuffer[i] = 1;
         }
       if(IsTurnedHummer(i + 1) == true)
         {
           ObjectCreate("Перевернутый Молот " + i, OBJ_ARROW, 0, Time[i+1], Low[i+1] - 
                        20*Point);
           ObjectSet("Перевернутый Молот " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
           ObjectSet("Перевернутый Молот " + i, OBJPROP_COLOR, Blue);
           CCodeBuffer[i] = 2;
         }
       if(IsViselnik(i + 1) == true)
         {
           ObjectCreate("Висельник " + i, OBJ_ARROW, 0, Time[i+1], High[i+1] + 20*Point);
           ObjectSet("Висельник " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
           ObjectSet("Висельник " + i, OBJPROP_COLOR, Red);
           CCodeBuffer[i] = 3;
         }
       if(IsHummer(i + 1) == true)
         {
           ObjectCreate("Молот " + i, OBJ_ARROW, 0, Time[i+1], Low[i+1] - 20*Point);
           ObjectSet("Молот " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
           ObjectSet("Молот " + i, OBJPROP_COLOR, Blue);
           CCodeBuffer[i] = 4;
         }
       if(IsBear(i + 1, i + 2) == true)
         {
           ObjectCreate("Медвежье поглощение " + i, OBJ_ARROW, 0, Time[i+1], High[i+1] + 
                        20*Point);
           ObjectSet("Медвежье поглощение " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
           ObjectSet("Медвежье поглощение " + i, OBJPROP_COLOR, Maroon);
           CCodeBuffer[i] = 5;
         }
       if(IsBull(i+1, i+2)==true)
         {
           ObjectCreate("Бычье поглощение " + i, OBJ_ARROW, 0, Time[i+1], Low[i+1] - 
                        20*Point);
           ObjectSet("Бычье поглощение " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
           ObjectSet("Бычье поглощение " + i, OBJPROP_COLOR, RoyalBlue);
           CCodeBuffer[i] = 6;
         }
       if(Zavesa_black(i + 1, i + 2) == true)
         {
           ObjectCreate("Завеса из темных облаков " + i, OBJ_ARROW, 0, Time[i], High[i] +
                        20*Point);
           ObjectSet("Завеса из темных облаков " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
           ObjectSet("Завеса из темных облаков " + i, OBJPROP_COLOR, OrangeRed);
           CCodeBuffer[i] = 7;
         }
       if(Prosvet(i + 1, i + 2) == true)
         {
           ObjectCreate("Просвет в облаках " + i, OBJ_ARROW, 0, Time[i], Low[i] - 
                        20*Point);
           ObjectSet("Просвет в облаках " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
           ObjectSet("Просвет в облаках " + i, OBJPROP_COLOR, SlateBlue);
           CCodeBuffer[i] = 8;
         }
       if(evening_star(i + 1, i + 2, i + 3) == true)
         {
           ObjectCreate("Вечерняя звезда " + i, OBJ_ARROW, 0, Time[i+2], High[i+2] + 
                        20*Point);
           ObjectSet("Вечерняя звезда " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
           ObjectSet("Вечерняя звезда " + i, OBJPROP_COLOR, Red);
           CCodeBuffer[i] = 9;
         }
       if(morning_star(i + 1, i + 2, i + 3) == true)
         {
           ObjectCreate("Утренняя звезда " + i, OBJ_ARROW, 0, Time[i+2], Low[i+2] - 
                        20*Point);
           ObjectSet("Утренняя звезда " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
           ObjectSet("Утренняя звезда " + i, OBJPROP_COLOR, DodgerBlue);
           CCodeBuffer[i] = 10;
         }
       if(IsEveningDodgiStar(i + 1, i + 2, i + 3) == true)
         {
           ObjectCreate("Вечерняя доджи звезда " + i, OBJ_ARROW, 0, Time[i+2], High[i+2] + 
                        20*Point);
           ObjectSet("Вечерняя доджи звезда " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
           ObjectSet("Вечерняя доджи звезда " + i, OBJPROP_COLOR, DeepPink);
           CCodeBuffer[i] = 11;
         }
       if(IsMorningDodgiStar(i + 1, i + 2, i + 3) == true)
         {
           ObjectCreate("Утренняя доджи звезда " + i, OBJ_ARROW, 0, Time[i+2], Low[i+2] - 
                        20*Point);
           ObjectSet("Утренняя доджи звезда " + i, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
           ObjectSet("Утренняя доджи звезда " + i, OBJPROP_COLOR, Blue);
           CCodeBuffer[i] = 12;
         }
       i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+