I would like to display RSI only on specified bars

 

I would like to display only the specified bars by RSI in order to lighten the indicator, but it is not displayed well. How to display RSI for only specified of bars?


//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Relative Strength Index"
#property strict

#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_buffers    1
#property indicator_color1     DodgerBlue
#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameters
input int InpRSIPeriod=14; // RSI Period
input int Max_Bars = 500; // Max Bars
//--- buffers
double ExtRSIBuffer[];
double ExtPosBuffer[];
double ExtNegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
   SetIndexBuffer(1,ExtPosBuffer);
   SetIndexBuffer(2,ExtNegBuffer);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtRSIBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="RSI("+string(InpRSIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input
   if(InpRSIPeriod<2)
     {
      Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpRSIPeriod);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
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[])
  {
   int    i,pos;
   double diff;
//---
   if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtRSIBuffer,false);
   ArraySetAsSeries(ExtPosBuffer,false);
   ArraySetAsSeries(ExtNegBuffer,false);
   ArraySetAsSeries(close,false);
//--- preliminary calculations
   pos=prev_calculated-1;
   if(pos<=InpRSIPeriod)
     {
      //--- first RSIPeriod values of the indicator are not calculated
      ExtRSIBuffer[0]=0.0;
      ExtPosBuffer[0]=0.0;
      ExtNegBuffer[0]=0.0;
      double sump=0.0;
      double sumn=0.0;
      for(i=1; i<=InpRSIPeriod; i++)
        {
         ExtRSIBuffer[i]=0.0;
         ExtPosBuffer[i]=0.0;
         ExtNegBuffer[i]=0.0;
         diff=close[i]-close[i-1];
         if(diff>0)
            sump+=diff;
         else
            sumn-=diff;
        }
      //--- calculate first visible value
      ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;
      ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;
      if(ExtNegBuffer[InpRSIPeriod]!=0.0)
         ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
      else
        {
         if(ExtPosBuffer[InpRSIPeriod]!=0.0)
            ExtRSIBuffer[InpRSIPeriod]=100.0;
         else
            ExtRSIBuffer[InpRSIPeriod]=50.0;
        }
      //--- prepare the position value for main calculation
      pos=InpRSIPeriod+1;
     }
//--- the main loop of calculations
   for(i=pos; i<Max_Bars&& !IsStopped(); i++)
     {
      diff=close[i]-close[i-1];
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
      if(ExtNegBuffer[i]!=0.0)
         ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
      else
        {
         if(ExtPosBuffer[i]!=0.0)
            ExtRSIBuffer[i]=100.0;
         else
            ExtRSIBuffer[i]=50.0;
        }
     }
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
  • www.mql4.com
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Kosei S: How to display RSI for only specified of bars?

    Why did you post the RSI indicator? You use iRSI to read its value into your indicator.

  3. You have only four choices:

    1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

    2. Try asking at:

    3. MT4: Learn to code it.
      MT5: Begin learning to code it.

      If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

    4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
                Hiring to write script - General - MQL5 programming forum (2019)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help (2017)

 
Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
I changed the code to display only the specified bar, but it is no displayed, so I would like to know how to display it.
input int Max_Bars = 500;
for(i=pos; i<Max_Bars&& !IsStopped(); i++)
 
Kosei S #: I changed the code to display only the specified bar, but it is no displayed, so I would like to know how to display it.

An RSI uses an EMAs in its calculations. You can't reduce RSI calculations just to a fixed number of bars without compromising its results.

The following post also applies to your case ...

Forum on trading, automated trading systems and testing trading strategies

Chaikin Volatility in Expert only current bar

Fernando Carreiro, 2023.06.14 00:51

You can't run an exponential moving average in the same way as a simple moving average.

An EMA requires at least 3.45×(Length+1) bars for 99.9% convergence. So for an EMA(30) you require at least 107 bars.

You will actually need much more to get the exact same value as the build in indicator.

Please read the following post ... https://www.mql5.com/en/forum/446533/page2#comment_46578197


 
Fernando Carreiro #:

An RSI uses an EMAs in its calculations. You can't reduce RSI calculations just to a fixed number of bars without compromising its results.

The following post also applies to your case ...


I see. So the formula for RSI is (A/A+B)*100 A=average uptrend B=average downtrend

can I get approximate results with own calculations?

 
Kosei S #:I see. So the formula for RSI is (A/A+B)*100 A=average uptrend B=average downtrend.can I get approximate results with own calculations?
You can, but the results will be different to the normal RSI.
 
double diff = 0;
double RSI_A = 0;
double RSI_B = 0;
for(int i=0; i<=13; i++){
       diff = Close[i] - Close[i-1];
       
       if(diff>0) RSI_A+=diff;
       else RSI_B-=diff;
       RSI[0]= (RSI_A/RSI_A+RSI_B)*100;
   }

I can caluculate the RSI of the current bar, but I cannot caluculate the RSI of the past. Do you have any good ideas?

 
Fernando Carreiro #:
You can, but the results will be different to the normal RSI.

I would like to try it

 
for(int i=0; i<Max_Bars; i++)
I would like to calculate the closed price of the last 14 bars including close[i] for bar i.

is it possible?

 
Kosei S #: I can caluculate the RSI of the current bar, but I cannot caluculate the RSI of the past. Do you have any good ideas?

First ask, yourself — why do you want to do this?

Is it to reduce the "load" when calculating RSI in an EA?

If yes, then the using an EMA is the perfect solution given that it is an incremental calculation. Instead of using the indicator, simply calculate the RSI incrementally in your EA.

Or is there another reason why you want this?