//+------------------------------------------------------------------+
//|                                                     TriMAgen.mq4 |
//|                               Copyright © 2013, Gehtsoft USA LLC |
//|                                            http://fxcodebase.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow

extern int Length=20;
extern int Price=0;      // Applied price
                         // 0 - Close
                         // 1 - Open
                         // 2 - High
                         // 3 - Low
                         // 4 - Median
                         // 5 - Typical
                         // 6 - Weighted  

double TriMAgen[];
double MA[];
int len1, len2;

int init()
{
 IndicatorShortName("Triangular Moving Average generalized");
 IndicatorDigits(Digits);
 SetIndexStyle(0,DRAW_LINE);
 SetIndexBuffer(0,TriMAgen);
 SetIndexStyle(1,DRAW_NONE);
 SetIndexBuffer(1,MA);
 
 len1=MathFloor((Length+1.)/2);
 len2=MathCeil((Length+1.)/2);
 
 return(0);
}

int deinit()
{

 return(0);
}

int start()
{
 if(Bars<=Length) return(0);
 int ExtCountedBars=IndicatorCounted();
 if (ExtCountedBars<0) return(-1);
 int limit=Bars-2;
 if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1;
 int pos;
 pos=limit;
 while(pos>=0)
 {
  MA[pos]=iMA(NULL, 0, len1, 0, MODE_SMA, Price, pos);
  pos--;
 } 
 
 int i;
 double sum;
 pos=limit;
 while(pos>=0)
 {
  sum=0;
  for (i=0;i<len2;i++)
  {
   sum=sum+MA[pos+i];
  }
  TriMAgen[pos]=sum/len2;
  pos--;
 }  
 
 return(0);
}

