//+------------------------------------------------------------------+
//|                                         Brooky_Macd_Switcher.mq4 |
//|                     Copyright © 2010, www.Brooky-Indicators.com. |
//|                             http://www.www.Brooky-Indicators.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, www.Brooky-Indicators.com."
#property link      "http://www.www.Brooky-Indicators.com"
extern string You_Are_Following_Another = "( www.Brooky-Indicators.com ) Indicator ";


#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Orange
#property indicator_color2 Blue
#property indicator_color3 Silver
#property indicator_color4 Red
#property indicator_color5 Lime
#property indicator_color6 Silver



#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 1
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 1

//---- input parameters

extern string    Note_1 = "Fast Macd for Buy Signals";
extern int       Fast_Ema_Buy=12;
extern int       Slow_Ema_Buy=26;
extern int       Signal_Sma_Buy=9;

extern string    Note_2 = "Slower Macd for Sell Signals";
extern int       Fast_Ema_Sell=19;
extern int       Slow_Ema_Sell=39;
extern int       Signal_Sma_Sell = 9;

extern string    Note_3 = " Display ";


//---- buffers
double Buy_Main_Buff[];
double Sell_Main_Buff[];
double Buy_Sig_Buff[];
double Sell_Sig_Buff[];
double Flat_Sig_Buff[];
double Flat_Main_Buff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   string bm = StringConcatenate("Buy Main","F("+Fast_Ema_Buy+")"," S(",Slow_Ema_Buy,")"," Sig(",Signal_Sma_Buy,")");
   string sm = StringConcatenate("Sell Main","F("+Fast_Ema_Sell+")"," S(",Slow_Ema_Sell,")"," Sig(",Signal_Sma_Sell,")");  
   string fm = StringConcatenate("Flat Main",0); 
   string bs = StringConcatenate("Buy Sig","F("+Fast_Ema_Buy+")"," S(",Slow_Ema_Buy,")"," Sig(",Signal_Sma_Buy,")");
   string ss = StringConcatenate("Sell Sig","F("+Fast_Ema_Sell+")"," S(",Slow_Ema_Sell,")"," Sig(",Signal_Sma_Sell,")");   
   string fs = StringConcatenate("Flat Sig",0); 
   
     
   SetIndexBuffer(0,Buy_Main_Buff);SetIndexStyle(0,DRAW_HISTOGRAM);SetIndexLabel(0,bm);
   
   SetIndexBuffer(1,Sell_Main_Buff);SetIndexStyle(1,DRAW_HISTOGRAM);SetIndexLabel(1,sm);
   
   SetIndexBuffer(2,Flat_Main_Buff);SetIndexStyle(2,DRAW_HISTOGRAM);SetIndexLabel(2,fm);   
   
   SetIndexBuffer(3,Buy_Sig_Buff);SetIndexStyle(3,DRAW_LINE);SetIndexLabel(3,bs);
   
   SetIndexBuffer(4,Sell_Sig_Buff);SetIndexStyle(4,DRAW_LINE);SetIndexLabel(4,ss);

   SetIndexBuffer(5,Flat_Sig_Buff);SetIndexStyle(5,DRAW_LINE);SetIndexLabel(5,fs);
   
   IndicatorShortName("Brooky MACD Switcher ");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  int start()
    {
     int limit;
     int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
  //---- main loop
     for(int i=0; i<limit; i++)
       {
       
   Buy_Main_Buff[i]=0;
   Sell_Main_Buff[i]=0;
   Buy_Sig_Buff[i]=0;
   Sell_Sig_Buff[i]=0;
   Flat_Sig_Buff[i]=0;
   Flat_Main_Buff[i]=0;
       
double buy_main = iMACD(NULL,0,Fast_Ema_Buy,Slow_Ema_Buy,Signal_Sma_Buy,PRICE_CLOSE,MODE_MAIN,i);
double buy_sig = iMACD(NULL,0,Fast_Ema_Buy,Slow_Ema_Buy,Signal_Sma_Buy,PRICE_CLOSE,MODE_SIGNAL,i);

double sell_main = iMACD(NULL,0,Fast_Ema_Sell,Slow_Ema_Sell,Signal_Sma_Sell,PRICE_CLOSE,MODE_MAIN,i);
double sell_sig = iMACD(NULL,0,Fast_Ema_Sell,Slow_Ema_Sell,Signal_Sma_Sell,PRICE_CLOSE,MODE_SIGNAL,i);

if(buy_main<=0 || buy_main>=0 && buy_sig<=0)
{
Buy_Sig_Buff[i]=buy_sig;
Buy_Main_Buff[i]= buy_main;

Sell_Sig_Buff[i]=0;//;
   Flat_Sig_Buff[i]=sell_sig;
Sell_Main_Buff[i]=0;//;
   Flat_Main_Buff[i]=sell_main;
}

if(sell_main>=0 || sell_main<=0 && sell_sig>=0)
{
Buy_Sig_Buff[i]=0;//;
   Flat_Sig_Buff[i]=buy_sig;
Buy_Main_Buff[i]= 0;//;
   Flat_Main_Buff[i]=buy_main;
Sell_Sig_Buff[i]=sell_sig;
Sell_Main_Buff[i]=sell_main;
}

       }
  //---- done
     return(0);
    }