Implement StochasticRSI

 
Hello MQL4 Community,

I hope everyone is doing well. I am currently working on an automated trading strategy and need some assistance in modifying my existing MQL4 code. Specifically, I would like to create a Stochastic RSI indicator similar to the one available on TradingView.

Here's a brief overview of my existing code and what I'm trying to achieve:
double StochRSIValue = 0.0;

// Validate input parameters
if (StochRSI_Period >= 2 && StochRSI_KPeriod >= 1 && StochRSI_DPeriod >= 1 && StochRSI_Slowing >= 1)
{
    // Calculate RSI value
    double rsiValue = iRSI(Symbol(), 0, StochRSI_Period, PRICE_CLOSE, 0);

    // Calculate Stochastic Oscillator for RSI
    double stochRSI = 0.0;

    for (int i = StochRSI_Period - 1; i >= 0; i--)
    {
        double rsiHigh = iHighest(NULL, 0, MODE_MAIN, StochRSI_Period, i);
        double rsiLow = iLowest(NULL, 0, MODE_MAIN, StochRSI_Period, i);

        if (rsiHigh != rsiLow)
        {
            stochRSI = (rsiValue - rsiLow) / (rsiHigh - rsiLow) * 100;
            break;
        }
    }

    // Calculate StochRSI
    StochRSIValue = stochRSI;
}
else
{
    Print("Invalid input parameters for StochRSI calculation.");
}
Here is the TradingView Stochastic RSI indicator I would like to replicate:

