


//----
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepPink
#property indicator_width1 3
#property indicator_width2 3
#property indicator_level1 0.00
#property indicator_level2 0.1
#property indicator_level3 -0.1
#property indicator_levelcolor Gold

extern bool alertsOn        = false;
extern bool alertsOnCurrent = true;
extern bool alertsMessage   = true;
extern bool alertsSound     = false;
extern bool alertsEmail     = false;


//---- buffers
double pair[];
double pair2[];
double trend[];
double val;



//---- parameters
// for monthly
int mn_per = 12;
int mn_fast = 3;
// for weekly
int w_per = 9;
int w_fast = 3;
// for daily
int d_per = 5;
int d_fast = 3;
// for H4
int h4_per = 12;
int h4_fast = 2;
// for H1
int h1_per = 24;
int h1_fast = 8;
// for M30
int m30_per = 16;
int m30_fast = 2;
// for M15
int m15_per = 16;
int m15_fast = 4;
// for M5
int m5_per = 12;
int m5_fast = 3;
// for M1
int m1_per = 30;
int m1_fast = 10;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  
   IndicatorBuffers(3);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(0, pair);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(1, pair2);
   SetIndexBuffer(2, trend);
   IndicatorShortName(Symbol() + "(" + Period() + "): ");
   SetIndexLabel(0, Symbol()); 
 
  IndicatorBuffers(3);


  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   
   double OPEN, HIGH, LOW, CLOSE;
   int counted_bars=IndicatorCounted();

double rel,negative,positive;
//---- checking for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- the last bar will be recounted
   if(counted_bars > 0) 
       counted_bars -= 10;
   limit = Bars - counted_bars;
//---- main cycle
   int Price = 6;
   int Mode = 3;
   int per1, per2;
   switch(Period())
     {
       case 1:     per1 = m1_per;  per2 = m1_fast;  break;
       case 5:     per1 = m5_per;  per2 = m5_fast;  break;
       case 15:    per1 = m15_per; per2 = m15_fast; break;
       case 30:    per1 = m30_per; per2 = m30_fast; break;
       case 60:    per1 = h1_per;  per2 = h1_fast;  break;
       case 240:   per1 = h4_per;  per2 = h4_fast;  break;
       case 1440:  per1 = d_per;   per2 = d_fast;   break;
       case 10080: per1 = w_per;   per2 = w_fast;   break;
       case 43200: per1 = mn_per;  per2 = mn_fast;  break;
     }
   for(int i = limit; i >= 0; i--)
     {
       OPEN    = pp(Mode, PRICE_OPEN,  i, per1, per2);
       HIGH    = pp(Mode, PRICE_HIGH,  i, per1, per2);
       LOW     = pp(Mode, PRICE_LOW,   i, per1, per2);
       CLOSE   = pp(Mode, PRICE_CLOSE, i, per1, per2);
      
       pair[i]  = 0;
       pair2[i] = 0;
       trend[i] = trend[i+1];
       
       val=(OPEN + HIGH + LOW + CLOSE) / 4;
       if(val > 0)  { pair[i]  = val; trend[i] =  1; }
       if(val < 0)  { pair2[i] = val; trend[i] = -1; }
   }
     
   //
   //
   //
   //
   //
   
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;

         //
         //
         //
         //
         //
         
         if (trend[whichBar] != trend[whichBar+1])
         if (trend[whichBar] == 1)
               doAlert("up");
         else  doAlert("down");       
   }
   
   //
   //
   //
   //
   //
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double pp(int Mode, int Price, int i, int per1, int per2)
  {
   return((iMA(Symbol(), 0, per2, 0, Mode,Price, i)-
           iMA(Symbol(), 0, per1, 0, Mode,Price, i)));
  }  
  

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

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," complex pairs color trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"complex pairs color "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

