 //+------------------------------------------------------------------+//
//|                                        Leledc-Macd-T3            |//
//+------------------------------------------------------------------+//
#property copyright "leledc"
#property link      "leledc@yahoo.it"


#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  Lime
#property  indicator_color2  Red
#property  indicator_color3  White
#property  indicator_color4  Lime
#property  indicator_color5  Red

#property indicator_width1 3
#property indicator_width2 3

//---- indicator parameters
extern bool ShowHistogram=true;
extern bool   T3Original =false;
extern int    T3_PeriodFast=5;
extern int    T3_PeriodSlow=35;
extern int    T3_PeriodSignal=5;
extern double T3_Factor=0.618;
double     MacdBuffer[];
double     Up[],Down[];
double     SignalBuffer[];
double     MacdBufferLine[],MacdBufferLineUp[],MacdBufferLineDown[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
IndicatorBuffers(7);
if (ShowHistogram){
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
     }
     else{
   SetIndexStyle(0,DRAW_LINE,0,2);
   SetIndexStyle(1,DRAW_LINE,0,2);
   SetIndexStyle(3,DRAW_LINE,0,2);
   SetIndexStyle(4,DRAW_LINE,0,2);
}
   SetIndexStyle(2,DRAW_LINE,0,2);
   
   SetIndexStyle(5,DRAW_NONE);
   SetIndexStyle(6,DRAW_NONE);
   SetIndexBuffer(0,Up);
   SetIndexBuffer(1,Down);
   SetIndexBuffer(2,SignalBuffer);
   SetIndexBuffer(3,MacdBufferLineUp);
   SetIndexBuffer(4,MacdBufferLineDown);
   SetIndexBuffer(5,MacdBuffer);
   SetIndexBuffer(6,MacdBufferLine);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   ArraySetAsSeries(SignalBuffer, TRUE);
   ArraySetAsSeries(MacdBuffer, TRUE);

   IndicatorShortName("");
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   return(0);
  }
int start()
  {
  
int i,j,k,limit;
 int counted_bars = IndicatorCounted();
    if ( counted_bars < 0 ) {
        return( -1 );
    }
    if ( counted_bars > 0 ) {
        counted_bars--;
    }
  limit = Bars - counted_bars; 
   
  if ( ArraySize( SignalBuffer ) != ArraySize( MacdBuffer ) ) {
        ArraySetAsSeries( SignalBuffer, false );
        ArrayResize( SignalBuffer, ArraySize( MacdBuffer ) );
        ArraySetAsSeries( SignalBuffer, true );
    }
  for (i=limit; i>=0; i--)
{
  

  MacdBuffer[i]=iCustom(Symbol(), 0,"T3",T3Original,T3_PeriodFast,T3_Factor,0, i)-iCustom(Symbol(), 0, "T3",T3Original,T3_PeriodSlow,T3_Factor,0, i);
  }
    for (i=limit; i>=0; i--)
{
    MacdBufferLine[i]=iCustom(Symbol(), 0,"T3",T3Original,T3_PeriodFast,T3_Factor,0, i)-iCustom(Symbol(), 0, "T3",T3Original,T3_PeriodSlow,T3_Factor,0, i);

}
 
 for (i=limit; i>=0; i--)
{
      SignalBuffer[i]=iMAOnArray(MacdBuffer,0,T3_PeriodSignal,0,MODE_SMA,i);
      }
for (i=limit; i>=0; i--)
{
 MacdBufferLineUp[i]=EMPTY_VALUE;
  MacdBufferLineDown[i]=EMPTY_VALUE;
  Up[i]=EMPTY_VALUE;
  Down[i]=EMPTY_VALUE;
 if(ShowHistogram){
 if(MacdBuffer[i]>MacdBuffer[i+1]){
 Up[i]=MacdBuffer[i];

 }
  if(MacdBuffer[i]<MacdBuffer[i+1]){
Down[i]=MacdBuffer[i];

}
}
 if(ShowHistogram==false){
 if(MacdBuffer[i]>MacdBuffer[i+1]){
 MacdBufferLineUp[i]=MacdBuffer[i];
 MacdBufferLineUp[i+1]=MacdBuffer[i+1];

 }
  if(MacdBuffer[i]<MacdBuffer[i+1]){
   MacdBufferLineDown[i]=MacdBuffer[i];
  MacdBufferLineDown[i+1]=MacdBuffer[i+1];
}
}
}
  

   return(0);
  }
 

