RSI indicator code

To add comments, please log in or register
luckyvictor
179
luckyvictor  

Hi all


I have used the code below to replicate the RSI indicator, as a small learning exercise, but I get different result then the built in one that I apply on graph.


Why?


Below is the screenshot, grey is the MT4 built-in one, blue is mine, both are calculated using Price_close.


double RSI_14[];       //arrow buffer

int init()
  {
   //---- indicator
   
   SetIndexBuffer(0,RSI_14);
   SetIndexLabel(0,"RSI_14");
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);

   
//----
   return(0);
  }

int start() {
   int i,                           // Bar index
       n,                           // Formal parameter
       Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      RSI_14[i]=iRSI(Symbol(),0,14,PRICE_CLOSE,i);     
      i--;
   }
   return(0);
}
Seng Joo Thio
754
Seng Joo Thio  
luckyvictor:

I have used the code below to replicate the RSI indicator, as a small learning exercise, but I get different result then the built in one that I apply on graph. Why?

You need to provide more information, such as screenshots comparing the differences, and/or more complete codes with your data/array declarations, etc. 

luckyvictor
179
luckyvictor  
Seng Joo Thio:

You need to provide more information, such as screenshots comparing the differences, and/or more complete codes with your data/array declarations, etc. 

Hi, I have updated my question. Please have a look and correct any mistake I made.

Seng Joo Thio
754
Seng Joo Thio  
luckyvictor:

Hi, I have updated my question. Please have a look and correct any mistake I made.

Ok, looks like you'll just need to add these two lines (highlighted):

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
luckyvictor
179
luckyvictor  
Seng Joo Thio:

Ok, looks like you'll just need to add these two lines (highlighted):

luckyvictor
179
luckyvictor  
Why they would make the difference?
luckyvictor
179
luckyvictor  
Seng Joo Thio:

Ok, looks like you'll just need to add these two lines (highlighted):

it makes no difference.

Seng Joo Thio
754
Seng Joo Thio  
luckyvictor:

it makes no difference.

You can refer to the documents here: https://docs.mql4.com/basis/preprosessor/compilation

They should set the minimum and maximum prices of your indicator. If they don't make any difference, it's time to show your complete code so I can run it on my terminal to see.

Program Properties (#property) - Preprocessor - Language Basics - MQL4 Reference
Program Properties (#property) - Preprocessor - Language Basics - MQL4 Reference
  • docs.mql4.com
Every MQL4-program allows to specify additional specific parameters named #property that help client terminal in proper servicing for programs without the necessity to launch them explicitly. This concerns external settings of indicators, first of all. Properties described in included files are completely ignored. Properties must be specified...
luckyvictor
179
luckyvictor  

I think it is running ok now, after I have restarted my computer.......


but now, then say I want to apply RSI 3 on RSI 14, it does not show. RSI 3 is always zero.


RSI_14[i]=iRSI(Symbol(),0,14,PRICE_CLOSE,i); 
RSI_3[i]=iRSI(Symbol(),0,3,RSI_14[i],i);
Seng Joo Thio
754
Seng Joo Thio  
luckyvictor:

but now, then say I want to apply RSI 3 on RSI 14, it does not show. RSI 3 is always zero.

This is what you should use: https://docs.mql4.com/indicators/irsionarray

luckyvictor
179
luckyvictor  
Seng Joo Thio:
This is what you should use: https://docs.mql4.com/indicators/irsionarray

I have made the following changes and I still don't get a sensible graph, zero all time.


      RSI_14[i]=iRSI(Symbol(),0,14,PRICE_CLOSE,i); 
      RSI_5[i]=iRSIOnArray(RSI_14,14,5,i);     

in here, I use 14 number of element, average period of 5, I tried either i or 0 for shift, and dont get a graph.

What have I misunderstood?

12
To add comments, please log in or register