//+------------------------------------------------------------------+
//|                                                Ratio MACD_v2.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 1
#property indicator_color1 Silver


//---- input parameters
/*************************************************************************
PRICE_CLOSE    0 Close price. 
PRICE_OPEN     1 Open price. 
PRICE_HIGH     2 High price. 
PRICE_LOW      3 Low price. 
PRICE_MEDIAN   4 Median price, (high+low)/2. 
PRICE_TYPICAL  5 Typical price, (high+low+close)/3. 
PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4. 
You must use the numeric value of the Applied Price that you want to use
when you set the 'applied_price' value with the indicator inputs.
**************************************************************************/

extern string timeFrame     = "current time frame";
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
extern int applied_price=0;


double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int    TimeFrame;

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

int init()
{
   SetIndexBuffer(0,ExtMapBuffer1);
      SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexDrawBegin(1,SignalSMA);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
   
   //
   //
   //
   //
   //

   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_OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")"+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);

      ExtMapBuffer1[i]=iOsMA(NULL,TimeFrame,FastEMA,SlowEMA,SignalSMA,applied_price,shift1);  

         //
         //
         //
         //
         //
         
         if(TimeFrame <= Period() || iBarShift(NULL,TimeFrame,Time[i])==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++)
   			ExtMapBuffer1[i+k] = k*factor*ExtMapBuffer1[i+n] + (1.0-k*factor)*ExtMapBuffer1[i];
   }
   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);
}