//+------------------------------------------------------------------+
//|                                                  TriangularMAmq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  Red
#property indicator_width1  3
#property indicator_width2  3
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//
//
//

extern string TimeFrame  = "Current time frame";
extern int    HalfLength = 12;
extern int    Price      = PRICE_CLOSE;

double buffer1[];
double UpH[];
double DnH[]; 
double NuH[];   
double trend[];

//
//
//
//
//

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int init()
{
   HalfLength=MathMax(HalfLength,1);
   IndicatorBuffers(4);
   SetIndexBuffer(0,UpH); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexDrawBegin(0,HalfLength);
   SetIndexBuffer(1,DnH); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexDrawBegin(1,HalfLength);   
   SetIndexBuffer(2,buffer1); 
   SetIndexBuffer(3,trend); 
   
      //
      //
      //
      //
      //
     
        indicatorFileName = WindowExpertName();
        calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
        returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
        timeFrame         = stringToTimeFrame(TimeFrame); 
        
      //
      //
      //
      //
      //
     
     IndicatorShortName(timeFrameToString(timeFrame)+"  TMAcentered Histo");
return(0);
}  
     
int deinit() { return(0); }




//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,j,k,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars+HalfLength,Bars-1);
           if (returnBars) { UpH[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame==Period())
   {
     for (i=limit;i>=0;i--)
     {
        double sum  = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i);
        double sumw = (HalfLength+1);
        for(j=1, k=HalfLength; j<=HalfLength; j++, k--)
        {
           sum  += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j);
           sumw += k;
           if (j<=i)
           {
              sum  += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j);
              sumw += k;
           }
        }
        buffer1[i] = sum/sumw; 
      
        //
        //
        //
        //
        //
       
        trend[i] = trend[i+1];
        UpH[i] = EMPTY_VALUE;
        DnH[i] = EMPTY_VALUE; 
        if (buffer1[i]>buffer1[i+1]) trend[i] = 1; 
        if (buffer1[i]<buffer1[i+1]) trend[i] =-1; 
        if (trend[i] == 1) UpH[i] = 1;
        if (trend[i] ==-1) DnH[i] = 1;  
   }
   return(0);
   }
      
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit;i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HalfLength,Price,3,y);
         UpH[i]   = EMPTY_VALUE;
         DnH[i]   = EMPTY_VALUE; 
         if (trend[i] == 1) UpH[i] = 1;
         if (trend[i] ==-1) DnH[i] = 1;   
      }
return(0);         
}


//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}
