//+------------------------------------------------------------------+
//|                                              Ema-cycle-identifier|
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Mod Pacois"//Mod Pacois
#property link      ""//Mod Pacois

#property indicator_separate_window
#property indicator_buffers 4 
#property indicator_color1  Gray
#property indicator_color2  Lime
#property indicator_color3  Red
#property indicator_color4  clrNONE
#property indicator_level1 0
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
//#property indicator_maximum 1.1
//#property indicator_minimum -1.1

 int key = 8763765;
 double Smooth = 1.0;
int bars = 300;
 int X_Filtr = 20;
int Progression = 10;
 int Len = 12;
 //extern int BarBollinger=5;
extern int SensitivityUp = -10;
extern int SensitivityDn = 10;
 bool tma2_nrp=false; 
 
 extern int EmaPeriod=12;
extern ENUM_MA_METHOD MODE=MODE_SMA;
extern ENUM_APPLIED_PRICE PRICE=PRICE_CLOSE;
 
 
extern string  ______________SetAlert;
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = false;
extern bool   alertsSound      = true;
extern bool   alertsNotify     = false;
extern bool   alertsEmail      = false;
extern string soundFile        = "alert2.wav";

extern string  _____________SetArrows;
extern bool   arrowsVisible    = true;
extern bool   LineVisible      = false;
extern string arrowsIdentifier = "Ema-cycle-identifier";
extern double arrowsDisplaceUp = 1.0;
extern double arrowsDisplaceDn = 0.5;
extern color  arrowsUpColor    = Magenta;
extern color  arrowsDnColor    = Yellow;
extern int    arrowsUpCode     = 233;
extern int    arrowsDnCode     = 234;
extern int    arrowsUpSize     = 1;
extern int    arrowsDnSize     = 1;

extern bool   HistoPrice          = false;




 ENUM_TIMEFRAMES    timeFrame        = 0;



string IndicatorName1 = "Ema-cycle-identifier";



//
//
//
//
//

double rsi1[];
double rsi2[];
double rsi3[];
double rsi4[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   timeFrame = MathMax(timeFrame,_Period);
   IndicatorBuffers(5);
   SetIndexBuffer(0,rsi1);if(HistoPrice)SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(1,rsi2);SetIndexStyle(1,DRAW_HISTOGRAM,0,3);
   SetIndexBuffer(2,rsi3);SetIndexStyle(2,DRAW_HISTOGRAM,0,3);
  //SetIndexBuffer(3,rsi4);if(HistoPrice2)SetIndexStyle(3,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(4,trend);
        
  // SetLevelValue(1,  Level1  );
  // SetLevelValue(2,  Level2  );   
 //  SetLevelValue(3,  Level3  );
  // SetLevelValue(4,  Level4  );   
         
  
   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(MathMax(Bars-counted_bars,3*timeFrame/_Period),Bars-1);

   //
   //
   //
   //
   //
   //
   

   

   for (i=limit; i >= 0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      
      
      rsi1[i]=  iMA(0,0,1,0,3,PRICE_CLOSE,i)/Point- iMA(0,0,EmaPeriod,0,MODE,PRICE,i)/Point;
       
     // rsi1[i]=  Low[i]/Point-Low[i+5]/Point;

    // rsi2[i]=  High[i]/Point-High[i+5]/Point;
  //  rsi1[i]=  High[i]/Point-  iBands ( NULL , 0,14,2,0, PRICE_CLOSE , MODE_UPPER , i)/Point;
      
   //   rsi3[i]=    iBands ( NULL , 0,14,2,0, PRICE_CLOSE , MODE_UPPER , i)/Point-iBands ( NULL , 0,14,2,0, PRICE_CLOSE , MODE_UPPER , i+BarBollinger)/Point;
      
      
      
      rsi2[i]=  iCustom(NULL,0 , "cycle-identifier" ,1 ,i)*100;
        rsi3[i]=  iCustom(NULL,0 , "cycle-identifier" ,2 ,i)*100;
      
     // 'cycle-identifier.mq4'	cycle-identifier.mq4	1	1

      
      
      
    

     
     
         trend[i] = 0;
         
         
         if (  rsi1[i+1]<SensitivityUp
         
         &&rsi2[i+1]<0
         
         // && rsi4[i]>0 
          )                                
         trend[i]= 1;
                                             
        
         if (  rsi1[i+1]>SensitivityDn
       &&  rsi3[i+1]>0
        // && rsi3[i]<0
        // &&rsi2[i]>0
         // && rsi4[i]>0 
        // && rsi1[i+1]>0  
         )
            
         trend[i]=-1;
                                                                                
                                                                                                                                    
            
         manageArrow(i); 
            
            
       
            
            
            
            
            
            
            
    }
    
    //
    //
    //
    //
    //
      
    if (alertsOn)
    {
       if (alertsOnCurrent)
            int whichBar = 0;
       else     whichBar = 1;
       if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("Buy");
       else  doAlert("Sell");       
    }

return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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(timeFrame+" "+Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Ema-cycle-identifier ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," Ema-cycle-identifier " +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"Ema_cycle-identifier "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (arrowsVisible)
   {
     if (trend[i] != trend[i+1])
     {
       deleteArrow(Time[i]);
       if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
       if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize,true);
     }                                            
      
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,int theSize, bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      if(LineVisible)
         ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
         ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode );
         ObjectSet(name,OBJPROP_COLOR,    theColor);
         ObjectSet(name,OBJPROP_WIDTH,    theSize );      
         
         if (up)
              ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsDisplaceDn * gap);      
         else ObjectSet(name,OBJPROP_PRICE1, Low[i] - arrowsDisplaceUp * 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);
}



