[Help] Custom RSI is Missing Some Portion of Indicator Lines

 

[Help] Custom RSI is Missing Some Portion of Indicator Lines in 4-Hour timeframe (GBPUSD).

I used SMA to get average gain/lose instead of Wilder's SMMA the default mode in MT4/MT5. If I used SMMA then everything works as expected but If I use SMA then some portion of lines is missing.

I can't figure out what cause of this problem. I uploaded the image and code.

Custom RSI Missing Indicator Lines

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 clrDodgerBlue

extern int inp_period = 10;  

double RSI_buffer[];
double pos_buffer[];
double neg_buffer[];

int init()
{
   string short_name;
   
   IndicatorBuffers(3);
   
   SetIndexBuffer(0, RSI_buffer);
   SetIndexBuffer(1, pos_buffer);
   SetIndexBuffer(2, neg_buffer);
   
   //--- set levels   
   IndicatorSetInteger(INDICATOR_LEVELS,2);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,30);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,70);
   //--- set maximum and minimum for subwindow 
   IndicatorSetDouble(INDICATOR_MINIMUM,0);
   IndicatorSetDouble(INDICATOR_MAXIMUM,100);

   //--- name for DataWindow and indicator subwindow label
   short_name = "My RSI("+string(inp_period)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0, short_name);
   
   
   return 0;
}

int start()
{
   int limit;
   int counted_bars=IndicatorCounted();
   
  //---- check for possible errors
   if(counted_bars<0) return(-1);
   
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   limit=Bars-counted_bars;
   
   for(int i=0; i<limit; i++)
   {    
      double now_close = iClose(Symbol(), Period(), i);
      double prev_close = iClose(Symbol(), Period(), i+1);
      
      double ma = (now_close - prev_close);
      
      if(ma > 0)
      {
         pos_buffer[i] = ma;
         neg_buffer[i] = 0.0;
      }
      else
      {
         neg_buffer[i] = MathAbs(ma);
         pos_buffer[i] = 0.0;
      }
   }
   
   for(int x=0; x<limit; x++)   
   {
      double avg_gain = iMAOnArray(pos_buffer, 0, inp_period, 0, MODE_SMA, x);
      double avg_lose = iMAOnArray(neg_buffer, 0, inp_period, 0, MODE_SMA, x);
      if(avg_lose != 0)
      {
         double rs = avg_gain/avg_lose;
         RSI_buffer[x] = 100.0-(100.0/(1+rs));
      }
   }

 return 0;
}
 

Bug in build 1090.
          Indicator compiles but won't show ?? - Average Directional Movement Index, ADX - Technical Indicators - MQL5 programming forum

Give the buffer(s) a type:

#property   indicator_type1   DRAW_LINE                                  // Bug 1090 must set.
:

or

SetIndexBuffer(0, RSI_buffer);
SetIndexStyle(0, DRAW_LINE);
:
 
I tried that but not working.
 
Help me pls.
 
Musngi:
I tried that but not working.

In your case it is not related to the drawing style bug of build 1090 (missing a few values can never be caused by that bug) but to your code

In this part :

if(avg_lose != 0)
      {
         double rs = avg_gain/avg_lose;
         RSI_buffer[x] = 100.0-(100.0/(1+rs));
      }

you do not have a case when avg_lose==0 (and then you have empty value of your drawing buffer). Fix that and it will work


PS: in your place I would invert the loop direction. It is not changing the result of your current code, but it is always better to calculate from past to future, not the opposite way - some of the best know repainters are made the way you are using that kind of loops and I doubt that you will be happy in cases when your code starts to repaint because of the wrong loop direction
 
Mladen Rakic:

In your case it is not related to the drawing style bug of build 1090 (missing a few values can never be caused by that bug) but to your code

In this part :

you do not have a case when avg_lose==0 (and then you have empty value of your drawing buffer). Fix that and it will work


PS: in your place I would invert the loop direction. It is not changing the result of your current code, but it is always better to calculate from past to future, not the opposite way - some of the best know repainters are made the way you are using that kind of loops and I doubt that you will be happy in cases when your code starts to repaint because of the wrong loop direction
Can you elaborate more? 

If I didn't use "avg_lose != 0", there's an error: division zero.. I always do that to my other indicator when there's a division. Maybe this is a bug?

I want to know if starting loop from past data to present is natural way to code the indicator?? Then which are default in MT4/MT5? Past to Present?


 
Musngi:
Can you elaborate more? 

If I didn't use "avg_lose != 0", there's an error: division zero.. I always do that to my other indicator when there's a division. Maybe this is a bug?

I want to know if starting loop from past data to present is natural way to code the indicator?? Then which are default in MT4/MT5? Past to Present?


What value does the RSI_buffer[nnn] have when avg_lose == 0?

You did not assign any value to RSI_buffer for that case and the default assigned by metatrader in that case is EMPTY_VALUE - which is the "missing" value in your example. Assign some controlled value to RSI_buffer when avg_lose == 0 and it will be OK


As of loops : according to you, what is the natural way? From future to past? If you think so, would you mind explaining why would that be a natural way (in any trading platform and in any coding language)
 
Mladen Rakic:

What value does the RSI_buffer[nnn] have when avg_lose == 0?

You did not assign any value to RSI_buffer for that case and the default assigned by metatrader in that case is EMPTY_VALUE - which is the "missing" value in your example. Assign some controlled value to RSI_buffer when avg_lose == 0 and it will be OK


As of loops : according to you, what is the natural way? From future to past? If you think so, would you mind explaining why would that be a natural way (in any trading platform and in any coding language)
I will try it later.

I don't know but I see some example code here that start from "0" (present data) not past data, posted by some member here. I read somewhere in MQL4 Documentation that built-in function start from zero or current data. It's still confused me.
 
 
vnathwani:
Still not work. I think thats buffer type is not the cause of this missing line value.
Reason: