//+------------------------------------------------------------------+
//|                                                  Force Histo.mq4 |
//|                                                              cja |
//+------------------------------------------------------------------+
#property copyright "cja"
#property link      "ccjjaa@gmail.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Gray
#property indicator_style1 2
#property indicator_color2 LimeGreen
#property indicator_color3 Orange
#property indicator_color4 Gray
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2

//---- input parameters

extern string TimeFrame    = "Current time frame";
extern int	  Force_Period	= 5;
extern int    Force_Method	= 0;
extern int    Force_Price	= 0;
extern bool   Interpolate  = true;

string name;
         
//---- indicator buffers
double Force[];
double forceUP[];
double forceDN[];
double zero[];

int    timeFrame;
string indicatorFileName;
bool   returnBars;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorDigits(2);
  
   SetIndexBuffer(0,zero);
   SetIndexBuffer(1,forceUP); SetIndexDrawBegin(1,Force_Period);  SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,forceDN); SetIndexDrawBegin(2,Force_Period);  SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,Force);   SetIndexDrawBegin(3,Force_Period);

   SetIndexLabel(0,"zero line");
   SetIndexLabel(1,"Force Index");
   SetIndexLabel(2,"Force Index");
   SetIndexLabel(3,"Force Index");

      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame=="returnBars";     if (returnBars) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
      name              = (timeFrameToString(timeFrame)+" Force Histo");
   IndicatorShortName(name); 
   return(0);
}
int deinit() { return(0); }  

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

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { zero[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (timeFrame>Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for( i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);

      Force[i] = iForce(NULL,timeFrame,Force_Period,Force_Method,Force_Price,y); 
         if(Force[i]>=0) 
               {forceUP[i]=Force[i]; forceDN[i]=0; }
         else  {forceDN[i]=Force[i]; forceUP[i]=0; }
      zero[i] = 0;
      
      if (timeFrame <= Period() || !Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

      //
      //
      //
      //
      //

      datetime time = iTime(NULL,timeFrame,y);
         for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
         for(int k = 1; k < n; k++)
         {
            Force[i+k] = Force[i] + (Force[i+n] - Force[i])*k/n;
               if (forceUP[i+k] != 0) forceUP[i+k] = Force[i+k];
               if (forceDN[i+k] != 0) forceDN[i+k] = Force[i+k];
         }               
   }
   return(0);
}
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

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

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}