Having issues getting my code to work properly.

 
I'm an amateur coder. I'm having issues getting my indicator to work properly. The following is a RSI based momentum indicator that paints candlesticks based on RSI level + Inside Bar and Pin Bar patterns.

The indicator looks like it works. However, the [0] or [i] bar repaints to normal candle color when working out side of range of the compiled bar's range. And every bar after that doesn't paint at all or it's distorted. I've looked over my code and can't find the issue. Here is the code.
//+------------------------------------------------------------------+
//|                            Momentum Inside Bars and Pin Bars.mq5 |
//|                                          Copyright 2019, Jiemasu |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                    RSI CANDLES WITH PIN BARS.mq5 |
//|                                          Copyright 2019, Jiemasu |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                 RSI CANDLES WITH INSIDE BARS.mq5 |
//|                                          Copyright 2019, Jiemasu |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Jiemasu"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   1
//--- plot Colors
#property indicator_label1  "Colors"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrLime,clrRed,clrBlue,clrLightGray
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         buffer_open[],buffer_high[],buffer_low[],buffer_close[];
double         Candle_Colors[];
double         buffer_tmp[1];
double         buffer_RSI[];
int            handle_RSI=0;

input int      RSI_Period=50;
input int      RSILvlU=55;
input int      RSILvlD=45;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,buffer_open,INDICATOR_DATA);
   SetIndexBuffer(1,buffer_high,INDICATOR_DATA);
   SetIndexBuffer(2,buffer_low,INDICATOR_DATA);
   SetIndexBuffer(3,buffer_close,INDICATOR_DATA);
   
   SetIndexBuffer(4,Candle_Colors,INDICATOR_COLOR_INDEX);
   
   SetIndexBuffer(5,buffer_RSI,INDICATOR_CALCULATIONS);
   
   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,5);
   
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,Lime);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,Blue);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,Red);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,3,Cyan);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,4,LightGray);
   
   handle_RSI=iCustom(_Symbol,_Period,"Examples\\RSI",RSI_Period);
//---
   return(0);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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 limit;
if (prev_calculated == 0)
       limit = prev_calculated + handle_RSI + 1;
      else
       limit = prev_calculated - 1;
   
   for(int i=limit;i<=rates_total;i++)
     {
         CopyBuffer(handle_RSI,0,BarsCalculated(handle_RSI)-i-1,1,buffer_tmp);
       
       
         buffer_RSI[i]=buffer_tmp[0];
         
         buffer_open[i]=open[i];
         buffer_high[i]=high[i];
         buffer_low[i]=low[i];
         buffer_close[i]=close[i];
         
         double      Up_Period=.70;
         double      Dn_Period=.30;

         double upbar = (high[i] - low[i]) * Up_Period + low[i];
         double dnbar = (high[i] - low[i]) * Dn_Period + low[i];
         
         if (buffer_RSI[i]>=RSILvlU && low[i-1] < low[i] && low[i-1] < close[i] && low[i-1] < open[i] && high[i-1] > high[i] && high[i-1] > close[i] && high[i-1] > open[i] && low[i-1] < low[i] && high[i-1] > high[i])
               {
                  Candle_Colors[i] = 4;
               }
            else if (buffer_RSI[i] <= RSILvlD && low[i-1] < low[i] && low[i-1] < close[i] && low[i-1] < open[i] && high[i-1] > high[i] && high[i-1] > close[i] && high[i-1] > open[i] && low[i-1] < low[i] && high[i-1] > high[i])
               {
                  Candle_Colors[i] = 4;
               }
               
            else if (buffer_RSI[i] >= RSILvlU && close[i] >= upbar && open[i] >= upbar && high[i] > open[i] && high[i] > close[i])
               {
                  Candle_Colors[i] = 3;
               }
            else if (buffer_RSI[i] <= RSILvlD && close[i] <= dnbar && open[i] <= dnbar && low[i] < open[i] && low[i] < close[i])
               {
                  Candle_Colors[i] = 3;
               }
         
         else if(buffer_RSI[i]>=RSILvlU)
         {
            Candle_Colors[i] = 0;
         }
         else if(buffer_RSI[i] <= RSILvlD)
            {
               Candle_Colors[i] = 2;
            }
        /*  else if (buffer_RSI[i]>=RSILvlU && low[i-1] < low[1] && low[i-1] < close[i] && low[i-1] < open[i] && high[i-1] > high[i] && high[i-1] > close[i] && low[i-1] < low[i] && high[i-1] > high[i])
               {
                  Candle_Colors[i]=3;
               }
            else if (buffer_RSI[i]<=RSILvlD && low[i-1] < low[1] && low[i-1] < close[i] && low[i-1] < open[i] && high[i-1] > high[i] && high[i-1] > close[i] && low[i-1] < low[i] && high[i-1] > high[i])
               {
                  Candle_Colors[i]=3;
               }*/
           
            else if (close[i] < RSILvlU && open[i] < RSILvlU && high[i] < RSILvlU && low[i] < RSILvlU)
            {
               Candle_Colors[i]=1;
            }
            
            else if (close[i] > RSILvlD && open[i] > RSILvlD && high[i] > RSILvlD && low[i] > RSILvlD)
            {
               Candle_Colors[i]=1;
            }
      }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


 
tradingmarkets4pips:
I'm an amateur coder. I'm having issues getting my indicator to work properly. The following is a RSI based momentum indicator that paints candlesticks based on RSI level + Inside Bar and Pin Bar patterns.

The indicator looks like it works. However, the [0] or [i] bar repaints to normal candle color when working out side of range of the compiled bar's range. And every bar after that doesn't paint at all or it's distorted. I've looked over my code and can't find the issue. Here is the code.

Try these changes:

if (prev_calculated == 0)
       limit = 1;prev_calculated + handle_RSI + 1;
      else
       limit = prev_calculated - 1;
   
   for(int i=limit;i<=rates_total;i++)

Reason: