//+--------------------------------------------------------------------------------------------+
//|                                                                                    KRI.mq4 |
//|                                                                 Kairi Relative Index (KRI) |
//|                                                             Copyright © 2011, Serega Lykov |
//|                                                                 http://mtexperts.narod.ru/ |
//+--------------------------------------------------------------------------------------------+

#property copyright "Copyright © 2011, Serega Lykov"
#property link      "http://mtexperts.narod.ru/"

//---- property of indicator ------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Gold

//---- external parameters --------------------------------------------------------------------+
extern int    MA_Period                   = 14;
extern int    MA_Method                   = 0;
extern int    MA_Price                    = 0;

//---- buffers --------------------------------------------------------------------------------+
static double KRI[];
static double MA[];

//---- global variables -----------------------------------------------------------------------+

//---------------------------------------------------------------------------------------------+
//---- initialization of indicator ------------------------------------------------------------+
//---------------------------------------------------------------------------------------------+
int init()
  {
   if(MA_Method < 0 || MA_Method > 3) MA_Method = 0;
   //---- set a "short" name of the indicator -------------------------------------------------+
   IndicatorShortName(StringConcatenate("KRI(",GetStringMAMethod(MA_Method),"(",DoubleToStr(MA_Period,0),"))"));
   //---- the 1 additional buffer -------------------------------------------------------------+
   IndicatorBuffers(2);
   //---- set a accuracy of values of the indicator -------------------------------------------+
   IndicatorDigits(4);
   //---- set a style for lines ---------------------------------------------------------------+
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   //---- set an arrays for lines -------------------------------------------------------------+
   SetIndexBuffer(0,KRI);
   SetIndexBuffer(1,MA);
   //---- set a names for lines ---------------------------------------------------------------+
   SetIndexLabel(0,StringConcatenate("KRI(",DoubleToStr(MA_Period,0),")"));
   //---- set a first bar for drawing the lines -----------------------------------------------+
   SetIndexDrawBegin(0,MA_Period);
   //---- set a levels ------------------------------------------------------------------------+
   SetLevelValue(0,0.0);
   SetLevelStyle(STYLE_DOT,1,White);
   //---- finish of initialization ------------------------------------------------------------+
   return(0);
   Print("---- Programming by Serega Lykov, http://mtexperts.narod.ru/ ----");
  }

//---------------------------------------------------------------------------------------------+
//---- deinitialization of indicator ----------------------------------------------------------+
//---------------------------------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//---------------------------------------------------------------------------------------------+
//---- main cycle -----------------------------------------------------------------------------+
//---------------------------------------------------------------------------------------------+
int start()
  {
   //---- amount not changed bars after last call of the indicator ----------------------------+
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be counted ----------------------------------------------------+
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   //---- get values of MA --------------------------------------------------------------------+
   for(int i=limit; i>=0; i--) MA[i] = iMA(NULL,0,MA_Period,0,MA_Method,MA_Price,i);
   //---- calculate values of KRI -------------------------------------------------------------+
   for(i=limit; i>=0; i--)
     {
      if(MA[i] == 0.0) KRI[i] = EMPTY_VALUE;
      else KRI[i] = (Close[i] - MA[i]) / MA[i] * 100.0;
     }
   //---- finish of iteration -----------------------------------------------------------------+
   return(0);
  }
    
//---------------------------------------------------------------------------------------------+
//---- GetStringMAMethod ----------------------------------------------------------------------+
//-------------------------------- programming by Serega Lykov, http://mtexperts.narod.ru/ ----+
//---------------------------------------------------------------------------------------------+
string GetStringMAMethod(int ma_method)
  {
   switch(ma_method)
     {
      case 1:  string str_ma_method = "EMA";  break;
      case 2:         str_ma_method = "SMMA"; break;
      case 3:         str_ma_method = "LWMA"; break;
      default:        str_ma_method = "SMA";
     }
   return(str_ma_method);
  }
 
//-------------------------------- programming by Serega Lykov, http://mtexperts.narod.ru/ ----+