Navigation:Home > Content >

NonLagZigZag_v4_mod-mtf.mq4

Time: 2018-07-20 | Download file:NonLagZigZag_v4_mod-mtf.mq4

//+------------------------------------------------------------------+
//|                                              NonLagZigZag_v4.mq4 |
//|                                Copyright © 2007, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Gold
#property indicator_width1 2

extern string  TimeFrame      = "Current time frame";
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         =   14;  //Period of NonLagMA
extern double  PctFilter      =    2;  //Dynamic filter in decimals
extern int     PriceMode      =    0;  //Price Mode: 0-High/Low, 1-Close
extern int     AlertMode      =    0;  //Alert Mode: 0-off,1-on
extern int     WarningMode    =    0;  //Warning Mode: 0-off,1-on
extern bool    ShowFibo       =    true;

extern bool    ShowFiboLevel1 =    true;
extern double  FiboLevel1     =    0.0;
extern bool    ShowFiboLevel2 =    true;
extern double  FiboLevel2     =    0.236;
extern bool    ShowFiboLevel3 =    true;
extern double  FiboLevel3     =    0.382;
extern bool    ShowFiboLevel4 =    true;
extern double  FiboLevel4     =    0.500;
extern bool    ShowFiboLevel5 =    true;
extern double  FiboLevel5     =    0.618;
extern bool    ShowFiboLevel6 =    true;
extern double  FiboLevel6     =    1.00;
extern bool    ShowFiboLevel7 =    true;
extern double  FiboLevel7     =    1.618;
extern bool    ShowFiboLevel8 =    true;
extern double  FiboLevel8     =    2.618;
extern bool    ShowFiboLevel9 =    true;
extern double  FiboLevel9     =    4.236;

extern color   FiboLineColor  = CLR_NONE;  
extern color   FiboLevelColor = Aqua;
extern int     FiboLineStyle  =    0;
extern int     FiboLevelStyle =    0;

double ZZBuffer[];
double MABuffer[];
double trend[];
double Del[];
double AvgDel[];

int      ilow, ihigh, nlow, nhigh, prevnhigh,prevnlow, BarsBack;  
double   alfa[];
datetime lotime,hitime;
int      i, Phase, Len, Cycle=4, Back=0;
double   Coeff, beta, t, Sum, Weight, g;
double   pi = 3.1415926535;
int      pt1=0,pt0=0;
double   pzz1=0,pzz0=0, minzz=0, maxzz=0; 
bool     UpTrendAlert=false, DownTrendAlert=false;
bool     fTime=true;

string indicatorFileName;
bool   returnBars;
int    timeFrame;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,ZZBuffer);
   SetIndexBuffer(1,MABuffer);
   SetIndexBuffer(2,trend);
   SetIndexBuffer(3,Del);
   SetIndexBuffer(4,AvgDel); 
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="NonLagZigZag("+Length+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"NonLagZigZag");
//----
   SetIndexEmptyValue(0,0.0);   
   SetIndexDrawBegin(0,Length*Cycle+Length);
