//------------------------------------------------------------------
// original idea by Alexander Kirilyuk M.
//8/30/21 https://forex-station.com/viewtopic.php?p=1295443038#p1295443038
/*
RSI14, RSI16, RSI18, RSI20, RSI22,
RSI24, RSI26, RSI28, RSI30, RSI32,
RSI34, 

RSI36, RSI38, RSI40, RSI42, RSI44,
RSI46, RSI48, RSI50, RSI52, RSI54,
RSI56,

RSI58, RSI60, RSI62, RSI64, RSI68,
RSI70, RSI72, RSI74, RSI76, RSI78,
RSI80

*/
#property copyright "mladen"
#property link      "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 55
#property indicator_color1   clrLimeGreen
#property indicator_color2   clrMediumVioletRed
#property indicator_color3   clrOrange
#property indicator_color4   clrNONE
#property indicator_color5   clrNONE
#property indicator_color6   clrLimeGreen
#property indicator_color7   clrMediumVioletRed
#property indicator_color8   clrOrange
#property indicator_color9   clrNONE
#property indicator_color10  clrNONE

#property indicator_color11   clrLimeGreen
#property indicator_color12   clrMediumVioletRed
#property indicator_color13   clrOrange
#property indicator_color14   clrNONE
#property indicator_color15   clrNONE
#property indicator_color16   clrLimeGreen
#property indicator_color17   clrMediumVioletRed
#property indicator_color18   clrOrange
#property indicator_color19   clrNONE
#property indicator_color20   clrNONE

#property indicator_color21   clrLimeGreen //5
#property indicator_color22   clrMediumVioletRed
#property indicator_color23   clrOrange
#property indicator_color24   clrNONE
#property indicator_color25   clrNONE
#property indicator_color26   clrLimeGreen //6
#property indicator_color27   clrMediumVioletRed
#property indicator_color28   clrOrange
#property indicator_color29   clrNONE
#property indicator_color30   clrNONE

#property indicator_color31   clrLimeGreen //7
#property indicator_color32   clrMediumVioletRed
#property indicator_color33   clrOrange
#property indicator_color34   clrNONE
#property indicator_color35   clrNONE
#property indicator_color36   clrLimeGreen  //8
#property indicator_color37   clrMediumVioletRed
#property indicator_color38   clrOrange
#property indicator_color39   clrNONE
#property indicator_color40   clrNONE

#property indicator_color41   clrLimeGreen
#property indicator_color42   clrMediumVioletRed
#property indicator_color43   clrOrange
#property indicator_color44   clrNONE
#property indicator_color45   clrNONE
#property indicator_color46   clrLimeGreen //10
#property indicator_color47   clrMediumVioletRed
#property indicator_color48   clrOrange
#property indicator_color49   clrNONE
#property indicator_color50   clrNONE

#property indicator_color51   clrLimeGreen
#property indicator_color52   clrMediumVioletRed
#property indicator_color53   clrOrange
#property indicator_color54   clrNONE
#property indicator_color55   clrNONE

extern ENUM_TIMEFRAMES    TimeFrame             = PERIOD_CURRENT;
extern int                ARSIPeriod            = 14;
extern int                ARSIstep              = 2;
extern ENUM_APPLIED_PRICE ARSIPrice             = 0;
extern int                period                = 20;
extern ENUM_MA_METHOD     ma_method             = 1;
extern bool               alertsOn              = false;
extern bool               alertsOnCurrent       = false;
extern bool               alertsSound           = false;
extern bool               alertsMessage         = false;
extern bool               alertsNotification    = false;
extern bool               alertsEmail           = false;
//button template start1; copy and paste
extern string             button_note1          = "------------------------------";
extern int                btn_Subwindow         = 0;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "RSI";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;                        
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = clrRed;
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 20;                                   
extern int                button_y              = 13;                                   
extern int                btn_Width             = 60;                                
extern int                btn_Height            = 20;                               
extern string             button_note2          = "------------------------------";

bool show_data = true;
bool recalc    = true;
string indicatorFileName, IndicatorName, IndicatorObjPrefix,buttonId;
//button template end1; copy and paste

