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


#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Orange
#property indicator_width1 2
#property indicator_color2 DodgerBlue
#property indicator_width2 4
#property indicator_color3 Magenta
#property indicator_width3 4
//---- input parameters
/*****************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
********************************************************************/
//---- 
extern int TimeFrame=60;
extern int     Price          = 0;  //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
extern int     Length         = 9;  //Period of NonLagMA
extern int     Displace       = 0;  //DispLace or Shift 
extern double  PctFilter      = 0;  //Dynamic filter in decimal
extern int     Color          = 1;  //Switch of Color mode (1-color)  
extern int     ColorBarBack   = 1;  //Bar back for color mode
extern double  Deviation      = 0;  //Up/down deviation        
extern int     AlertMode      = 0;  //Sound Alert switch (0-off,1-on) 
extern int     WarningMode    = 0;  //Sound Warning switch(0-off,1-on) 
extern string note1 = "Price(OCHLMTF)Length(period)Displace(shift)Color(0/1)";

//---- indicator buffers
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double trend[];
double Del[];
double AvgDel[];

double alfa[];
int i, Phase, Len,Cycle=4;
double Coeff, beta, t, Sum, Weight, g;
double pi = 3.1415926535;    
bool   UpTrendAlert=false, DownTrendAlert=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(6);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MABuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,UpBuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,DnBuffer);
   SetIndexBuffer(3,trend);
   SetIndexBuffer(4,Del);
   SetIndexBuffer(5,AvgDel); 
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="NonLagMA["+Length+"]TF("+TimeFrame+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,""+short_name+"");
   SetIndexLabel(1,""+short_name+"");
   SetIndexLabel(2,""+short_name+"");
//----
   SetIndexShift(0,Displace*TimeFrame/Period());
   SetIndexShift(1,Displace*TimeFrame/Period());
   SetIndexShift(2,Displace*TimeFrame/Period());
   
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexEmptyValue(2,EMPTY_VALUE);
   
   SetIndexDrawBegin(0,Length*Cycle+Length+1);
   SetIndexDrawBegin(1,Length*Cycle+Length+1);
   SetIndexDrawBegin(2,Length*Cycle+Length+1);
//----
   Coeff =  3*pi;
   Phase = Length-1;
   Len = Length*4 + Phase;  
   ArrayResize(alfa,Len);
   Weight=0;    
      
      for (i=0;i<Len-1;i++)
      {
      if (i<=Phase-1) t = 1.0*i/(Phase-1);
      else t = 1.0 + (i-Phase+1)*(2.0*Cycle-1.0)/(Cycle*Length-1.0); 
      beta = MathCos(pi*t);
      g = 1.0/(Coeff*t+1);   
      if (t <= 0.5 ) g = 1;
      alfa[i] = g * beta;
      Weight += alfa[i];
      }
//----
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="Period_M1"; break;
      case 5 : TimeFrameStr="Period_M5"; break;
      case 15 : TimeFrameStr="Period_M15"; break;
      case 30 : TimeFrameStr="Period_M30"; break;
      case 60 : TimeFrameStr="Period_H1"; break;
      case 240 : TimeFrameStr="Period_H4"; break;
      case 1440 : TimeFrameStr="Period_D1"; break;
      case 10080 : TimeFrameStr="Period_W1"; break;
      case 43200 : TimeFrameStr="Period_MN1"; break;
      default : TimeFrameStr="Current Timeframe";
   } 
 //--  
   return(0);
  }
//+------------------------------------------------------------------+
//|   MTF_NonLagMA_v7.1                                              |
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,shift,limit,y=0,counted_bars=IndicatorCounted();
    
// Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
      limit=Bars-counted_bars+TimeFrame/Period();
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; 
   
 /***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator timeframe
   Rule 3:  Use 'y' for the indicator's shift value
 **********************************************************/  
    MABuffer[i]=iCustom(NULL,TimeFrame,"NonLagMA_v7.1", Price,Length,Displace,
                 PctFilter,Color,ColorBarBack,Deviation,AlertMode,WarningMode,0,y);
    UpBuffer[i]=iCustom(NULL,TimeFrame,"NonLagMA_v7.1", Price,Length,Displace,
                 PctFilter,Color,ColorBarBack,Deviation,AlertMode,WarningMode,1,y);
    DnBuffer[i]=iCustom(NULL,TimeFrame,"NonLagMA_v7.1", Price,Length,Displace,
                 PctFilter,Color,ColorBarBack,Deviation,AlertMode,WarningMode,2,y);
     }  
 //----  Refresh buffers +++++++++++++++++++++ upgrade by  Raff  
   if (TimeFrame>Period()) {
     int PerINT=TimeFrame/Period()+1;
     datetime TimeArr[]; ArrayResize(TimeArr,PerINT);
     ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period()); 
     for(i=0;i<PerINT+1;i++) {if (TimeArr[i]>=TimeArray[0]) {
//----
 /***********************************************************    
    Refresh buffers:         buffer[i] = buffer[0];
 ************************************************************/  

        MABuffer[i]= MABuffer[0];
        UpBuffer[i]= UpBuffer[0];
        DnBuffer[i]= DnBuffer[0];

//----
   } } }
//+++++++++++++++++++++++++++++++++++++++++++++  Raff  ++++++
   return(0);
  }