//+------------------------------------------------------------------+
//|                                                     TmaSlope.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, zznbrm"
                          
//---- indicator settings
#property indicator_separate_window
//#property  indicator_buffers 8 
#property  indicator_buffers 4 
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Yellow

#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2


//---- input parameters
extern int eintPeriod = 56;

extern double minaverage=0.2;
extern int candlestoaverage=1000;

//---- indicator buffers

double gadblUp2[];

double gadblDn2[];
double gadblExt[];
double gadblSlope[];
//double gadblTma[];
//double gadblPrev[];

double gdblTruePoint;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{              
   //IndicatorBuffers( 8 );    
   IndicatorBuffers( 4 );
   IndicatorDigits( 5 );
   IndicatorShortName( "TmaSlope" );
   gdblTruePoint = genTruePoint();
      
   SetIndexBuffer( 0, gadblUp2 );    SetIndexLabel( 0, "Up1" );       SetIndexStyle( 0, DRAW_HISTOGRAM );
   SetIndexBuffer( 1, gadblDn2 );    SetIndexLabel( 1, "Dn1" );       SetIndexStyle( 1, DRAW_HISTOGRAM );

   SetIndexBuffer( 2, gadblExt );    SetIndexLabel( 2, "ext" );       SetIndexStyle( 2, DRAW_HISTOGRAM );
   SetIndexBuffer( 3, gadblSlope );  SetIndexLabel( 3, "TMA Slope" );    SetIndexStyle( 3, DRAW_NONE );

      
   SetIndexEmptyValue( 0, 0.0 );
   SetIndexEmptyValue( 1, 0.0 );
   SetIndexEmptyValue( 2, 0.0 );
   SetIndexEmptyValue( 3, 0.0 );

          
   return( 0 );
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return( 0 );
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars = IndicatorCounted();
   	double Down;
	double up;
	double ave;
double neglimit,poslimit;
	int countup,countdown;
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars > 0 ) counted_bars--;
               
   int intLimit = MathMin( Bars - 1, Bars - counted_bars + eintPeriod );
   
   double dblTma, dblPrev;
   
   for( int inx = intLimit; inx >= 0; inx-- )
   {   
      //gadblTma[inx] = calcTma( inx );
      //gadblPrev[inx] = calcTma( inx+1 );
      //gadblSlope[inx] = ( gadblTma[inx] - gadblPrev[inx] ) / gdblTruePoint;
      dblTma = calcTma( inx );
      dblPrev = calcTma( inx+1 );
      gadblSlope[inx] = ( dblTma - dblPrev ) / gdblTruePoint;
      
    
      gadblUp2[inx] = 0.0;
      gadblDn2[inx] = 0.0;
      gadblExt[inx] = 0.0;
      
   
  
   for (int k=candlestoaverage;k>=0;k--)
	{
	if(gadblSlope[k]>= 0)
	{

	up+=gadblSlope[k];
	countup++;
	}
	


		if(gadblSlope[k]<0)
	{
	
	countdown++;
	Down+=gadblSlope[k];
	
	}



	}
	if(countdown!=0)
neglimit=Down/countdown*minaverage;
	if(countup!=0)
poslimit=up/countup*minaverage;
/*	ObjectDelete("MinAveDown");
	ObjectCreate("MinAveDown",OBJ_HLINE,WindowFind( "TmaSlope"),0,Down/countdown*minaverage);
	ObjectDelete("MinAveUp");
	ObjectCreate("MinAveUp",OBJ_HLINE,WindowFind( "TmaSlope"),0,up/countup*minaverage);*/

if ( gadblSlope[inx] > poslimit )      
{
gadblUp2[inx] = gadblSlope[inx]; 
gadblExt[inx]= 0;  
gadblDn2[inx]=0;
}

      else if ( gadblSlope[inx] <=poslimit && gadblSlope[inx] >= neglimit)      
      {
       gadblExt[inx] = gadblSlope[inx];
       gadblUp2[inx]= 0;  
gadblDn2[inx]=0;
}
       
      else if ( gadblSlope[inx] < neglimit ) 
      {
      gadblDn2[inx] = gadblSlope[inx];
        gadblExt[inx] = 0;
       gadblUp2[inx]= 0; 
       }
Comment(neglimit,"\n",poslimit);
     }
   return( 0 );
}

//+------------------------------------------------------------------+
//| genTruePoint()                                                   |
//+------------------------------------------------------------------+
double genTruePoint( string strPair="" )
{
   int intDigits;
   double dblPoint;
   
   if ( strPair == "" || strPair == Symbol() )    intDigits = Digits;
   else                                           intDigits = MarketInfo( strPair, MODE_DIGITS );
      
   // Reset point value for 5 digit brokers
   switch( intDigits )
   {
      case 5:
      case 4:     dblPoint = 0.0001;   break;
      case 3:
      case 2:     dblPoint = 0.01;     break;
      case 1:     dblPoint = 0.1;      break;
      case 0:     dblPoint = 0.0;      break;
      default:    dblPoint = 0.0001;
   }
   
   return( dblPoint );
}

//+------------------------------------------------------------------+
//| calcTma()                                                        |
//+------------------------------------------------------------------+
double calcTma( int inx )
{
   double dblSum  = (eintPeriod+1)*Close[inx];
   double dblSumw = (eintPeriod+1);
   int jnx, knx;
         
   for ( jnx = 1, knx = eintPeriod; jnx <= eintPeriod; jnx++, knx-- )
   {
      dblSum  += ( knx * Close[inx+jnx] );
      dblSumw += knx;

      if ( jnx <= inx )
      {
         dblSum  += ( knx * Close[inx-jnx] );
         dblSumw += knx;
      }
   }
   
   return( dblSum / dblSumw );
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 



   


