//+------------------------------------------------------------------+
//|    mod pacois                 dynamic balance point_distance.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 1
//#property indicator_color1  clrNONE
#property indicator_color1  DeepSkyBlue

#property indicator_width1  2

//
//
//
//
//

extern ENUM_TIMEFRAMES dbpTimeFrameForHighLow = PERIOD_CURRENT;
extern int             dbpLength              = 5;
extern int PeriodEma =1;
extern ENUM_MA_METHOD MODE=MODE_SMA;
extern ENUM_APPLIED_PRICE PRICE=PRICE_CLOSE;
extern bool Value2_On_Off =false;


    

//
//
//
//
//

double dbp[];
double Distdbp[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
    
      IndicatorBuffers(2);
  SetIndexBuffer(0,Distdbp);SetIndexStyle (0,DRAW_HISTOGRAM,0,3);
  
  SetIndexBuffer(1,dbp);
    dbpLength = MathMax(1,dbpLength);
  
   //
   //
   //
   //
   //
   
   IndicatorShortName("Dynamic balance point_distance ("+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;
      
      
      Distdbp[i]=iMA(NULL,0,PeriodEma,0,MODE,PRICE,i)/Point - dbp[i]/Point  ;
      
      if(Value2_On_Off)
      
       Distdbp[i]=iMA(NULL,0,PeriodEma,0,MODE,PRICE,i) - dbp[i]  ;
      
      
   }
   
   //
   //
   //
   //
   //
   
   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("");
}
