
//+------------------------------------------------------------------+
//| MojoFX T3.mq4                                                    |
//| http://groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/ |
//+------------------------------------------------------------------+
#property copyright "MojoFX - Conversion only"
#property link "http://groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/"

/*-------------------------------------------------------------------
MAtri-colored T3 Coral:

This indicator uses the core coding of the referenced copyright indy
and has been enhanced with the addition of the tri-color feature.  
Cobra suggested the extern value "b" be changed from ".04",
which was in the monochromatic T3 Coral indy, to the value ".03"
and it remains user selectable.  The "Indicator_On?" entry in the
Indicator Window is just a simple way of turning the indy on/off 
so you do not have to remove it from the chart when you do not want 
it displayed.
                                                     - Traderathome
--------------------------------------------------------------------*/
                           
#property indicator_chart_window
#property indicator_buffers  4
#property indicator_color1   Yellow      
#property indicator_color2   Green
#property indicator_color3   FireBrick
#property indicator_color4   CLR_NONE

#property indicator_style1   2      
#property indicator_style2   2
#property indicator_style3   2

extern bool   Indicator_On?  = true;
extern int    MA_Period      = 55;
extern double b              = 0.3; //monochromatic coral came with ".04"
/*--------------------------------------------------------------------
MA= 7, b= 0.8 yields the same as T3MAopt.mq4, only in tri-color.
MA= 7, b= 0.3 yields the same as THV3 T3.mq4, only in tri-color.
---------------------------------------------------------------------*/
//---- buffers & variables
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double MapBuffer[];
double e1[],e2[],e3[],e4[],e5[],e6[];
double c1,c2,c3,c4;
double n,w1,w2,b2,b3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
   {
   IndicatorBuffers(4);
   
   //---- drawing settings
  
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3); 
   SetIndexBuffer(3,MapBuffer); 
     
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
 
   //---- define Indicator Short Name ----
   IndicatorShortName("MAtri-colored T3 Coral ("+MA_Period+") ");

   //---- variable reset    
   b2=b*b; b3=0; b3=b2*b; 
   c1=-b3;
   c2=(3*(b2+b3));
   c3=-3*(2*b2+b+b3);
   c4=(1+3*b+b3+3*b2);
   n=MA_Period;
   if (n<1) n=1;
   n = 1 + 0.5*(n-1);
   w1 = 2 / (n + 1);
   w2 = 1 - w1;

   return(0);
   }
   
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
   {
   return(0);
   }  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   if (Indicator_On? == false){return(0);}
   double MA_Cur, MA_Prev;
   int limit;
   int counted_bars=IndicatorCounted();
   if (counted_bars<0) return (-1);
   if (counted_bars>0) counted_bars--;
   limit=(Bars-counted_bars)-1;

   //---- indicator calculation
   ArrayResize(e1, Bars+1);
   ArrayResize(e2, Bars+1);
   ArrayResize(e3, Bars+1);
   ArrayResize(e4, Bars+1);
   ArrayResize(e5, Bars+1);
   ArrayResize(e6, Bars+1);

   for(int i=limit; i>=0; i--)
      {
      e1[Bars-i] = w1*Close[i] + w2*e1[(Bars-i)-1];
      e2[Bars-i] = w1*e1[Bars-i] + w2*e2[(Bars-i)-1];
      e3[Bars-i] = w1*e2[Bars-i] + w2*e3[(Bars-i)-1];
      e4[Bars-i] = w1*e3[Bars-i] + w2*e4[(Bars-i)-1];
      e5[Bars-i] = w1*e4[Bars-i] + w2*e5[(Bars-i)-1];
      e6[Bars-i] = w1*e5[Bars-i] + w2*e6[(Bars-i)-1];
  
      MapBuffer[i]=c1*e6[Bars-i] + c2*e5[Bars-i] + c3*e4[Bars-i] + c4*e3[Bars-i];
      MA_Cur = MapBuffer[i];
      MA_Prev = MapBuffer[i+1];
             
      //---- color coding -------------------------------------------- 
      ExtMapBuffer1[i] = MA_Cur; //yellow
      ExtMapBuffer2[i] = MA_Cur; //green                
      ExtMapBuffer3[i] = MA_Cur; //red 
           
      if (MA_Prev > MA_Cur)
         {
         ExtMapBuffer2[i] = EMPTY_VALUE;       
         }
      else if (MA_Prev < MA_Cur) 
         {
         ExtMapBuffer3[i] = EMPTY_VALUE;      
         }
      else {ExtMapBuffer1[i] = EMPTY_VALUE;}         
      } 
      //---- end color coding and loop ------------------------------- 
    
   return(0);
   }
   
//+------------------------------------------------------------------+
//|             End of Program                                       |
//+------------------------------------------------------------------+   

