//+------------------------------------------------------------------+
//|                                                    MA_BBandsV2.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//|                                        E-MAIL:40468962@qq.com    |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  clrWhite
#property indicator_color2  clrWhite
#property indicator_color3  clrBlue
#property indicator_color4  clrOrangeRed
#property indicator_color5  clrYellow
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  1
#property  indicator_width4  1
#property  indicator_width5  1



extern int    MACDFastPeriod = 8;
extern int    MACDLowPeriod  = 10;
extern int    MACDsignal     = 3 ;
extern int    MAFastPeriod   = 4; 
extern int    MoveShift      = 0;
extern double BBDeviation    = 1 ; //0.5
extern int    BBPeriod       = 15 ;

double bb_high[],bb_low[],arrow_down[],arrow_up[],ma_center[],trend[];
//-------------------

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,bb_high);   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,bb_low);    SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,arrow_down);SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,234);
   SetIndexBuffer(3,arrow_up);  SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,233);  
   SetIndexBuffer(4,ma_center); SetIndexStyle(4,DRAW_LINE);  
   SetIndexBuffer(5,trend);
   return(0);
  }
//----------------------------------------
 
int deinit(){return(0);}
//---------------------------------------- 
int start()
{ 
    int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = fmin(Bars-counted_bars,Bars-1); 
    double OsMA_0, OsMA_1,ma_high_short,ma_low_short;
    for(i=limit; i>=0; i--)   
    {
     
       bb_high[i+MoveShift]   = iBands(NULL,0,BBPeriod,BBDeviation,0,PRICE_HIGH,MODE_UPPER,i);
       ma_high_short          = iMA(NULL,0,MAFastPeriod,0,MODE_LWMA,PRICE_HIGH,i);  
       bb_low[i+MoveShift]    = iBands(NULL,0,BBPeriod,BBDeviation,0,PRICE_LOW ,MODE_LOWER,i);
       ma_low_short           = iMA(NULL,0,MAFastPeriod,0,MODE_LWMA,PRICE_LOW,i);
       ma_center[i+MoveShift] = (bb_high[i+MoveShift] + bb_low[i + MoveShift])/2;
       OsMA_0 = iOsMA(NULL,0,MACDFastPeriod,MACDLowPeriod,MACDsignal,PRICE_CLOSE,i) ;
       OsMA_1 = iOsMA(NULL,0,MACDFastPeriod,MACDLowPeriod,MACDsignal,PRICE_CLOSE,i+1) ;

       arrow_up[i]   = EMPTY_VALUE;
       arrow_down[i] = EMPTY_VALUE;
       trend[i] = 0;
       if(ma_high_short > bb_high[i] && OsMA_1 >0 && OsMA_0 <0) trend[i] =-1;
       if(ma_low_short  < bb_low[i]  && OsMA_1 <0 && OsMA_0 >0) trend[i] = 1;
       if (i<Bars-1 && trend[i]!=trend[i+1])
       {
          if (trend[i] ==  1) arrow_up[i]   = Low[i] -iATR(NULL,0,15,i);
          if (trend[i] == -1) arrow_down[i] = High[i]+iATR(NULL,0,15,i);
       }
 
     } 
return(0);
} 



