#property copyright "Copyright © 2009, Alexander Kalinovski"
#property link      "http://forexfactory.com/showthread.php?p=7439940"
#property version   "1.01"

#property indicator_chart_window
#property indicator_buffers 8

input int                   P0                               = 90; 
input int                   P1                               = 120; 
input int                   P2                               = 180; 
input int                   P3                               = 240;
input int                   P4                               = 360;
input int                   P5                               = 480;
input int                   P6                               = 720;
input int                   P7                               = 1440;
input bool                  Show_P0                          = true;
input bool                  Show_P1                          = true;
input bool                  Show_P2                          = true;
input bool                  Show_P3                          = true;
input bool                  Show_P4                          = true;
input bool                  Show_P5                          = true;
input bool                  Show_P6                          = true;
input bool                  Show_P7                          = true;
input color                 Color0                           = clrHotPink;
input color                 Color1                           = clrLime;
input color                 Color2                           = clrOrange;
input color                 Color3                           = clrBlue;
input color                 Color4                           = clrRed;
input color                 Color5                           = clrGreen;
input color                 Color6                           = clrSienna;
input color                 Color7                           = clrDarkViolet;
input int                   ArrowCode                        = 108;

double FractalsBuffer0[];
double FractalsBuffer1[];
double FractalsBuffer2[];
double FractalsBuffer3[];
double FractalsBuffer4[];
double FractalsBuffer5[];
double FractalsBuffer6[];
double FractalsBuffer7[];

static int periods[8];
static color colors[8];
static bool shows[8];

string TimeFr[]={"P0","P1","P2","P3","P4","P5","P6","P7"};

int init()
{int t1;

periods[0] = P0; periods[1] = P1; periods[2] = P2; periods[3] = P3; periods[4] = P4; periods[5] = P5; periods[6] = P6; periods[7] = P7;

   shows[0] = Show_P0;
   shows[1] = Show_P1;
   shows[2] = Show_P2;
   shows[3] = Show_P3;
   shows[4] = Show_P4;
   shows[5] = Show_P5;
   shows[6] = Show_P6;
   shows[7] = Show_P7;
   
   colors[0] = Color0;
   colors[1] = Color1;
   colors[2] = Color2;
   colors[3] = Color3;
   colors[4] = Color4;
   colors[5] = Color5;
   colors[6] = Color6;
   colors[7] = Color7;

   SetIndexBuffer(0, FractalsBuffer0);
   SetIndexBuffer(1, FractalsBuffer1);
   SetIndexBuffer(2, FractalsBuffer2);
   SetIndexBuffer(3, FractalsBuffer3);
   SetIndexBuffer(4, FractalsBuffer4);
   SetIndexBuffer(5, FractalsBuffer5);
   SetIndexBuffer(6, FractalsBuffer6);
   SetIndexBuffer(7, FractalsBuffer7); 

t1=0; while(t1<=7) {SetIndexEmptyValue(t1,0.0); SetIndexStyle(t1, DRAW_ARROW,EMPTY,EMPTY,colors[t1]); SetIndexArrow(t1,ArrowCode); t1++;}
ShowLegend(); return(0);}
 
int deinit()
{int t1;

t1=0; while(t1<=7) {ObjectDelete(TimeFr[t1]); t1++;} return(0);}

//------------------------------------------------------------------
int start()
{int t1;
  
t1=Bars; while(t1>=1)
{if (shows[0]) {DetectFractal(FractalsBuffer0,P0,t1);}
if (shows[1]) {DetectFractal(FractalsBuffer1,P1,t1);}
if (shows[2]) {DetectFractal(FractalsBuffer2,P2,t1);}
if (shows[3]) {DetectFractal(FractalsBuffer3,P3,t1);}
if (shows[4]) {DetectFractal(FractalsBuffer4,P4,t1);}
if (shows[5]) {DetectFractal(FractalsBuffer5,P5,t1);}
if (shows[6]) {DetectFractal(FractalsBuffer6,P6,t1);}
if (shows[7]) {DetectFractal(FractalsBuffer7,P7,t1);} t1--;}
      
return(0);}

void DetectFractal(double &buffer[],int x6,int x7)
{if (Fractal(1,x6,x7)==1) {buffer[x7]=High[x7];} 
else if (Fractal(-1,x6,x7)==1) {buffer[x7]=Low[x7];}
else {buffer[x7]=0;}} 

int Fractal(int x5,int x6,int x7)
{int t1,t6;

if (x6<Period()) {return(-1);}  
t6=(x6/Period())*2+MathCeil((x6/Period())/2);   
if (x7<t6 || x7>Bars-t6) {return(-1);}

t1=1; while(t1<=t6)
{if (x5==1) {if (High[x7+t1]>High[x7]) {return(-1);} if (High[x7-t1]>=High[x7]) {return(-1);}}     
if (x5==-1) {if (Low[x7+t1]<Low[x7]) {return(-1);} if (Low[x7-t1]<=Low[x7]) {return(-1);}}
t1++;}
   
return(1);}  

string GetPeriodName(int period)
{
   switch (period)
   {
      case 90:   return ("M90");
      case 120:  return ("H2");
      case 180:  return ("H3");
      case 240:  return ("H4");
      case 360:  return ("H6");
      case 480:  return ("H8");
      case 720:  return ("H12");
      case 1440: return ("D1");
      default:   return ("M" + period);
   }
}

void ShowLegend()
{int t1,t4=3;
   
t1=0; while(t1<=7) {if (shows[t1]) {ObjectCreate(TimeFr[t1],OBJ_LABEL,0,0,0); ObjectSetText(TimeFr[t1],GetPeriodName(periods[t1]),8,"Verdana",colors[t1]);
ObjectSet(TimeFr[t1],OBJPROP_XDISTANCE,t4); ObjectSet(TimeFr[t1],OBJPROP_YDISTANCE,20); t4+=30;} t1++;}}





