Need Help Fixing RSI Range Marker Indicator Code

 

Hello MQL5 Community,

I am currently working on developing a custom RSI Range Marker Indicator for MetaTrader 5 and need some assistance. The indicator is supposed to plot visual markers on the trading chart when the RSI enters or exits a defined range (e.g., between 22 and 24), but it's not functioning as expected. No markers are displayed on the chart, and I'm unsure where the issue lies.

Objective of the Indicator: The goal is to create an indicator that:

  • Plots a dot at the candle where the RSI first rises to or above 22.
  • Plots another dot at the candle where the RSI first exceeds 24.
  • Helps in visualizing the RSI entering and exiting specified ranges to identify potential overbought or oversold conditions.

Current Code:

//+------------------------------------------------------------------+
//|                                          RSI Range Marker.mq5   |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com|
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot properties
#property indicator_label1  "RSI Start Marker"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_label2  "RSI End Marker"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int     rsiPeriod  = 14;       // Period for the RSI calculation
input double  rangeStart = 22.0;     // Lower boundary of the RSI range
input double  rangeEnd   = 24.0;     // Upper boundary of the RSI range
input double  plotOffset = 0.001;    // Offset for plotting markers, in percentage
//--- indicator buffers
double startBuffer[];
double endBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, startBuffer);
   SetIndexBuffer(1, endBuffer);
   IndicatorSetInteger(INDICATOR_DIGITS, 5);
   IndicatorSetString(INDICATOR_SHORTNAME, "RSI Range Marker (" + IntegerToString(rsiPeriod) + ")");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(startBuffer, true);
   ArraySetAsSeries(endBuffer, true);
   int begin = rates_total - prev_calculated > rsiPeriod ? rates_total - prev_calculated : rsiPeriod;
   for(int i = begin; i < rates_total; i++)
     {
      double rsi = iRSI(NULL, 0, rsiPeriod, i);
      double plotPrice = 0;
      if(rsi >= rangeStart && iRSI(NULL, 0, rsiPeriod, i-1) < rangeStart)
        {
         plotPrice = low[i] - plotOffset * (high[i] - low[i]);
         startBuffer[i] = plotPrice;
         PlotIndexSetInteger(0, PLOT_ARROW, i, int(159));
        }
      if(rsi <= rangeEnd && iRSI(NULL, 0, rsiPeriod, i+1) > rangeEnd)
        {
         plotPrice = high[i] + plotOffset * (high[i] - low[i]);
         endBuffer[i] = plotPrice;
         PlotIndexSetInteger(1, PLOT_ARROW, i, int(159));
        }
     }
   return(rates_total);
  }


  

Request: Could anyone please review my code and suggest what might be wrong or how it can be improved? Any advice on how to ensure the markers are displayed correctly would be greatly appreciated.

Thank you in advance for your time and help!

Best regards,

Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • 599.00 USD
  • 2024.04.26
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2.       double rsi = iRSI(NULL, 0, rsiPeriod, i);

    This is a MT4 call. “i” is not a valid applied price.

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. This is a MT4 call. “i” is not a valid applied price.

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

updated the post, thanks, but i dont quite understand your suggestion on fixing the problem 

Reason: