//+------------------------------------------------------------------+
//|                                        dynamic balance point.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  DeepSkyBlue
#property indicator_width1  2

//
//
//
//
//

extern ENUM_TIMEFRAMES dbpTimeFrameForHighLow = PERIOD_CURRENT;
extern int             dbpLength              = 5;

//
//
//
//
//

double dbp[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
    SetIndexBuffer(0,dbp);
      dbpLength = MathMax(1,dbpLength);

   //
   //
   //
   //
   //
   
   IndicatorShortName("Dynamic balance point ("+dbpLength+"-"+timeFrameToString(dbpTimeFrameForHighLow)+")");
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         int limit=MathMax(Bars-counted_bars,3*dbpTimeFrameForHighLow/Period());

   //
   //
   //
   //
   //
   
   for(int i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,dbpTimeFrameForHighLow,Time[i])+dbpLength-1;
      int x = iBarShift(NULL,0,iTime(NULL,dbpTimeFrameForHighLow,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;
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}   

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}
