//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100

extern int NumOfBars=0;
extern color RB2_LineColor=Red;
extern color RB1_LineColor=Magenta;
extern color Pivot_LineColor=DodgerBlue;
extern color SB1_LineColor=Green;
extern color SB2_LineColor=Lime;
extern color ZonesColor=C'35,35,35';


int iwindow=0;
string iname="Jackson Zones";
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName(iname);
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void place_text(string text,int x,double price,color c,int hwnd)
  {
   string  buff_str="jack_"+text+x+price;
//OBJ_LABEL
   if(ObjectFind(buff_str)==-1)
     {
      ObjectCreate(buff_str,OBJ_TEXT,iwindow,Time[x],price);
     }
//ObjectSet(buff_str,OBJPROP_COLOR,c);
   ObjectSet(buff_str,OBJPROP_TIME1,Time[x]);
   ObjectSet(buff_str,OBJPROP_PRICE1,price);
//ObjectSet(buff_str,OBJPROP_FONTSIZE,10);
   ObjectSetText(buff_str,text,10,"Arial",c);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void create_rectangle(int id,int from,int to,color c,double p0,double p1,int long,int hwnd)
  {
   string  buff_str="jack_"+id;
   ObjectCreate(buff_str,OBJ_RECTANGLE,hwnd,Time[from],p0,Time[to],p1);
   ObjectSet(buff_str,OBJPROP_COLOR,c);
   ObjectSet(buff_str,OBJPROP_RAY,long);
   ObjectSet(buff_str,OBJPROP_STYLE,STYLE_DOT);
   ObjectSet(buff_str,OBJPROP_BACK,true);
   ObjectSet(buff_str,OBJPROP_WIDTH,1);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void create_line(int line,int from,int to,color c,double p0,double p1,int long,int hwnd,int style)
  {
   string  buff_str="jack_"+line;
   ObjectCreate(buff_str,OBJ_TREND,hwnd,Time[from],p0,Time[to],p1);
   ObjectSet(buff_str,OBJPROP_RAY,long);
   ObjectSet(buff_str,OBJPROP_COLOR,c);
   ObjectSet(buff_str,OBJPROP_XDISTANCE,100);
   ObjectSet(buff_str,OBJPROP_YDISTANCE,100);
   ObjectSet(buff_str,OBJPROP_STYLE,style);
   ObjectSet(buff_str,OBJPROP_WIDTH,1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void delete_obj()
  {
   string  buff_str="";
   for(int i=ObjectsTotal()-1;i>=0;i--)
     {
      buff_str=ObjectName(i);
      if(StringFind(buff_str,"jack_",0)==0) ObjectDelete(buff_str);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   delete_obj();
   return(0);
  }

int last=0,pbar=0;
double C,D,B,A,E,SB1,SB2,RB1,RB2,R,o,c;
int fb_zone_stats[6][4];
int zone[6];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void update_table(int c)
  {
   int col[6] = {27,22,17,12,7,2};
   int row[5] = {75,55,35,15,95};
   place_text("Zones",35,row[4],ZonesColor,iwindow);
   place_text(">=RB2",35,row[0],RB2_LineColor,iwindow);
   place_text(">=RB1",35,row[1],RB1_LineColor,iwindow);
   place_text("<=SB1",35,row[2],SB1_LineColor,iwindow);
   place_text("<=SB2",35,row[3],SB2_LineColor,iwindow);

   for(int i=1;i<7;i++)
     {
      color cl=ZonesColor;
      if(i==c) cl=Pivot_LineColor;
      place_text(i,col[i-1],row[4],cl,iwindow);
      for(int j=0;j<4;j++)
         if(zone[i-1]!=0)
            place_text((fb_zone_stats[i-1][j]*100)/zone[i-1],col[i-1],row[j],cl,iwindow);
      else
         place_text(fb_zone_stats[i-1][j],col[i-1],row[j],cl,iwindow);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void draw_zones(int shift)
  {
   static int acc=0;
   create_rectangle(acc,pbar,shift,ZonesColor,RB2,E,0,0); acc++;
   create_line(acc,pbar,shift,RB2_LineColor,E,E,0,0,STYLE_SOLID); acc++;
   create_rectangle(acc,pbar,shift,ZonesColor,RB1,D,0,0); acc++;
   create_line(acc,pbar,shift,RB1_LineColor,D,D,0,0,STYLE_SOLID); acc++;
   create_line(acc,pbar,shift,Pivot_LineColor,C,C,0,0,STYLE_SOLID); acc++;
   create_line(acc,pbar,shift,SB1_LineColor,B,B,0,0,STYLE_SOLID); acc++;
   create_rectangle(acc,pbar,shift,ZonesColor,B,SB1,0,0); acc++;
   create_line(acc,pbar,shift,SB2_LineColor,A,A,0,0,STYLE_SOLID); acc++;
   create_rectangle(acc,pbar,shift,ZonesColor,A,SB2,0,0); acc++;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   iwindow = WindowFind(iname);
   if(last == Bars) return (0);
   last=Bars;
   delete_obj();
   int limit=NumOfBars;
   if(limit==0 || limit>Bars) limit=Bars;
   int b,l = 0;


      for(int i=0;i<6;i++)
        {
         fb_zone_stats[i][0] = 0;
         fb_zone_stats[i][1] = 0;
         fb_zone_stats[i][2] = 0;
         fb_zone_stats[i][3] = 0;
         zone[i]=0;
        }


   for(i=limit-1;i>=0;i--)
     {
      b=iBarShift(NULL,PERIOD_D1,Time[i]);
      if(b!=l)
        {
         // draw zones
         if(l!=0) draw_zones(i);
         pbar = i;

         l=b;
         int b_=TimeDayOfWeek(Time[i]);
         if(b_==1) b+=2; else b+=1;
         double cl = iClose(NULL,PERIOD_D1,b);
         double hi = iHigh(NULL,PERIOD_D1,b);
         double lo = iLow(NULL,PERIOD_D1,b);
         R=(hi-lo);

         C=(cl+hi+lo)/3;//PIVOT

         E = C + 0.011;//PIVOT+RANGE
         D = C + 0.004;//PIVOT+RANGE/2
                       // C
         B = C - 0.004;
         A = C - 0.011;

         o = iOpen (NULL,PERIOD_D1,l);
         c = iClose(NULL,PERIOD_D1,l);


            int zo=6;
            if(o<A) zo=1;
            else if(o<E && o>=D) zo = 5;
            else if(o<D && o>=C) zo = 4;
            else if(o<C && o>=B) zo = 3;
            else if(o<B && o>=A) zo = 2;

            zone[zo]++;


         RB2 = C + R * 1.382;
         RB1 = C + R * 0.618;
         SB1 = C - R * 0.618;
         SB2 = C - R * 1.382;



            if(c<=SB2) fb_zone_stats[zo][3]++;
            if(c<=SB1) fb_zone_stats[zo][2]++;
            if(c>=RB1) fb_zone_stats[zo][1]++;
            if(c>=RB2) fb_zone_stats[zo][0]++;

        }
     }

   update_table(zo);

// draw last zone levels
   draw_zones(0);

   return(0);
  }
//+------------------------------------------------------------------+