//+------------------------------------------------------------------+
//|                                         Murrey_Math_Modified.mq4 |
//|                                          rewritten by CrazyChart |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Murrey_Math_Modified. MetaTrader4 conversion created by PaVeL (CrazyChart)"
#property link      "mailto:newcomer2003@yandex.ru"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 DarkBlue
#property indicator_color2 DarkViolet
#property indicator_color3 Aqua
#property indicator_color4 DarkViolet
#property indicator_color5 DarkBlue

   
//---- input parameters
extern int       CountBars = 500;
extern int       ExtParam2;
extern int       ExtParam3;
extern double myVar;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];

//---- input parameters
extern int       beginer=0;
extern int       periodtotake=64;
extern int       SomeVar=0;

int shift=0,i2=0,WorkTime=0,Periods=0;
 
double sum=0,v1=0,v2=0,fractal=0;
double v45=0,mml00=0,mml0=0,mml1=0,mml2=0,mml3=0,mml4=0,mml5=0,mml6=0,mml7=0,mml8=0,mml9=0,mml98=0,mml99=0;
double range=0,octave=0,mn=0,mx=0,price=0;
double finalH=0,finalL=0;
double x1=0,x2=0,x3=0,x4=0,x5=0,x6=0,y1=0,y2=0,y3=0,y4=0,y5=0,y6;
//string textArray[mm11_txt,mm12_txt,mm1_txt,mm2_txt,mm3_txt,mm4_txt,mm5_txt,mm6_txt,mm7_txt,mm8_txt,mm9_txt,mm-1_txt,mm-2_txt];
string textArray[13]={"mm11_txt","mm12_txt","mm1_txt","mm2_txt","mm3_txt","mm4_txt","mm5_txt","mm6_txt","mm7_txt","mm8_txt","mm9_txt","mm-1_txt","mm-2_txt"};
//string lineArray[13]={"mm11","mm12","mm1","mm2","mm3","mm4","mm5","mm6","mm7","mm8","mm9","mm-1","mm-2"};

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
string short_name;
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexDrawBegin(0,0);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
   SetIndexDrawBegin(1,0);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   SetIndexDrawBegin(2,0);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexEmptyValue(3,0.0);
   SetIndexDrawBegin(3,0);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexEmptyValue(4,0.0);
   SetIndexDrawBegin(4,0);
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   ObjectsDeleteAll(0, OBJ_HLINE); 
   int count=ArraySize(textArray);
   for(int ch=0;ch<count;ch++) {
      ObjectDelete(textArray[ch]);
      //ObjectDelete(lineArray[ch]);
   }
   ObjectDelete("mm-sss");
   //ObjectsDeleteAll(0, OBJ_TEXT); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    limit,counted_bars=IndicatorCounted(),i;
    if (CountBars>=Bars) CountBars=Bars;
   SetIndexDrawBegin(0,Bars-CountBars);
   SetIndexDrawBegin(1,Bars-CountBars);
   SetIndexDrawBegin(2,Bars-CountBars);
   SetIndexDrawBegin(3,Bars-CountBars);
   SetIndexDrawBegin(4,Bars-CountBars);
   
//int    counted_bars=IndicatorCounted(),CountBars;
//if (CountBars>=Bars) CountBars=Bars;
  //SetIndexDrawBegin(0,Bars-CountBars);
  //SetIndexDrawBegin(1,Bars-CountBars);
//for (shift=1;shift<=0;shift--) {
   if (counted_bars<0) return(-1);
//---- last counted bar will be recounted
   //if (counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i=1;i<=0;i++) {
   ExtMapBuffer1[Bars-i]=0.0; 
   ExtMapBuffer2[Bars-i]=0.0; 
   ExtMapBuffer3[Bars-i]=0.0; 
   ExtMapBuffer4[Bars-i]=0.0; 
   ExtMapBuffer5[Bars-i]=0.0; 
   
   
   }
   
   
   
   
if( (WorkTime != Time[0]) || (Periods != Period()) ) {
for (i=limit;i>0;i--) {
//price
   ExtMapBuffer1[i]=0;
   ExtMapBuffer2[i]=0;
   ExtMapBuffer3[i]=0;
   ExtMapBuffer4[i]=0;
   ExtMapBuffer5[i]=0;



v1=(Low[Lowest(NULL,0,MODE_LOW,periodtotake+SomeVar,i+beginer)]);
v2=(High[Highest(NULL,0,MODE_HIGH,periodtotake+SomeVar,i+beginer)]);

//We would like to figure out these fractals
//triangle set OK
   if( v2<=250000 && v2>25000 )
   fractal=100000;
   else
   if( v2<=25000 && v2>2500 )
   fractal=10000;
   else
   if( v2<=2500 && v2>250 )
   fractal=1000;
   else
   if( v2<=250 && v2>25 )
   fractal=100;
   else
   if( v2<=25 && v2>12.5 )
   fractal=12.5;
   else
   if( v2<=12.5 && v2>6.25)
   fractal=12.5;
   else
   if( v2<=6.25 && v2>3.125 )
   fractal=6.25;
   else
   if( v2<=3.125 && v2>1.5625 )
   fractal=3.125;
   else
   if( v2<=1.5625 && v2>0.390625 )
   fractal=1.5625;
   else
   if( v2<=0.390625 && v2>0)
   fractal=0.1953125;
//debugging((((
//v2=1.5625; 
//v2=0.391625;

range=(v2-v1);

sum=MathFloor(MathLog(fractal/range)/MathLog(2));

octave=fractal*(MathPow(0.5,sum));
//Alert("������ ����� �������, ��... ����� = "+ octave);
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 when we have no sbj, du {}
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;



v45=(finalH-finalL)/8;


mml00=(finalL-v45*2);  //-2/8
mml0=(finalL-v45);  //-1/8
mml1=(finalL);// 0/8
mml2=(finalL+v45);// 1/8
mml3=(finalL+2*v45); // 2/8
mml4=(finalL+3*v45); //  3/8
mml5=(finalL+4*v45); //  4/8
mml6=(finalL+5*v45); //  5/8
mml7=(finalL+6*v45); //  6/8 
mml8=(finalL+7*v45);// 7/8
mml9=(finalL+8*v45);// 8/8
mml99=(finalL+9*v45);// +1/8
mml98=(finalL+10*v45);// +2/8

ExtMapBuffer1[i]=mml00;
ExtMapBuffer2[i]=mml0;
ExtMapBuffer3[i]=mml1;
ExtMapBuffer4[i]=mml99;
ExtMapBuffer5[i]=mml98;





//ExtMapBuffer1[shift]=0.0;
//ExtMapBuffer2[shift]=0.0;
 if(GetLastError()!=0) Alert("Some error message"+GetLastError());
} 


  WorkTime    = Time[0];
   Periods= Period();
} 
   
   
//----
   return(0);
 }