//+------------------------------------------------------------------+
//|                                                   ARSI2_10.1.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 11
#property indicator_color1  clrYellow
#property indicator_color2  clrGold
#property indicator_color3  clrOrange
#property indicator_color4  clrDarkOrange
#property indicator_color5  clrOrangeRed
#property indicator_color6  clrGreen
#property indicator_color7  clrSeaGreen
#property indicator_color8  clrMediumSeaGreen
#property indicator_color9  clrLimeGreen
#property indicator_color10 clrLime
#property indicator_color11 clrDeepSkyBlue
#property indicator_width11 3
#property strict
//---- input parameters
input int       period1 = 2, period2 = 2, period3 = 2, period4 = 2, period5 = 2, period6 = 2,
                 period7 = 2, period8 = 2, period9 = 2,  period10 = 2 ;
input int              SigMA = 5;
input ENUM_MA_METHOD SigMode = MODE_EMA;     
//-------
double ARSI[],ARSI1[],ARSI2[],ARSI3[],ARSI4[],ARSI5[],ARSI6[],ARSI7[],ARSI8[],ARSI9[], SIGMA[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorDigits(_Digits);
   //IndicatorBuffers(11);   
   SetIndexBuffer(0, ARSI, INDICATOR_DATA);  SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(1, ARSI1,INDICATOR_DATA);  SetIndexStyle(1, DRAW_LINE);  
   SetIndexBuffer(2, ARSI2,INDICATOR_DATA);  SetIndexStyle(2, DRAW_LINE);    
   SetIndexBuffer(3, ARSI3,INDICATOR_DATA);  SetIndexStyle(3, DRAW_LINE);
   SetIndexBuffer(4, ARSI4,INDICATOR_DATA);  SetIndexStyle(4, DRAW_LINE);
   SetIndexBuffer(5, ARSI5,INDICATOR_DATA);  SetIndexStyle(5, DRAW_LINE);
   SetIndexBuffer(6, ARSI6,INDICATOR_DATA);  SetIndexStyle(6, DRAW_LINE);
   SetIndexBuffer(7, ARSI7,INDICATOR_DATA);  SetIndexStyle(7, DRAW_LINE);
   SetIndexBuffer(8, ARSI8,INDICATOR_DATA);  SetIndexStyle(8, DRAW_LINE);
   SetIndexBuffer(9, ARSI9,INDICATOR_DATA);  SetIndexStyle(9, DRAW_LINE);
   SetIndexBuffer(10,SIGMA,INDICATOR_DATA);  SetIndexStyle(10,DRAW_LINE); 
return(INIT_SUCCEEDED);
}
//+======================================================================+

void OnDeinit(const int reason) { }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int  OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-2; 
   for (; i>=0 && !_StopFlag; i--)
   {
      if (rates_total - period1 <= i)
      {
         ARSI[i] = close[i];     
      } 
      else 
      {
         double sc  = (fabs(iRSI(NULL,0,period1, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc1 = (fabs(iRSI(NULL,0,period2, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc2 = (fabs(iRSI(NULL,0,period3, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc3 = (fabs(iRSI(NULL,0,period4, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc4 = (fabs(iRSI(NULL,0,period5, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc5 = (fabs(iRSI(NULL,0,period6, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc6 = (fabs(iRSI(NULL,0,period7, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc7 = (fabs(iRSI(NULL,0,period8, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc8 = (fabs(iRSI(NULL,0,period9, PRICE_CLOSE, i) / 100 - 0.5) * 2);
         double sc9 = (fabs(iRSI(NULL,0,period10,PRICE_CLOSE, i) / 100 - 0.5) * 2);
         ARSI[i]  = ARSI[i+1]  + sc  * (close[i] - ARSI[i+1]);
         ARSI1[i] = ARSI1[i+1] + sc1 * (ARSI[i]  - ARSI1[i+1]);
         ARSI2[i] = ARSI2[i+1] + sc2 * (ARSI1[i] - ARSI2[i+1]);
         ARSI3[i] = ARSI3[i+1] + sc3 * (ARSI2[i] - ARSI3[i+1]);
         ARSI4[i] = ARSI4[i+1] + sc4 * (ARSI3[i] - ARSI4[i+1]);
         ARSI5[i] = ARSI5[i+1] + sc5 * (ARSI4[i] - ARSI5[i+1]);
         ARSI6[i] = ARSI6[i+1] + sc6 * (ARSI5[i] - ARSI6[i+1]);
         ARSI7[i] = ARSI7[i+1] + sc7 * (ARSI6[i] - ARSI7[i+1]);
         ARSI8[i] = ARSI8[i+1] + sc8 * (ARSI7[i] - ARSI8[i+1]);
         ARSI9[i] = ARSI9[i+1] + sc9 * (ARSI8[i] - ARSI9[i+1]);
         SIGMA[i] = iMAOnArray(ARSI9,0,SigMA,0,SigMode,i);
      }
   }  
return(rates_total);
}
