//+------------------------------------------------------------------+
//|                                   AusDoc-FractalStructureMTF.mq4 |
//|                                                                  |
//| Useful for studying market structure and market flow and         |
//| particularly the impact of and relationships between timeframes. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, AusDoc"
#property link      "http://www.FXGears.com"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Crimson
#property indicator_width1 1
#property indicator_color2 Green
#property indicator_width2 1
#property indicator_color3 Crimson
#property indicator_width3 1
#property indicator_color4 Green
#property indicator_width4 1
#property indicator_color5 Crimson
#property indicator_width5 2
#property indicator_color6 Green
#property indicator_width6 2
#property indicator_color7 Crimson
#property indicator_width7 2
#property indicator_color8 Green
#property indicator_width8 2


//---- input parameters
/*************************************************************************
Timeframe   Enter
=========   =====
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
**************************************************************************/
extern bool    TurnIndicatorOn = true;
extern string  Enter_number_of_minutes_for_each_timeframe = "Stick to standard MT4 periods.";
extern int     TimeFrame1      = 15;
extern int     TimeFrame2      = 60;
extern int     TimeFrame3      = 240;
extern int     TimeFrame4      = 1440;
extern string  Set_colors_and_width_on_the = "indicator Colors settings tab.";


//---- buffers
double ExtMapBuffer0[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
//---- indicator line

   SetIndexBuffer(0,ExtMapBuffer0);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,167);
   SetIndexBuffer(1,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,167);
   
   SetIndexBuffer(2,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,167);
   SetIndexBuffer(3,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,167);
   
   SetIndexBuffer(4,ExtMapBuffer4);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,167);
   SetIndexBuffer(5,ExtMapBuffer5);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,167);
   
   SetIndexBuffer(6,ExtMapBuffer6);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,167);
   SetIndexBuffer(7,ExtMapBuffer7);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,167);      
   
//---- name for DataWindow and indicator subwindow label   
   
   IndicatorShortName("MTF Fractals");  
  
//----
   return(0);
 }
 
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
 
//+------------------------------------------------------------------+
//| MTF Fractals                                                     |
//+------------------------------------------------------------------+
int start()
  {
//  
   if(TurnIndicatorOn){  
//  
   datetime TimeArray[];
   int    i,limit,y=0,counted_bars=IndicatorCounted();
    
// Plot defined timeframe onto current timeframe (if it will fit)
   if (Period() <= TimeFrame1)
   {  
      ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame1); 
      limit=Bars-counted_bars+TimeFrame1/Period();
      for(i=0,y=0;i<limit;i++)
      {
         if (Time[i]<TimeArray[y]) y++;
         ExtMapBuffer0[i]=iFractals(NULL,TimeFrame1,1,y);
         ExtMapBuffer1[i]=iFractals(NULL,TimeFrame1,2,y);
      }
   } 
// 
   if (Period() <= TimeFrame2)
   {      
      ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame2);
      limit=Bars-counted_bars+TimeFrame2/Period(); 
      for(i=0,y=0;i<limit;i++)
      {
         if (Time[i]<TimeArray[y]) y++;  
         ExtMapBuffer2[i]=iFractals(NULL,TimeFrame2,1,y);
         ExtMapBuffer3[i]=iFractals(NULL,TimeFrame2,2,y);
      }
   }
// 
   if (Period() <= TimeFrame3)
   {     
      ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame3);
      limit=Bars-counted_bars+TimeFrame3/Period(); 
      for(i=0,y=0;i<limit;i++)
      {
         if (Time[i]<TimeArray[y]) y++;  
         ExtMapBuffer4[i]=iFractals(NULL,TimeFrame3,1,y);
         ExtMapBuffer5[i]=iFractals(NULL,TimeFrame3,2,y);
      }
   }
// 
   if (Period() <= TimeFrame4)
   {     
      ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame4); 
      limit=Bars-counted_bars+TimeFrame4/Period();
      for(i=0,y=0;i<limit;i++)
      {
         if (Time[i]<TimeArray[y]) y++;  
         ExtMapBuffer6[i]=iFractals(NULL,TimeFrame4,1,y);
         ExtMapBuffer7[i]=iFractals(NULL,TimeFrame4,2,y);
      } 
   }  
      
   }// end if TurnIndicatorOn
//
   return(0);
  }
//+------------------------------------------------------------------+