Wrong RSI on my EA project - page 2

 
Your code can be greatly simplified and made much more performant. In MT4 you don't need to create arrays and store rsi values in them. Just call iRSI every time you need its value - it will work much faster than your current code because calling iRSI does not lead to the indicator being recalculated, but simply returns the ready value.
 
Vladislav Boyko #:
calling iRSI does not lead to the indicator being recalculated, but simply returns the ready value

Until you call RefreshRates. Calling RefreshRates will lead to recalculation of the indicator, and the next iRSI call after RefreshRates may return a value different from the one it returned before RefreshRates.

And remember that my statement is only valid for the current symbol and the current timeframe.

 
Vladislav Boyko #:
Your code can be greatly simplified and made much more performant. In MT4 you don't need to create arrays and store rsi values in them. Just call iRSI every time you need its value - it will work much faster than your current code because calling iRSI does not lead to the indicator being recalculated, but simply returns the ready value.
In fact, the arrays are there because I expect to create an EA based on TDI and others indicators. I will change the others function and use iRSI. I look at that tomorow and return to you with the result. Thank you.
 
navtrack1959 #:
In fact, the arrays are there because I expect to create an EA based on TDI and others indicators.

In principle, copying values into an array may be advisable if your other code expects an array of RSI values.

navtrack1959 #:
I will change the others function and use iRSI. I look at that tomorow and return to you with the result. Thank you.

Avoiding copying values into arrays won't solve your current problem.

I've already described why your code doesn't work as you expect:

https://www.mql5.com/en/forum/465710#comment_53078444

 
Vladislav Boyko #:

In principle, copying values into an array may be advisable if your other code expects an array of RSI values.

Avoiding copying values into arrays won't solve your current problem.

I've already described why your code doesn't work as you expect:

https://www.mql5.com/en/forum/465710#comment_53078444

Hello, I have tried to use the refreshrate without any success. 

void OnTick() {
    #ifndef __MQL4_TEST__
    if (!RefreshRates()) {
        Print("Failed to refresh rates at ", TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS));
        return;  // Only return in live trading if rates can't be refreshed
    }
    #endif

    // Trading logic here
    double rsiValue = iRSI(_Symbol, _Period, 13, PRICE_CLOSE, 0);
    Print("RSI at ", TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS), ": ", rsiValue);

    // More trading logic can follow
}

I have the following message in the journal when I test it on the strategy tester :  2024.04.19 16:23:33.129 2024.02.20 00:15:00 WAT EURUSD,M15: Failed to refresh rates at 2024.02.20 00:15:00 2024.04.19 16:23:33.223 2024.02.20 00:30:00 WAT EURUSD,M15: Failed to refresh rates at 2024.02.20 00:30:00 ... and so on. 

Find also the preceding function used : 

void OnTick() {
    // Check for a new bar
    static datetime lastBarTime = 0;
    datetime currentBarTime = iTime(_Symbol, _Period, 0);

    if (currentBarTime != lastBarTime) {
        lastBarTime = currentBarTime;
       
        if (RefreshRates()) {
            // Calculate RSI after ensuring data is fresh
            double rsiValue = iRSI(_Symbol, _Period, 13, PRICE_CLOSE, 0);
            Print("RSI after data refresh: ", rsiValue);

        } else {
            Print("Failed to refresh rates at ", TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS));
        }
    }
}
Note that I am new as a programmer
 
navtrack1959 #:

Let's go in order. I highlighted the line from your original code that caused you to not update the RSI value on the current bar.

Forum on trading, automated trading systems and testing trading strategies

Wrong RSI on my EA project

navtrack1959, 2024.04.16 20:56

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.");
        }
    }
}
Did you understand why you were getting values calculated only from the opening price?