//+------------------------------------------------------------------+
//|			                                    TDI-RT-H-RSI.mq4     |
//|         						          Copyright © 2012 Zoidy |
//+------------------------------------------------------------------+

#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Yellow
#property indicator_color3 Red
#property indicator_color4 Blue
#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";

double TSLupAboveRSI[];
double TSLupBelowRSI[];
double TSLdownBelowRSI[];
double TSLdownAboveRSI[];

int init()
  {
   IndicatorShortName("TDI - TSL crossing RSI");
   SetIndexBuffer(0,TSLupBelowRSI);
   SetIndexBuffer(1,TSLupAboveRSI);
   SetIndexBuffer(2,TSLdownAboveRSI);
   SetIndexBuffer(3,TSLdownBelowRSI);
   
   SetIndexStyle(0,DRAW_HISTOGRAM); 
   SetIndexStyle(1,DRAW_HISTOGRAM); 
   SetIndexStyle(2,DRAW_HISTOGRAM); 
   SetIndexStyle(3,DRAW_HISTOGRAM); 
   
   SetIndexLabel(0, "TSL Up - Below RSI" ); 
   SetIndexLabel(1, "TSL Up - Above RSI" ); 
   SetIndexLabel(2, "TSL Down - Above RSI" ); 
   SetIndexLabel(3, "TSL Down - Below RSI" );

   return(0);
  }

double rsiPriceLine0, rsiPriceLine1, tradeSignalLine0, tradeSignalLine1;

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");
		*/
      	
      tradeSignalLine0 = iCustom(NULL, 0, TDI_RT_Indicator_Name, 5, i);
      tradeSignalLine1 = iCustom(NULL, 0, TDI_RT_Indicator_Name, 5, i+1);
      rsiPriceLine0 = iCustom(NULL, 0, TDI_RT_Indicator_Name, 4, i);
       
	  // work out histograms
	  TSLupBelowRSI[i] = 0;
	  TSLupAboveRSI[i] = 0;
	  TSLdownAboveRSI[i] = 0;
	  TSLdownBelowRSI[i] = 0;
	  
	  // TSL going up
	  if(tradeSignalLine0 > tradeSignalLine1) {
	  	if(tradeSignalLine0 < rsiPriceLine0) {
	      TSLupBelowRSI[i] = signal_level;
	  	}
	  	else {
	      TSLupAboveRSI[i] = signal_level;
	  	}
	  }
	  // TSL going down
	  else if(tradeSignalLine0 < tradeSignalLine1) {
	  	if(tradeSignalLine0 > rsiPriceLine0) {
	      TSLdownAboveRSI[i] = signal_level;
	  	}
	  	else {
 	      TSLdownBelowRSI[i] = signal_level;
 	   	}
 	  }
    } 
//----
   return(0);
  }

//+------------------------------------------------------------------+