Synchronize RSI value with EA

 

Hi, newbie here. I'm trying to synchronize indicator values (for example: RSI) with EA.

Here's the code

#property strict
#include <Trade\Trade.mqh>

CTrade trade;

input int RSIPeriod = 9;
int rsiHandle;

int OnInit()
{
    rsiHandle = iRSI(_Symbol, PERIOD_H1, RSIPeriod, PRICE_CLOSE);
    return INIT_SUCCEEDED;
}

void OnTick()
{
    double rsiArray[];
    if (CopyBuffer(rsiHandle, 0, 0, 1, rsiArray) <= 0) return;
    double rsi = NormalizeDouble(rsiArray[0], 1);

    Print(" RSI: ", rsi);
}

When I tested it with the Strategy Tester, the RSI value on the Print shows differently from what is on the Data Window.

It prints (the last one is at the bottom):

2025.03.01 22:30:26.751 2025.02.27 23:58:45    RSI: 40.4
2025.03.01 22:30:26.751 2025.02.27 23:58:46    RSI: 40.4
2025.03.01 22:30:26.751 2025.02.27 23:58:48    RSI: 40.4
2025.03.01 22:30:26.751 2025.02.27 23:58:50    RSI: 40.4
2025.03.01 22:30:26.751 2025.02.27 23:58:52    RSI: 40.5
2025.03.01 22:30:26.751 2025.02.27 23:58:54    RSI: 40.5
2025.03.01 22:30:26.751 2025.02.27 23:58:59    RSI: 40.5

On the Data Window accordingly:

27.71
35.83
43.52
48.4
40.49
36.06
40.50

What is the reason of this mistake and maybe anyone can fix this. It would be great if anyone can help. Thank you!

 
Aurex Lions:

Hi, newbie here. I'm trying to synchronize indicator values (for example: RSI) with EA.

Here's the code

When I tested it with the Strategy Tester, the RSI value on the Print shows differently from what is on the Data Window.

It prints (the last one is at the bottom):

On the Data Window accordingly:

What is the reason of this mistake and maybe anyone can fix this. It would be great if anyone can help. Thank you!

Try the following : 

input int RSIPeriod = 9;
int rsiHandle;
double prevRsi=0.0;
int OnInit()
  {
  prevRsi=0.0;
  rsiHandle = iRSI(_Symbol, PERIOD_H1, RSIPeriod, PRICE_CLOSE);
  return(INIT_SUCCEEDED);
  }
void OnTick()
{
    double rsiArray[];
    if (CopyBuffer(rsiHandle, 0, 0, 2, rsiArray) <= 0) return;
    double rsi = NormalizeDouble(rsiArray[1], 2);
    if(rsi!=prevRsi){
    Print("RSI "+rsi);
    prevRsi=rsi;
    }
}

 
Lorentzos Roussos #:

Try the following : 

Thank you for your response. I'm afraid the code you gave haven't correctly shows the values yet. It shows another different series of the RSI values.
 
Aurex Lions #:
Thank you for your response. I'm afraid the code you gave haven't correctly shows the values yet. It shows another different series of the RSI values.

Hmm can you post a screenshot of how you make the comparison ?

 
Lorentzos Roussos #:

Hmm can you post a screenshot of how you make the comparison ?

It just solved. It turns out that the values on:
Print("RSI "+rsi);
are printed on every tick. The RSI values on the data value made after a bar closed. 
So to get exact same values, the Print() needs to be in a function that detect a closing bar.