//+------------------------------------------------------------------+
//|                                               Complex_pairs1.mq4 |
//|                                                           arzuma |
//|              http://onix-trade.net/forum/index.php?showtopic=107 |
//|              this mod by mrtools                                 |          
//+------------------------------------------------------------------+
#property link      "www.forex-tsd.com"      
#property copyright "www.forex-tsd.com"    

#property indicator_separate_window
#property indicator_buffers    3
#property indicator_color1     clrGreen
#property indicator_color2     clrRed
#property indicator_color3     clrRed
#property indicator_width1     2
#property indicator_width2     2
#property indicator_width3     2
#property indicator_level1     0
#property indicator_levelcolor clrMediumOrchid





extern ENUM_TIMEFRAMES TimeFrame         = PERIOD_CURRENT;
extern int             JmaFast           = 0;
extern int             JmaSlow           = 0;
extern double          SmoothPhase       = 0;
extern bool            SmoothDouble      = false;
extern bool            arrowsVisible     = false;
extern bool            arrowsOnFirst     = false;
extern bool            arrowsOnSlope     = false;
extern bool            arrowsOnZeroCross = false;
extern string          arrowsIdentifier  = "jcomplex arrows1";
extern double          arrowsUpperGap    = 0.5;
extern double          arrowsLowerGap    = 0.5;
extern color           arrowsUpColor     = clrLimeGreen;
extern color           arrowsDnColor     = clrRed;
extern int             arrowsUpCode      = 241;
extern int             arrowsDnCode      = 242;
extern bool            alertsOn          = false;
extern bool            alertsOnCurrent   = false;
extern bool            alertsOnSlope     = false;
extern bool            alertsOnZeroCross = false;
extern bool            alertsMessage     = false;
extern bool            alertsSound       = false;
extern bool            alertsNotify      = false;
extern bool            alertsEmail       = false;
extern string          soundFile         = "alert2.wav";
extern int             mn_per            = 12;
extern int             mn_fast           = 3;
extern int             w_per             = 9;
extern int             w_fast            = 3;
extern int             d_per             = 5;
extern int             d_fast            = 3;
extern int             h4_per            = 18;
extern int             h4_fast           = 6;
extern int             h1_per            = 24;
extern int             h1_fast           = 8;
extern int             m30_per           = 16;
extern int             m30_fast          = 2;
extern int             m15_per           = 16;
extern int             m15_fast          = 4;
extern int             m5_per            = 12;
extern int             m5_fast           = 3;
extern int             m1_per            = 30;
extern int             m1_fast           = 10;
extern bool            Interpolate       = true;
extern bool            MultiColor        = false;



//
//
//
//
//

