//+------------------------------------------------------------------+
#property indicator_chart_window
//+------------------------------------------------------------------+ 
extern bool _UseFixedDate = true;
extern string _FixedDate = "2008.01.01 00:00";
extern int _Period = 300;
extern color  mml_clr = CornflowerBlue;
extern bool   frame=true;
extern color  frame_color = C'0,25,0';
//+------------------------------------------------------------------+
int _Count_Barn;
double _High, _Low;

double  dmml = 0,
        sum  = 0,
        v1 = 0,
        v2 = 0,
        mn = 0,
        mx = 0,
        x1 = 0,
        x2 = 0,
        x3 = 0,
        x4 = 0,
        x5 = 0,
        x6 = 0,
        y1 = 0,
        y2 = 0,
        y3 = 0,
        y4 = 0,
        y5 = 0,
        y6 = 0,
        octave = 0,
        SR = 0,
        range   = 0,
        finalH  = 0,
        finalL  = 0,
        mml[13];

//+------------------------------------------------------------------+         
int     
        bn_v1   = 0,
        bn_v2   = 0,
        OctLinesCnt = 13,
        i = 0;
//+------------------------------------------------------------------+
int init() {
  _High = -123.0;
  _Low = -123.0;
  _Count_Barn = -123;
   return(0);
  }
