//+------------------------------------------------------------------+
//|                                 dynamic balance point pivots.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1  DeepSkyBlue
#property indicator_color2  DeepSkyBlue
#property indicator_color3  DeepSkyBlue
#property indicator_color4  Red
#property indicator_color5  Red
#property indicator_color6  Red

//
//
//
//
//

extern int    dbpLength              = 5;
extern ENUM_TIMEFRAMES dbpTimeFrameForHighLow = PERIOD_D1;
extern ENUM_TIMEFRAMES dbpTimeFrameForPivots  = PERIOD_D1;

//
//
//
//
//

double dbp[];
double res1[];
double res2[];
double res3[];
double sup1[];
double sup2[];
double sup3[];

int timeFrame;
int timeFramePivots;

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

int init()
{
   IndicatorBuffers(7);
   SetIndexBuffer(0,res3);
   SetIndexBuffer(1,res2);
   SetIndexBuffer(2,res1);
   SetIndexBuffer(3,sup1);
   SetIndexBuffer(4,sup2);
   SetIndexBuffer(5,sup3);
   SetIndexBuffer(6,dbp);
      dbpLength = MathMax(1,dbpLength);
      timeFrame       = MathMax(dbpTimeFrameForHighLow,_Period);
      timeFramePivots = MathMax(dbpTimeFrameForPivots,_Period);

   //
   //
   //
   //
   //
   
   IndicatorShortName("Dynamic balance point ("+dbpLength+"-"+timeFrameToString(timeFrame)+")");
   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--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i])+dbpLength-1;
      int x = iBarShift(NULL,0,iTime(NULL,timeFrame,y));
      int l = x-i+1;
         
         double hi = High[iHighest(NULL,0,MODE_HIGH,l,i)];
         double lo = Low[iLowest(NULL,0,MODE_LOW,l,i)];

            dbp[i] = (hi+lo+Close[i])/3.0;
      
      //
      //
      //
      //
      //
      
         y = iBarShift(NULL,timeFramePivots,Time[i])+1;
         x = iBarShift(NULL,timeFramePivots,Time[i]);
               datetime timeStart = iTime(NULL,timeFramePivots,y);
               datetime timeEnd   = iTime(NULL,timeFramePivots,x);
         y = iBarShift(NULL,0,timeStart);
         x = iBarShift(NULL,0,timeEnd)+1;
         l = y-x+1;
            
               hi = dbp[ArrayMaximum(dbp,l,x)];
               lo = dbp[ArrayMinimum(dbp,l,x)];
         
      //
      //
      //
      //
      //
         
         res1[i] = 2.0*dbp[x]-lo;
         res2[i] = dbp[x]+(hi-lo);
         res3[i] = hi+2.0*(dbp[x]-lo);
               
         sup1[i] = 2.0*dbp[x]-hi;
         sup2[i] = dbp[x]-(hi-lo);
         sup3[i] = lo-2.0*(hi-dbp[x]);
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}  
/* 
Resistance1 = 2 * BalancePoint - low;
Resistance2 = BalancePoint + (high - low);
Resistance3 = high + 2 * (BalancePoint - low);

Support1 = 2 * BalancePoint - high;
Support2 = BalancePoint - (high - low);
Support3 = low - 2 * (high - BalancePoint);
*/

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

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}