//+------------------------------------------------------------------+
//|   23 Maart 2016             TMA_ML_COL_SLINE.mq4                 !
//+------------------------------------------------------------------+
#property copyright "AHGDP"
#property link      "TMA_ML_COL_4BANDS"

#property indicator_chart_window
#property indicator_buffers   6
#property indicator_color1   clrNONE
#property indicator_color2   clrMagenta 
#property indicator_color3   clrMagenta
#property indicator_color4   clrYellow
#property indicator_color5   clrYellow

extern int    HalfLength      = 9;//*************
extern int    Price           = PRICE_MEDIAN;// _CLOSE

extern int    LineSizeCen     = 4 ;
extern int    LineStyleCen    = 0 ;

extern int    CountBars       = 500 ;

string indicatorFileName;
bool   calculateValue;
bool   returnBars;

double tma[];
double tmaUpa[];
double tmaUpb[];
double tmaDna[];
double tmaDnb[];
double trend1[];
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(6);
    HalfLength=MathMax(HalfLength,1);
 
   SetIndexStyle (0,  DRAW_LINE, LineStyleCen, LineSizeCen );
   SetIndexBuffer(0,  tma);
   SetIndexStyle (1,  DRAW_LINE, LineStyleCen, LineSizeCen );
   SetIndexBuffer(1,  tmaUpa);
   SetIndexStyle (2, DRAW_LINE, LineStyleCen, LineSizeCen );
   SetIndexBuffer(2, tmaUpb);
   SetIndexStyle (3, DRAW_LINE, LineStyleCen, LineSizeCen );
   SetIndexBuffer(3, tmaDna);
   SetIndexStyle (4, DRAW_LINE, LineStyleCen, LineSizeCen );
   SetIndexBuffer(4, tmaDnb);
   
   SetIndexBuffer(5, trend1);
      
      indicatorFileName = WindowExpertName();
    
   
   IndicatorShortName("");

  
//=================================================================           
   return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int deinit()
 { 
 return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
   int i,j,k,limit;

     SetIndexDrawBegin(0,Bars-CountBars);
     SetIndexDrawBegin(1,Bars-CountBars);
     SetIndexDrawBegin(2,Bars-CountBars);
     SetIndexDrawBegin(3,Bars-CountBars);
     SetIndexDrawBegin(4,Bars-CountBars);
     SetIndexDrawBegin(5,Bars-CountBars);
    
     
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-1,Bars-counted_bars+HalfLength);
            if (returnBars)  { tma[0] = limit+1; return(0); }

//==================================================================   
  
       if (trend1[limit]== 1) CleanPoint(limit,tmaUpa,tmaUpb);
       if (trend1[limit]==-1) CleanPoint(limit,tmaDna,tmaDnb);
      
      for (i=limit; i>=0; i--)
      {
         double sum  = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMMA,Price,i);// SMA
         double sumw = (HalfLength+1);
         
         for(j=1, k=HalfLength; j<=HalfLength; j++, k--)
         {
            sum  += k*iMA(NULL,0,1,0,MODE_SMMA,Price,i+j);// SMA
            sumw += k;

            if (j<=i)
            {
               sum  += k*iMA(NULL,0,1,0,MODE_SMMA,Price,i-j);// SMA
               sumw += k;
            }      
         
               tmaUpa[i] = EMPTY_VALUE;
               tmaUpb[i] = EMPTY_VALUE;
               tmaDna[i] = EMPTY_VALUE;
               tmaDnb[i] = EMPTY_VALUE;
               trend1[i] = trend1[i+1];
         
               if (tma[i] > tma[i+1])                       trend1[i] =  1;
               if (tma[i] < tma[i+1])                       trend1[i] = -1;
               if (tma[i] > tma[i+1] && tma[i] < tma[i+1])  trend1[i] =  0;
               if (trend1[i] ==  1)    PlotPoint(i,tmaUpa,tmaUpb,tma);
               if (trend1[i] == -1)    PlotPoint(i,tmaDna,tmaDnb,tma);
   
               tma[i]     = sum/sumw;
           
      } }  
//========================================================================  
      return(0);            
   }

//+-------------------------------------------------------------------                                                                
//+-------------------------------------------------------------------

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}
//+-------------------------------------------------------------------                                                                
//+-------------------------------------------------------------------

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE)
              {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
              }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    =  EMPTY_VALUE;
      }      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}
//+-------------------------------------------------------------------
//+-------------------------------------------------------------------