//+------------------------------------------------------------------+
//|                                            Ozy_histo_alert_1.mq4 |
//|                                                     Vorchunozavr |
//|                                                                  |
//+------------------------------------------------------------------+
//+ Взято у "ExcStrategy" "http://www.ExcStrategy.ru"
//+ Переделано под гистограмму. Не рисует)
//+ Прикрутил алерт.
//+ http://forum.tradelikeapro.ru/index.php?topic=5792.0
//+------------------------------------------------------------------+

#property strict
//----
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1

#property indicator_buffers 3
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Tomato
#property indicator_width1  2
#property indicator_width2  2

input int   Period_1 = 2;
input int   Period_2 = 2;
input bool  UseAlert = true;

bool nexttrend, New_Bar, flag_up = false, flag_down = false;
double minh, maxl, up[], down[], trend[];
//-------------------------------------------------
int init () 
{
    SetIndexBuffer (0, up);
    SetIndexStyle (0, DRAW_HISTOGRAM);
    SetIndexBuffer (1, down);
    SetIndexStyle (1, DRAW_HISTOGRAM);

    SetIndexBuffer (2, trend);
    SetIndexStyle (2, DRAW_NONE);
    //----
    IndicatorShortName("Ozy_histo_alert_1");
    SetIndexLabel(0, "Down");
    SetIndexLabel(1, "Up");
    //----
    nexttrend = 0;
    minh = High[Bars - 1];
    maxl = Low[Bars - 1];
    //----
    return (0);
}
//----
int start ()
{

    double atr, ll, hh, lma, hma;
    int workbar = 1;
    int c = IndicatorCounted ();
    if(c < 0) 
    {
        return (- 1);
    }
 
    for (int i = Bars - 1 - c; i >= workbar; i --) 
    {
        if(Bars - i > 5 + MathMax(Period_1, Period_2))
        {
            ll = iLow(Symbol (), Period (), iLowest (Symbol (), Period (), MODE_LOW, Period_1, i));
            hh = iHigh(Symbol (), Period (), iHighest (Symbol (), Period (), MODE_HIGH, Period_1, i));
            lma = iMA(NULL, 0, Period_2, 0, MODE_SMA, PRICE_LOW, i);
            hma = iMA(NULL, 0, Period_2, 0, MODE_SMA, PRICE_HIGH, i);
            trend[i] = trend[i + 1];
            atr = iATR(Symbol (), 0, 100, i) / 2;
        
            if(nexttrend == 1) 
            {
                maxl = MathMax (ll, maxl);
                if(hma < maxl && Close[i] < Low[i + 1]) 
                {
                    trend[i] = 1;
                    nexttrend = 0;
                    minh = hh;
                }
            }
            if(nexttrend == 0)
            {
                minh = MathMin (hh, minh);
                if(lma > minh && Close[i] > High[i + 1]) 
                {
                    trend[i] = 0;
                    nexttrend = 1;
                    maxl = ll;
                }
            }
            if(trend[i] == 0 && trend[i+1] == 0)
            {
               up[i] = 1;
               down[i] = EMPTY_VALUE;
            }
            if(trend[i] == 1 && trend[i+1] == 1)
            {
               up[i] = EMPTY_VALUE;
               down[i] = 1;
            }
            if(trend[i] == 0 && trend[i+1] == 1)
            {
               up[i] = 1;
               down[i] = EMPTY_VALUE;
            }
            if(trend[i] == 1 && trend[i+1] == 0)
            {
               up[i] = EMPTY_VALUE;
               down[i] = 1;
            }  
        }
    }
    
    up[0] = EMPTY_VALUE;
    down[0] = EMPTY_VALUE;
    
    if(UseAlert)
    {
      if(up[1] == EMPTY_VALUE && flag_up == false) flag_up = true;
      if(down[1] == EMPTY_VALUE && flag_down == false) flag_down = true;
      if(up[1] == 1 && flag_up == true)
      {
         flag_up = false;
         Alert(Symbol()+"  Ozy_histo_alert:"+"  UP");
      }
      if(down[1] == 1 && flag_down == true)
      {
         flag_down = false;
         Alert(Symbol()+"  Ozy_histo_alert:"+"  DOWN");
      }
      
    }
    
    return (0);
}
//+------------------------------------------------------------------+
