/* True Strength Index 

/*The True Strength Index (TSI) is a momentum-based indicator, 
developed by William Blau. Designed to determine both trend and overbought/oversold conditions,
the TSI is applicable to intraday time frames as well as long term trading.

The True Strength Index is a variation of the Relative Strength indicator. 
It uses a double smoothed exponential moving average of price momentum to minimize choppy price
changes and highlight spot trend changes with little or no time lag. 
An increasing True Strength value indicates increasing momentum in the direction of the price movement.

Long Term - First Period used in the double exponential smoothing of the momentum.

Short Term - Second Period used in the double exponential smoothing of momentum.

Fine Tuned by Linuxser , August, 2007 for TSD  http://www.forex-tsd.com/              

*/



#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//#property indicator_maximum 100
//#property indicator_minimum -100
#property indicator_level1 0
#property indicator_level2 25
#property indicator_level3 -25


//---- input parameters
extern int       Price= 0;   // Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close)
extern int       SmoothingP=20;
extern int       DoubleSmoothingP=13;
extern int       SignalPeriod=7;
extern bool      Diff_Histogram=false;
//---- buffers
double TSI_Buffer[];
double SignalBuffer[];
double MTM_Buffer[];
double EMA_MTM_Buffer[];
double EMA2_MTM_Buffer[];
double ABSMTM_Buffer[];
double EMA_ABSMTM_Buffer[];
double EMA2_ABSMTM_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(8);
   SetIndexBuffer(2, MTM_Buffer);
   SetIndexBuffer(3, EMA_MTM_Buffer);
   SetIndexBuffer(4, EMA2_MTM_Buffer);
   SetIndexBuffer(5, ABSMTM_Buffer);
   SetIndexBuffer(6, EMA_ABSMTM_Buffer);
   SetIndexBuffer(7, EMA2_ABSMTM_Buffer);

   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TSI_Buffer);
   SetIndexLabel(0,"TSI");
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,SignalBuffer);
   SetIndexLabel(1,"Signal");
   IndicatorShortName("TSI v1"+"("+SmoothingP+","+DoubleSmoothingP+","+SignalPeriod+")");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit,i;
   limit=Bars-counted_bars-1;
   for (i=Bars-1;i>=0;i--)
      {
      MTM_Buffer[i]=iMA(NULL,0,1,0,0,Price,i) - iMA(NULL,0,1,0,0,Price,i+1); //Close[i]-Close[i+1];
      ABSMTM_Buffer[i]=MathAbs(MTM_Buffer[i]);
      }
      
   for (i=Bars-1;i>=0;i--)
      {
      EMA_MTM_Buffer[i]=iMAOnArray(MTM_Buffer,0,SmoothingP,0,MODE_EMA,i);
      EMA_ABSMTM_Buffer[i]=iMAOnArray(ABSMTM_Buffer,0,SmoothingP,0,MODE_EMA,i);
      }

   for (i=Bars-1;i>=0;i--)
      {
      EMA2_MTM_Buffer[i]=iMAOnArray(EMA_MTM_Buffer,0,DoubleSmoothingP,0,MODE_EMA,i);
      EMA2_ABSMTM_Buffer[i]=iMAOnArray(EMA_ABSMTM_Buffer,0,DoubleSmoothingP,0,MODE_EMA,i);
      }

   for (i=limit;i>=0;i--)
      {
      TSI_Buffer[i]=100.0*EMA2_MTM_Buffer[i]/EMA2_ABSMTM_Buffer[i];
      }
   for (i=limit;i>=0;i--)
      {
      SignalBuffer[i]=iMAOnArray(TSI_Buffer,0,SignalPeriod,0,MODE_SMA,i);
      }
      
   //---- Different Styles      
   if(Diff_Histogram == true)
      {
         for (i=limit;i>=0;i--)
         {
               TSI_Buffer[i]=(100.0*EMA2_MTM_Buffer[i]/EMA2_ABSMTM_Buffer[i])-SignalBuffer[i];
               SetIndexStyle(0,DRAW_HISTOGRAM);
               SetIndexStyle(1,DRAW_NONE);              
         } 
         }     
      
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+