//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 White
#property indicator_color2 Blue

extern string TimeFrame       = "Current time frame";
extern int    FastEMA         = 12;
extern int    SlowEMA         = 26;
extern int    SignalSMA       = 9;
extern bool   DrawArrows      = true;
extern int	  Arrows_width		= 1;
extern bool	  DrawTrendLine	= true;
extern int	  TrendLine_width	= 2;
extern int	  TrendLine_style	= STYLE_SOLID;
extern color  color_DN		   = White;
extern color  color_UP		   = Blue;
extern int	  BarsCount			= 500;
extern string UniqueID        = "MACDLine1";

double Buff_up[];
double Buff_dn[];
double sum[];

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//----------------------------------------------------
//
//----------------------------------------------------
//
//
//
//
//

int init() 
{
      Comment("www.ProfitF.com");
   IndicatorBuffers(3);
      SetIndexBuffer(0, Buff_dn); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,159);
      SetIndexBuffer(1, Buff_up); SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(2,159);
      SetIndexBuffer(2, sum);
   
      //
      //
      //
      //
      //
       
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
         calculateValue    = TimeFrame == "calculateValue"; if (calculateValue) return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
          
      //
      //
      //
      //
      //
   
   	IndicatorShortName(timeFrameToString(timeFrame)+" MACD_line("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   return (0);
}

int deinit()
{
   for (int i = ObjectsTotal() - 1; i >= 0; i--) 
   {
      string sName = ObjectName(i); if (StringFind(sName, UniqueID) == 0) ObjectDelete(sName);
   }
	return(0);
}

//----------------------------------------------------
//
//----------------------------------------------------
//
//
//
//
//

int start() 
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { Buff_dn[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
      for(int i=limit; i>=0; i--) 
      {
         double macd   = iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,i);
         double signal = iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_SIGNAL,i);
         Buff_up[i] = EMPTY_VALUE;
         Buff_dn[i] = EMPTY_VALUE;
         sum[i]     = sum[i+1];
            if (macd>signal) { sum[i] += 0.1; Buff_up[i] = sum[i]; }
            if (macd<signal) { sum[i] -= 0.1; Buff_dn[i] = sum[i]; }

            //
            //
            //
            //
            //
            
            if (DrawArrows)
            {
               if(macd<signal && Buff_dn[i+1]==EMPTY_VALUE) DrawAr("dn",i);    
               if(macd>signal && Buff_up[i+1]==EMPTY_VALUE) DrawAr("up",i);    
            }
				if(DrawTrendLine && i<=BarsCount)
            {
               ObjectDelete(UniqueID+"_trend_price_"+Time[i]);
               for (int k=0; (i+k)<Bars-1; k++)
               {
                  if (Buff_up[i]!=EMPTY_VALUE && Buff_up[i+k]==EMPTY_VALUE) break;
                  if (Buff_dn[i]!=EMPTY_VALUE && Buff_dn[i+k]==EMPTY_VALUE) break;
               }
               string name     = "_trend_price_"+Time[i+k-1];
               color  theColor = color_DN; if (Buff_up[i]!=EMPTY_VALUE) theColor = color_UP;
                  
                  //
                  //
                  //
                  //
                  //
                  
					   trend(name, 0, Time[i+k-1],Open[i+k], Time[i]+Period()*60-1, Open[i], TrendLine_width, TrendLine_style, theColor);
            }
      }
      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]);               
          Buff_dn[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastEMA,SlowEMA,SignalSMA,DrawArrows,Arrows_width,DrawTrendLine,TrendLine_width,TrendLine_style,color_DN,color_UP,BarsCount,UniqueID,0,y);
          Buff_up[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastEMA,SlowEMA,SignalSMA,DrawArrows,Arrows_width,DrawTrendLine,TrendLine_width,TrendLine_style,color_DN,color_UP,BarsCount,UniqueID,1,y);
   }
return(0);
         
}

//----------------------------------------------------
//
//----------------------------------------------------
//
//
//
//
//

void trend( string name, int window, datetime time1, double level1, datetime time2, double level2, int width, int style, color col )
{
	ObjectDelete	( UniqueID + name );
	ObjectCreate	( UniqueID + name, OBJ_TREND, window, time1, level1, time2, level2 );
	ObjectSet		( UniqueID + name, OBJPROP_COLOR, col );
	ObjectSet		( UniqueID + name, OBJPROP_RAY, false );
	ObjectSet		( UniqueID + name, OBJPROP_WIDTH, width );
	ObjectSet		( UniqueID + name, OBJPROP_STYLE, style );
}
void DrawAr(string ssName, int i)
{
    string sName=UniqueID+":"+ssName+":"+Time[i];
    ObjectCreate(sName, OBJ_ARROW, 0, Time[i], 0);
    if(ssName=="up")
    {
         ObjectSet(sName, OBJPROP_ARROWCODE,  225);
         ObjectSet(sName, OBJPROP_PRICE1, Low[i+1]-iATR(NULL,0,20,i)/2);
         ObjectSet(sName, OBJPROP_COLOR, color_UP);
    }
    if(ssName=="dn")
    {
         ObjectSet(sName, OBJPROP_ARROWCODE,  226);
         ObjectSet(sName, OBJPROP_PRICE1, High[i+1]+iATR(NULL,0,20,i)/2);
         ObjectSet(sName, OBJPROP_COLOR, color_DN);    
    }    
    ObjectSet(sName, OBJPROP_WIDTH, Arrows_width);
} 

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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 char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}