//+------------------------------------------------------------------+
//|                                                      WPRfast.mq4 |
//|                                                                  |
//|                                        Ramdass - Conversion only |
//+------------------------------------------------------------------+
#property  copyright "Author - OlegVS"

#property indicator_separate_window
#property indicator_minimum -1.000000
#property indicator_maximum 1.000000
#property indicator_buffers 2
#property indicator_color1  clrDeepSkyBlue
#property indicator_color2  clrCrimson
#property indicator_width1  2
#property indicator_width2  2
//---- input parameters
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;    // Timeframe to use
extern int             P         = 9;
extern int             n1        = 9;
extern int             n2        = 49;
//---- buffers
double Up[];
double Down[];
double count[];
string indicatorFileName;
#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,P,n1,n2,_buff,_ind)
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
 {
 
   IndicatorBuffers(3);
   SetIndexBuffer(0,Up);   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,Down); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,count);
   
   indicatorFileName = WindowExpertName();
   TimeFrame         = fmax(TimeFrame,_Period);  
   
   IndicatorShortName(timeFrameToString(TimeFrame)+" WPRfast");
   return(0);
  }
//+------------------------------------------------------------------+
//| WPRfast                                                          |
//+------------------------------------------------------------------+
int start()
{

   int counted_bars=IndicatorCounted();
   int i,limit;
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         limit = fmin(Bars-counted_bars,Bars-1); count[0]=limit;
            if (TimeFrame!=_Period)
            {
               limit = (int)fmax(limit,fmin(Bars-1,_mtfCall(2,0)*TimeFrame/_Period));
               for (i=limit;i>=0 && !_StopFlag; i--)
               {
                  int y = iBarShift(NULL,TimeFrame,Time[i]);
                     Up[i]   = _mtfCall(0,y);
                     Down[i] = _mtfCall(1,y);   
               }      
     return(0);
     } 
     
     for(i=limit; i>=0; i--)
     {
        Down[i]=EMPTY_VALUE; Up[i]=EMPTY_VALUE;    
        double V1=iWPR(NULL,0,P,i)*iWPR(NULL,0,P,i)/100;
        double V2=MathCeil(V1);
        if (V2<n1) {Up[i]=V2;} 
        if (V2>n2) {Down[i]=-(V2);}
     }
   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("");
}