//+------------------------------------------------------------------+
//|                                                    KG MA BAR.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2007, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 5
#property  indicator_buffers 3
#property  indicator_color1  Yellow
#property  indicator_color2  Blue
#property  indicator_color3  Red
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
//---- indicator buffers
double     ExtBuffer0[];
double     ExtBuffer1[];
double     ExtBuffer2[];
double     ExtBuffer3[];
double     ExtBuffer4[];
double     ExtBuffer5[];
double     ExtBuffer6[];
double     ExtBuffer7[];
int TF=0;
extern int MA=2;
extern int MA_Mode=0;
extern int Price_Type=6;
extern int Band_MA=12;
extern int SD=1;
string tanda;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   IndicatorDigits(Digits+2);
   SetIndexDrawBegin(0,38);
   SetIndexDrawBegin(1,38);
   SetIndexDrawBegin(2,38);
//---- 4 indicator buffers mapping
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
//---- name for DataWindow and indicator subwindow label
   
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
//---- initialization done
   return(0);
  }
  
 
  
//+------------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator                               |
//+------------------------------------------------------------------+
int start()
  {
   int    i,limit;
   int    counted_bars=IndicatorCounted();
   double prev,current,MACurr;
   double stoch1,stoch2;
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
     //---- macd counted in the 1-st additional buffer
   for(i=0; i<limit; i++)
      ExtBuffer3[i]=iBands(NULL,0,Band_MA,SD,0,PRICE_WEIGHTED,MODE_UPPER,i);//iCustom(NULL, 0, "KG Bands",Band_MA,SD,Price_Type,1,i);//
      
   //---- signal line counted in the 2-nd additional buffer
   for(i=0; i<limit; i++)
      ExtBuffer4[i]=iBands(NULL,0,Band_MA,SD,0,PRICE_WEIGHTED,MODE_LOWER,i);//iCustom(NULL, 0, "KG Bands",Band_MA,SD,Price_Type,2,i);//
   //---- dispatch values between 2 buffers
   bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current=ExtBuffer3[i];
      prev=ExtBuffer4[i];
      MACurr=ExtBuffer5[i];
      if(iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)>current || iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)<prev) up=true;
      if(iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)<current && iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)>prev) up=false;
      if(!up)
        {
         ExtBuffer2[i]=0.0;
         ExtBuffer1[i]=0.0;
         ExtBuffer0[i]=1;
        }
      if(up)
        {
         if (iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)>current)
         {
            ExtBuffer1[i]=1;
            ExtBuffer2[i]=0.0;
            ExtBuffer0[i]=0.0;
         }
         else
         {
            ExtBuffer1[i]=0.0;
            ExtBuffer2[i]=1;
            ExtBuffer0[i]=0.0;
         }
        }
      else
         ExtBuffer0[i]=1;
         //ExtBuffer1[i]=0.0;
         //ExtBuffer2[i]=0.0;
         
       if(TF == 0)
         {tanda = Period();}
       if(TF == 1)
         {tanda = "M1";}
       if(TF == 5)
         {tanda = "M5";}
       if(TF == 15)
         {tanda = "M15";}
       if(TF == 30)
         {tanda = "M30";}
       if(TF == 60)
         {tanda = "H1";}
       if(TF == 240)
         {tanda = "H4";}
       if(TF == 1440)
         {tanda = "D1";}
       if(TF == 10080)
         {tanda = "W1";}        
       if(TF == 43200)
         {tanda = "M1";}        
       
       IndicatorShortName("[All Blue Above 35SMA High-BUY][All Red Below 35SMA Low-SELL]");
     }
   //---- done
   return(0);
  }
//+------------------------------------------------------------------+

