//+------------------------------------------------------------------+
//|                                                 AddSignal_v1.mq4 |
//|                           Copyright © 2007, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Red


//---- indicator parameters
extern string x              = "Jurik Settings";
extern int    FastLength     = 5;
extern int    SlowLength     = 10;
extern string xx             = "Jurik STC Settings";
extern int    Phase          = 0;
extern int    STCPeriod      = 10;
extern int    FastMAPeriod   = 23;
extern int    FastMAPhase    = 0;
extern int    SlowMAPeriod   = 50;
extern int    SlowMAPhase    = 0;
extern int    FilterMode     = 3;
extern string N_RSXPeriod    = "JRSX Settings";
extern int    RsxLength      = 14;
extern int    Price          = 0;
extern int    SmoothLength   = 5;
extern int    SmoothPhase    = 0;
extern int    AlertMode      = 0;    // Alert Mode: 0-off,1-on
extern string note7          = "Arrow Type";
extern string note8          = "0=Thick, 1=Thin, 2=Hollow, 3=Round";
extern string note9          = "4=Fractal, 5=Diagonal Thin";
extern string note10         = "6=Diagonal Thick, 7=Diagonal Hollow";
extern string note11         = "8=Thumb, 9=Finger";
extern int    ArrowType      = 1;
extern int    arrowsize      = 1;

//---- indicator buffers
double CrossUp[];
double CrossDown[]; 
double trend[];

bool   UpTrendAlert=false, DownTrendAlert=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
      
   if (ArrowType == 0) {
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 234);
   }
   else if (ArrowType == 1) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 225);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 226);
   }
   else if (ArrowType == 2) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 241);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 242);
   }
   else if (ArrowType == 3) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 221);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 222);
   }
   else if (ArrowType == 4) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 217);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 218);
   }
   else if (ArrowType == 5) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 228);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 230);
   }
   else if (ArrowType == 6) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 236);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 238);
   }
   else if (ArrowType == 7) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 246);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 248);
   }
   else if (ArrowType == 8) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 67);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 68);
   }
   else if (ArrowType == 9) { 
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW,0,arrowsize); SetIndexArrow(0, 71);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW,0,arrowsize); SetIndexArrow(1, 72);
   }
   SetIndexBuffer(2,trend);

   return(0);
}
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   int CountedBars=IndicatorCounted();
   if (CountedBars<0) return(-1);
   if (CountedBars>0) CountedBars--;
   limit=Bars-CountedBars;
//---- 
   for(int i=limit; i>=0; i--)
   { 
   double RSI = iCustom(Symbol(),0,"rsx on jurik smooth-1",RsxLength,Price,SmoothLength,SmoothPhase,0,i);
   
   double haMA1 = iCustom(Symbol(),0,"Jurik filter simple 1",FastLength,Price,Phase,0,i);
   double haMA2 = iCustom(Symbol(),0,"Jurik filter simple 1",SlowLength,Price,Phase,0,i);

   
   double MACD    = iCustom(Symbol(),0,"jurik STC nrp",STCPeriod,FastMAPeriod,FastMAPhase,SlowMAPeriod,SlowMAPhase,FilterMode,0,i);
   double SigMACD = iCustom(Symbol(),0,"jurik STC nrp",STCPeriod,FastMAPeriod,FastMAPhase,SlowMAPeriod,SlowMAPhase,FilterMode,0,i+1);
   
   trend[i]=trend[i+1]; 
   if (RSI > 50 && haMA1>haMA2 && MACD>SigMACD) trend[i]= 1; 
   if (RSI < 50 && haMA1<haMA2 && MACD<SigMACD) trend[i]=-1; 
      
      if(trend[i]>0)
	   {
	   if (trend[i+1]<0) CrossUp[i] = Low[i]-0.5*iATR(NULL,0,10,i);
	   CrossDown[i]=EMPTY_VALUE; 
	   }
	   else
	   if(trend[i]<0)
	   {
	   if (trend[i+1]>0) CrossDown[i] = High[i]+0.5*iATR(NULL,0,10,i);
	   CrossUp[i]=EMPTY_VALUE; 
	   }
   	         
//----------   
      if (i==0 && AlertMode > 0)
      {
         if (trend[i+1] > 0 && Volume[0]>1)
         {
            if (trend[i+2]<0 && !UpTrendAlert)
	         {
	         string Message = " "+Symbol()+" M"+Period()+": Signal for BUY";
	         Alert (Message); 
	         UpTrendAlert=true; DownTrendAlert=false;
	         } 
	      }
	      else
	      if (trend[i+1] < 0 && Volume[0]>1)
	      {
            if ( trend[i+2]>0 && !DownTrendAlert)
	         {
	         Message = " "+Symbol()+" M"+Period()+": Signal for SELL";
	         Alert (Message); 
	         DownTrendAlert=true; UpTrendAlert=false;     
	         }
	      } 	  
      }	
	} 	         

//---- done
   return(0);
}
//+------------------------------------------------------------------+