[Help] I created Custom RSI indicator manually but it's don't work. How to calculate Average Gains/Loses??

 

[Help] I created Custom RSI indicator manually but it's don't work. How to calculate Average Gains/Loses??

https://en.wikipedia.org/wiki/Relative_strength_index#Calculation

I'm using MQL4. There's no built-in function for Average Gains/Loses. If you know how to code that average gains/loses, please correct my code or change it completely.

double avg_gain, avg_lose, avg, gain, lose, res;
double count = 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;
     
  //---- main loop
   for(int i=0; i<limit; i++)    
   {
      double now_close = iClose(Symbol(), Period(), i);
      double prev_close = iClose(Symbol(), Period(), i+1);
      
      double RF = (now_close - prev_close);
      count += 1;
      if(count == 9) //Period(9)
      {
         avg_gain = gain/9;
         avg_lose = lose/9;

         avg = avg_gain/avg_lose;

         res = 100-(100/(1+avg));
         
         USD_Buffer[i] = res;
         
         count = 0.0;
         gain = 0.0;
         lose = 0.0;
      }
      else
      {
         if(RF > 0.0)
         {
            gain += RF;
         }
         else
         {
            gain += 0;
         }

         if(RF < 0.0)
         {
            lose += MathAbs(RF);
         }
         else
         {
            lose += 0;
         }
        
      }
   }
}


 
It's still no comment from expert?
 
  1. avg_gain = gain/9;
    What you have is utter babel, starting with the undefined variable gain.

  2. Musngi: I created Custom RSI indicator manually but it's don't work. How to calculate Average Gains/Loses??
    Why don't you look at the real RSI code and find out.
 
whroeder1:
  1. What you have is utter babel, starting with the undefined variable gain.

  2. Why don't you look at the real RSI code and find out.
1. I declared the variable "gain" as global variable.

2. That's ugly and unreadable codebases. I don't want to read that shitty C++ style code.. Are you developer of that ugly code?

3. I think it's impossible to code that Average Gain/Lose in MQL4. What a horrible language can't program in straightforward code.
 
Musngi:
1. I declared the variable "gain" as global variable.

2. That's ugly and unreadable codebases. I don't want to read that shitty C++ style code.. Are you developer of that ugly code?

3. I think it's impossible to code that Average Gain/Lose in MQL4. What a horrible language can't program in straightforward code.
  1. So what is it's value? As previously answered, "undefined."

  2. What code? Go to the <data folder>\MQL4\indicators\RSI.mq4 and look at it. No OOP code at all.

  3. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  4. "ugly code" "horrible" " setting for extern?" "I don't understand" "You are damn arrogant" "Could you elaborate more?" I had you listed a year ago as a troll with your vague questions and absurd responses. Apparently you haven't changed. So I will mark you again, and ignore you.
 
whroeder1:
  1. So what is it's value? As previously answered, "undefined."

  2. What code? Go to the <data folder>\MQL4\indicators\RSI.mq4 and look at it. No OOP code at all.

  3. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  4. "ugly code" "horrible" " setting for extern?" "I don't understand" "You are damn arrogant" "Could you elaborate more?" I had you listed a year ago as a troll with your vague questions and absurd responses. Apparently you haven't changed. So I will mark you again, and ignore you.
1. Why it became undefined? I don't understand.
2. Yes, No OOP but I can't understand the flow of the code. It so bizarre, I can't deciphered the code.
3. Because your MQL site keep redirecting me in new version MQL5 forums. You should blamed the web developer.
4. There's nothing wrong when you talk straightforward manner with little hard talk and teasing. LoL.
5. Please show to us your great wisdom on how to create average gain/lose in mql4. Please make it the Code Elegant and Easy to Understand.
 

It's still no answer from expert?

 
Musngi:

It's still no answer from expert?

Please stop doing that (to be clear updating your post to get your topic at the top of the section).

Everyone saw your topic and everyone decided to not answer for some reason.

 
Alain Verleyen:

Please stop doing that (to be clear updating your post to get your topic at the top of the section).

Everyone saw your topic and everyone decided to not answer for some reason.

Don't worry I discovered the answer but I have two problem: first, I don't know if my algorithm is correct and second, there's an additional two numbers in the upper left corner of indicator subwindow.

Its works even I didn't use SetIndexStyle(). It confused me.

img_custom_rsi

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 clrPink

extern int inp_period = 9;  

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 = "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_SMMA, x);
      double avg_lose = iMAOnArray(neg_buffer, 0, inp_period, 0, MODE_SMMA, x);
      if(avg_lose != 0)
      {
         double rs = avg_gain/avg_lose;
         RSI_buffer[x] = 100.0-(100.0/(1+rs));
      }
   }

 return 0;
}
Reason: