/*------------------------------------------------------------------------------------
   Name: TDI-RT-Clone.mq4
   
   Description: A clone of the TDI indicator. 
                The volatility bands and the market base line are not exactly the same 
                but they are close enough. 
                	          
-------------------------------------------------------------------------------------*/
// Indicator properties
#property copyright "www.xaphod.com"
#property link      "www.xaphod.com"
#property strict
#property version "1.600"
#property description "A clone of the TDI-RT indicator"
#property description "RT indicating that it updates in Real Time, ie on every tick."
#property description "The volatility bands and the market base line are not exactly the same but they are close enough."
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 SteelBlue
#property indicator_width1 7
#property indicator_color2 CLR_NONE
#property indicator_width2 2
#property indicator_color3 Black
#property indicator_width3 5
#property indicator_color4 CLR_NONE
#property indicator_width4 1
#property indicator_color5 CLR_NONE
#property indicator_width5 1
#property indicator_color6 CLR_NONE
#property indicator_width6 1
#property indicator_level1 32
#property indicator_level2 50
#property indicator_level3 68
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor DimGray

#define INDICATOR_NAME "TDI-RT-Clone"

// Indicator parameters
extern int                RSI_Period=10;            
extern ENUM_APPLIED_PRICE RSI_Price=PRICE_CLOSE; 
extern int                Volatility_Band=34;
extern int                RSISignal_Period=4;   
extern ENUM_MA_METHOD     RSISignal_Mode=MODE_SMA;
extern int                TradeSignal_Period=4;      
extern ENUM_MA_METHOD     TradeSignal_Mode=MODE_SMA; 
input color Up_Arrow=clrBlue;
input color Down_Arrow=clrRed;
input int size=2;  // Arrow size
input int max_bars=1240; //Max. baras back that draw arrows
input bool draw_arrows=true; //Draw arroes in the indicator subwindow



// Global module varables
double gdaRSI[];
double gdaRSISig[];
double gdaTradeSig[];
double gdaMktBase[];
double gdaVolaTop[];
double gdaVolaBtm[];

datetime hora;
int contador=0;
//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, gdaMktBase);
  SetIndexLabel(0,"Market Base");
  SetIndexStyle(1, DRAW_LINE);
  SetIndexBuffer(1, gdaTradeSig);
  SetIndexLabel(1,"Trade Signal");
  SetIndexStyle(2, DRAW_LINE);
  SetIndexBuffer(2, gdaRSISig);
  SetIndexLabel(2,"RSI Signal");   
  SetIndexStyle(3, DRAW_LINE);
  SetIndexBuffer(3, gdaVolaTop);
  SetIndexLabel(3,"Volatility Top");   
  SetIndexStyle(4, DRAW_LINE);
  SetIndexBuffer(4, gdaVolaBtm);
  SetIndexLabel(4,"Volatility Bottom");   
  SetIndexStyle(5, DRAW_NONE);
  SetIndexBuffer(5, gdaRSI);
  SetIndexLabel(5,NULL);
  IndicatorDigits(1);
  IndicatorShortName(INDICATOR_NAME);
  return(0);
}

//-----------------------------------------------------------------------------
// function: deinit()
// Description: Custom indicator deinitialization function.
//-----------------------------------------------------------------------------
int deinit() {
   return (0);
}


