//------------------------------------------------------------------
// original idea for this indicator from Godfreyh
// this version by mladen
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4

extern int    DrawBars      = 0;
extern int    RsiPeriod     = 14;
extern color  WickColor     = Gray;
extern color  BodyUpColor   = LimeGreen;
extern color  BodyDownColor = PaleVioletRed;
extern int    BodyWidth     = 4;
extern bool   DrawAsBack    = false;
extern string UniqueID      = "Rsi bars 1";

//
//
//
//
//

double open[];
double close[];
double high[];
double low[];
int    window;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,open);  SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,close); SetIndexStyle(1,DRAW_NONE);
   SetIndexBuffer(2,high);  SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(3,low);   SetIndexStyle(3,DRAW_NONE);
	IndicatorShortName(UniqueID); 
   return(0);
}

//
//
//
//
//

int deinit()
{
   string lookFor       = UniqueID+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int start()
{
   int countedBars = IndicatorCounted();
      if (countedBars<0) return(-1);
      if (countedBars>0) countedBars--;
         int drawBars = DrawBars; if (drawBars<1) drawBars = Bars;
         int limit = MathMin(MathMin(Bars-countedBars,Bars-1),drawBars);
            window = WindowFind(UniqueID);

   //
   //
   //
   //
   //
   
   for (int i=limit; i>=0; i--)
   {
      open[i]  = iRSI(NULL,0,RsiPeriod,PRICE_OPEN ,i);
      close[i] = iRSI(NULL,0,RsiPeriod,PRICE_CLOSE,i);
      high[i]  = iRSI(NULL,0,RsiPeriod,PRICE_HIGH ,i);
      low[i]   = iRSI(NULL,0,RsiPeriod,PRICE_LOW  ,i);
         drawCandle(i);
   }
   return(0);
}

  
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
  
void drawCandle(int i)
{
   datetime time = Time[i];
   string   name = UniqueID+":"+time+":";
   
      ObjectCreate(name,OBJ_TREND,window,0,0,0,0);
         ObjectSet(name,OBJPROP_COLOR,WickColor);
         ObjectSet(name,OBJPROP_TIME1,time);
         ObjectSet(name,OBJPROP_TIME2,time);
         ObjectSet(name,OBJPROP_PRICE1,MathMax(high[i],MathMin(close[i],open[i])));
         ObjectSet(name,OBJPROP_PRICE2,MathMin(low[i] ,MathMax(close[i],open[i])));
         ObjectSet(name,OBJPROP_RAY ,false);
         ObjectSet(name,OBJPROP_BACK,DrawAsBack);
      
   //
   //
   //
   //
   //
         
   name = name+"body";
      ObjectCreate(name,OBJ_TREND,window,0,0,0,0);
         ObjectSet(name,OBJPROP_TIME1,time);
         ObjectSet(name,OBJPROP_TIME2,time);
         ObjectSet(name,OBJPROP_PRICE1,open[i]);
         ObjectSet(name,OBJPROP_PRICE2,close[i]);
         ObjectSet(name,OBJPROP_WIDTH,BodyWidth);
         ObjectSet(name,OBJPROP_RAY  ,false);
         ObjectSet(name,OBJPROP_BACK,DrawAsBack);
         if (open[i]<close[i])
               ObjectSet(name,OBJPROP_COLOR,BodyUpColor);
         else  ObjectSet(name,OBJPROP_COLOR,BodyDownColor);
}