/*
   Multple Time Frame Trix T3 Indicator for THV System
   This Multiple Time Frame TRIX T3 is for MTF purposes
   The original TRIX T3 remains untouched (orginal by Luis Damiani)
   
   Make sure the variable (strBaseCodeFileName) is set to the Original Trix filename...
*/

#property copyright "Modified MTF Trix by Peter D."

#property indicator_separate_window

#property indicator_buffers     8
#property indicator_color1      Aquamarine     
#property indicator_color2      LightPink   
#property indicator_color3      Lime      
#property indicator_color4      Pink       
#property indicator_color5      Green       
#property indicator_color6      Red       
#property indicator_color7      Lime      
#property indicator_color8      Red  
extern string     paiInfo0 = "<<< When autopair is true, uses pair on chart>>>";
extern bool       autoPair=true;
extern string     Pair = "GBPJPY";
     
//---- input parameters


extern bool       autotimeframe=true;
extern int        TimeFrame= -1;
extern string     TimeFrameInfo0 = "<<< When autotimeframe is true>>>";
extern string     TimeFrameInfo1 = "";
extern string     TimeFrameInfo2 = "use: '0' for current timeframe";
extern string     TimeFrameInfo3 = "-1  for next higher tf i.e 5m if chart is 1m";
extern string     TimeFrameInfo4 = "-2 for two tfs up i.e 15m if chart is 1 m";
extern string     TimeFrameInfo5 = "-3 for 3 tfs up i.e 30 m if chart is 1m ";
extern string     TimeFrameInfo6 = "-4 for 4 tfs up  ie 60 m if chart is 1m";



extern bool       showhisto=true;

extern string     obosInfo2 = "use HighLow_Levels_Override_Value to override autovals";
extern bool        showOBOS=true;
extern double HighLow_Levels_Override_Value   = 0;
extern color HighLine_Color = FireBrick;
extern color ZeroLine_Color = DimGray;
extern color LowLine_Color = DarkGreen;
extern int   Line_Style = STYLE_DOT; 
extern string     _ = "";
extern string     __ = "<<< Number of bars >>>";
extern int        TrixNumberOfBars=750;

extern string     ___ = "";
extern string     ____ = "<<< Trix settings >>>";
extern int        A_t3_period=20;
extern int        B_t3_period_ac=35;
extern color Color1=Silver;
extern color Color2=Aquamarine;
extern color Color3=LightPink;
extern color Color4=Silver;
extern int Line=2;
int               is_A_open_price=0;
int               diferential=0;
extern string note3n ="0.7 is like (THV V3) Trix4.01 div, 1.2 is like trix V6 (THV V4) ";
extern double            hot=1.2;

string strBaseCodeFileName = "THV T3 Trix";
string   indicatorName;
//---- Arrays for Trix values
double dblMain[];
double dblSignal[];

// Indicator Buffers //
double dblTrixFastUp[];
double dblTrixFastDown[];
double dblTrixSlowUp[];
double dblTrixSlowDown[];
double     ind_buffer1[];
double     ind_buffer2[];
double     ind_buffer3[];
double     ind_buffer4[];
string ShortName;

#define iName  "THV MTF Trix_"
#define vers   "29.Nov.2010"
#define major  "1"
#define minor  "10"
#define Prefix "MTFTrix_"
datetime t;
string t1;
int aTimeFrame;
//+------------------------------------------------------------------+
//| init()
//+------------------------------------------------------------------+
int init()
  {
  
   if(autotimeframe==true)
   {
  
   aTimeFrame=getPeriod(TimeFrame);
 
   }
   else aTimeFrame=TimeFrame;
   
   
   string strTimeFrame = GetPeriodName(aTimeFrame);
   t=TimeLocal( ) ;
   t1=TimeToStr(t,TIME_SECONDS);
   ShortName = t1+ iName +"(" +strTimeFrame +")";
   
   indicatorName = ShortName;
   IndicatorShortName(ShortName);
   
  //cleanLINESoninit(strTimeFrame);

//---- indicators
//("");
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,dblTrixFastUp);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,dblTrixFastDown);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,dblTrixSlowUp);
   SetIndexStyle(7,DRAW_LINE);
   SetIndexBuffer(7,dblTrixSlowDown);
   
   if(!SetIndexBuffer(3,ind_buffer1) &&
      !SetIndexBuffer(0,ind_buffer2) &&
      !SetIndexBuffer(1,ind_buffer3)  &&
      !SetIndexBuffer(2,ind_buffer4)  )
       Print("cannot set indicator buffers!");
  
  if (showhisto==true)
  {
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID, Line, Color2); //Green
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID, Line,Color3);//Red
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID, Line,Color4);//Silver
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID, Line,Color3);//Silver
   }
   else if (showhisto==false)
   {
   SetIndexStyle(0,DRAW_NONE,STYLE_SOLID, Line, Color2); //Green
   SetIndexStyle(1,DRAW_NONE,STYLE_SOLID, Line,Color3);//Red
    SetIndexStyle(2,DRAW_NONE,STYLE_SOLID, Line,Color4);//Silver
     SetIndexStyle(3,DRAW_NONE,STYLE_SOLID, Line,Color4);//Silver
  
   }
   if (showOBOS==true)
   {

    
    drawOBOS(strTimeFrame);
   
   }
   
    else if (showOBOS==false)
     {
     cleanLINESoninit(strTimeFrame);
    
    }
    cleanLINESoninit(strTimeFrame);
    drawOBOS(strTimeFrame);
    if (showOBOS==false)
     {
     cleanLINESoninit(strTimeFrame);
    
    }
      return(0);
  }

