Navigation:Home > Content >

UniZigZagSemaphore_v4.mq4

Time: 2013-07-25 | Download file:UniZigZagSemaphore_v4.mq4

//+------------------------------------------------------------------+
//|                                        UniZigZagSemaphore_v4.mq4 |
//|                                Copyright © 2013, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: [email protected] |
//+------------------------------------------------------------------+


#property copyright "Copyright © 2013, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 LightPink
#property indicator_color2 LightBlue
#property indicator_color3 Coral
#property indicator_color4 CornflowerBlue
#property indicator_color5 Tomato
#property indicator_color6 Turquoise
#property indicator_color7 OrangeRed
#property indicator_color8 RoyalBlue


#property indicator_width1 0
#property indicator_width2 0
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 2
#property indicator_width6 2
#property indicator_width7 3
#property indicator_width8 3


//

//---- input parameters
extern int     UpBandPrice       =                    2;  //Upper Band Price(0...6)
extern int     LoBandPrice       =                    3;  //Lower Band Price(0...6) 
extern string  ReversalValues    = "0.25;0.382;0.5;1.0";  //Reversal Values according to Retrace Method
extern int     RetraceMethod     =                    1;  //Retrace Method: 0-channel,1-% of Price,2Price Change in pips,3-Ratio of ATR(50)
extern bool    ShowZigZag        =                false;
extern int     AlertMode         =                    0;
extern int     SoundsNumber      =                    5;   //Number of sounds after Signal
extern int     SoundsPause       =                    5;   //Pause in sec between sounds 
extern string  UpSound           =          "alert.wav";
extern string  DnSound           =         "alert2.wav";
extern int     EmailMode         =                    0;
extern int     EmailsNumber      =                    1;


double upZZ1[];
double dnZZ1[];  
double upZZ2[];
double dnZZ2[];  
double upZZ3[];
double dnZZ3[];  
double upZZ4[];
double dnZZ4[];  

int      trend[][3];  
datetime prevtime[], hiTime[][2], loTime[][2];
double   periods[], upBand[][2], loBand[][2], hiValue[][2], loValue[][2], _point;
string   TF, IndicatorName, prevmess, prevemail;
datetime pTime, preTime, ptime;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   IndicatorName = WindowExpertName(); 
   IndicatorShortName(IndicatorName+"("+ReversalValues+")");
    
   stringToDoubleArray(ReversalValues,";",periods);
      
   SetIndexBuffer(0,upZZ1); 
   SetIndexBuffer(1,dnZZ1); 
   SetIndexBuffer(2,upZZ2); 
   SetIndexBuffer(3,dnZZ2); 
   SetIndexBuffer(4,upZZ3); 
   SetIndexBuffer(5,dnZZ3); 
   SetIndexBuffer(6,upZZ4); 
   SetIndexBuffer(7,dnZZ4); 
   
   int zznum = ArraySize(periods); 
   
   for(int i=0;i 0) limit = Bars - counted_bars - 1;     
   if (counted_bars < 1) 
   {
   limit=Bars-1;    
      for(i=0;i 0) _uniZigZag(upZZ1,dnZZ1,0,periods[0],limit,counted_bars);
   if(periods[1] > 0) _uniZigZag(upZZ2,dnZZ2,1,periods[1],limit,counted_bars);
   if(periods[2] > 0) _uniZigZag(upZZ3,dnZZ3,2,periods[2],limit,counted_bars);  
   if(periods[3] > 0) _uniZigZag(upZZ4,dnZZ4,3,periods[3],limit,counted_bars);
     
   return(0);   
}

