//+------------------------------------------------------------------+
//|                                             TrendStrength_v1.mq4 |
//|                           Copyright © 2006, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_separate_window
#property indicator_buffers   2
#property indicator_color1    LightBlue
#property indicator_width1    2 
#property indicator_color2    Tomato
//---- input parameters
extern int     Mode        =    0; // 0-RSI method; 1-Stoch method; 2-ADX method  
extern int     Length      =   34; // Period of evaluation
extern int     Smooth      =    5; // Period of smoothing
extern int     Price       =    0; // Price mode : 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted
extern int     ModeMA      =    3; // Mode of Moving Average
extern double  Deviation   =   10;
  
//---- buffers
double Strength[];
double SigBuffer[];
double Bulls[];
double Bears[];
double AvgBulls[];
double AvgBears[];
double SmthBulls[];
double SmthBears[];

int    time[2],TREND[2];
double SMIN[2],SMAX[2];
bool   Expert=true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(8);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(0,Strength);
   SetIndexBuffer(1,SigBuffer);
   
   SetIndexBuffer(2,SmthBulls);
   SetIndexBuffer(3,SmthBears);
   SetIndexBuffer(4,Bulls);
   SetIndexBuffer(5,Bears);
   SetIndexBuffer(6,AvgBulls);
   SetIndexBuffer(7,AvgBears);
//---- name for DataWindow and indicator subwindow label
   string short_name="TrendStrength("+Mode+","+Length+","+Smooth+","+ModeMA+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Trend");
   SetIndexLabel(1,"Signal");
       
//----
   SetIndexDrawBegin(0,Length+Smooth);
   SetIndexDrawBegin(1,Length+Smooth);
   
   
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int      shift, limit, trend, MaxBar,counted_bars=IndicatorCounted();
   double   Price1, Price2, smax[1], smin[1], bsmax, bsmin;
//---- 
   if (Bars-1<Length+Smooth+1)return(0);
   if (counted_bars<0)return(-1);
 
   if (counted_bars>0) counted_bars--;
 
   MaxBar=Bars-1-Length-Smooth-1;
   limit=Bars-counted_bars-1; 

   if (limit>MaxBar)
   {
      for (shift=limit;shift>=MaxBar;shift--) 
      { 
      Strength[Bars-shift]=0;  
      SigBuffer[Bars-shift]=0;    
      Bulls[Bars-shift]=0;    
      Bears[Bars-shift]=0;  
      AvgBulls[Bars-shift]=0;    
      AvgBears[Bars-shift]=0;  
      SmthBulls[Bars-shift]=0;    
      SmthBears[Bars-shift]=0;  
      } 
   limit=MaxBar;
   }
   //----
   if(ArrayResize(smin,limit+2)!=limit+2)return(-1);
   if(ArrayResize(smax,limit+2)!=limit+2)return(-1);         
   
   int Tnew=Time[limit+1];

   if (limit<MaxBar)
   
      if (Tnew==time[1])
      {
      trend=TREND[1];
      smin[limit+1]=SMIN[1];
      smax[limit+1]=SMAX[1];
      Expert=false;
      } 
      else 
      if (Tnew==time[0])
      {
      trend=TREND[0];
      smin[limit+1]=SMIN[0];
      smax[limit+1]=SMAX[0];
      }  
   else
   {
   if (Tnew>time[1])Print("Error 1");
   else Print("Error 2");
   return(-1);  
   }
  
   
   if(counted_bars>0) limit=Bars-counted_bars;
   limit--;
   
   for( shift=limit; shift>=0; shift--)
      {
      Price1 = iMA(NULL,0,1,0,0,Price,shift);
      Price2 = iMA(NULL,0,1,0,0,Price,shift+1); 
      
         if (Mode==0)
         {
         Bulls[shift] = 0.5*(MathAbs(Price1-Price2)+(Price1-Price2));
         Bears[shift] = 0.5*(MathAbs(Price1-Price2)-(Price1-Price2));
         }
        
         if (Mode==1)
         {
         bsmax=High[Highest(NULL,0,MODE_HIGH,Length,shift)];
         bsmin=Low[Lowest(NULL,0,MODE_LOW,Length,shift)];
         
         Bulls[shift] = Price1 - bsmin;
         Bears[shift] = bsmax - Price1;
         }
         
         if (Mode==2)
         {
         double Range = High[shift]-Low[shift];
         double EffRange = Close[shift] - Open[shift];
         Bulls[shift] = Range - 0.5*(MathAbs(EffRange)-EffRange);
         Bears[shift] = Range - 0.5*(MathAbs(EffRange)+EffRange);
         }
      
      
      }
      
      for( shift=limit; shift>=0; shift--)
      {
      AvgBulls[shift]=iMAOnArray(Bulls,0,Length,0,ModeMA,shift);     
      AvgBears[shift]=iMAOnArray(Bears,0,Length,0,ModeMA,shift);
      }
      
      for( shift=limit; shift>=0; shift--)
      {
      SmthBulls[shift]=iMAOnArray(AvgBulls,0,Smooth,0,ModeMA,shift);     
      SmthBears[shift]=iMAOnArray(AvgBears,0,Smooth,0,ModeMA,shift);
      }
                
      for( shift=limit; shift>=0; shift--)
      {
      if((SmthBulls[shift]+SmthBears[shift])>0) 
      Strength[shift]=100*SmthBulls[shift]/(SmthBulls[shift]+SmthBears[shift]);
      smin[shift] = (1-Deviation/100.0)*Strength[shift]; 
      smax[shift] = (1+Deviation/100.0)*Strength[shift];
      
      if (Strength[shift]>smax[shift+1])  trend=1; 
	   if (Strength[shift]<smin[shift+1])  trend=-1;

	     if(trend>0)
	     {
	     if (smin[shift]<smin[shift+1]) smin[shift]=smin[shift+1];
	     SigBuffer[shift]=smin[shift];
	     //DnBuffer[i]=EMPTY_VALUE;
	     }
	     else
	     {
	     if(smax[shift]>smax[shift+1]) smax[shift]=smax[shift+1];
	     SigBuffer[shift]=smax[shift];
	     //UpBuffer[i]=EMPTY_VALUE;
	     }
      
        if ((shift==2)||((shift==1)&&(Expert==true)))
        {
        time [shift-1]=Time [shift];
     
        TREND[shift-1]=trend;
        SMIN[shift-1]=smin[shift];
        SMAX[shift-1]=smax[shift];
        }
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+