double pair[];
double pairUa[];
double pairUb[];
double trend[];
double slope[];
string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int init()
{

   IndicatorBuffers(5);
   SetIndexBuffer(0,pair);
   SetIndexBuffer(1,pairUa);
   SetIndexBuffer(2,pairUb);
   SetIndexBuffer(3,trend);
   SetIndexBuffer(4,slope);
      
      //
      //
      //
      //
      //
         
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame==-99;
         TimeFrame         = MathMax(TimeFrame,_Period);
         
         switch(Period())
         {
            case PERIOD_M1:  JmaSlow = m1_per;  JmaFast = m1_fast;  break;
            case PERIOD_M5:  JmaSlow = m5_per;  JmaFast = m5_fast;  break;
            case PERIOD_M15: JmaSlow = m15_per; JmaFast = m15_fast; break;
            case PERIOD_M30: JmaSlow = m30_per; JmaFast = m30_fast; break;
            case PERIOD_H1:  JmaSlow = h1_per;  JmaFast = h1_fast;  break;
            case PERIOD_H4:  JmaSlow = h4_per;  JmaFast = h4_fast;  break;
            case PERIOD_D1:  JmaSlow = d_per;   JmaFast = d_fast;   break;
            case PERIOD_W1:  JmaSlow = w_per;   JmaFast = w_fast;   break;
            case PERIOD_MN1: JmaSlow = mn_per;  JmaFast = mn_fast;  break;
         }
         
     //
     //
     //
     //
     //
   
     IndicatorShortName(timeFrameToString(TimeFrame)+" "+Symbol());
   return(0);
}
int deinit()
{
   deleteArrows();

return(0);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int start()
{
   
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { pair[0] = limit+1; return(0); }
   
   //
   //
   //
   //
   //
   
   if (TimeFrame == Period())
   {
     if (MultiColor && slope[limit]==-1) CleanPoint(limit,pairUa,pairUb);
      
       for (i=limit; i>=0; i--)
       {
          double op  = iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_OPEN,i), JmaFast,SmoothPhase,SmoothDouble,i,  0)-
                       iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_OPEN,i), JmaSlow,SmoothPhase,SmoothDouble,i, 20);
          double hi  = iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i), JmaFast,SmoothPhase,SmoothDouble,i, 40)-
                       iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i), JmaSlow,SmoothPhase,SmoothDouble,i, 60);
          double lo  = iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW,i),  JmaFast,SmoothPhase,SmoothDouble,i, 80)-
                       iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW,i),  JmaSlow,SmoothPhase,SmoothDouble,i,100);
          double cl  = iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i),JmaFast,SmoothPhase,SmoothDouble,i,120)-
                       iDSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i),JmaSlow,SmoothPhase,SmoothDouble,i,140);
          
             if (hi + lo!=0)
                   pair[i] = (op+hi+lo+cl)/4;
             else  pair[i] = 0; 
              
             pairUa[i] = EMPTY_VALUE;
             pairUb[i] = EMPTY_VALUE; 
             trend[i]  = trend[i+1];
             slope[i]  = slope[i+1];
             
             if (pair[i] > pair[i+1]) slope[i] =  1;
             if (pair[i] < pair[i+1]) slope[i] = -1;        
             if (pair[i] > 0)         trend[i] =  1;
             if (pair[i] < 0)         trend[i] = -1;
             if (MultiColor && slope[i]==-1) PlotPoint(i,pairUa,pairUb,pair);
             
             //
             //
             //
             //
             //
             
             if (arrowsVisible)
             {
                ObjectDelete(arrowsIdentifier+":1:"+Time[i]);
                ObjectDelete(arrowsIdentifier+":2:"+Time[i]);
                string lookFor = arrowsIdentifier+":"+Time[i]; ObjectDelete(lookFor);
                if (arrowsOnSlope && slope[i] != slope[i+1])
                {
                  if (slope[i] == 1) drawArrow("1",0.5,i,arrowsUpColor,arrowsUpCode,false);
                  if (slope[i] ==-1) drawArrow("1",0.5,i,arrowsDnColor,arrowsDnCode,true);
                }
                if (arrowsOnZeroCross && trend[i] != trend[i+1])
                {
                  if (trend[i] == 1) drawArrow("2",1,i,arrowsUpColor,arrowsUpCode,false);
                  if (trend[i] ==-1) drawArrow("2",1,i,arrowsDnColor,arrowsDnCode,true);
                }
              
            }
             
      }
      
      if (alertsOn)
      {
        if (alertsOnCurrent)
             int whichBar = 0;
        else     whichBar = 1;
        static datetime time1 = 0;
        static string   mess1 = "";
        if (alertsOnSlope && slope[whichBar] != slope[whichBar+1])
        {
           if (slope[whichBar] ==  1 ) doAlert(time1,mess1,whichBar,"sloping up");
           if (slope[whichBar] == -1 ) doAlert(time1,mess1,whichBar,"sloping down");
        }         
        static datetime time2 = 0;
        static string   mess2 = "";
        if (alertsOnZeroCross && trend[whichBar] != trend[whichBar+1])
        {
           if (trend[whichBar] ==  1 ) doAlert(time2,mess2,whichBar,"crossing zero up");
           if (trend[whichBar] == -1 ) doAlert(time2,mess2,whichBar,"crossing zero down");
        }         
     }           
     return (0);
    } 
    
    //
    //
    //
    //
    //       
   
    limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
    if (MultiColor && slope[limit]==-1) CleanPoint(limit,pairUa,pairUb);
    for (i=limit;i>=0; i--)
    {
       int y = iBarShift(NULL,TimeFrame,Time[i]);
          pair[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,JmaFast,JmaSlow,SmoothPhase,SmoothDouble,arrowsVisible,arrowsOnFirst,arrowsOnSlope,arrowsOnZeroCross,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnZeroCross,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,0,y);
          pairUa[i] = EMPTY_VALUE;
          pairUb[i] = EMPTY_VALUE; 
          slope[i]  = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,JmaFast,JmaSlow,SmoothPhase,SmoothDouble,arrowsVisible,arrowsOnFirst,arrowsOnSlope,arrowsOnZeroCross,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnZeroCross,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,4,y);
          
          //
          //
          //
          //
          //
      
          if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

          //
          //
          //
          //
          //

          datetime time = iTime(NULL,TimeFrame,y);
             for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
             for(int k = 1; k < n; k++) pair[i+k] = pair[i] + (pair[i+n] - pair[i]) * k/n;
                                         
      }
      if (MultiColor) for (i=limit;i>=0;i--) if (slope[i]==-1) PlotPoint(i,pairUa,pairUb,pair);
return(0);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}

//
//
//
//
//

double wrk[][160];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iDSmooth(double price, double length, double phase, bool isDouble, int i, int s=0)
{
   if (isDouble)
         return (iSmooth(iSmooth(price,MathSqrt(length),phase,i,s),MathSqrt(length),phase,i,s+10));
   else  return (iSmooth(price,length,phase,i,s));
}

//
//
//
//
//

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]);
}

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

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

void drawArrow(string nameAdd, double gapMul, int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+nameAdd+":"+Time[i];
   double gap  = iATR(NULL,0,20,i)*gapMul;   
   
      //
      //
      //
      //
      //
      
      int add = 0; if (!arrowsOnFirst) add = _Period*60-1;
      ObjectCreate(name,OBJ_ARROW,0,Time[i]+add,0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, int forBar, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," ",timeFrameToString(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Complex pairs on jurik ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Complex pairs on jurik "),message);
          if (alertsSound)   PlaySound(soundFile);
   }
}
           

  