//+------------------------------------------------------------------+
//| start()
//+------------------------------------------------------------------+
int start()
  {
   
  
    reinit(); 
   
   datetime dtmTime[];
   
   int j;
   int intMaxVal;
   int intTotBars = IndicatorCounted();
   
if (aTimeFrame !=Period())
{
  

  intMaxVal = (Bars - intTotBars)+aTimeFrame;
 }
 else  
 {
 intMaxVal = Bars - intTotBars; 

 }

   ArrayCopySeries(dtmTime, MODE_TIME, Symbol(), aTimeFrame); 
   ArrayResize(dblMain, intMaxVal);ArrayResize(dblSignal, intMaxVal);
   
   
   
   
   j = 0;
   
   // Calculate TRIX Values //
   for(int i=0; i<intMaxVal; i++)
   {
      if (Time[i] < dtmTime[j]) j++; 
      
  
      dblMain[i]=iCustom(NULL, aTimeFrame, strBaseCodeFileName, TrixNumberOfBars, A_t3_period, B_t3_period_ac,hot, MODE_MAIN, j);
      dblSignal[i]=iCustom(NULL, aTimeFrame, strBaseCodeFileName, TrixNumberOfBars, A_t3_period, B_t3_period_ac,hot, MODE_SIGNAL, j);
 
     
     ind_buffer1[i]=dblMain[i]-  dblSignal[i];
     
    
   }

 
   
    
   for(i=intMaxVal-2; i>=0; i--)
   {
      dblTrixFastUp[i] = EMPTY_VALUE; dblTrixFastDown[i] = EMPTY_VALUE;
      dblTrixSlowUp[i] = EMPTY_VALUE; dblTrixSlowDown[i] = EMPTY_VALUE;
      ind_buffer3[i]= EMPTY_VALUE;
      ind_buffer2[i]= EMPTY_VALUE;
      ind_buffer4[i]= EMPTY_VALUE;
      if (dblMain[i+1] < dblMain[i])
      {
         if (dblTrixFastUp[i+1] == EMPTY_VALUE) dblTrixFastUp[i+1] = dblMain[i+1];
         dblTrixFastUp[i] = dblMain[i];
      }
      else if (dblMain[i+1]>dblMain[i])
      {
         if (dblTrixFastDown[i+1] == EMPTY_VALUE) dblTrixFastDown[i+1] = dblMain[i+1];
         dblTrixFastDown[i] = dblMain[i];
      }
      else
      {
         if (dblTrixFastUp[i+1]   != EMPTY_VALUE) dblTrixFastUp[i]   = dblTrixFastUp[i+1];
         if (dblTrixFastDown[i+1] != EMPTY_VALUE) dblTrixFastDown[i] = dblTrixFastDown[i+1];
      }

      if(dblSignal[i+1]<dblSignal[i])
      {
         if (dblTrixSlowUp[i+1] == EMPTY_VALUE) dblTrixSlowUp[i+1] = dblSignal[i+1];
         dblTrixSlowUp[i] = dblSignal[i];
      }   
      else if (dblSignal[i+1]>dblSignal[i])
      {
         if (dblTrixSlowDown[i+1] == EMPTY_VALUE) dblTrixSlowDown[i+1] = dblSignal[i+1];
         dblTrixSlowDown[i] = dblSignal[i];
      }
      else
      {
         if (dblTrixSlowUp[i+1]   != EMPTY_VALUE) dblTrixSlowUp[i]   = dblTrixSlowUp[i+1];
         if (dblTrixSlowDown[i+1] != EMPTY_VALUE) dblTrixSlowDown[i] = dblTrixSlowDown[i+1];
      }
      
      
      if (dblSignal[i]<dblMain[i] && dblMain[i+1]-  dblSignal[i+1]>dblMain[i]-  dblSignal[i])
      {
      ind_buffer3[i]=dblMain[i]-  dblSignal[i];
      }
      else if (dblSignal[i]<dblMain[i] && dblMain[i+1]-  dblSignal[i+1]<dblMain[i]-  dblSignal[i])
      {
       ind_buffer2[i]=dblMain[i]-  dblSignal[i];
      }
      else  if (dblSignal[i]>dblMain[i] && dblMain[i+1]-  dblSignal[i+1]<dblMain[i]-  dblSignal[i])
      {
      ind_buffer2[i]=ind_buffer1[i];
      }
      else if (dblSignal[i]>dblMain[i] && dblMain[i+1]-  dblSignal[i+1]>dblMain[i]-  dblSignal[i])
      {
       ind_buffer3[i]=ind_buffer1[i];
      }
      
     else{ind_buffer4[i]=dblMain[i]-  dblSignal[i];} 
      
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

//-----------------------------------------------------------------------------//
//  Get TimeFrame
//-----------------------------------------------------------------------------//
   
 int reinit()
  { 
   init();  
  }
  
 
 
 
int getPeriod(int thetf )
  {
    int tf =0;
 
   string msgText="Invalid timeframe. Please enter a valid timeframe";
   
       if (validateInput(TimeFrame)==false)
       {
       Alert(msgText);
       }
        
       if (thetf==-1)
       
       {
       tf = getPeriod1();
       }
       
       if (thetf==-2)
       
       {
       tf = getPeriod2();
       }
       
       if (thetf==-3)
       
       {
       tf = getPeriod3();
       }
       
       if (thetf==-4)
       
       {
       tf = getPeriod4();
       }
     
        return( tf);
       
 }
 
 int getPeriod1()

{

      switch(Period())
        {
        int thePeriod;
        
         case 1:
            thePeriod=5; 
            break;
         case 5:
           thePeriod=15;
            break;
         case 15:
            thePeriod=30;
            break;
         case 30:
          thePeriod=60;
            break;
         case 60:
           thePeriod=240;
            break;
         case 240:
            thePeriod=1440; 
            break;
         case 1440:
            thePeriod=10080; 
            break;
         case 10080:
            thePeriod=43200; 
            break;
         case 43200:
            thePeriod=43200;
            break;
        }
     
        return(thePeriod);
 } 
 
 int getPeriod2()

{

      switch(Period())
        {
        int thePeriod;
        
         case 1:
            thePeriod=15; 
            break;
         case 5:
           thePeriod=30;
            break;
         case 15:
            thePeriod=60;
            break;
         case 30:
          thePeriod=240;
            break;
         case 60:
           thePeriod=1440;
            break;
         case 240:
            thePeriod=10080; 
            break;
         case 1440:
            thePeriod=43200; 
            break;
         case 10080:
            thePeriod=43200; 
            break;
         case 43200:
            thePeriod=43200;
            break;
        }
     
        return(thePeriod);
 }             
int getPeriod3()

{

      switch(Period())
        {
        int thePeriod;
        
         case 1:
            thePeriod=30; 
            break;
         case 5:
           thePeriod=60;
            break;
         case 15:
            thePeriod=240;
            break;
         case 30:
          thePeriod=1440;
            break;
         case 60:
           thePeriod=10080;
            break;
         case 240:
            thePeriod=43200; 
            break;
         case 1440:
            thePeriod=43200; 
            break;
         case 10080:
            thePeriod=43200; 
            break;
         case 43200:
            thePeriod=43200;
            break;
        }
     
        return(thePeriod);
 }  
 
 int getPeriod4()

{

      switch(Period())
        {
        int thePeriod;
        
         case 1:
            thePeriod=60; 
            break;
         case 5:
           thePeriod=240;
            break;
         case 15:
            thePeriod=1440;
            break;
         case 30:
          thePeriod=10080;
            break;
         case 60:
           thePeriod=43200;
            break;
         case 240:
            thePeriod=43200; 
            break;
         case 1440:
            thePeriod=43200; 
            break;
         case 10080:
            thePeriod=43200; 
            break;
         case 43200:
            thePeriod=43200;
            break;
        }
     
        return(thePeriod);
 }    
 
 
 bool validateInput(int theval)
 {
 bool theanswer=false;
      switch(theval)
        {
         
            case -1:
            theanswer=true;
            break;  
            
            case -2:
            theanswer=true;
            break;  
           
            case -3:
            theanswer=true;
            break;  
            
            case -4:
            theanswer=true;
            break;  
            
            case 1:
            theanswer=true;
            break;
            
           case 0:
            theanswer=true;
            break;  
            
         case 1:
            theanswer=true;
            break;
         case 5:
           theanswer=true;
           break;
         case 15:
            theanswer=true;
            break;
         case 30:
           theanswer=true;
           break;
          
         case 60:
           theanswer=true;
           break;
         case 240:
            theanswer=true;
            break;
         case 1440:
           theanswer=true;
           break;
        
           case 10080:
            theanswer=true;
            break;
         case 43200:
           theanswer=true;
           break;
         
           
          }
          
          return (theanswer);
 }
 
 string GetPeriodName(int intPeriod)
{
   string strPeriod = "unknown";

   if (intPeriod == PERIOD_M1)  strPeriod = "M1";
   if (intPeriod == PERIOD_M5)  strPeriod = "M5";
   if (intPeriod == PERIOD_M15) strPeriod = "M15";
   if (intPeriod == PERIOD_M30) strPeriod = "M30";
   if (intPeriod == PERIOD_H1)  strPeriod = "H1";
   if (intPeriod == PERIOD_H4)  strPeriod = "H4";
   if (intPeriod == PERIOD_D1)  strPeriod = "D1";
   if (intPeriod == PERIOD_W1)  strPeriod = "W1";
   if (intPeriod == PERIOD_MN1) strPeriod = "MN1";
   if (intPeriod == 0) strPeriod = GetPeriodName(Period());

   return(strPeriod);
      }      
      
      
   void drawOBOS(string strTimeFrame)
   {   
       double ZHL;
   if (aTimeFrame==1)ZHL = 0.0002; 
   if (aTimeFrame==5)ZHL = 0.00025;
   if (aTimeFrame==15)ZHL = 0.0005; 
   if (aTimeFrame==30)ZHL = 0.0008;
   if (aTimeFrame==60)ZHL = 0.0012; 
   if (aTimeFrame==240)ZHL = 0.003;
   if (aTimeFrame==1440)ZHL = 0.005; 
   if (aTimeFrame==10080)ZHL = 0.08;
   if (aTimeFrame==43200)ZHL = 0.015;
   if(HighLow_Levels_Override_Value > 0) {ZHL = HighLow_Levels_Override_Value;}
   double ZH = ZHL; double ZL = -1* ZHL;
   
 
   drawLine(Prefix +strTimeFrame+ "line_HL", ZH, HighLine_Color, Line_Style);
   drawLine(Prefix + strTimeFrame+"line_ZL", 0,  ZeroLine_Color, Line_Style);
   drawLine(Prefix + strTimeFrame+"line_LL", ZL, LowLine_Color, Line_Style);
   }
   
    void drawLine(string name, double price, color Col, int LvlStyle)
{     
         
   ObjectCreate(name, OBJ_HLINE,WindowFind(ShortName),Time[0],price);
   ObjectSet(name, OBJPROP_STYLE, LvlStyle);
   ObjectSet(name, OBJPROP_COLOR, Col);        
   ObjectSet(name, OBJPROP_WIDTH,1);
}

 int cleanLINESoninit(string astrTimeFrame)
{

static string thestrTimeFrame;
int i;
   for( i=ObjectsTotal()-1; i>-1; i--)
     {
       
     if (StringFind(ObjectName(i),Prefix+thestrTimeFrame)>=0)  ObjectDelete(ObjectName(i));
      if (StringFind(ObjectName(i),Prefix+astrTimeFrame)>=0)  ObjectDelete(ObjectName(i));
     }
   thestrTimeFrame =   astrTimeFrame; 
     
   return(0);   
   
}
   