//----
   
   Coeff =  3*pi;
   Phase = Length-1;
   Len = Length*Cycle + Phase;  
   ArrayResize(alfa,Len);
   Weight=0;    
      
      for (i=0;i0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { ZZBuffer[0] = limit+1; return(0); }
   
            if (timeFrame!=Period())
            {
               limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
               for (int i=limit; i>=0; i--)
               {
                   int y = iBarShift(NULL,timeFrame,Time[i]);             
                      ZZBuffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Price,Length,PctFilter,PriceMode,AlertMode,WarningMode,ShowFibo,
                                    ShowFiboLevel1,FiboLevel1,ShowFiboLevel2,FiboLevel2,ShowFiboLevel3,FiboLevel3,ShowFiboLevel4,FiboLevel4,ShowFiboLevel5,FiboLevel5,
                                    ShowFiboLevel6,FiboLevel6,ShowFiboLevel7,FiboLevel7,ShowFiboLevel8,FiboLevel8,ShowFiboLevel9,FiboLevel9,FiboLineColor,FiboLevelColor,
                                    FiboLineStyle,FiboLevelStyle,0,y);
               }
               return(0);
            }
   int    shift;
   double price,smin,smax;
   string aMessage="", wMessage="";
         
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1; 
   if ( counted_bars < 1 ) 
   {
   for(i=0;i=0;shift--) 
   {	
      Sum = 0;
      for (i=0;i<=Len-1;i++)
	   { 
      price = iMA(NULL,0,1,0,3,Price,i+shift);      
      Sum += alfa[i]*price;
      }
   
	if (Weight > 0) MABuffer[shift] = Sum/Weight;
   
   Del[shift] = MathAbs(MABuffer[shift] - MABuffer[shift+1]);
   
   double sumdel=0;
   for (i=0;i<=Length-1;i++) sumdel += Del[shift+i];
   
   AvgDel[shift] = sumdel/Length;
   
   double sumpow = 0;
   for (i=0;i<=Length-1;i++) sumpow += MathPow(Del[shift+i]-AvgDel[shift+i],2);
   double StdDev = MathSqrt(sumpow/Length); 
   
   double Filter = PctFilter * StdDev;       
   if (Filter < Point) Filter = Point;
   
   if( MathAbs(MABuffer[shift]-MABuffer[shift+1]) < Filter ) MABuffer[shift]=MABuffer[shift+1];
   
   trend[shift]=trend[shift+1];
   if (MABuffer[shift]-MABuffer[shift+1] > Filter) trend[shift]= 1;   
   if (MABuffer[shift+1]-MABuffer[shift] > Filter) trend[shift]=-1;
   
      if(trend[shift]>0)
      {
         if( trend[shift]!=trend[shift+1]) 
         {
         ilow = LowestBar(iBarShift(NULL,0,hitime,FALSE)-shift,shift);
         lotime = Time[ilow];
          
            if(PriceMode==0) ZZBuffer[ilow] = Low[ilow];
            else 
            ZZBuffer[ilow] = MathMin(Close[ilow],Open[ilow]); 
         }
         else
         if (shift==0) 
         {
         int hilen = iBarShift(NULL,0,lotime,FALSE);
         nhigh = HighestBar(hilen,0);
         
            if(PriceMode==0) ZZBuffer[nhigh] = High[nhigh];
            else   
            ZZBuffer[nhigh]=MathMax(Close[nhigh],Open[nhigh]);
         
         if (nhigh== 0) for (i=hilen-1;i>=1;i--) ZZBuffer[i]=0; 
         if (nhigh > 0) for (i=nhigh-1;i>=0;i--) ZZBuffer[i]=0; 
         }
      }
      
      if (trend[shift]<0)
      { 
         if( trend[shift]!=trend[shift+1]) 
         {
         ihigh = HighestBar(iBarShift(NULL,0,lotime,FALSE)-shift,shift);
         hitime=Time[ihigh];
            if(PriceMode==0) ZZBuffer[ihigh] = High[ihigh];
            else   
            ZZBuffer[ihigh] = MathMax(Close[ihigh],Open[ihigh]);
         }
         else
         if (shift==0) 
         {
         int lolen = iBarShift(NULL,0,hitime,FALSE);
         nlow = LowestBar(lolen,0);
            if(PriceMode==0) ZZBuffer[nlow] = Low[nlow];
            else 
            ZZBuffer[nlow] = MathMin(Close[nlow],Open[nlow]);
         if (nlow==0) for (i=lolen-1;i>=1;i--) ZZBuffer[i]=0; 
         if (nlow >0) for (i=nlow-1;i>=0;i--) ZZBuffer[i]=0; 
         }
      }   
      
      if(shift == 0 && (ShowFibo == true || AlertMode == 1 || WarningMode == 1))
      {
         if(trend[shift] > 0) 
         {
         int t1 = Time[nhigh]; 
         double zz1 = ZZBuffer[hilen]; 
         int t0 = Time[nhigh]; 
         double zz0 = ZZBuffer[nhigh];
         }
         else
         if(trend[shift] < 0)
         {
         t1 = Time[nlow]; 
         zz1 = ZZBuffer[lolen]; 
         t0 = Time[nlow]; 
         zz0 = ZZBuffer[nlow]; 
         }
                
         zz0 = NormalizeDouble(zz0,Digits);
                 
         if (ShowFibo == true && (t1 != pt1 || t0 != pt0 || zz1 != pzz1 || zz0 != pzz0))
         {
         ObjectDelete("FiboNLZZ" + Period());
      
         ObjectCreate("FiboNLZZ" + Period(), OBJ_FIBO, 0, t1, zz1, t0, zz0);
            
         ObjectSet("FiboNLZZ" + Period(),OBJPROP_FIBOLEVELS, 9);
         if (ShowFiboLevel1) {    
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+0, FiboLevel1);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 0, DoubleToStr(FiboLevel1*100,1)+" %$ "); }
         if (ShowFiboLevel2) {   
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+1, FiboLevel2);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 1, DoubleToStr(FiboLevel2*100,1)+" %$ "); }
         if (ShowFiboLevel3) {   
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+2, FiboLevel3);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 2, DoubleToStr(FiboLevel3*100,1)+" %$ "); }
         if (ShowFiboLevel4) {   
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+3, FiboLevel4);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 3, DoubleToStr(FiboLevel4*100,1)+" %$ "); }
         if (ShowFiboLevel5) {    
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+4, FiboLevel5);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 4, DoubleToStr(FiboLevel5*100,1)+" %$ "); }
         if (ShowFiboLevel6) {   
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+5, FiboLevel6);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 5, DoubleToStr(FiboLevel6*100,1)+" %$ "); }
         if (ShowFiboLevel7) {      
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+6, FiboLevel7);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 6, DoubleToStr(FiboLevel7*100,1)+" %$ "); }
         if (ShowFiboLevel8) {   
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+7, FiboLevel8);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 7, DoubleToStr(FiboLevel8*100,1)+" %$ "); }
         if (ShowFiboLevel9) {   
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_FIRSTLEVEL+8, FiboLevel9);
         ObjectSetFiboDescription("FiboNLZZ" + Period(), 8, DoubleToStr(FiboLevel9*100,1)+" %$ "); }
      
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_COLOR, FiboLineColor);
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_LEVELCOLOR, FiboLevelColor);
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_STYLE, FiboLineStyle);
         ObjectSet("FiboNLZZ" + Period(), OBJPROP_LEVELSTYLE, FiboLevelStyle);
         }
                  
         if (AlertMode > 0)
         {
            if (trend[shift+2]<0 && trend[shift+1]>0 && Volume[0]>1 && !UpTrendAlert)
            {
	         aMessage = " "+Symbol()+" M"+Period()+": NonLagZigZag is UP";
	         if(aMessage != "") Alert (aMessage); 
	         UpTrendAlert=true; DownTrendAlert=false;
	         } 
	 	      else
	         if (trend[shift+2]>0 && trend[shift+1]<0 && Volume[0]>1 && !DownTrendAlert)
            {
	         aMessage = " "+Symbol()+" M"+Period()+": NonLagZigZag is DOWN";
	         if(aMessage != "") Alert (aMessage); 
	         DownTrendAlert=true; UpTrendAlert=false;
	         }
         }	          	         
         
         if (WarningMode == 1)
         {
            if(fTime)
            {
            maxzz=zz0;
            minzz=zz0;
            fTime = false;
            }
                       
            if(trend[shift] > 0 && zz0 > maxzz)
            {
            wMessage = " "+Symbol()+" M"+Period()+": NonLagZigZag forms new HIGH="+DoubleToStr(zz0,Digits);
            if(wMessage != "") Alert (wMessage);
            maxzz = zz0;
            }
            else
            if(trend[shift] < 0 && zz0 < minzz)
            {
            wMessage = " "+Symbol()+" M"+Period()+": NonLagZigZag forms new LOW="+DoubleToStr(zz0,Digits);
            if(wMessage != "") Alert (wMessage);
            minzz = zz0;
            } 
         }
         
      pt1 = t1;
      pt0 = t0;
      pzz1 = zz1;
      pzz0 = zz0;
      }  
   }
   return(0);	
}

