//+------------------------------------------------------------------+
//|                                             TMA slope true 5.mq4 |
//|                                                         Kilian B |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "modified by Kilian B"
//old version pre build 600 #property copyright "Copyright © 2012, zznbrm"  
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property  indicator_separate_window
#property  indicator_buffers     4
#property  indicator_color1 clrGreen
#property  indicator_color2 clrRed
#property  indicator_color3 clrGray
#property  indicator_levelcolor  clrOrange

 
//---- input parameters

input int eintPeriod = 20;
input double threshold = 0.4;
input int atrPeriod = 100;

//---- indicator buffers
double gadblUp[];
double gadblDn[];
double gadblNeutral[];

double gadblSlope[]; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,gadblUp,INDICATOR_DATA);
   SetIndexBuffer(1,gadblDn,INDICATOR_DATA);
   SetIndexBuffer(2,gadblNeutral,INDICATOR_DATA);
   SetIndexBuffer(3,gadblSlope,INDICATOR_CALCULATIONS);
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexStyle(3,DRAW_NONE);
   
   
   SetLevelValue(0,threshold);
   SetLevelValue(1,-threshold);
   SetLevelStyle(STYLE_SOLID,2);
   
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int counted_bars = prev_calculated;
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars > 0 ) counted_bars--;
               
   int intLimit = MathMin( Bars - 1, Bars - counted_bars + eintPeriod );
   
   double dblTma, dblPrev;
   double atr; 

   // Calculate Tma Slope for current timeframe and plot
   for( int inx = intLimit; inx >= 0; inx-- )
   {   
      atr= iATR(NULL,0,atrPeriod,inx+10)*0.1;
      
      if (atr == 0) continue;      
      dblTma = calcTma( inx );
      dblPrev = calcPrev( inx );
      gadblSlope[inx] = ( dblTma - dblPrev ) / atr;
      
      gadblUp[inx] = 0.0;   
      gadblDn[inx] = 0.0;     
      gadblNeutral[inx] = 0.0;    
   
     if ( gadblSlope[inx] >= threshold)
     {
         gadblUp[inx] = gadblSlope[inx];
     }
     else if ( gadblSlope[inx] <= -threshold )
     {
         gadblDn[inx] = gadblSlope[inx];
     }
     else  
     {
         gadblNeutral[inx] = gadblSlope[inx];
     } 
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| calcTma()                                                        |
//+------------------------------------------------------------------+
double calcTma( int inx )
{
   return( iMA( Symbol(), Period(), eintPeriod+1, 0, MODE_LWMA, PRICE_CLOSE, inx ) );
}

//+------------------------------------------------------------------+
//| calcPrev()                                                       |
//+------------------------------------------------------------------+
double calcPrev( int inx )
{
   double dblSum  = (eintPeriod+1)*Close[inx+1];
   double dblSumw = (eintPeriod+1);
   int jnx, knx;
   
   // Add in the current (inx) bar
   dblSum  += ( eintPeriod * Close[inx] );
   dblSumw += eintPeriod;
         
   for ( jnx = 1, knx = eintPeriod; jnx <= eintPeriod; jnx++, knx-- )
   {
      dblSum  += ( knx * Close[inx+1+jnx] );
      dblSumw += knx;
   }
   
   return( dblSum / dblSumw );
}
 
 double calcTmaMTF( int inx , int tf)
{
   double dblSum  = (eintPeriod+1)*iClose(Symbol(),tf,inx);
   double dblSumw = (eintPeriod+1);
   int jnx, knx;
         
   for ( jnx = 1, knx = eintPeriod; jnx <= eintPeriod; jnx++, knx-- )
   {
      dblSum  += ( knx * iClose(Symbol(),tf,inx+jnx) );
      dblSumw += knx;

      if ( jnx <= inx )
      {
         dblSum  += ( knx * iClose(Symbol(),tf,inx-jnx) );
         dblSumw += knx;
      }
   }
   
   return( dblSum / dblSumw );
}