double ARSI1a[], ARSI1b[], buffer1a[], buffer1b[], trend1[];
double ARSI2a[], ARSI2b[], buffer2a[], buffer2b[], trend2[];
double ARSI3a[], ARSI3b[], buffer3a[], buffer3b[], trend3[];
double ARSI4a[], ARSI4b[], buffer4a[], buffer4b[], trend4[];
double ARSI5a[], ARSI5b[], buffer5a[], buffer5b[], trend5[];

double ARSI6a[], ARSI6b[], buffer6a[], buffer6b[], trend6[];
double ARSI7a[], ARSI7b[], buffer7a[], buffer7b[], trend7[];
double ARSI8a[], ARSI8b[], buffer8a[], buffer8b[], trend8[];
double ARSI9a[], ARSI9b[], buffer9a[], buffer9b[], trend9[];
double ARSI10a[], ARSI10b[], buffer10a[], buffer10b[], trend10[];
double ARSI11a[], ARSI11b[], buffer11a[], buffer11b[], trend11[];
bool   returnBars;
//+------------------------------------------------------------------------------------------------------------------+
//button template start2; copy and paste
string GenerateIndicatorName(const string target) 
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//+------------------------------------------------------------------------------------------------------------------+
int OnInit()
{
   IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorDigits(Digits);
   
   double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
      show_data = val != 0;

   IndicatorBuffers(55);
	SetIndexBuffer(0,ARSI1a); 
	SetIndexBuffer(1,buffer1a);
   SetIndexBuffer(2,buffer1b);
	SetIndexBuffer(3,ARSI1b); 
   SetIndexBuffer(4,trend1);   
	SetIndexBuffer(5,ARSI2a); 
	SetIndexBuffer(6,buffer2a);
   SetIndexBuffer(7,buffer2b);
	SetIndexBuffer(8,ARSI2b); 
   SetIndexBuffer(9,trend2); 
   
	SetIndexBuffer(10,ARSI3a); 
	SetIndexBuffer(11,buffer3a);
   SetIndexBuffer(12,buffer3b);
	SetIndexBuffer(13,ARSI3b); 
   SetIndexBuffer(14,trend3); 
	SetIndexBuffer(15,ARSI4a); 
	SetIndexBuffer(16,buffer4a);
   SetIndexBuffer(17,buffer4b);
	SetIndexBuffer(18,ARSI4b); 
   SetIndexBuffer(19,trend4); 

	SetIndexBuffer(20,ARSI5a); 
	SetIndexBuffer(21,buffer5a);
   SetIndexBuffer(22,buffer5b);
	SetIndexBuffer(23,ARSI5b); 
   SetIndexBuffer(24,trend5); 
	SetIndexBuffer(25,ARSI6a); 
	SetIndexBuffer(26,buffer6a);
   SetIndexBuffer(27,buffer6b);
	SetIndexBuffer(28,ARSI6b); 
   SetIndexBuffer(29,trend6); 
      
   SetIndexBuffer(30,ARSI7a); 
	SetIndexBuffer(31,buffer7a);
   SetIndexBuffer(32,buffer7b);
	SetIndexBuffer(33,ARSI7b); 
   SetIndexBuffer(34,trend7); 
	SetIndexBuffer(35,ARSI8a); 
	SetIndexBuffer(36,buffer8a);
   SetIndexBuffer(37,buffer8b);
	SetIndexBuffer(38,ARSI8b); 
   SetIndexBuffer(39,trend8); 

	SetIndexBuffer(40,ARSI9a); 
	SetIndexBuffer(41,buffer9a);
   SetIndexBuffer(42,buffer9b);
	SetIndexBuffer(43,ARSI9b); 
   SetIndexBuffer(44,trend9); 
	SetIndexBuffer(45,ARSI10a); 
	SetIndexBuffer(46,buffer10a);
   SetIndexBuffer(47,buffer10b);
	SetIndexBuffer(48,ARSI10b); 
   SetIndexBuffer(49,trend10); 

	SetIndexBuffer(50,ARSI11a); 
	SetIndexBuffer(51,buffer11a);
   SetIndexBuffer(52,buffer11b);
	SetIndexBuffer(53,ARSI11b); 
   SetIndexBuffer(54,trend11); 

   returnBars        = TimeFrame==-99;
   TimeFrame         = MathMax(TimeFrame,_Period);
      
      //
      //
      //
      //
      //
               
   IndicatorShortName(timeFrameToString(TimeFrame)+" RSI adaptive EMA nrp (" + ARSIPeriod+")"+IndicatorName);
   indicatorFileName = WindowExpertName();
   
   ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix + (btn_text);
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(0, buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(0, buttonId, OBJPROP_XDISTANCE, button_x);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason) { 
     ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
}
//+------------------------------------------------------------------------------------------------------------------+
void createButton(string buttonID,string buttonText,int width2,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (0,buttonID);
      ObjectCreate    (0,buttonID,OBJ_BUTTON,btn_Subwindow,0,0);
      ObjectSetInteger(0,buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(0,buttonID,OBJPROP_BORDER_TYPE,BORDER_RAISED);
      ObjectSetInteger(0,buttonID,OBJPROP_XSIZE,width2);
      ObjectSetInteger(0,buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (0,buttonID,OBJPROP_FONT,font);
      ObjectSetString (0,buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(0,buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(0,buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(0,buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(0,buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(0,buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(0,buttonID,OBJPROP_YDISTANCE,9999);
}
//+------------------------------------------------------------------------------------------------------------------+
void handleButtonClicks()
{
   if (ObjectGetInteger(0, buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(0, buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
   }
}
//+------------------------------------------------------------------------------------------------------------------+
void OnChartEvent(const int id, 
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   
   for (int banzai=0; banzai<indicator_buffers; banzai++)
     SetIndexStyle(banzai,DRAW_LINE);
     
   if (show_data)
       ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color); 
      else
      {
        ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
        for (int banzai2=0; banzai2<indicator_buffers; banzai2++)
            SetIndexStyle(banzai2,DRAW_NONE);
      }      
}
//button template end2; copy and paste
//+------------------------------------------------------------------------------------------------------------------+
int start() 
{ 
   int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { ARSI1a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI2a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI3a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI4a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI5a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI6a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI7a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI8a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI9a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI10a[0] = MathMin(limit+1,Bars-1); return(0); }
         if (returnBars) { ARSI11a[0] = MathMin(limit+1,Bars-1); return(0); }
   //
   //
   //
   //
   //
   if (TimeFrame == Period())
   {
      if (trend1[limit]==-1)  CleanPoint(limit,buffer1a,buffer1b);
      if (trend2[limit]==-1)  CleanPoint(limit,buffer2a,buffer2b);
      if (trend3[limit]==-1)  CleanPoint(limit,buffer3a,buffer3b);
      if (trend4[limit]==-1)  CleanPoint(limit,buffer4a,buffer4b);
      if (trend5[limit]==-1)  CleanPoint(limit,buffer5a,buffer5b);

      if (trend6[limit]==-1)  CleanPoint(limit,buffer6a,buffer6b);
      if (trend7[limit]==-1)  CleanPoint(limit,buffer7a,buffer7b);
      if (trend8[limit]==-1)  CleanPoint(limit,buffer8a,buffer8b);
      if (trend9[limit]==-1)  CleanPoint(limit,buffer9a,buffer9b);
      if (trend10[limit]==-1) CleanPoint(limit,buffer10a,buffer10b);
      if (trend11[limit]==-1) CleanPoint(limit,buffer11a,buffer11b);
      for(i=limit; i >= 0; i--)
         { 
               double sc1 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*0), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI1b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI1b[i] = ARSI1b[i+1] + sc1 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI1b[i+1]);
		         
		         double sc2 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*1), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI2b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI2b[i] = ARSI2b[i+1] + sc2 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI2b[i+1]);
		         
		         double sc3 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*2), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI3b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI3b[i] = ARSI3b[i+1] + sc3 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI3b[i+1]);
		         
		         double sc4 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*3), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI4b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI4b[i] = ARSI4b[i+1] + sc4 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI4b[i+1]);
		         
		         double sc5 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*4), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI5b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI5b[i] = ARSI5b[i+1] + sc5 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI5b[i+1]);

		         double sc6 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*5), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI6b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI6b[i] = ARSI6b[i+1] + sc6 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI6b[i+1]);

		         double sc7 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*6), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI7b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI7b[i] = ARSI7b[i+1] + sc7 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI7b[i+1]);

		         double sc8 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*7), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI8b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI8b[i] = ARSI8b[i+1] + sc8 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI8b[i+1]);

		         double sc9 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*8), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI9b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI9b[i] = ARSI9b[i+1] + sc9 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI9b[i+1]);

		         double sc10 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*9), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI10b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI10b[i] = ARSI10b[i+1] + sc10 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI10b[i+1]);

		         double sc11 = MathAbs(iRSI(NULL, 0, ARSIPeriod+(ARSIstep*10), ARSIPrice, i)/100.0 - 0.5) * 2.0;
		         if( Bars - i <= ARSIPeriod)
   			         ARSI11b[i] = iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i);
		         else	ARSI11b[i] = ARSI11b[i+1] + sc11 * (iMA(NULL,0,1,0,MODE_SMA,ARSIPrice,i) - ARSI11b[i+1]);
	      }
      for(i = limit; i >= 0; i--) 
      {
         ARSI1a[i] = iMAOnArray(ARSI1b,0,period,0,ma_method,i);
         buffer1a[i] = EMPTY_VALUE;
         buffer1b[i] = EMPTY_VALUE;
         trend1[i]   = trend1[i+1];
            if (ARSI1a[i]>ARSI1a[i+1]) trend1[i] = 1;
            if (ARSI1a[i]<ARSI1a[i+1]) trend1[i] =-1;
            if (trend1[i]==-1) PlotPoint(i,buffer1a,buffer1b,ARSI1a);
            
         ARSI2a[i] = iMAOnArray(ARSI2b,0,period,0,ma_method,i);
         buffer2a[i] = EMPTY_VALUE;
         buffer2b[i] = EMPTY_VALUE;
         trend2[i]   = trend2[i+1];
            if (ARSI2a[i]>ARSI2a[i+1]) trend2[i] = 1;
            if (ARSI2a[i]<ARSI2a[i+1]) trend2[i] =-1;
            if (trend2[i]==-1) PlotPoint(i,buffer2a,buffer2b,ARSI2a);
            
         ARSI3a[i] = iMAOnArray(ARSI3b,0,period,0,ma_method,i);
         buffer3a[i] = EMPTY_VALUE;
         buffer3b[i] = EMPTY_VALUE;
         trend3[i]   = trend3[i+1];
            if (ARSI3a[i]>ARSI3a[i+1]) trend3[i] = 1;
            if (ARSI3a[i]<ARSI3a[i+1]) trend3[i] =-1;
            if (trend3[i]==-1) PlotPoint(i,buffer3a,buffer3b,ARSI3a);

         ARSI4a[i] = iMAOnArray(ARSI4b,0,period,0,ma_method,i);
         buffer4a[i] = EMPTY_VALUE;
         buffer4b[i] = EMPTY_VALUE;
         trend4[i]   = trend4[i+1];
            if (ARSI4a[i]>ARSI4a[i+1]) trend4[i] = 1;
            if (ARSI4a[i]<ARSI4a[i+1]) trend4[i] =-1;
            if (trend4[i]==-1) PlotPoint(i,buffer4a,buffer4b,ARSI4a);

         ARSI5a[i] = iMAOnArray(ARSI5b,0,period,0,ma_method,i);
         buffer5a[i] = EMPTY_VALUE;
         buffer5b[i] = EMPTY_VALUE;
         trend5[i]   = trend5[i+1];
            if (ARSI5a[i]>ARSI5a[i+1]) trend5[i] = 1;
            if (ARSI5a[i]<ARSI5a[i+1]) trend5[i] =-1;
            if (trend5[i]==-1) PlotPoint(i,buffer5a,buffer5b,ARSI5a);

         ARSI6a[i] = iMAOnArray(ARSI6b,0,period,0,ma_method,i);
         buffer6a[i] = EMPTY_VALUE;
         buffer6b[i] = EMPTY_VALUE;
         trend6[i]   = trend6[i+1];
            if (ARSI6a[i]>ARSI6a[i+1]) trend6[i] = 1;
            if (ARSI6a[i]<ARSI6a[i+1]) trend6[i] =-1;
            if (trend6[i]==-1) PlotPoint(i,buffer6a,buffer6b,ARSI6a);

         ARSI7a[i] = iMAOnArray(ARSI7b,0,period,0,ma_method,i);
         buffer7a[i] = EMPTY_VALUE;
         buffer7b[i] = EMPTY_VALUE;
         trend7[i]   = trend7[i+1];
            if (ARSI7a[i]>ARSI7a[i+1]) trend7[i] = 1;
            if (ARSI7a[i]<ARSI7a[i+1]) trend7[i] =-1;
            if (trend7[i]==-1) PlotPoint(i,buffer7a,buffer7b,ARSI7a);

         ARSI8a[i] = iMAOnArray(ARSI8b,0,period,0,ma_method,i);
         buffer8a[i] = EMPTY_VALUE;
         buffer8b[i] = EMPTY_VALUE;
         trend8[i]   = trend8[i+1];
            if (ARSI8a[i]>ARSI8a[i+1]) trend8[i] = 1;
            if (ARSI8a[i]<ARSI8a[i+1]) trend8[i] =-1;
            if (trend8[i]==-1) PlotPoint(i,buffer8a,buffer8b,ARSI8a);

         ARSI9a[i] = iMAOnArray(ARSI9b,0,period,0,ma_method,i);
         buffer9a[i] = EMPTY_VALUE;
         buffer9b[i] = EMPTY_VALUE;
         trend9[i]   = trend9[i+1];
            if (ARSI9a[i]>ARSI9a[i+1]) trend9[i] = 1;
            if (ARSI9a[i]<ARSI9a[i+1]) trend9[i] =-1;
            if (trend9[i]==-1) PlotPoint(i,buffer9a,buffer9b,ARSI9a);

         ARSI10a[i] = iMAOnArray(ARSI10b,0,period,0,ma_method,i);
         buffer10a[i] = EMPTY_VALUE;
         buffer10b[i] = EMPTY_VALUE;
         trend10[i]   = trend10[i+1];
            if (ARSI10a[i]>ARSI10a[i+1]) trend10[i] = 1;
            if (ARSI10a[i]<ARSI10a[i+1]) trend10[i] =-1;
            if (trend10[i]==-1) PlotPoint(i,buffer10a,buffer10b,ARSI10a);

         ARSI11a[i] = iMAOnArray(ARSI11b,0,period,0,ma_method,i);
         buffer11a[i] = EMPTY_VALUE;
         buffer11b[i] = EMPTY_VALUE;
         trend11[i]   = trend11[i+1];
            if (ARSI11a[i]>ARSI11a[i+1]) trend11[i] = 1;
            if (ARSI11a[i]<ARSI11a[i+1]) trend11[i] =-1;
            if (trend11[i]==-1) PlotPoint(i,buffer11a,buffer11b,ARSI11a);
            
      }
      manageAlerts();
	return(0); 
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   if (trend1[limit]==-1) CleanPoint(limit,buffer1a,buffer1b);
   if (trend2[limit]==-1) CleanPoint(limit,buffer2a,buffer2b);
   if (trend3[limit]==-1) CleanPoint(limit,buffer3a,buffer3b);
   if (trend4[limit]==-1) CleanPoint(limit,buffer4a,buffer4b);
   if (trend5[limit]==-1) CleanPoint(limit,buffer5a,buffer5b);

   if (trend6[limit]==-1) CleanPoint(limit,buffer6a,buffer6b);
   if (trend7[limit]==-1) CleanPoint(limit,buffer7a,buffer7b);
   if (trend8[limit]==-1) CleanPoint(limit,buffer8a,buffer8b);
   if (trend9[limit]==-1) CleanPoint(limit,buffer9a,buffer9b);
   if (trend10[limit]==-1) CleanPoint(limit,buffer10a,buffer10b);
   if (trend11[limit]==-1) CleanPoint(limit,buffer11a,buffer11b);
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         trend1[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*0,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI1a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*0,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer1a[i] = EMPTY_VALUE;
         buffer1b[i] = EMPTY_VALUE;
         
         trend2[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*1,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI2a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*1,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer2a[i] = EMPTY_VALUE;
         buffer2b[i] = EMPTY_VALUE;
         
         trend3[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*2,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI3a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*2,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer3a[i] = EMPTY_VALUE;
         buffer3b[i] = EMPTY_VALUE;
         
         trend4[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*3,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI4a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*3,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer4a[i] = EMPTY_VALUE;
         buffer4b[i] = EMPTY_VALUE;
         
         trend5[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*4,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI5a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*4,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer5a[i] = EMPTY_VALUE;
         buffer5b[i] = EMPTY_VALUE;

         trend6[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*5,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI6a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*5,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer6a[i] = EMPTY_VALUE;
         buffer6b[i] = EMPTY_VALUE;

         trend7[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*6,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI7a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*6,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer7a[i] = EMPTY_VALUE;
         buffer7b[i] = EMPTY_VALUE;

         trend8[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*7,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI8a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*7,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer8a[i] = EMPTY_VALUE;
         buffer8b[i] = EMPTY_VALUE;

         trend9[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*8,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI9a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*8,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer9a[i] = EMPTY_VALUE;
         buffer9b[i] = EMPTY_VALUE;

         trend10[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*9,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI10a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*9,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer10a[i] = EMPTY_VALUE;
         buffer10b[i] = EMPTY_VALUE;

         trend11[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*10,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,4,y);
         ARSI11a[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,ARSIPeriod+ARSIstep*10,ARSIPrice,period,ma_method,alertsOn,alertsOnCurrent,alertsSound,alertsMessage,alertsNotification,alertsEmail,0,y);
         buffer11a[i] = EMPTY_VALUE;
         buffer11b[i] = EMPTY_VALUE;

   }
   for (i=limit;i>=0;i--) if (trend1[i]==-1) PlotPoint(i,buffer1a,buffer1b,ARSI1a);
   for (i=limit;i>=0;i--) if (trend2[i]==-1) PlotPoint(i,buffer2a,buffer2b,ARSI2a);
   for (i=limit;i>=0;i--) if (trend3[i]==-1) PlotPoint(i,buffer3a,buffer3b,ARSI3a);
   for (i=limit;i>=0;i--) if (trend4[i]==-1) PlotPoint(i,buffer4a,buffer4b,ARSI4a);
   for (i=limit;i>=0;i--) if (trend5[i]==-1) PlotPoint(i,buffer5a,buffer5b,ARSI5a);
   for (i=limit;i>=0;i--) if (trend6[i]==-1) PlotPoint(i,buffer6a,buffer6b,ARSI6a);
   for (i=limit;i>=0;i--) if (trend7[i]==-1) PlotPoint(i,buffer7a,buffer7b,ARSI7a);
   for (i=limit;i>=0;i--) if (trend8[i]==-1) PlotPoint(i,buffer8a,buffer8b,ARSI8a);
   for (i=limit;i>=0;i--) if (trend9[i]==-1) PlotPoint(i,buffer9a,buffer9b,ARSI9a);
   for (i=limit;i>=0;i--) if (trend10[i]==-1) PlotPoint(i,buffer10a,buffer10b,ARSI10a);
   for (i=limit;i>=0;i--) if (trend11[i]==-1) PlotPoint(i,buffer11a,buffer11b,ARSI11a);
return(0);
}
//+------------------------------------------------------------------------------------------------------------------+
void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      
      //
      //
      //
      //
      //
      
      static string   alertType1 = "";
      static datetime alertTime1 = 0;
      if (trend1[whichBar] != trend1[whichBar+1])
      {
         if (trend1[whichBar] ==  1) doAlert(alertType1,alertTime1,"rsi adaptive ema slope changed to up");
         if (trend1[whichBar] == -1) doAlert(alertType1,alertTime1,"rsi adaptive ema slope changed to down");
      }
   }
}
//+------------------------------------------------------------------------------------------------------------------+
void doAlert(string& previousAlert, datetime& previousTime, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message = timeFrameToString(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+"  "+doWhat;
          if (alertsMessage)      Alert(message);
          if (alertsEmail)        SendMail(StringConcatenate(Symbol(),"rsi adaptive ema"),message);
          if (alertsNotification) SendNotification(StringConcatenate(Symbol(),"rsi adaptive ema "+message));
          if (alertsSound)        PlaySound("alert2.wav");
   }
}
//+------------------------------------------------------------------------------------------------------------------+
void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}
//+------------------------------------------------------------------------------------------------------------------+
void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}
//+------------------------------------------------------------------------------------------------------------------+
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("");
}
//+------------------------------------------------------------------------------------------------------------------+