int LowestBar(int len,int k)
{
   double min = 10000000;   
   int lobar;
   
   for (int i=k+len-1;i>=k;i--)
   {
      if(PriceMode==0)
      {
         if(i==0)
         { 
            if(Low[i] < min) 
            {
            min = Low[i]; 
            lobar = i;
            }
         }
         else
         {
            if(Low[i] < min && Low[i] < Low[i-1]) 
            {
            min = Low[i]; 
            lobar = i;
            }
         }   
      }      
      else
      {
         if(i==0)
         { 
            if(MathMin(Close[i],Open[i]) < min) 
            {
            min = MathMin(Close[i],Open[i]); 
            lobar = i;
            }
         }
         else
         {
            if(MathMin(Close[i],Open[i]) < min && MathMin(Close[i],Open[i]) < MathMin(Close[i-1],Open[i-1])) 
            {
            min = MathMin(Close[i],Open[i]); 
            lobar = i;
            }
         }   
      }         
   }
   
   if(len<=0) lobar=k;
   
   return(lobar);
} 

int HighestBar(int len,int k)
{
   double max = -10000000;   
   int hibar;
   
   for (int i=k+len-1;i>=k;i--)
   {
      if(PriceMode==0)
      {
         if(i==0)
         {    
            if(High[i] > max) 
            {
            max = High[i]; 
            hibar = i;
            }
         }
         else
         {
            if(High[i] > max && High[i] > High[i-1]) 
            {
            max = High[i]; 
            hibar = i;
            }
         }
      }   
      else
      {
         if(i==0)
         {       
            if(MathMax(Close[i],Open[i]) > max && MathMax(Close[i],Open[i]) > MathMax(Close[i-1],Open[i-1])) 
            {
            max = MathMax(Close[i],Open[i]); 
            hibar = i;
            }
         }
         else
         {
            if(MathMax(Close[i],Open[i]) > max) 
            {
            max = MathMax(Close[i],Open[i]); 
            hibar = i;
            }
         }
      }
   }
   
   if(len<=0) hibar=k;
  
   return(hibar);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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 char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}
                                     

Recommend