Apply the RSI on a custom calculation rather than on a symbol

 

Hi,

I try to calculate an indicator using RSI (and then use it in an EA). But I don't want to apply the RSI to a symbol, I need to apply the RSI to a calculation I'm doing between different instruments. For the example, I am calculating a ratio between 2 symbols: DowJones and Nasdaq.

ratio = DJ/NQ

And then I want to calculate the RSI on this ratio: indicator = RSI( ratio, 14)

But how can I do that? Because in the RSI you can only put a symbol...

Thanks in advance for your help :)

 
Copy the RSI.mq4 to a new file, edit the file, replace the close[] with your ratio.
 

iRSIOnArray

Calculates the Relative Strength Index indicator on data, stored in array, and returns its value.

double  iRSIOnArray(
   double       array[],          // array with data
   int          total,            // number of elements
   int          period,           // averaging period
   int          shift             // shift
   );

Parameters

array[]

[in]  Array with data.

total

[in]  The number of items to be counted. 0 means the whole array.

period

[in]  Averaging period for calculation.

shift

[in]  Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).

Returned value

Numerical value of the Relative Strength Index indicator, calculated on data, stored in array[].

Note

Unlike iRSI(...), the iRSIOnArray() does not take data by symbol name, timeframe, the applied price. The price data must be previously prepared. The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries() function.

Example:

  if(iRSIOnArray(ExtBuffer,1000,14,0)>iRSI(NULL,0,14,PRICE_CLOSE,1)) return(0);

 


.
 
Thank you! I didn't see this iRSIOnArray function ^^'
 

@Keith Watford

I tried but i have a problem to display the right data. I'll put my code here and explain:

With the code below, I can correctly display the "BufferRatio" indicator (as shown in the screenshot below):

"Capture 1"


#property copyright "jiyu"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window

#property indicator_buffers 1

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3

double   BufferRSI[];
double   BufferRatio[];

extern string sym1   = "NDX100";
extern string sym2   = "SPX500";


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   IndicatorBuffers(2);
   
   SetIndexStyle(0,DRAW_LINE);
   
   SetIndexBuffer(1, BufferRSI);
   SetIndexBuffer(0, BufferRatio);

   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    counted_bars=IndicatorCounted();

   for(int i=0;i<counted_bars;i++)
   {
      BufferRatio[i]  = iClose(sym1, 0, i) / iClose(sym2, 0, i);
      BufferRSI[i]   = iRSIOnArray(BufferRatio, 50, 14, i);
   }
   
   return(rates_total);
  }

But i want to display the indicator "BufferRSI", so i tried to switch the "SetIndexBuffer" like this:

   SetIndexBuffer(0, BufferRSI);
   SetIndexBuffer(1, BufferRatio);

Result: "Capture 2" 
https://ibb.co/NCsxp2q

My indicator stay to zero... I don't know how to display "BufferRSI"?? I think that my calculation "iRSIOnArray" is ok (but i'm not sure...)

HELP :'(

Capture1
Capture1
  • ibb.co
Image Capture1 hébergée dans ImgBB
 
Completely fill the first buffer BEFORE trying to fill the  RSI OnArray. (2 loops)
 

Thanks! The values are calculated correctly in the debug mode :)

Reason: