//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
/*------------------------------------------------------------------------------------
   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. 
                	          
-------------------------------------------------------------------------------------*/
//+------------------------------------------------------------------+
//|                                                                  |
//|                     Traders Dynamic Index                        |
//|                                                                  |
//|  This hybrid indicator is developed to assist traders in their   |
//|  ability to decipher and monitor market conditions related to    |
//|  trend direction, market strength, and market volatility.        |
//|                                                                  | 
//|  Even though comprehensive, the T.D.I. is easy to read and use.  |
//|                                                                  |
//|  Green line = RSI Price line                                     |
//|  Red line = Trade Signal line                                    |
//|  Blue lines = Volatility Band                                    | 
//|  Yellow line = Market Base Line                                  |  
//|                                                                  |
//|  Trend Direction - Immediate and Overall                         |
//|   Immediate = Green over Red...price action is moving up.        |
//|               Red over Green...price action is moving down.      |
//|                                                                  |   
//|   Overall = Yellow line trends up and down generally between the |
//|             lines 32 & 68. Watch for Yellow line to bounces off  |
//|             these lines for market reversal. Trade long when     |
//|             price is above the Yellow line, and trade short when |
//|             price is below.                                      |        
//|                                                                  |
//|  Market Strength & Volatility - Immediate and Overall            |
//|   Immediate = Green Line - Strong = Steep slope up or down.      | 
//|                            Weak = Moderate to Flat slope.        |
//|                                                                  |               
//|   Overall = Blue Lines - When expanding, market is strong and    |
//|             trending. When constricting, market is weak and      |
//|             in a range. When the Blue lines are extremely tight  |                                                       
//|             in a narrow range, expect an economic announcement   | 
//|             or other market condition to spike the market.       |
//|                                                                  |               
//|                                                                  |
//|  Entry conditions                                                |
//|   Scalping  - Long = Green over Red, Short = Red over Green      |
//|   Active - Long = Green over Red & Yellow lines                  |
//|            Short = Red over Green & Yellow lines                 |    
//|   Moderate - Long = Green over Red, Yellow, & 50 lines           |
//|              Short= Red over Green, Green below Yellow & 50 line |
//|                                                                  |
//|  Exit conditions*                                                |   
//|   Long = Green crosses below Red                                 |
//|   Short = Green crosses above Red                                |
//|   * If Green crosses either Blue lines, consider exiting when    |
//|     when the Green line crosses back over the Blue line.         |
//|                                                                  |
//|                                                                  |
//|  IMPORTANT: The default settings are well tested and proven.     |
//|             But, you can change the settings to fit your         |
//|             trading style.                                       |
//|                                                                  |
//|                                                                  |
//|  Price & Line Type settings:                           |                
//|   RSI Price settings                                             |               
//|   0 = Close price     [DEFAULT]                                  |               
//|   1 = Open price.                                                |               
//|   2 = High price.                                                |               
//|   3 = Low price.                                                 |               
//|   4 = Median price, (high+low)/2.                                |               
//|   5 = Typical price, (high+low+close)/3.                         |               
//|   6 = Weighted close price, (high+low+close+close)/4.            |               
//|                                                                  |               
//|   RSI Price Line & Signal Line Type settings                                   |               
//|   0 = Simple moving average       [DEFAULT]                      |               
//|   1 = Exponential moving average                                 |               
//|   2 = Smoothed moving average                                    |               
//|   3 = Linear weighted moving average                             |               
//|                                                                  |
//|   Good trading,                                                  |   
//|                                                                  |
//|   Dean                                                           |                              
//+------------------------------------------------------------------+
// 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 9
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_color1    Lime
#property indicator_width1    14
#property indicator_color2    Gray
#property indicator_width2    14
#property indicator_color3    Red
#property indicator_width3    14

#define INDICATOR_NAME "TDI-RT-Clone"

// Indicator parameters
extern int                RSI_Period=14;
extern ENUM_APPLIED_PRICE RSI_Price=PRICE_CLOSE;
extern int                Volatility_Band=34;
extern int                RSISignal_Period=2;
extern ENUM_MA_METHOD     RSISignal_Mode=MODE_SMA;
extern int                TradeSignal_Period=7;
extern ENUM_MA_METHOD     TradeSignal_Mode=MODE_SMA;

// Global module varables
double Buy[];
double Wait[];
double Sell[];
double gdaRSI[];
double gdaRSISig[];
double gdaTradeSig[];
double gdaMktBase[];
double gdaVolaTop[];
double gdaVolaBtm[];
//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init()
  {
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,Buy);
   SetIndexLabel(0,"Buy");
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,Wait);
   SetIndexLabel(1,"Wait");
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,Sell);
   SetIndexLabel(2,"Sell");
   SetIndexBuffer(3,gdaVolaTop);
   SetIndexLabel(3,"Volatility Top");
   SetIndexBuffer(4,gdaVolaBtm);
   SetIndexLabel(4,"Volatility Bottom");
   SetIndexBuffer(5,gdaMktBase);
   SetIndexLabel(5,"Market Base");
   SetIndexBuffer(6,gdaTradeSig);
   SetIndexLabel(6,"Trade Signal");
   SetIndexBuffer(7,gdaRSISig);
   SetIndexLabel(7,"RSI Signal");
   SetIndexBuffer(8,gdaRSI);
   SetIndexLabel(8,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;

// Get unprocessed bars
   iCountedBars=IndicatorCounted();
   if(iCountedBars < 0) return (-1);
   if(iCountedBars>0) iCountedBars--;
   iNewBars=MathMin(Bars-iCountedBars,Bars-1);

// 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);
     }

   int s,l,c=IndicatorCounted();

   if( c < 0 ) return(-1);
   if(c==0)
      l=Bars;
   if(c>0)
      l=Bars-c;
   l--;

   for(s=l; s>=0; s--)
     {
      if(gdaRSISig[s]>gdaTradeSig[s] && gdaRSISig[s]>50 && gdaRSISig[s]<90 && gdaTradeSig[s]>50 && gdaTradeSig[s]<90)
        {
         Buy[s]=1;
         Sell[s]=0;
         Wait[s]=0;
        }
      if(gdaRSISig[s]<gdaTradeSig[s] && gdaRSISig[s]<50 && gdaRSISig[s]>10 && gdaTradeSig[s]<50 && gdaTradeSig[s]>10)
        {
         Buy[s]=0;
         Sell[s]=1;
         Wait[s]=0;
        }
      if(Buy[s]+Sell[s]>1)
        {
         Buy[s]=0;
         Sell[s]=0;
         Wait[s]=1;
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+