//+------------------------------------------------------------------+
int deinit() {

ObjectDelete("FF1");
ObjectDelete("MurreyLevels");   
ObjectDelete("MurreyLevels2");  
    
    
   return(0);
}
//+------------------------------------------------------------------+   
int start(){

if(_Count_Barn == IndicatorCounted() && _High == High[0] && _Low == Low[0]) return(0);
_Count_Barn = IndicatorCounted(); _High = High[0]; _Low = Low[0];
   if(_UseFixedDate){
     datetime _time = StrToTime(_FixedDate);
    _Period = iBarShift(NULL, 0, _time);
   }

   bn_v1 = Lowest(NULL,0,MODE_LOW,_Period,0);
   bn_v2 = Highest(NULL,0,MODE_HIGH,_Period,0); // changes when price exceeds hi/low

   v1 = Low[bn_v1];
   v2 = High[bn_v2];
//+------------------------------------------------------------------+    
//determine fractal.....
   if( v2<=250000 && v2>25000 )
   SR=100000;
   else
     if( v2<=25000 && v2>2500 )
     SR=10000;
     else
       if( v2<=2500 && v2>250 )
       SR=1000;
       else
         if( v2<=250 && v2>25 )
         SR=100;
         else
           if( v2<=25 && v2>12.5 )
           SR=12.5;
           else
             if( v2<=12.5 && v2>6.25)
             SR=12.5;
             else
               if( v2<=6.25 && v2>3.125 )
               SR=6.25;
               else
                 if( v2<=3.125 && v2>1.5625 )
                 SR=3.125;
                 else
                   if( v2<=1.5625 && v2>0.390625 )
                   SR=1.5625;
                   else
                     if( v2<=0.390625 && v2>0)
                     SR=0.1953125;
//+------------------------------------------------------------------+       
   range=(v2-v1);
   sum=MathFloor(MathLog(SR/range)/MathLog(2));
   octave=SR*(MathPow(0.5,sum));
   mn=MathFloor(v1/octave)*octave;
   if( (mn+octave)>v2 )
   mx=mn+octave; 
   else
     mx=mn+(2*octave);
//+------------------------------------------------------------------+ 
// calculating xx
//x2
    if( (v1>=(3*(mx-mn)/16+mn)) && (v2<=(9*(mx-mn)/16+mn)) )
    x2=mn+(mx-mn)/2; 
    else x2=0;
//x1
    if( (v1>=(mn-(mx-mn)/8))&& (v2<=(5*(mx-mn)/8+mn)) && (x2==0) )
    x1=mn+(mx-mn)/2; 
    else x1=0;

//x4
    if( (v1>=(mn+7*(mx-mn)/16))&& (v2<=(13*(mx-mn)/16+mn)) )
    x4=mn+3*(mx-mn)/4; 
    else x4=0;

//x5
    if( (v1>=(mn+3*(mx-mn)/8))&& (v2<=(9*(mx-mn)/8+mn))&& (x4==0) )
    x5=mx; 
    else  x5=0;

//x3
    if( (v1>=(mn+(mx-mn)/8))&& (v2<=(7*(mx-mn)/8+mn))&& (x1==0) && (x2==0) && (x4==0) && (x5==0) )
    x3=mn+3*(mx-mn)/4; 
    else x3=0;

//x6
    if( (x1+x2+x3+x4+x5) ==0 )
    x6=mx; 
    else x6=0;

     finalH = x1+x2+x3+x4+x5+x6;
// calculating yy
//y1
    if( x1>0 )
    y1=mn; 
    else y1=0;

//y2
    if( x2>0 )
    y2=mn+(mx-mn)/4; 
    else y2=0;

//y3
    if( x3>0 )
    y3=mn+(mx-mn)/4; 
    else y3=0;

//y4
    if( x4>0 )
    y4=mn+(mx-mn)/2; 
    else y4=0;

//y5
    if( x5>0 )
    y5=mn+(mx-mn)/2; 
    else y5=0;

//y6
    if( (finalH>0) && ((y1+y2+y3+y4+y5)==0) )
    y6=mn; 
    else y6=0;

    finalL = y1+y2+y3+y4+y5+y6;
//+------------------------------------------------------------------+ 

    for( i=0; i<OctLinesCnt; i++) {
         mml[i] = 0;
         }
         
   dmml = (finalH-finalL)/8;

   mml[0] =(finalL-dmml*2); //-2/8
   for( i=1; i<OctLinesCnt; i++) {
        mml[i] = mml[i-1] + dmml;
        }
        
        ObjectCreate("MurreyLevels",OBJ_FIBO, 0, Time[_Period], mml[10], Time[_Period], mml[2]);
        ObjectSet   ("MurreyLevels", OBJPROP_LEVELSTYLE, STYLE_DOT); 
        ObjectSet   ("MurreyLevels", OBJPROP_LEVELWIDTH, 1);  
        ObjectSet   ("MurreyLevels", OBJPROP_LEVELCOLOR, mml_clr);
        ObjectSet   ("MurreyLevels", OBJPROP_FIBOLEVELS, 11);
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+0, 0);
        ObjectSetFiboDescription("MurreyLevels",0,"OVER  [-1/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+0,-0.125 );
        ObjectSetFiboDescription("MurreyLevels",1,"MAIN SUPPORT  [0/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+1,0 );
        ObjectSetFiboDescription("MurreyLevels",2,"WEAK REVERSE  [1/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+2,0.125 );
        ObjectSetFiboDescription("MurreyLevels",3,"MAJOR REVERSE  [2/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+3,0.25 );
        ObjectSetFiboDescription("MurreyLevels",4,"BOTTOM  [3/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+4,0.375 );
        ObjectSetFiboDescription("MurreyLevels",5,"X POINT  [4/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+5,0.5 );
        ObjectSetFiboDescription("MurreyLevels",6,"TOP  [5/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+6,0.625 );
        ObjectSetFiboDescription("MurreyLevels",7,"MAJOR REVERSE  [6/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+7,0.75 );
        ObjectSetFiboDescription("MurreyLevels",8,"WEAK REVERSE  [7/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+8,0.875 );
        ObjectSetFiboDescription("MurreyLevels",9,"MAIN RESIST  [8/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+9,1 );
        ObjectSetFiboDescription("MurreyLevels",10,"OVER  [+1/8] - %$     ");
        ObjectSet   ("MurreyLevels", OBJPROP_FIRSTLEVEL+10,1.125 );
        
        ObjectCreate("MurreyLevels2",OBJ_FIBO, 0, Time[_Period], mml[12], Time[_Period], mml[0]);
        ObjectSet   ("MurreyLevels2", OBJPROP_STYLE,STYLE_SOLID);
        ObjectSet   ("MurreyLevels2", OBJPROP_LEVELSTYLE, STYLE_DOT);
        ObjectSet   ("MurreyLevels2", OBJPROP_LEVELWIDTH, 1); 
        ObjectSet   ("MurreyLevels2", OBJPROP_LEVELCOLOR, mml_clr);
        ObjectSet   ("MurreyLevels2", OBJPROP_FIBOLEVELS, 2);
        ObjectSetFiboDescription("MurreyLevels2",0,"OVER  [-2/8] - %$     ");
        ObjectSet   ("MurreyLevels2", OBJPROP_FIRSTLEVEL+0,0 );
        ObjectSetFiboDescription("MurreyLevels2",1,"OVER  [+2/8] - %$     ");
        ObjectSet   ("MurreyLevels2", OBJPROP_FIRSTLEVEL+1,1 );

        if (frame) { 
        ObjectCreate("FF1", OBJ_RECTANGLE, 0, Time[_Period], mml[4], Time[0]+Time[150], mml[8]);
        ObjectSet("FF1", OBJPROP_COLOR, frame_color); 
} 
//+------------------------------------------------------------------+  
 
  return(0);
}