void _uniZigZag(double& upZZ[],double& dnZZ[],int index,double retrace,int limit,int counted_bars)
{
   int i, nlow, nhigh;
   double hi, lo;
   
   
   for(int shift=limit;shift>=0;shift--) 
   {	
      if(prevtime[index] != Time[shift])
      {
      hiTime[index][1]  = hiTime[index][0];
      loTime[index][1]  = loTime[index][0];
      upBand[index][1]  = upBand[index][0];
      loBand[index][1]  = loBand[index][0];
      hiValue[index][1] = hiValue[index][0];
      loValue[index][1] = loValue[index][0];
      trend[index][2]   = trend[index][1];
      trend[index][1]   = trend[index][0];
      prevtime[index]   = Time[shift];
      }
               
      if(shift < Bars - retrace)
      {
      hiTime[index][0]  = hiTime[index][1];
      loTime[index][0]  = loTime[index][1];
      upBand[index][0]  = upBand[index][1];
      loBand[index][0]  = loBand[index][1];   
      hiValue[index][0] = hiValue[index][1];
      loValue[index][0] = loValue[index][1];   
      trend[index][0]   = trend[index][1];
      
      hi  = iMA(NULL,0,1,0,0,UpBandPrice,shift); 
      lo  = iMA(NULL,0,1,0,0,LoBandPrice,shift);            
          
         
         switch(RetraceMethod)
         {
         case 0: upBand[index][0] = iMA(NULL,0,1,0,0,UpBandPrice,HighestBar(retrace,shift,0)); //iHighest(NULL,0,UpBandPrice,retrace,shift));
                 loBand[index][0] = iMA(NULL,0,1,0,0,LoBandPrice, LowestBar(retrace,shift,0)); //iLowest (NULL,0,LoBandPrice,retrace,shift)); 
                 break;
         
         case 1: if(hi > upBand[index][0])   
                 {
                 upBand[index][0] = hi;
                 loBand[index][0] = upBand[index][0] * (1-0.01*retrace); 
                 }
                 
                 if(lo < loBand[index][0])  
                 {
                 loBand[index][0] = lo;
                 upBand[index][0] = loBand[index][0] * (1+0.01*retrace); 
                 }
                 break;
         
         case 2: if(hi >= upBand[index][0])   
                 {
                 upBand[index][0] = hi;
                 loBand[index][0] = upBand[index][0] - retrace *_Point; 
                 }
                 
                 if(lo <= loBand[index][0])  
                 {
                 loBand[index][0] = lo;
                 upBand[index][0] = loBand[index][0] + retrace *_Point; 
                 }
                 break;
                     
         case 3: double atr = iATR(NULL,0,50,shift);
                 
                 if(hi >= upBand[index][0])   
                 {
                 upBand[index][0] = hi;
                 loBand[index][0] = upBand[index][0] - retrace * atr; 
                 }
                 
                 if(lo <= loBand[index][0])  
                 {
                 loBand[index][0] = lo;
                 upBand[index][0] = loBand[index][0] + retrace * atr; 
                 }
                 break;        
         }

         
      
         if(lo < loBand[index][1] && (hi <= upBand[index][1] ||(hi > upBand[index][1] && Close[shift] < Close[shift+1])) && trend[index][0] >= 0 && loBand[index][1] > 0) 
         {
         trend[index][0] =-1; 
         
         int hibar = HighestBar(iBarShift(NULL,0,loTime[index][0],FALSE)-shift,shift,1);
         
         hiValue[index][0] = iMA(NULL,0,1,0,0,UpBandPrice,hibar);
         hiTime[index][0]  = Time[hibar];
      
         upZZ[hibar] = hiValue[index][0]; 
         }
         //else
         if(hi > upBand[index][1] && (lo >= loBand[index][1] ||(lo < loBand[index][1] && Close[shift] > Close[shift+1])) && trend[index][0] <= 0 && upBand[index][1] > 0) 
         {
         trend[index][0] = 1; 
          
         int lobar = LowestBar(iBarShift(NULL,0,hiTime[index][0],FALSE) - shift,shift,1);
      
         loValue[index][0] = iMA(NULL,0,1,0,0,LoBandPrice,lobar);
         loTime[index][0] = Time[lobar];
      
         dnZZ[lobar] = loValue[index][0];
         }   
         
         
         
         if(shift == 0)
         { 
         upZZ[shift] = 0;
         dnZZ[shift] = 0;     
            
            if(trend[index][0] > 0) 
            {
            int hilen = iBarShift(NULL,0,loTime[index][0],FALSE);
            nhigh = HighestBar(hilen,0,1);
            for (i=hilen;i>=0;i--) upZZ[i] = 0; 
            upZZ[nhigh] = iMA(NULL,0,1,0,0,UpBandPrice,nhigh);
            }   

            if(trend[index][0] < 0)
            { 
            int lolen = iBarShift(NULL,0,hiTime[index][0],FALSE);
            nlow = LowestBar(lolen,0,1);
            for (i=lolen;i>=0;i--) dnZZ[i] = 0; 
            dnZZ[nlow] = iMA(NULL,0,1,0,0,LoBandPrice,nlow);
            }
         }
      }
   }

   if(AlertMode > 0)
   {
   bool upsignal = trend[index][1] > 0 && trend[index][2] <= 0;                  
   bool dnsignal = trend[index][1] < 0 && trend[index][2] >= 0;
               
      if(upsignal || dnsignal)
      {
      
         if(isNewBar(Period()))
         {
         BoxAlert(upsignal,"ZigZag No."+(index+1)+" : UpTrend Signal, Open: "+DoubleToStr(Open[0],Digits));   
         BoxAlert(dnsignal,"ZigZag No."+(index+1)+" : DownTrend Signal, Open: "+DoubleToStr(Open[0],Digits)); 
         }
      
      WarningSound(upsignal,SoundsNumber,SoundsPause,UpSound,Time[1]);
      WarningSound(dnsignal,SoundsNumber,SoundsPause,DnSound,Time[1]);
         
         if(EmailMode > 0)
         {
         EmailAlert(upsignal,"ZigZag No."+(index+1)+" UpTrend!","  : UpTrend Signal, Open: "+DoubleToStr(Open[0],Digits),EmailsNumber); 
         EmailAlert(dnsignal,"ZigZag No."+(index+1)+" DownTrend!"," : DownTrend Signal, Open: "+DoubleToStr(Open[0],Digits),EmailsNumber); 
         }
      }
   }
}



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