///-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
  int iNewBars, iCountedBars, i; 
  double base,signal;
  
  
  // Get unprocessed bars
  iCountedBars=IndicatorCounted();
  if(iCountedBars < 0) return (-1); 
  if(iCountedBars>0) iCountedBars--;
  iNewBars=Bars-iCountedBars;

  // Calc TDI data
  for(i=iNewBars-1; i>=0; i--) {
    gdaRSI[i] = iRSI(NULL,0,RSI_Period,RSI_Price,i); 
  }
  for(i=iNewBars-1; i>=0; i--) {  
    gdaRSISig[i]=iMAOnArray(gdaRSI,0,RSISignal_Period,0,RSISignal_Mode,i);
    gdaTradeSig[i]=iMAOnArray(gdaRSI,0,TradeSignal_Period,0,TradeSignal_Mode,i);
    gdaMktBase[i]=iMAOnArray(gdaRSI,0,Volatility_Band,0,MODE_SMA,i);    
    gdaVolaTop[i]=gdaMktBase[i]+1.6185 * iStdDevOnArray(gdaRSI,0,Volatility_Band,0,MODE_SMA,i);
    gdaVolaBtm[i]=gdaMktBase[i]-1.6185 * iStdDevOnArray(gdaRSI,0,Volatility_Band,0,MODE_SMA,i);
    
    if (draw_arrows)
            {
                           if (i<iNewBars-2 && i<max_bars)
                         {
                            if (gdaMktBase[i+1]>gdaTradeSig[i+1])
                                  if (gdaMktBase[i]<gdaTradeSig[i]) 
                                           {
                         contador++;
                                                      ObjectCreate(0,StringConcatenate("up",DoubleToStr(contador)),OBJ_ARROW_UP,1,Time[i],gdaMktBase[i]-5);
                                                                              ObjectSetInteger(0,StringConcatenate("up",DoubleToStr(contador)),OBJPROP_COLOR,Up_Arrow);
                                                                              ObjectSetInteger(0,StringConcatenate("up",DoubleToStr(contador)),OBJPROP_WIDTH,size);
                                       }
                               if (gdaMktBase[i+1]<gdaTradeSig[i+1])
                                    if (gdaMktBase[i]>gdaTradeSig[i]) 
                                       {
                                       
                                       contador++;
                                       ObjectCreate(0,StringConcatenate("down",DoubleToStr(contador)),OBJ_ARROW_DOWN,1,Time[i],gdaMktBase[i]+14);
                                       ObjectSetInteger(0,StringConcatenate("down",DoubleToStr(contador)),OBJPROP_COLOR,Down_Arrow);
                                       ObjectSetInteger(0,StringConcatenate("down",DoubleToStr(contador)),OBJPROP_WIDTH,size);
            }          
    }        
      }         
            
    
  } 
  
  
  base=NormalizeDouble(gdaMktBase[0],1);
  signal=NormalizeDouble(gdaTradeSig[0],1);
  
  if (NewBar())
      {
      if (gdaMktBase[2]>gdaTradeSig[2])
         if (gdaMktBase[1]<gdaTradeSig[1]) 
            {
            Alert("TDI-RT-Clone ---------  Up Arrow");
            if (draw_arrows)
            {
            contador++;
            ObjectCreate(0,StringConcatenate("up",DoubleToStr(contador)),OBJ_ARROW_UP,1,Time[1],gdaMktBase[1]-5);
            ObjectSetInteger(0,StringConcatenate("up",DoubleToStr(contador)),OBJPROP_COLOR,Up_Arrow);
            ObjectSetInteger(0,StringConcatenate("up",DoubleToStr(contador)),OBJPROP_WIDTH,size);
            }
            }
         
         
         
      if (gdaMktBase[2]<gdaTradeSig[2])
         if (gdaMktBase[1]>gdaTradeSig[1]) 
            {
            Alert("TDI-RT-Clone ---------  Down Arrow");
            if (draw_arrows)
            {
            contador++;
            ObjectCreate(0,StringConcatenate("down",DoubleToStr(contador)),OBJ_ARROW_DOWN,1,Time[1],gdaMktBase[1]+14);
            ObjectSetInteger(0,StringConcatenate("down",DoubleToStr(contador)),OBJPROP_COLOR,Down_Arrow);
            ObjectSetInteger(0,StringConcatenate("down",DoubleToStr(contador)),OBJPROP_WIDTH,size);
            }
            }   
      }
    
  
  return(0);
}
//+------------------------------------------------------------------+

bool NewBar()
   {
   if (Time[0]!=hora)   
      {
      hora=Time[0];
      return true;
      }
   return false;
   }


