//+------------------------------------------------------------------+
//|                                         MTF_NonLagMA_v7.1 SW.mq4 |
//|                                Copyright © 2007, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_width1 2
#property indicator_color2 DarkViolet
#property indicator_width2 2
#property indicator_color3 Yellow
#property indicator_width3 2
#property indicator_maximum 2
#property indicator_minimum 0
//---- input parameters
extern int     TimeFrame      = 0;
extern int     Price          = 0;
extern int     Length         = 9;
extern double  PctFilter      = 0;
extern double  IndicatorValue = 1;

//---- indicator buffers
double Signal[];
double UpBuffer[];
double DnBuffer[];
double trend[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,Signal);
   SetIndexArrow(0,167);
   
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(1,UpBuffer);
   SetIndexArrow(1,167);
   
   
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexBuffer(2,DnBuffer);
   SetIndexArrow(2,167);
   
   SetIndexBuffer(3,trend);
    
   string short_name;
//---- 
   switch(TimeFrame)
   {
      case 1      : string TF="M1"; break;
      case 5      : TF="M5"; break;
      case 15     : TF="M15"; break;
      case 30     : TF="M30"; break;
      case 60     : TF="H1"; break;
      case 240    : TF="H4"; break;
      case 1440   : TF="D1"; break;
      case 10080  : TF="W1"; break;
      case 43200  : TF="MN1"; break;
      default     : TF="Current";
   }
   
//---- name for DataWindow and indicator subwindow label
   short_name="MTF_NonLagMA_v7.1SW("+TF+","+Price+","+Length+","+DoubleToStr(PctFilter,2)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Signal");
   SetIndexLabel(1,"Up");
   SetIndexLabel(2,"Dn");
//----
   SetIndexDrawBegin(0,5*Length);
   SetIndexDrawBegin(1,5*Length);
   SetIndexDrawBegin(2,5*Length);
//----

   return(0);
  }

//+------------------------------------------------------------------+
//| MTF_NonLagMA_v7.1SW                                                      |
//+------------------------------------------------------------------+
int start()
{
   datetime TimeArray[];
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   if(TimeFrame==0) TimeFrame = Period();
   
   if ( counted_bars > 0 )  limit=Bars-counted_bars+TimeFrame/Period();
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-TimeFrame/Period(); 
   if ( counted_bars < 1 ) 
   
   for(i=0;i<Bars-1;i++)//for(i=1;i<5*Length;i++) 
   {
   Signal[i]=0;//IndicatorValue;    
   UpBuffer[i]=0;//IndicatorValue;  
   DnBuffer[i]=0;//IndicatorValue;  
   }
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
   
   for(shift=0,int y=0;shift<limit;shift++)
   {
   if (Time[shift]<TimeArray[y]) y++;
   trend[shift] = iCustom(NULL,TimeFrame,"NonLagMA_v7.1",Price,Length,0,PctFilter,1,0,0,0,0,3,y);
   }
   
   for(shift=limit;shift>=0;shift--)
   {
      if (trend[shift]>0)
      {  
         if (trend[shift+1]<0) 
         {
         Signal[shift]=IndicatorValue; 
         UpBuffer[shift] = EMPTY_VALUE;
         }
         else 
         {
         Signal[shift]=EMPTY_VALUE;
         UpBuffer[shift] = IndicatorValue;
         }
      DnBuffer[shift] = EMPTY_VALUE;
      }
      else 
      if (trend[shift]<0) 
      {
      
         if (trend[shift+1]>0) 
         { 
         Signal[shift]=IndicatorValue;
         DnBuffer[shift] = EMPTY_VALUE;
         }
         else 
         {
         Signal[shift]=EMPTY_VALUE;
         DnBuffer[shift] = IndicatorValue;
         }
      
      UpBuffer[shift] = EMPTY_VALUE;
      }
   }
   
	return(0);	
}

