//+------------------------------------------------------------------+
//|                                           MML_Multi_Option_v1.23 |
//+------------------------------------------------------------------|
//|    acknowledgement to Vladislav Goshkov (VG) - 4vg@mail.ru       |
//|                      && Alex.Piech.FinGeR for the major MM lines |
//|    acknowledgement to xard777@connectfree.co.uk for the routine  |
//|    to show the minor lines.                                      |
//|                                                                  |
//|      code changes to show the last bar arrow if StepBack > 0     |
//|      and to change position of MM text on MM lines               |
//|                                            10/04/09  DA          |
//+------------------------------------------------------------------+
#property copyright "Vladislav Goshkov (VG) && Alex.Piech.FinGeR && xard777@connectfree.co.uk"
#property link      "4vg@mail.ru && regnif@gmx.net && xard777@connectfree.co.uk"
#property indicator_chart_window

// ============================================================================================
// * Line 8/8 & 0/8 (Ultimate Support and Ultimate Resistance).
// * Those lines are the most strong concerning Support and Resistance.
// ============================================================================================
//* Line 7/8  (Weak, Place to Stop and Reverse).
//* This line is weak. If suddenly the price was going too fast and too far and stops around this line 
//* it means the price will reverse down very soon. If the price did not stop near this line, this price 
//* will continue the movement to the line 8/8.
// ============================================================================================
//* Line 1/8  (Weak, Place to Stop and Reverse).
//* This line is weak. If suddenly the price was going too fast and too far and stops around this line 
//* it means the price will reverse up very soon. If the price did not stop near this line, this price
//* will continue the movement down to the line 0/8.
// ============================================================================================
//* Line 2/8 and 6/8 (Pivot, Reverse)
//* Those two lines yield the line 4/8 only to the strength to reverse the price movement.
// ============================================================================================
//* Line 5/8 (Top of Trading Range)
//* The price is spending the about 40% of the time on the movement between the lines 5/8 and 3/8. 
//* If the price is moving near line 5/8 and stopping near the line during the 10 - 12 days, so it means 
//* that it is necessary to sell in this "bonus zone" (some people are doing like this) but if the price
//* is keeping the tendency to stay above the 5/8 line, so it means that the price will be above. 
//* But if the price is droping below 5/8 line it means that the price will continue falling 
//* to the next level of resistance.
// ============================================================================================
//* Line 3/8 (Bottom of Trading Range).
//* If the price is below this line and in uptrend it means that it will be very difficult for the price
//* to break this level. 
//* If the price broke this line during the uptrend and staying above during the 10 12 days it means that
//* the price will be above this line during the 40% of its time moving between this line and 5/8 line.
// ============================================================================================
//* Line 4/8 (Major Support/Resistance Line).
//* It is the major line concerning support and resistance.
//* This level is the better for the new sell or buy. 
//* It is the strong level of support of the price is above 4/8. It is the fine resistance line
//* if the price is below this 4/8 line. 
// ============================================================================================

#property indicator_buffers 7

#property indicator_color1 White
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Tomato
#property indicator_color4 Aqua
#property indicator_color5 Tomato
#property indicator_color6 DeepSkyBlue
#property indicator_color7 White

//---- buffers
double mml0[];// [-2]/8
double mml1[];//  [0]/8
double mml2[];//  [2]/8
double mml3[];//  [4]/8
double mml4[];//  [6]/8
double mml5[];//  [8]/8
double mml6[];//  [2]/8


extern int P = 64;  
extern int MMPeriod = 60;

//+------------------------------------------------------------------+ 

double  dmml = 0,
        dvtl = 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,
        fractal = 0,
        range   = 0,
        finalH  = 0,
        finalL  = 0,
        mml[13];

        
int     bn_v1   = 0,
        bn_v2   = 0,
        CurPeriod = 0,
        i = 0;
int     NewPeriod=0;

static datetime prevTime = 0;
static double prev_dmml = 0;
static double prev_finalL = 0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
//+------------------------------------------------------------------+ 
   

//---- indicators  IndicatorCounted
   CurPeriod = Period();
   if(MMPeriod>CurPeriod)
      NewPeriod   = P*MathCeil(MMPeriod/CurPeriod);
   else NewPeriod = P;
 
// mml0
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, mml0);
   IndicatorShortName("dynamic mml");
   SetIndexLabel(0, "[-2/8]");
// mml0 1
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, mml1);
   SetIndexLabel(1, "[0/8]");
// mml0 1
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, mml2);
   SetIndexLabel(2, "[2/8]");
// mml0 2
   SetIndexStyle(3, DRAW_LINE);
   SetIndexBuffer(3, mml3);
   SetIndexLabel(3, "[4/8]");
// mml0 2
   SetIndexStyle(4, DRAW_LINE);
   SetIndexBuffer(4, mml4);
   SetIndexLabel(4, "[6/8]");
// mml0 3
   SetIndexStyle(5, DRAW_LINE);
   SetIndexBuffer(5, mml5);
   SetIndexLabel(5, "[8/8]");
// mml0 3
   SetIndexStyle(6, DRAW_LINE);
   SetIndexBuffer(6, mml6);
   SetIndexLabel(6, "[+2/8]");
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
//---- TODO: add your code here

Comment(" ");   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {

//---- TODO: add your code here
   Print("Start");
   
   datetime curTime = iTime(NULL, CurPeriod, 0);
   if(prevTime != curTime) {

      prevTime = curTime;   
      for (int shift = 0; shift < Bars; shift++) 
      {
         double cur_finalH, cur_finalL;
         CalculateMML(NewPeriod, shift, cur_finalH, cur_finalL);
         double cur_dmml = (cur_finalH - cur_finalL)/8;
 
         if ((cur_dmml!=dmml)&&(cur_finalL!=finalL)) {
            Print("shift: ", shift, "  finalH: ", cur_finalH, "  finalL: ", cur_finalL, "  dmml: ", cur_dmml); 
            dmml = cur_dmml;
            finalL = cur_finalL;
         }
               
         mml0[shift] =(finalL-dmml*2); //-2/8
         mml1[shift] = mml0[shift] + 2*dmml;
         mml2[shift] = mml1[shift] + 2*dmml;
         mml3[shift] = mml2[shift] + 2*dmml;
         mml4[shift] = mml3[shift] + 2*dmml;
         mml5[shift] = mml4[shift] + 2*dmml;
         mml6[shift] = mml5[shift] + 2*dmml;
      }  //  end for
   } // end if 

//---- End Of Program
   return(0);
}
 
void CalculateMML(int period, int shift, double& finalH, double& finalL)
{
   //price
   bn_v1 = Lowest(NULL, 0, MODE_LOW, period + shift, shift);
   bn_v2 = Highest(NULL, 0, MODE_HIGH, period + shift, shift);

   v1 = Low[bn_v1];
   v2 = High[bn_v2];

   //determine fractal.....
   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;
      
   range=(v2-v1);
   sum=MathFloor(MathLog(fractal/range)/MathLog(2));
   octave=fractal*(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;
}

