//+------------------------------------------------------------------+
//|                                         .mq4 |
//|                         |
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  
  +------------------------------------------------------------------+
*/   

#property indicator_minimum 0.0
#property indicator_maximum 1.5

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3

extern string QtyOfSignals_ = " 2,3,4 signals to align";
extern string xQtyOfSignals_ = "Moving Average is Compulsory";
extern int QtyOfSignals = 3;
extern bool SyncBarDirection = true;
extern string MovingAverage = " Moving Average Parameters";
extern bool MA_Compulsory = True;
extern int MaPeriod = 100;
extern string PSAR = " Parabolic SAR Parameters";
extern double Step = 0.02;
extern double Maximum = 0.2;
extern string BollingerBand = " Bollinger Band Parameters";
extern int BollngerPeriod = 20;
extern int Deviation = 2;
extern string Stochastics_ = "Stochastics Parameters";
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
extern int MA_Method=0; // SMA 0, EMA 1, SMMA 2, LWMA 3
extern string _MA_Method="SMA0 EMA1 SMMA2 LWMA3";
extern int PriceField=0;
extern string note_Price="PriceField:  Low/High = 0, Close/Close = 1";
extern int OverBoughtLevel  =70;
extern int OverSoldLevel    =30;

double CrossUp[];
double CrossDown[];
//double None[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators


   SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 3);


   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 3);
   SetIndexBuffer(1, CrossDown);
   SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 3);


//----
   return(0);
  }
//DRAW_HISTOGRAM
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
//   double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = 0; i <= limit; i++) {
   
      counter=i;
      for (counter=i ;counter<=i+9;counter++)
      
  //     CrossUp[i] = 0;
  //     CrossDown[i] = 0;
        int BarDirectionBuy,BarDirectionSell;
      double MANow, MaBuy, MaSell;
       double BBandNow, BandBuy, BandSell;
       double PSARNow, PSARBuy, PSARSell;
       double fastMAnow, slowMAnow, fastMAprevious1,fastMAprevious2, slowMAprevious;
      double StochBuy,StochSell;
      MANow=iMA(NULL,0,MaPeriod,0,MODE_EMA,PRICE_CLOSE,0+i);
      PSARNow=iSAR(NULL,0,Step,Maximum,0+i);
      BBandNow = iBands(NULL,0,BollngerPeriod,Deviation,0,PRICE_CLOSE,0,0+i);
      fastMAnow=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, 0+i);
      fastMAprevious1=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i+1);
      fastMAprevious2=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i+2);
      slowMAnow=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_SIGNAL, 0+i);
      slowMAprevious=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_SIGNAL, i+1);
      CrossUp[i]=0;
      CrossDown[i]=0;
  

  

 
 
      if(iClose(NULL,0,0+i) < MANow) MaSell = 1;
      if(iClose(NULL,0,0+i) > MANow )MaBuy = 1;
      if(iClose(NULL,0,0+i) < BBandNow) BandSell = 1;
      if(iClose(NULL,0,0+i) > BBandNow )BandBuy = 1;
      if(iClose(NULL,0,0+i) < PSARNow) PSARSell = 1;
      if(iClose(NULL,0,0+i) > PSARNow )PSARBuy = 1;

      if(fastMAnow > OverBoughtLevel) StochSell = 1;
      if(fastMAnow < OverBoughtLevel 
         && (fastMAprevious1 > OverBoughtLevel ||
               fastMAprevious2 > OverBoughtLevel))  StochSell= 1;
 
      if(fastMAnow < OverSoldLevel) StochBuy = 1;
      if(fastMAnow > OverSoldLevel 
         && (fastMAprevious1 < OverSoldLevel ||
               fastMAprevious2 < OverSoldLevel))   StochBuy = 1;
      if(!SyncBarDirection)  BarDirectionBuy = 1; 
      if(SyncBarDirection &&(iClose(NULL,0,i+1)>iOpen(NULL,0,i+1)) 
        && (iClose(NULL,0,i+0)>iOpen(NULL,0,i+0))) BarDirectionBuy = 1;
      if(!SyncBarDirection)  BarDirectionSell = 1; 
      if(SyncBarDirection &&(iClose(NULL,0,i+1)<iOpen(NULL,0,i+1)) 
        && (iClose(NULL,0,i+0)<iOpen(NULL,0,i+0))) BarDirectionSell = 1; 
 
 
       if(
        BarDirectionBuy ==1 
        && (MaBuy + BandBuy + PSARBuy + StochBuy) > (QtyOfSignals - 1) 
        && (!MA_Compulsory || MaBuy == 1 )
         )CrossUp[i] = 1;
          
       if(
        BarDirectionSell ==1  
        && (MaSell + BandSell + PSARSell + StochSell) > (QtyOfSignals - 1) 
        && (!MA_Compulsory || MaSell == 1) 
         )CrossDown[i] = 1;

 /*     
     Comment(
  CrossDown[0],"   ",CrossUp[i]);    
*/
   }

}
   
