//+------------------------------------------------------------------+
//|                                                Ratio Awesome.mq4 |
//| The Ratio code was written by 2007, Christof Risch (iya)         |
//| All credits to him                                               |
//| Linuxser 2007                                                    |
//| mladen   2008                                                    |
//+------------------------------------------------------------------+
#property copyright "Under The GNU General Public License V3"
#property link      "www.gnu.org"

#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  Green
#property  indicator_color2  Red
#property  indicator_color3  DarkSlateGray
#property  indicator_width3  2

//
//
//
//
//

extern string timeFrame         = "current time frame";
extern bool   interpolateColors = true;

//
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int    TimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,ExtMapBuffer1); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexEmptyValue(0,0.00);
   SetIndexBuffer(1,ExtMapBuffer2); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexEmptyValue(1,0.00);
   SetIndexBuffer(2,ExtMapBuffer3);
   
   //
   //
   //
   //
   //

   TimeFrame = stringToTimeFrame(timeFrame);
   
   string TimeFrameStr;
   switch(TimeFrame)
   {
      case PERIOD_M1:  TimeFrameStr="(M1)";      break;
      case PERIOD_M5:  TimeFrameStr="(M5)";      break;
      case PERIOD_M15: TimeFrameStr="(M15)";     break;
      case PERIOD_M30: TimeFrameStr="(M30)";     break;
      case PERIOD_H1:  TimeFrameStr="(H1)";      break;
      case PERIOD_H4:  TimeFrameStr="(H4)";      break;
      case PERIOD_D1:  TimeFrameStr="(Dayly)";   break;
      case PERIOD_W1:  TimeFrameStr="(Weekly)";  break;
      case PERIOD_MN1: TimeFrameStr="(Monthly)"; break;
      default :        TimeFrameStr="";
   }
   IndicatorShortName("Ratio AO "+TimeFrameStr);
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars = IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           if (TimeFrame > Period()) limit = MathMax(limit,TimeFrame/Period());

   //
   //
   //
   //
   //
   
	for(i = limit; i >= 0; i--)
	{
		int      shift1 = iBarShift(NULL,TimeFrame,Time[i]);
		datetime time1  = iTime    (NULL,TimeFrame,shift1);

         ExtMapBuffer3[i]=iAO(NULL,TimeFrame,shift1);

         if(ExtMapBuffer3[i]  < ExtMapBuffer3[i+1]) { ExtMapBuffer2[i]=ExtMapBuffer3[i];   ExtMapBuffer1[i]=0.0; }
         if(ExtMapBuffer3[i]  > ExtMapBuffer3[i+1]) { ExtMapBuffer1[i]=ExtMapBuffer3[i];   ExtMapBuffer2[i]=0.0; }
         if(ExtMapBuffer3[i] == ExtMapBuffer3[i+1]) { ExtMapBuffer1[i]=ExtMapBuffer1[i+1]; ExtMapBuffer2[i]=ExtMapBuffer2[i+1]; }

         //
         //
         //
         //
         //
         
         if(TimeFrame <= Period() || shift1==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

         //
         //
	      //   apply interpolation
	      //
	      //
		 
  		   for(int n = 1; i+n < Bars && Time[i+n] >= time1; n++) continue;	

  		   double factor = 1.0 / n;
         for(int k = 1; k < n; k++)
         {
     			ExtMapBuffer3[i+k] = k*factor*ExtMapBuffer3[i+n] + (1.0-k*factor)*ExtMapBuffer3[i];
     			if (interpolateColors)
        			{
     			       ExtMapBuffer1[i+k] = k*factor*ExtMapBuffer1[i+n] + (1.0-k*factor)*ExtMapBuffer1[i];
     			       ExtMapBuffer2[i+k] = k*factor*ExtMapBuffer2[i+n] + (1.0-k*factor)*ExtMapBuffer2[i];
        			}
     			else
     	   		{
        	   		if (ExtMapBuffer1[i+k]!=0) ExtMapBuffer1[i+k]=ExtMapBuffer3[i+k];
        		     	if (ExtMapBuffer2[i+k]!=0) ExtMapBuffer2[i+k]=ExtMapBuffer3[i+k];
               }        			
     		}
   }         
   return(0);
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   for(int l = StringLen(tfs)-1; l >= 0; l--)
   {
      int char = StringGetChar(tfs,l);
          if((char > 96 && char < 123) || (char > 223 && char < 256))
               tfs = StringSetChar(tfs, l, char - 32);
          else 
              if(char > -33 && char < 0)
                  tfs = StringSetChar(tfs, l, char + 224);
   }

   //
   //
   //
   //
   //
   
   int tf=0;
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
   return(tf);
}