
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

extern int BarN = 500;
extern bool	EnableAlert = true;
extern bool	EnableMailAlert = true;

double sell_buffer[];
double buy_buffer[];

double old_sell_buffer;

int init()
{
    SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 2, Red);
    SetIndexBuffer(0, sell_buffer);
    SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 2, Blue);
    SetIndexBuffer(1, buy_buffer);

    old_sell_buffer = sell_buffer[0];

    return 0;
}

int deinit()
{
    return 0;
}

int start()
{
    int last_state = 0,
        state = 0,
        last_high_index = BarN,
        last_low_index = BarN;

    double last_high = High[last_high_index],
           last_low = Low[last_low_index],
           low,
           high;

    for (int i = BarN; i >= 0; i--)
    {
        // нахождение минимального и максимального бара
        low = Low[i + 54];
        high = High[i + 54];
        for (int j = i + 53; j > i; j--)
        {
            if (Low[j] < low){
                low = Low[j];
            }

            if (High[j] > high){
                high = High[j];
            }
        }

        if (Low[i] < low
                && High[i] > high) {
            state = 2;

             if (last_state == 1){
                last_high_index = i + 1;
            }

            if (last_state == -1){
                last_low_index = i + 1;
            }
        } else if (Low[i] < low){
            state = -1;
        } else if (High[i] > high){
            state = 1;
        } 

         if (state != last_state 
               && last_state != 0){
            
            if(state == 2){
                state = -last_state;
            }

            last_high = High[i];
            last_low = Low[i];
        }

        last_state = state;

        if(state == 1){
            if(High[i] >= last_high)
            {
               last_high = High[i];
               last_high_index = i;
            }

            buy_buffer[i] = 1;
            sell_buffer[i] = 0;
        } else if(state == -1){
            if(Low[i] <= last_low)
            {
                last_low = Low[i];
                last_low_index = i;
            }

            buy_buffer[i] = 0;
            sell_buffer[i] = 1;
        }
    }

    if(old_sell_buffer != sell_buffer[0])
    {
        send_notification();
        old_sell_buffer = sell_buffer[0];
    }

    return 0;
}

void send_notification(){
    string msg = StringFormat("[UltraFilter][%s] - %s", Symbol(), get_timeframe());

    if(EnableAlert){
      Alert(msg); 
    } 

    if(EnableMailAlert){
        SendMail(msg,msg);
    } 
}

string get_timeframe(){
    switch(Period())
    {
        case PERIOD_M1:
            return "M1";
        case PERIOD_M5:
            return "M5";
        case PERIOD_M15:
            return "M15";
        case PERIOD_M30:
            return "M30";
        case PERIOD_H1:
            return "H1";
        case PERIOD_H4  :
            return "H4";
        case PERIOD_D1  :
            return "D1";
        case PERIOD_W1:
            return "W1";
        case PERIOD_MN1:
            return "MN1";
        default:
            return "UKNOWN";
    }
}