//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  PaleVioletRed
#property indicator_color2  DeepSkyBlue
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//
//
//

extern string TimeFrame        = "Current time frame";
extern int    EmaPeriod        = 13;
extern int    MacdFastPeriod   = 12;
extern int    MacdSlowPeriod   = 26;
extern int    MacdSignalPeriod =  9;
extern int    Price            = PRICE_CLOSE;

//
//
//
//
//

double impulseDn[];
double impulseUp[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

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

int init()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0, impulseDn); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1, impulseUp); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,trend);

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
   
      //
      //
      //
      //
      //

   IndicatorShortName(timeFrameToString(timeFrame)+" Elder impulse");
   return(0);
}
int deinit() { return(0); }

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

double work[][4];
#define _ema   0
#define _macdv 1
#define _macds 2
#define _macdo 3

//
//
//
//
//

int start()
{
   double alpha = 2.0 / (1.0+MacdSignalPeriod);
   int i,r,limit,counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { impulseDn[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
      if (ArrayRange(work,0) != Bars) ArrayResize(work,Bars);
      for(i=limit, r=Bars-i-1; i>=0; i--,r++)
      {
         work[r][_ema]   = iMA(NULL,0,EmaPeriod,0,MODE_EMA,Price,i); 
         work[r][_macdv] = iMA(NULL,0,MacdFastPeriod,0,MODE_EMA,Price,i)-iMA(NULL,0,MacdSlowPeriod,0,MODE_EMA,Price,i);
         work[r][_macds] = work[r-1][_macds]+alpha*(work[r][_macdv]-work[r-1][_macds]);
         work[r][_macdo] = work[r][_macdv]-work[r][_macds];
       
         //
         //
         //
         //
         //
         
         impulseDn[i]=EMPTY_VALUE;
         impulseUp[i]=EMPTY_VALUE;
         trend[i]    =EMPTY_VALUE;
            if (work[r][_ema]<work[r-1][_ema] && work[r][_macdo]<work[r-1][_macdo]) trend[i] = -1;
            if (work[r][_ema]>work[r-1][_ema] && work[r][_macdo]>work[r-1][_macdo]) trend[i] =  1;
            if (trend[i] == -1) impulseDn[i] = 1;
            if (trend[i] ==  1) impulseUp[i] = 1;
      }
      return(0);
   }     

   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         trend[i]     = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",EmaPeriod,MacdFastPeriod,MacdSlowPeriod,MacdSignalPeriod,Price,2,y);
         impulseDn[i] = EMPTY_VALUE;
         impulseUp[i] = EMPTY_VALUE;
      if (trend[i] == -1) impulseDn[i] = 1;
      if (trend[i] ==  1) impulseUp[i] = 1;
   }
   return(0);         
}

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

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

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}