//+------------------------------------------------------------------+
//|                                       PriceChannel_Signal_v1.mq4 |
//|                           Copyright © 2007, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//|                          digital version with jjma mod. John Last|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"




#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Aqua
#property indicator_color2 Crimson
#property indicator_color3 Aqua
#property indicator_color4 Crimson
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 3
#property indicator_width4 3




extern string timeFrame     = "Current time frame";
extern int     Length       = 7;    // Price Channel Period
extern double  Risk         = 3;    // Risk Factor in units (0...10) 
extern int     UseReEntry   = 0;    // Re-Entry Mode: 0-off,1-on
extern int     AlertMode    = 0;    // Alert Mode: 0-off,1-on
extern int     SmoothLength = 3;
extern int     SmoothPhase  = 5;
extern int     Price        = 0;





double UpSignal[];
double DnSignal[]; 
double UpEntry[];
double DnEntry[]; 
double smax[];
double smin[];
double trend[];
int    TimeFrame;
int    atrTimeFrame;

bool UpTrendAlert  = false;
bool DownTrendAlert= false;
datetime prevTime; 




int init()
{
   
   IndicatorBuffers(7);
   SetIndexBuffer(0,UpSignal); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,108); SetIndexLabel(0,"UpSignal");
   SetIndexBuffer(1,DnSignal); SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,108); SetIndexLabel(1,"DnSignal");
   SetIndexBuffer(2,UpEntry);  SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,241); SetIndexLabel(2,"UpRe-Entry");
   SetIndexBuffer(3,DnEntry);  SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,242); SetIndexLabel(3,"DnRe-Entry");
   SetIndexBuffer(4,smax);
   SetIndexBuffer(5,smin);
   SetIndexBuffer(6,trend);
   
   TimeFrame = stringToTimeFrame(timeFrame);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
   return(0);
}


int start()
{
   
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=MathMax(MathMin(Bars-counted_bars,Bars-1),TimeFrame/Period());   
         
   for(i=limit; i>=0; i--)
   { 
   int y = iBarShift(NULL,TimeFrame   ,Time[i]);
   
   double price  = iSmooth(iMA(NULL,TimeFrame,1,0,MODE_SMA,   Price,y  ),SmoothLength,SmoothPhase,y,   0);
   double price1 = iSmooth(iMA(NULL,TimeFrame,1,0,MODE_SMA,   Price,y+1),SmoothLength,SmoothPhase,y+1,10);
   double price2 = iSmooth(iMA(NULL,TimeFrame,1,0,MODE_SMA,   Price,y+2),SmoothLength,SmoothPhase,y+2,20);
   double hi = iSmooth(High[iHighest(NULL,TimeFrame,MODE_HIGH,Length,y)],SmoothLength,SmoothPhase,y,  30);   
   double lo = iSmooth(Low[iLowest(NULL,  TimeFrame,MODE_LOW, Length,y)],SmoothLength,SmoothPhase,y,  40); 
   
   smax[i] = hi - (33-Risk)*(hi-lo)/100;
   smin[i] = lo + (33-Risk)*(hi-lo)/100;
   
   trend[i]=trend[i+1]; 
	   
	if ( price > smax[i])  trend[i]=1; 
	if ( price < smin[i])  trend[i]=-1;

      if(trend[i]>0)
	   {
	   if (trend[i+1]<0) UpSignal[i] = Low[i]-0.5*iATR(NULL,0,Length,i);
	   if (UseReEntry > 0 && price > smax[i] && price1 <= smax[i+1]) UpEntry[i] = Low[i]-0.5*iATR(NULL,0,Length,i); 
	   DnSignal[i]=EMPTY_VALUE; DnEntry[i]=EMPTY_VALUE;
	   }
	   else
	   if(trend[i]<0)
	   {
	   if (trend[i+1]>0) DnSignal[i] = High[i]+0.5*iATR(NULL,0,Length,i);
	   if (UseReEntry > 0 && price < smin[i] && price1 >= smin[i+1]) DnEntry[i] = High[i]+0.5*iATR(NULL,0,Length,i); 
	   UpSignal[i]=EMPTY_VALUE; UpEntry[i]=EMPTY_VALUE;
	   }
	   
      if (i==0 && AlertMode > 0)
      {
         if (trend[i] > 0 && Volume[i]>0)
         {
            if (trend[i+1]<0 && !UpTrendAlert)
	         {
	         string Message = " "+Symbol()+stringToTimeFrame(timeFrame)+": Signal for BUY";
	         Alert (Message); 
	         UpTrendAlert=true; DownTrendAlert=false;
	         } 
	      
	         if (UseReEntry>0 && trend[i+1]>0 && price1 > smax[i] && price2 < smax[i+1] && Time[i]!=prevTime)
	         {
	         Message = " "+Symbol()+stringToTimeFrame(timeFrame)+": Re-Entry for BUY";
	         Alert (Message);
	         prevTime = Time[i];
	         }
	      }
	      else
	      if (trend[i] < 0 && Volume[i]>0)
	      {
            if ( trend[i+1]>0 && !DownTrendAlert)
	         {
	         Message = " "+Symbol()+stringToTimeFrame(timeFrame)+": Signal for SELL";
	         Alert (Message); 
	         DownTrendAlert=true; UpTrendAlert=false;     
	         }
	         
	         if (UseReEntry>0 && trend[i+1]<0 && price1 < smin[i] && price2 > smin[i+1] && Time[i]!=prevTime)
	         {
	         Message = " "+Symbol()+stringToTimeFrame(timeFrame)+": Re-Entry for SELL";
	         Alert (Message);
	         prevTime = Time[i];
	         }
	      } 	  
      }	       
   }
   return(0);
}
//+------------------------------------------------------------------+

    
double wrk[][50];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

  
double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax+s];
      double del2   = price - wrk[r-1][bsmin+s];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
            if (wrk[r][avolty+s] > 0)
               double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}


int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
         char = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
          else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}