//+++======================================================================+++
//+++                TriangularMA centered.mq4   ///   mladen              +++
//+++======================================================================+++
#property copyright   "mladen"
#property link        "mladenfx@gmail.com"
#property description "© Tankk, 6 may 2016, http://forexsystemsru.com/. Алгоритм расчёта от 'mladen' не тронут. Выбор всех настроек в главном окне."

#property indicator_separate_window
#property indicator_buffers 3

enum LNtype { LINE, HISTO };
//+++======================================================================+++
//+++                 Custom indicator input parameters                    +++
//+++======================================================================+++

extern int         FastHalfLength  =  24;   //50;
extern int         SlowHalfLength  =  96;   //100;
extern ENUM_APPLIED_PRICE   Price  =  PRICE_CLOSE;

extern LNtype             Display  =  LINE;
extern color              TrendUP  =  MediumSeaGreen,
                          TrendDN  =  OrangeRed,
                         ZeroLine  =  DimGray;
extern int              TrendSize  =  2,
                          ZRLSize  =  0;
extern ENUM_LINE_STYLE TrendStyle  =  STYLE_SOLID,            
                         ZRLStyle  =  STYLE_SOLID;
extern bool               ShowZRL  =  true;

//+++======================================================================+++
//+++                     Custom indicator buffers                         +++
//+++======================================================================+++
double Uptrend[], Dntrend[], buffer2[];
double ExtMapBuffer[], buffer3[], buffer4[];
double pricesArray[];
//+++======================================================================+++
//+++              Custom indicator initialization function                +++
//+++======================================================================+++
int init()
{
   FastHalfLength=MathMax(FastHalfLength,1);
   SlowHalfLength=MathMax(SlowHalfLength,1);

   IndicatorBuffers(7);   
   
   SetIndexBuffer(0,Uptrend);   SetIndexDrawBegin(0,SlowHalfLength);
   SetIndexBuffer(1,Dntrend);   SetIndexDrawBegin(1,SlowHalfLength);
   SetIndexBuffer(2,buffer2);
   
      int LNT = DRAW_LINE;    if (Display == HISTO) LNT = DRAW_HISTOGRAM;    
      SetIndexStyle(0,LNT,TrendStyle,TrendSize,TrendUP);
      SetIndexStyle(1,LNT,TrendStyle,TrendSize,TrendDN);
      
      int ZRLT = DRAW_NONE;   if (ShowZRL == true) ZRLT = DRAW_LINE;    
      SetIndexStyle(2,ZRLT,ZRLStyle,ZRLSize,ZeroLine);
      
         SetIndexLabel(0,"UPtrend");
         SetIndexLabel(1,"DNtrend");
         SetIndexLabel(2,NULL);
      
   SetIndexBuffer(3,ExtMapBuffer);   
   ArraySetAsSeries(ExtMapBuffer,true); 
         
   SetIndexBuffer(4,buffer3);
   SetIndexBuffer(5,buffer4);
   SetIndexBuffer(6,pricesArray);
   
   IndicatorShortName("TMA MACD v1 TT ["+FastHalfLength+"-"+SlowHalfLength+"]"); 
   
//+++
   return(0);
}
//+++======================================================================+++
//+++              Custom indicator deinitialization function              +++
//+++======================================================================+++
int deinit()
{
   return(0);
}
//+++======================================================================+++
//+++                 Custom indicator iteration function                  +++
//+++======================================================================+++

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           limit=MathMax(limit,FastHalfLength);
           limit=MathMax(limit,SlowHalfLength);
           
//+++======================================================================+++
    double trend[];
    ArrayResize(trend, limit); 
    ArraySetAsSeries(trend, true); 
//+++======================================================================+++
//+++======================================================================+++

   for (i=limit;i>=0;i--) pricesArray[i] = iMA(NULL,0,1,0,MODE_SMA,Price,i);
   for (i=limit;i>=0;i--)
   {
      buffer3[i] = calculateTma(pricesArray,FastHalfLength,i);
      buffer4[i] = calculateTma(pricesArray,SlowHalfLength,i);
      ExtMapBuffer[i] = buffer3[i]-buffer4[i];
      buffer2[i] = 0; 
      
//+++======================================================================+++

        trend[i] = trend[i+1];
        if (ExtMapBuffer[i]> ExtMapBuffer[i+1]) trend[i] =1;
        if (ExtMapBuffer[i]< ExtMapBuffer[i+1]) trend[i] =-1;
    
    if (trend[i]>0)
     {  
      Uptrend[i] = ExtMapBuffer[i]; 
      if (trend[i+1]<0) Uptrend[i+1]=ExtMapBuffer[i+1];
      Dntrend[i] = EMPTY_VALUE;
     }
     
    else if (trend[i]<0)
     { 
      Dntrend[i] = ExtMapBuffer[i]; 
      if (trend[i+1]>0) Dntrend[i+1]=ExtMapBuffer[i+1];
      Uptrend[i] = EMPTY_VALUE;
     }     
   }
   
//+++
   return(0);
}

//+++======================================================================+++
//+++                TriangularMA centered.mq4   ///   mladen              +++
//+++======================================================================+++

double calculateTma(double& prices[], int halfLength, int i)
{
   int j,k;

   double sum  = (halfLength+1)*prices[i];
   double sumw = (halfLength+1);

   for(j=1, k=halfLength; j<=halfLength; j++, k--)
   {
      sum  += k*prices[i+j];
      sumw += k;

      if (j<=i)
       {
        sum  += k*prices[i-j];
        sumw += k;
       }
   }
   
//+++
   return(sum/sumw);
}

//+++======================================================================+++
//+++                TriangularMA centered.mq4   ///   mladen              +++
//+++======================================================================+++