//+------------------------------------------------------------------+
//|                                                    SimpleBars.mq4|
//|                                                  excelf@gmail.com|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Ivan Kornilov. All rights reserved."
#property link "excelf@gmail.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_color4 Green

#define SIGNAL_NONE        0//Пустой сигнал
#define SIGNAL_BUY         1//Сигнал на покупку 
#define SIGNAL_SELL       -1//Сигнал на продажу 
#define SIGNAL_TRADE_ALLOW 3//Сигнал разрешающий торговлю


extern int period = 5;
extern bool useClose = true;
extern int width = 2;

double bufferRed1[];
double bufferGreen1[];
double bufferRed2[];
double bufferGreen2[];
double trand[];

int init() {
    IndicatorBuffers(5);
 
    SetIndexBuffer(0, bufferRed1);
    SetIndexBuffer(1, bufferGreen1);
    SetIndexBuffer(2, bufferRed2);
    SetIndexBuffer(3, bufferGreen2);
    SetIndexBuffer(4, trand);
    
    IndicatorDigits(Digits+1);
    SetIndexStyle(0, DRAW_HISTOGRAM, 0, 1);
    SetIndexStyle(1, DRAW_HISTOGRAM, 0, 1);
    SetIndexStyle(2, DRAW_HISTOGRAM, 0, width);
    SetIndexStyle(3, DRAW_HISTOGRAM, 0, width);
}


int start() {
   int counted_bars=IndicatorCounted();

   if(counted_bars > 0) {
       counted_bars--;
   }
   int limit = Bars - counted_bars;

   int j;
   double buyPrice;
   double sellPrice;
   for(int i = limit - 1; i >= 0; i--) {
        if(useClose) {
            buyPrice  = Close[i];//(High[i]+Low[i])/2; 
            sellPrice = Close[i]; //(High[i]+Low[i])/2;
        } else {
            buyPrice = Low[i];
            sellPrice = High[i];
        }
        if(trand[i + 1] == SIGNAL_NONE || trand[i + 1] == EMPTY_VALUE) {
            if(Close[i] > Open[i]) {
               trand[i] = SIGNAL_BUY;
           } else {
               trand[i] = SIGNAL_SELL;
           }
        } else {
            if(trand[i + 1] == SIGNAL_BUY) {
                if(buyPrice > Low[i + 1]) {
                   trand[i] = SIGNAL_BUY;
                } else {
                   for(j = 2; j <= period; j++) {
                        if(buyPrice > Low[i + j]) {
                           trand[i] = SIGNAL_BUY;
                        break;
                        } else {
                           trand[i] = SIGNAL_SELL;
                        }
                   }
                }
            } else if(trand[i + 1] == SIGNAL_SELL) {
                if(sellPrice < High[i + 1]) {
                   trand[i] = SIGNAL_SELL;
                } else {
                    for(j = 2; j <= period; j++) {
                        if(sellPrice < High[i + j]) {
                           trand[i] = SIGNAL_SELL;
                           break;
                        } else {
                           trand[i] = SIGNAL_BUY;
                        }
                   }
                }
            }   
        }

        if(trand[i] == SIGNAL_SELL) {//RED BAR 
            bufferRed1[i]  = High[i];
            bufferGreen1[i] = Low[i]; 
            bufferRed2[i]  = MathMax(Open[i], Close[i]); 
            bufferGreen2[i] = MathMin(Open[i], Close[i]);   
        } else if(trand[i] == SIGNAL_BUY)  {
            bufferRed1[i]  = Low[i];
            bufferGreen1[i] = High[i]; 
            bufferRed2[i]  = MathMin(Open[i], Close[i]);  
            bufferGreen2[i] = MathMax(Open[i], Close[i]);    
        } 
    }
}