int LowestBar(int len,int k,int opt)
{
   double min = 10000000;   
   
   if(len <= 0) int lobar = k;
   else   
   for (int i=k+len-1;i>=k;i--)
   {
   double lo0 = iMA(NULL,0,1,0,0,LoBandPrice,i);
   if(opt==1) double lo1 = iMA(NULL,0,1,0,0,LoBandPrice,i-1);
   if((opt==1 && (i==0 || (i > 0/*&& lo0 < lo1*/)) && lo0 <= min) || (opt==0 && lo0 <= min)) {min = lo0; lobar = i;}
   }   
   
   return(lobar);
} 

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

int HighestBar(int len,int k,int opt)
{
   double max = -10000000;   
   
   if(len <= 0) int hibar = k;
   else
   for (int i=k+len-1;i>=k;i--)
   {
   double hi0 = iMA(NULL,0,1,0,0,UpBandPrice,i);
   if(opt==1) double hi1 = iMA(NULL,0,1,0,0,UpBandPrice,i-1);  
   if((opt==1 && (i==0 || (i > 0 /*&& hi0 > hi1*/)) && hi0 >= max) || (opt==0 && hi0 >= max)) {max = hi0; hibar = i;}
   }   

   return(hibar);
} 

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

bool stringToDoubleArray(string text,string delim,double& array[],int check = 0) 
{
   int start;
   int pos;
   int cnt;
   
   if(StringFind(text, delim) < 0) 
   {
   ArrayResize(array, 1);
   array[0] = StrToDouble(text);
   } 
   else 
   {
   start = 0;
   pos = 0;
   cnt = 0;
      while(pos > -1) 
      {
      cnt++;
      pos = StringFind(text, delim, start);
      ArrayResize(array, cnt);
         if (pos > -1) 
         {
         if (pos - start > 0) array[cnt - 1] = StrToDouble(StringSubstr(text, start, pos - start));
         } else array[cnt - 1] = StrToDouble(StringSubstr(text, start, 0));
      
      start = pos + 1;
      }
   }
   
   if (check == 0 || check == ArraySize(array)) return (TRUE);
   
   return (FALSE);
}	         
   

string tf(int timeframe)
{
   switch(timeframe)
   {
   case PERIOD_M1:   return("M1");
   case PERIOD_M5:   return("M5");
   case PERIOD_M15:  return("M15");
   case PERIOD_M30:  return("M30");
   case PERIOD_H1:   return("H1");
   case PERIOD_H4:   return("H4");
   case PERIOD_D1:   return("D1");
   case PERIOD_W1:   return("W1");
   case PERIOD_MN1:  return("MN1");
   default:          return("Unknown timeframe");
   }
}

bool isNewBar(int tf)
{
   bool res=false;
   
   if(tf >= 0)
   {
      if (iTime(NULL,tf,0)!= pTime)
      {
      res=true;
      pTime=iTime(NULL,tf,0);
      }   
   }
   else res = true;
   
   return(res);
}

bool BoxAlert(bool cond,string text)   
{      
   string mess = " "+Symbol()+" "+TF + ":" + " " + IndicatorName + text;
   
   if (cond && mess != prevmess)
	{
	Alert (mess);
	prevmess = mess; 
	return(true);
	} 
  
   return(false);  
}

bool Pause(int sec)
{
   if(TimeCurrent() >= preTime + sec) {preTime = TimeCurrent(); return(true);}
   
   return(false);
}

void WarningSound(bool cond,int num,int sec,string sound,datetime ctime)
{
   static int i;
   
   if(cond)
   {
   if(ctime != ptime) i = 0; 
   if(i < num && Pause(sec)) {PlaySound(sound); ptime = ctime; i++;}       	
   }
}

bool EmailAlert(bool cond,string text1,string text2,int num)   
{      
   string subj = "New " + text1 +" Signal from " + IndicatorName + "!!!";    
   string mess = " "+Symbol()+" "+TF + ":" + " " + IndicatorName + text2;
   
   if (cond && mess != prevemail)
	{
	if(subj != "" && mess != "") for(int i=0;i        

Recommend