//+------------------------------------------------------------------+
//|                                               Dynamic pivots.mq4 |
//|                                                                  |
//| made after an article of Autin Passamonte (TASC, July 2010)      |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1  DeepSkyBlue
#property indicator_color2  DeepSkyBlue
#property indicator_color3  DeepSkyBlue
#property indicator_color4  DimGray
#property indicator_color5  Red
#property indicator_color6  Red
#property indicator_color7  Red
#property indicator_style4  STYLE_DOT

//
//
//
//
//

extern double PivotLevel1            = 0.38;
extern double PivotLevel2            = 0.61;
extern double PivotLevel3            = 0.00;
extern bool   AlternativeCalculation = true;

//
//
//
//
//

double ul3[];
double ul2[];
double ul1[];
double mid[];
double dl1[];
double dl2[];
double dl3[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,ul3);
   SetIndexBuffer(1,ul2);
   SetIndexBuffer(2,ul1);
   SetIndexBuffer(3,mid);
   SetIndexBuffer(4,dl1);
   SetIndexBuffer(5,dl2);
   SetIndexBuffer(6,dl3);
      
   IndicatorShortName("Dymanic pivots");
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

   for (i=limit; i>=0; i--)
   {
      datetime startTime = StrToTime(TimeToStr(Time[i],TIME_DATE)+" 00:00");
      int      startBar  = iBarShift(NULL,0,startTime);
      int      length    = startBar-i+1;
      double   dayHigh   = High[iHighest(NULL,0,MODE_HIGH,length,i)];
      double   dayLow    = Low[iLowest(NULL,0,MODE_LOW,length,i)];
      double   range     = dayHigh-dayLow;

         //
         //
         //
         //
         //
         
         mid[i] = (dayHigh+dayLow)/2.0;
         if (AlternativeCalculation)
         {
            ul3[i] = (dayHigh+mid[i])/2.0;
            dl3[i] = (dayLow +mid[i])/2.0;
            if (PivotLevel1>0) if (PivotLevel1>0.5) ul1[i] = dayLow+range*PivotLevel1; else dl1[i] = dayLow+range*PivotLevel1;
            if (PivotLevel2>0) if (PivotLevel2>0.5) ul2[i] = dayLow+range*PivotLevel2; else dl2[i] = dayLow+range*PivotLevel2;
         }
         else
         {
            if (PivotLevel1>0) { ul1[i] = mid[i]+range*PivotLevel1; dl1[i] = mid[i]-range*PivotLevel1; }
            if (PivotLevel2>0) { ul2[i] = mid[i]+range*PivotLevel2; dl2[i] = mid[i]-range*PivotLevel2; }
            if (PivotLevel3>0) { ul3[i] = mid[i]+range*PivotLevel3; dl3[i] = mid[i]-range*PivotLevel3; }
         }
   }

   //
   //
   //
   //
   //
      
   return(0);      
}