1. Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)
    Dismiss Notice

Divergent RSI code mql4

Discussion in 'Belajar MQL dari Nol' started by rickyandreas290292, 24 Jun 2023.

  1. rickyandreas290292

    rickyandreas290292 New Member

    Equity
    Credit
    Ref Point
    // Input parameters
    input int period = 14; // RSI period
    input double deviation = 0.0001; // Deviation threshold for divergence detection

    // Function to check for bullish divergence
    bool isBullishDivergence(int bar)
    {
    double rsiValue = iRSI(NULL, 0, period, PRICE_CLOSE, bar);
    double priceValue = Close[bar];
    double rsiPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 1);
    double pricePrev = Close[bar + 1];
    double rsiPrevPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 2);
    double pricePrevPrev = Close[bar + 2];

    if (rsiValue > rsiPrev && rsiPrev < rsiPrevPrev && priceValue > pricePrev && pricePrev < pricePrevPrev)
    {
    double rsiDeviation = MathAbs(rsiValue - rsiPrev);
    double priceDeviation = MathAbs(priceValue - pricePrev);
    if (rsiDeviation > deviation && priceDeviation < deviation)
    {
    return true;
    }
    }

    return false;
    }

    // Function to check for bearish divergence
    bool isBearishDivergence(int bar)
    {
    double rsiValue = iRSI(NULL, 0, period, PRICE_CLOSE, bar);
    double priceValue = Close[bar];
    double rsiPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 1);
    double pricePrev = Close[bar + 1];
    double rsiPrevPrev = iRSI(NULL, 0, period, PRICE_CLOSE, bar + 2);
    double pricePrevPrev = Close[bar + 2];

    if (rsiValue < rsiPrev && rsiPrev > rsiPrevPrev && priceValue < pricePrev && pricePrev > pricePrevPrev)
    {
    double rsiDeviation = MathAbs(rsiValue - rsiPrev);
    double priceDeviation = MathAbs(priceValue - pricePrev);
    if (rsiDeviation > deviation && priceDeviation < deviation)
    {
    return true;
    }
    }

    return false;
    }

    // OnTick function
    void OnTick()
    {
    int startBar = 2; // Start bar index for divergence checking
    int endBar = Bars - 1; // End bar index

    for (int bar = startBar; bar <= endBar; bar++)
    {
    if (isBullishDivergence(bar))
    {
    // Bullish divergence detected
    // Perform necessary actions
    Print("Bullish Divergence at Bar ", bar);
    }

    if (isBearishDivergence(bar))
    {
    // Bearish divergence detected
    // Perform necessary actions
    Print("Bearish Divergence at Bar ", bar);
    }
    }
    }
     
  2. Niguru

    Niguru Member Credit Hunter

    Equity
    Credit
    Ref Point
    5/5,
    This is my review for this thread:
    Strategi pengelolaan RSI yang bagus.
    Cukup bagus untuk EA yang dijalankan pada XAUUSD.
    Sayangnya saya belum berhasil menerapkan strategi ini pada forex pair (GBP, EUR, JPY, CHF, AUD, dll.)

    Terima kasih sharingnya.
     
  3. SulthonNurfalah

    SulthonNurfalah New Member

    Equity
    Credit
    Ref Point
    5/5,
    This is my review for this thread:
    Thanks for sharing mql4 code divergence
     

Share This Page