//+------------------------------------------------------------------+
//|			                                    TDI-RT-H-MBL.mq4     |
//|         						          Copyright © 2012 Zoidy |
//+------------------------------------------------------------------+

#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Yellow
#property indicator_width1  4
#property indicator_width2  4
#property indicator_width3  4
#property indicator_width4  4
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100

extern int signal_level = 50;

extern string TDI_RT_Indicator_Name = "TDI_RT";

extern bool Include_RSI = true;

double MBLup[];
double MBLdown[];
double MBLblue[];
double MBLyellow[];

int init()
  {
   int num_histograms = 2;

   IndicatorShortName("TDI - Market Base Line");
   SetIndexBuffer(0,MBLup);
   SetIndexBuffer(1,MBLdown);
   SetIndexLabel(0, "Market Base Line Up" );
   SetIndexLabel(1, "Market Base Line Down" ); 

   if(Include_RSI) {
	   SetIndexBuffer(2,MBLblue);
	   SetIndexBuffer(3,MBLyellow);
	   SetIndexLabel(2, "Market Base Line Up - RSI below" ); 
	   SetIndexLabel(3, "Market Base Line Down - RSI above" ); 
	   num_histograms = 4;		
   }

   for(int i=0; i<num_histograms; i++) {
     SetIndexStyle(i,DRAW_HISTOGRAM); 
   }

   return(0);
  }

double marketBaseLine0, marketBaseLine1, rsiPriceLine0;

int start()
  {
   int counted_bars=IndicatorCounted();
   int limit = Bars-counted_bars-1;

   for (int i=limit;i>=0;i--)  
      {
		/*
		   SetIndexBuffer(1,UpZone); SetIndexLabel(1,"VB High"); 
		   SetIndexBuffer(2,marketBaseLineBuf); SetIndexLabel(2,"Market Base Line"); 
		   SetIndexBuffer(3,DnZone); SetIndexLabel(3,"VB Low"); 
		   SetIndexBuffer(4,rsiPriceLineBuf); SetIndexLabel(4,"RSI Price Line");
		   SetIndexBuffer(5,tradeSignalLineBuf); SetIndexLabel(5,"Trade Signal Line");
		*/
      	
      marketBaseLine0 = iCustom(NULL, 0, TDI_RT_Indicator_Name, 2, i);
      marketBaseLine1 = iCustom(NULL, 0, TDI_RT_Indicator_Name, 2, i+1);
      rsiPriceLine0 = iCustom(NULL, 0, TDI_RT_Indicator_Name, 4, i);

	  // work out histograms
	  MBLup[i] = 0;
	  MBLdown[i] = 0;
	  
	  // MBL going up
	  if(marketBaseLine0 > marketBaseLine1) {
	      if(Include_RSI && rsiPriceLine0 < marketBaseLine0) {
			MBLblue[i] = signal_level; 	          	
	      }
	      else {
	      	MBLup[i] = signal_level;
	      }
	  }
	  // MBL going down
	  else if(marketBaseLine0 < marketBaseLine1) {
	      if(Include_RSI && rsiPriceLine0 > marketBaseLine0) {
			MBLyellow[i] = signal_level; 	          	
	      }
	      else {
	      	MBLdown[i] = signal_level;
	      }
 	  }
    } 
//----
   return(0);
  }

//+------------------------------------------------------------------+
