//+------------------------------------------------------------------+
//|                                                  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_separate_window
#property indicator_buffers 5
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 Blue
#property indicator_color4 OrangeRed
#property indicator_color5 Yellow

#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[];
double bb_low[];
double arrow_down[];
double arrow_up[];
double ma_center[];
//-------------------

int init()
  {
  
   IndicatorBuffers(5);
  
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,bb_high);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,bb_low);
   
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexBuffer(2,arrow_down);
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexBuffer(3,arrow_up);
   SetIndexEmptyValue(3,0.0);
   
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ma_center);   

   
   return(0);
  }
//----------------------------------------
 
int deinit()
  {return(0);}
//---------------------------------------- 
int start()
  { 
  Comment( " MA_BBands " );  
     
    
    int limit = Bars-IndicatorCounted();  
    double OsMA_0, OsMA_1;
    double ma_high_short;
    double ma_low_short;
    int i;
    
    
    
    for( i=0;i < limit ; i++)
        
     {
     
      bb_high[i + MoveShift]        = iBands(Symbol(),Period(),BBPeriod,BBDeviation,0,PRICE_HIGH,MODE_UPPER,i);
       ma_high_short                = iMA(Symbol(),Period(),MAFastPeriod,0,MODE_LWMA,PRICE_HIGH,i);  
           
       bb_low[i + MoveShift]        = iBands(Symbol(),Period(),BBPeriod,BBDeviation,0,PRICE_LOW ,MODE_LOWER,i);
       ma_low_short                 = iMA(Symbol(),Period(),MAFastPeriod,0,MODE_LWMA,PRICE_LOW,i);
       ma_center[i + MoveShift]     = ( bb_high[i + MoveShift] + bb_low[i + MoveShift]) /2;
       
  
       OsMA_0 = iOsMA(Symbol(),Period(),MACDFastPeriod,MACDLowPeriod,MACDsignal,PRICE_CLOSE,i) ;
       OsMA_1 = iOsMA(Symbol(),Period(),MACDFastPeriod,MACDLowPeriod,MACDsignal,PRICE_CLOSE,i+1) ;

      
      if(  ma_high_short > bb_high[i] && OsMA_1 >0 && OsMA_0 <0 )
        arrow_down[i] = High[i] + iATR(Symbol(),0,200,i);
      if(  ma_low_short < bb_low[i] && OsMA_1 <0 && OsMA_0 >0 )
        arrow_up[i] = Low[i] - iATR(Symbol(),0,200,i);
 
     } 
     
   return(0);
   
   
  } 