- The indicator should have two lines (K and D) displayed on the chart.
- The K and D lines should be color-coded similarly to the TradingView example (#2962FF for K and #FF6D00 for D).
- There should be three horizontal lines at levels 80 (Upper Band), 50 (Middle Band), and 20 (Lower Band), as shown in the TradingView script.
//@version=5
indicator(title="Stochastic RSI", shorttitle="Stoch RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
plot(k, "K", color=#2962FF)
plot(d, "D", color=#FF6D00)
h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")

My existing MQL4 code relies on the following external parameters, which users can customize according to their preferences:

StochRSI_Period = 14
StochRSI_KPeriod = 3
StochRSI_DPeriod = 3
StochRSI_Slowing = 3
StochRSI_OverboughtLevel = 80
StochRSI_OversoldLevel = 20

I highly appreciate the assistance of the MQL4 community in helping to modify my existing code to create the Stochastic RSI indicator as described above. Any guidance, code examples, or insights would be greatly appreciated. If you need further details or code snippets, please let me know.

for now all i see in strategy tester in mt4 is the rsi indicator. The second image is what i'm trying to achieve.


Thank you in advance for your help!

Regards,

Benjamin


 
Stoch RSI MT4
Stoch RSI MT4
  • www.mql5.com
The Stochastic RSI indicator (Stoch RSI) is essentially an indicator of an indicator. It is used in technical analysis to provide a stochastic calculation to the RSI indicator. This means that it is a measure of RSI relative to its own high/low range over a user defined period of time. The Stochastic RSI is an oscillator that calculates a value between 0 and 1 which is then plotted as a line. This indicator is primarily used for identifying overbought and oversold conditions.
 

Hei Yashar Seyyedin,

Thats true, I have seen it. the thing is that I'm trying to get it to work from within my expert advisor and not as a file include inside the ea.

 
One thing to mention is iHighest and iLowest return the index and can only be applied to OHLC prices not indicators.
Please read the documentation. You have to implement the highest and lowest values of rsi yourself.
 
Yashar Seyyedin #:
One thing to mention is iHighest and iLowest return the index and can only be applied to OHLC prices not indicators.
Please read the documentation. You have to implement the highest and lowest values of rsi yourself.

Hei Yashar Seyyedin,

I have updated my code:

double StochRSIValue = 0.0;

// Validate input parameters
if (StochRSI_Period >= 2 && StochRSI_KPeriod >= 1 && StochRSI_DPeriod >= 1 && StochRSI_Slowing >= 1)
{
    // Calculate RSI value
    double rsiValue = iRSI(Symbol(), 0, StochRSI_Period, PRICE_CLOSE, 0);

    // Calculate highest and lowest RSI values
    double highestRSI = -DBL_MAX;
    double lowestRSI = DBL_MAX;

    for (int i = StochRSI_Period - 1; i >= 0; i--)
    {
        double currentRSI = iRSI(Symbol(), 0, StochRSI_Period, PRICE_CLOSE, i);

        if (currentRSI > highestRSI)
            highestRSI = currentRSI;

        if (currentRSI < lowestRSI)
            lowestRSI = currentRSI;
    }

    // Calculate Stochastic Oscillator for RSI
    double stochRSI = 0.0;

    if (highestRSI != lowestRSI)
    {
        stochRSI = (rsiValue - lowestRSI) / (highestRSI - lowestRSI) * 100;
    }

    // Calculate StochRSI
    StochRSIValue = stochRSI;
}
else
{
    Print("Invalid input parameters for StochRSI calculation.");
}

I now calculate the highest and lowest RSI values within the specified StochRSI_Period and then use these values to calculate the Stochastic RSI.

 
OTMT Howard #:

Hei Yashar Seyyedin,

I have updated my code:

I now calculate the highest and lowest RSI values within the specified StochRSI_Period and then use these values to calculate the Stochastic RSI.

stochRSI value is not the end of story. You should calculate %K and %D to complete the task.

Something close to this:

kArray[i]=iMAOnArray(stochRSIArray, Bars, smoothK, 0, MODE_SMA, i);
dArray[i]=iMAOnArray(kArray, Bars, smoothD, 0, MODE_SMA, i);
 
Yashar Seyyedin #:

stochRSI value is not the end of story. You should calculate %K and %D to complete the task.

Something close to this:

Hei Yashar,


is this whatt you had in mind.

double StochRSIValue = 0.0;

// Validate input parameters
if (StochRSI_Period >= 2 && StochRSI_KPeriod >= 1 && StochRSI_DPeriod >= 1 && StochRSI_Slowing >= 1)
{
    // Calculate RSI value
    double rsiValue = iRSI(Symbol(), 0, StochRSI_Period, PRICE_CLOSE, 0);

    // Calculate highest and lowest RSI values
    double highestRSI = -DBL_MAX;
    double lowestRSI = DBL_MAX;

    for (int i = StochRSI_Period - 1; i >= 0; i--)
    {
        double currentRSI = iRSI(Symbol(), 0, StochRSI_Period, PRICE_CLOSE, i);

        if (currentRSI > highestRSI)
            highestRSI = currentRSI;

        if (currentRSI < lowestRSI)
            lowestRSI = currentRSI;
    }

    // Calculate Stochastic Oscillator for RSI
    double stochRSI = 0.0;

    if (highestRSI != lowestRSI)
    {
        stochRSI = (rsiValue - lowestRSI) / (highestRSI - lowestRSI) * 100;
    }

    // Assign the value to StochRSIValue
    StochRSIValue = stochRSI;

    // Calculate StochRSI %D (using StochRSI_DPeriod)
    int periodForD = StochRSI_DPeriod;
    double sumForD = 0.0;

    for (int i = 0; i < periodForD; i++) {
        sumForD += StochRSIValue;
    }

    double StochRSID = sumForD / periodForD;

    // Now, I have both %K (StochRSIValue) and %D (StochRSID) values for StochRSI
}
else
{
    Print("Invalid input parameters for StochRSI calculation.");
}

I am baffled to the method you suggested earlier. I'd be happy to hear what method you have..

 

Are you using chatgpt? 

You need an array to memorize previous values of stochRSI to calculate the %K.

Also You need an array to memorize previous values of %K to calculate the %D.
 
Yashar Seyyedin #:

Are you using chatgpt? 

You need an array to memorize previous values of stochRSI to calculate the %K.

Also You need an array to memorize previous values of %K to calculate the %D.

For the implemetation of getting ideas then changing it to my design and state yes that is it

the other thing is when i add arrays the expert is throwing errors 

That is if you have any other way to have them stored there without it breaking and throwing errors

 
OTMT Howard #: For the implemetation of getting ideas then changing it to my design and state yes that is it

If you are using ChatGPT and then coming here to request help with it, then that is not acceptable.

It produces horrible code, mixing MQL4 and MQL5, which cannot be easily fixed by newbie coders.

Either you know how to fix the code yourself, or please stop using ChatGPT.

No help will be provided for ChatGPT generated code on this forum.

 
Fernando Carreiro #:

If you are using ChatGPT and then coming here to request help with it, then that is not acceptable.

It produces horrible code, mixing MQL4 and MQL5, which cannot be easily fixed by newbie coders.

Either you know how to fix the code yourself, or please stop using ChatGPT.

No help will be provided for ChatGPT generated code on this forum.

I see. No Harm intended though. good to know this detail.