//+------------------------------------------------------------------+
//|                                                 BB+RSI_Combo.mq4 |
//|                                Copyright 2012, Rubber Ducky Inc. |
//|                           http://rubberduckytrading.blogspot.ca/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Rubber Ducky Inc."
#property link      "http://rubberduckytrading.blogspot.ca/"

#property indicator_separate_window
#property indicator_buffers 2

#property indicator_color1 Red
#property indicator_width1 2

#property indicator_color2 Blue
#property indicator_width2 2

//---- Inputs ----
extern string BOLLINGER_BAND_SETTINGS = "----------------------------------------------------------------";
extern bool   BB_ExpandContract = true;
extern int    BB_Period         = 20;
extern int    BB_Deviation      = 2;
extern int    BB_AppPrice       = 0;// 0=Close, 1=Open, 2=High, 3=Low, 4=Medain, 5=Typical, 6=Weighted
extern int    BB_Shift          = 0;
extern string RSI_SETTINGS      = "----------------------------------------------------------------";
extern bool   RSI_CrossOver     = true;
extern int    RSI_Period        = 21;
extern int    RSI_AppPrice      = 0;// 0=Close, 1=Open, 2=High, 3=Low, 4=Medain, 5=Typical, 6=Weighted
extern int    RSI_Level         = 50; //Crossover Level
extern string MOVING_AVERAGE_SETTINGS = "----------------------------------------------------------------";
extern bool   MA_Filter         = false;
extern int    MA_Period         = 50;
extern int    MA_AppPrice       = 0;// 0=Close, 1=Open, 2=High, 3=Low, 4=Medain, 5=Typical, 6=Weighted
extern int    MA_Method         = 0;// 0=SMA, 1=EMA, 2=SMMA, 3=LWMA
extern int    MA_Shift          = 0;

//---- Buffers ----
double dnBar[];
double upBar[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){
   SetIndexBuffer(0,dnBar);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2,Red);
   SetIndexLabel(0,"Down Bar");
   
   SetIndexBuffer(1,upBar);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2,Blue);
   SetIndexLabel(1,"Up Bar");
   
   IndicatorShortName("BB+RSI+MA Combo");
   return(0);
   }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit(){return(0);}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){
   int i,limit;
   double bbUpper1,bbUpper2,bbLower1,bbLower2;
   double bbSqueeze1,bbSqueeze2;
   double rsi1,rsi2;
   double ma1,ma2;
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return(-1);
   if (counted_bars > 0) counted_bars--;
   limit = Bars - counted_bars;
   
   //---- Bollinger Bands and RSI calculation ----
   for (i=1; i<limit; i++){
      bbUpper1 = iBands(Symbol(),0,BB_Period,BB_Deviation,BB_Shift,BB_AppPrice,MODE_UPPER,i);
      bbUpper2 = iBands(Symbol(),0,BB_Period,BB_Deviation,BB_Shift,BB_AppPrice,MODE_UPPER,i+1);
      bbLower1 = iBands(Symbol(),0,BB_Period,BB_Deviation,BB_Shift,BB_AppPrice,MODE_LOWER,i);
      bbLower2 = iBands(Symbol(),0,BB_Period,BB_Deviation,BB_Shift,BB_AppPrice,MODE_LOWER,i+1);
      
      bbSqueeze1 = bbUpper1 - bbLower1;
      bbSqueeze2 = bbUpper2 - bbLower2;
      
      rsi1 = iRSI(Symbol(),0,RSI_Period,RSI_AppPrice,i);
      rsi2 = iRSI(Symbol(),0,RSI_Period,RSI_AppPrice,i+1);
      
      ma1 = iMA(Symbol(),0,MA_Period,MA_Shift,MA_Method,MA_AppPrice,i);
      ma2 = iMA(Symbol(),0,MA_Period,MA_Shift,MA_Method,MA_AppPrice,i+1);
      
      if (BB_ExpandContract == true){ 
         if (bbSqueeze1 > bbSqueeze2 && High[i] >= bbUpper1){
            if (RSI_CrossOver == true && rsi1 > RSI_Level){
               if (MA_Filter == true && Close[i] > ma1){
                  upBar[i] = 1;
                  dnBar[i] = 0;
                  }
               else if (MA_Filter == false){
                  upBar[i] = 1;
                  dnBar[i] = 0;
                  }
               }
            else if (RSI_CrossOver == false){
               upBar[i] = 1;
               dnBar[i] = 0;
               }
            }  
         if (bbSqueeze1 > bbSqueeze2 && Low[i] <= bbLower1){
            if (RSI_CrossOver == true && rsi1 < RSI_Level){
               if (MA_Filter == true && Close[i] < ma1){
                  dnBar[i] = 1;
                  upBar[i] = 0;
                  }
               else if (MA_Filter == false){
                  dnBar[i] = 1;
                  upBar[i] = 0;
                  }
               }
            else if (RSI_CrossOver == false){
               dnBar[i] = 1;
               upBar[i] = 0;
               }
            }
         }
      }

   return(0);
   }
//+------------------------------------------------------------------+