Wrong RSI on my EA project

 
Hello Everybody,

I try to create an EA based on RSI calculation. I am using the following fonction :

void CalculateRSIValues() {
    int totalBars = MathMin(BarsToCalculate, iBars(_Symbol, _Period));
    Print("BarsToCalculateMathMin:RSI ", BarsToCalculate);
    Print("Taille de GlobalRSIValues avant le remplissage: ", ArraySize(GlobalRSIValues)); 
    ArrayResize(GlobalRSIValues, totalBars);
    ArraySetAsSeries(GlobalRSIValues, true);

    for (int i = 0; i < totalBars; i++) {
        GlobalRSIValues[i] = iRSI(_Symbol, _Period, RSI_Period, PRICE_CLOSE, i);
        Print("Prix Close T[", i, "] = ", Close[i]); 

    // Ajout d'un log pour vérifier la valeur du RSI pour T[0]
    if (totalBars > 0) {
        Print("RSI pour T[0] calculé à ", GlobalRSIValues[0]);
        Print("Valeur RSI pour T[0]: ", iRSI(_Symbol, _Period, RSI_Period, PRICE_CLOSE, 0));
    }
}


The problem is the value for the RSI calculated regarding the bar T[0] is wrong.
The following T[1], ... are good.

2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[0] = 1.07767 (WRONG VALUE)
2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[1] = 1.07773 (GOOD VALUE)
2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[2] = 1.07782 (GOOD VALUE)
2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[3] = 1.07804 (GOOD VALUE)
2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[4] = 1.07804 (GOOD VALUE)
2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[5] = 1.07781 (GOOD VALUE)
2024.02.20 00:15:00  WAT EURUSD,M15: Prix Close T[6] = 1.07782 (GOOD VALUE)

2024.04.15 20:19:49.844 2024.02.20 00:15:00  WAT EURUSD,M15: RSI pour T[0] calculé à 48.89256312391394 (WRONG VALUE)

Follwing is the RSI calculated with Excel


DATE HEURE Open Price High Low Close Price Volume C (Gain) D (Loss) E (Avg Gain) F (Avg Loss) G (RS) H (RSI)

2024.02.19 22:45 1.07783 1.07799 1.07777 1.07782 158 0.00E+00 1.00E-05 7.15E-05 5.45E-05 1.311870754 56.7449868
2024.02.19 23:00 1.07784 1.07796 1.07776 1.07781 104 0.00E+00 1.00E-05 6.60E-05 5.11E-05 1.29210708 56.37202081
2024.02.19 23:15 1.07782 1.07806 1.07776 1.07804 154 2.30E-04 0.00E+00 7.86E-05 4.71E-05 1.667483142 62.51147817
2024.02.19 23:30 1.07804 1.0782 1.07802 1.07804 117 0.00E+00 0.00E+00 7.25E-05 4.35E-05 1.667483142 62.51147817
2024.02.19 23:45 1.07807 1.07815 1.07782 1.07782 164 0.00E+00 2.20E-04 6.70E-05 5.71E-05 1.173134426 53.98351856
2024.02.20 00:00 1.07785 1.07785 1.07687 1.07773 216 0.00E+00 9.00E-05 6.18E-05 5.96E-05 1.036898919 50.90576214
2024.02.20 00:15 1.07767 1.07788 1.0773 1.07777 254 4.00E-05 0.00E+00 6.01E-05 5.50E-05 1.09281312 52.21742493

After debugging, I found that the T[0] RSI is determined with the Open Price instead of the Close Price.

How can I correct this ?

Thank you for your help.

Alain

 
navtrack1959:
the value for the RSI calculated regarding the bar T[0] is wrong

How did you understand this? How did you check this?

 
navtrack1959:
After debugging, I found that the T[0] RSI is determined with the Open Price instead of the Close Price.

I just attached the RSI to the chart. The RSI value for the current bar changes when a new tick arrives. This proves that your statement, which I am quoting, is incorrect (if your statement were true, the RSI value for the current bar would not change, since PRICE_OPEN does not change with the arrival of a new tick).

 

I have checked. iRSI for the current bar returns the correct value

#property strict

void OnTick()
  {
   Comment("#0 = " + DoubleToString(iRSI(_Symbol, _Period, 14, PRICE_CLOSE, 0), 4));
  }

void OnDeinit(const int reason)
  {
   Comment("");
  }
 
Vladislav Boyko #:

I have checked. iRSI for the current bar returns the correct value

Thank you for your reply. I will check with the OnTick you suggest.
 

I still have the same problem. I have tried with two different MT4.

2024.02.20 00:00:00  WAT-TDI EURUSD,M15: Heure actuelle: 2024.02.20 00:00:00

2024.02.20 00:00:00  WAT-TDI EURUSD,M15: Prix de clôture actuel (T[0]): 1.07785 (Wrong Value)

2024.02.20 00:00:00  WAT-TDI EURUSD,M15: Prix de clôture précédent (T[1]): 1.07782


2024.02.20 00:15:00  WAT-TDI EURUSD,M15: Heure actuelle: 2024.02.20 00:15:00

2024.02.20 00:15:00  WAT-TDI EURUSD,M15: Prix de clôture actuel (T[0]): 1.07767 (Wrong Value)

2024.02.20 00:15:00  WAT-TDI EURUSD,M15: Prix de clôture précédent (T[1]): 1.07773

 

DATA

DATE / TIME / OPEN PRICE / CLOSE PRICE

2024.02.20 00:00 1.07785 / 1.07773

2024.02.20 00:15 1.07767 / 1.07777

 
Vladislav Boyko #:

How did you understand this? How did you check this?

The value of the RSI on the graph is correct. However the one on the journal shows a different number. 

Following is the OnTick I used to check the values :

#property strict

void OnTick() {
    Print("Current Time: ", TimeToStr(TimeCurrent(), TIME_DATE | TIME_SECONDS));
    Print("Cuurent Price Close (T[0]): ", Close[0]);
    Print("Previous Price Close (T[1]): ", Close[1]);
}

 
navtrack1959 #:

The value of the RSI is correct. However the one on the journal shows a different number. 

Following is the OnTick I used to check the values :

Why do you copy indicator values into arrays? In MT4 this is not necessary. For the current symbol and timeframe, calling iRSI again will not lead to the indicator being recalculated. Calling iRSI again will return a ready-made value

 
navtrack1959 #:
However the one on the journal shows a different number. 

How did you figure this out? Your code doesn't print the indicator value.

navtrack1959 #:
shows a different number

The journal shows a different value than what is shown where?

 

Following the OnTick. Assure declaring before datetime lastCandleTime = 0;

void OnTick() {
    datetime currentCandleTime = iTime(_Symbol, _Period, 0);
    datetime previousCandleTime = iTime(_Symbol, _Period, 1);

    Print("Current time: ", TimeToStr(TimeCurrent(), TIME_DATE | TIME_SECONDS));
    Print("Heure de la dernière bougie (T[0]): ", TimeToStr(currentCandleTime, TIME_DATE | TIME_SECONDS));
    Print("Heure de la bougie précédente (T[1]): ", TimeToStr(previousCandleTime, TIME_DATE | TIME_SECONDS));
    Print("Price Close of T[0]: ", Close[0]);
    Print("Price Close of T[1]: ", Close[1]);

    if (currentCandleTime != lastCandleTime) {
        lastCandleTime = currentCandleTime;

        Print("Procéder avec les calculs pour la nouvelle bougie.");
        CalculateRSIValues();
        Print("RSI Values Calculated");

        if (ArraySize(GlobalRSIValues) > 0) {
            Print("Tous les tableaux sont correctement remplis. Exécution de la logique de trading.");
            ExecuteTradingLogic(
                GlobalRSIValues[0],
            );
        } else {
            Print("Un ou plusieurs tableaux nécessaires sont vides. Aucune action de trading prise.");
        }
    }
}
 
navtrack1959 #:
Following the OnTick. Assure declaring before datetime lastCandleTime = 0;
navtrack1959:
After debugging, I found that the T[0] RSI is determined with the Open Price instead of the Close Price.

You only call iRSI when a new bar opens. At the moment the bar opens, its opening price is equal to its closing price (and also equal to the high and low). Do you understand what the problem is?

Reason: