Rsi

How can I code to get current 1 hour RSI but it will be attached in 1 minute chart. I use iRSI(Symbol(),60,RSI_Period,PRICE_CLOSE,LAST_BAR);
but it doesn’t seem to be working. The value is wrong.

The form of the iRSI call looks ok. Are you trying to draw it like a normal indicator on the chart or just return the LAST_BAR value? If you’re trying to draw it on your chart then LAST_BAR needs to be in your for/while loop like for(LAST_BAR = Bars-RSI_Period-1; LAST_BAR >=0; LAST_BAR–) or if a while loop, while(LAST_BAR >=0) then make sure to decrement LAST_BAR within our code.

If that doesn’t help, post in the whole block of code and I’ll see if I can help get it working.

I need to get the value of 1 hour RSI every minute or real time value of RSI. Is that posible?

Yes - possible. You can access other time frames from a 1 minute chart and get them real time.

BUt the value is wrong. If I use PRICE_CLOSE wouldn’t it takes the value from the last closed bar? If current time is 3:24 how to I get the value of 1 hour RSI at 3:24 and not the RSI at 3:00 or 4:00.

Price constants - MQL4 Documentation

That’s built into the RSI function. iRSI matches the time frame (60) that is entered to the applied price price type (PRICE_CLOSE) that you enter so it draws the input price from the right time frame. I’ll attach a picture that I created doing just what you describe - a 60 minute RSI on a 1 minute chart - overlayed on top of a 1 hour chart with the identical RSI value.

Hmm, I can’t seem to attach a picture - not sure if it’s my new status or lack of knowledge on my part. Edit I got something - hopefully you can see what I mean. If you still need code I’ll post my code for the indicator.


Hi FXEZ, not sure what’s in the image. I think i need the code. You are so helpful. Thanks

Here is the code for the RSI test indicator. It references a 60 minute (default RSITime) chart’s RSI.

//+------------------------------------------------------------------+
//|                                                     RSI test.mq4 |
//|                               Copyright © 2011, Patrick M. White |
//|                     https://sites.google.com/site/marketformula/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Patrick M. White"
#property link      "https://sites.google.com/site/marketformula/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 Yellow

extern int RSILength = 20;
extern int RSITime =   60;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   string short_name;
   short_name="RSI("+RSILength+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexDrawBegin(0,RSILength);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
   if(counted_bars <0) return(-1);
   if(counted_bars<1)
      for(i=1;i<=RSILength;i++) ExtMapBuffer1[Bars-i]=0.0;
   i=Bars-RSILength-1;
   if(counted_bars>=RSILength) i=Bars-counted_bars-1;
   while (i >=0) {
      ExtMapBuffer1[i] = iRSI(NULL, RSITime, RSILength, PRICE_CLOSE, i);
      i--;
   }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

How can I use the code and include it in my code?

Why don’t you post your code then maybe I’ll have something I can comment on?

I use this. I try to include RSI-real.mq4 and call the function using Print(RSI(14));

#include “RSI-real.mq4”

#define DECIMAL_CONVERSION 10
#define COMMENT_DIGITS 2

// defines for evaluating entry conditions
#define LAST_BAR 1
#define THIS_BAR 0
#define NEGATIVE_VALUE -1

// defines for managing trade orders
#define RETRYCOUNT 2
#define RETRYDELAY 500

#define LONG 1
#define SHORT -1
#define ALL 0

#define ORDER_COMMENT “EA Order”
#define ORDER_COMMENT_DOUBLE “EA double Order”

#property copyright "Ian"
extern double Lots = 0.006;
extern double DoubleLots = 0.012;
//LossWhenEnter = 0.03 1003
extern double LossWhenEnter = -3;

extern int RSI_Period = 14;
extern double RSI_BuyBarrier = 73;
extern double RSI_SellBarrier = 27;
extern double StopLoss = 500;
extern double TakeProfit = 200;
extern int MagicNumber = 99999;
extern bool WriteScreenshots = true;

datetime lastAlertTime;
int Slippage = 2;
int PrevAction = 0;
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
string display = “”;

int decimalConv = 1;
int minSLTPdstnc;

int init()
{
if (Digits == 3 || Digits == 5) {

     decimalConv =     DECIMAL_CONVERSION;
  }


  // get miniumum stop/take profit distance
  minSLTPdstnc = MarketInfo(Symbol(), MODE_STOPLEVEL);
  
  Print("Broker: " + AccountCompany());
  Comment("Loading... awaiting new tick. very long");

return(0);
}

int deinit() { return(0); }

int start()
{

Print(RSI(14));
return;

}

Is there a reason you’re referencing RSI-real.mq4 instead of using the built-in RSI calculations? If it’s a custom version that you have to use, you will need to edit it or save as to create a copy and edit it so it will produce a 60 minute chart’s RSI. If it does a fancy calculation then you’ll have to try and figure out how to reference the 60 minute data in that adaptation. Even if this is the case, the following should help.

If there is no particular reason and you’re just trying to reference the regular RSI indicator with the 60 minute time frame, you can do it easily by just making a couple of changes in your code. First, at the top add the following near the other external inputs:
extern int RSI_Time = 60;

Then down at the bottom of your code in the start function, replace this line: Print(RSI(14)); with the following:

Print(iRSI(NULL, RSI_TIME, RSI_PERIOD, PRICE_CLOSE, 0));

RSI_TIME is the input you will use to change between time frames. The default is a H1 or 60 minute time frame.

RSI_PERIOD is an external input in your code that isn’t being used with the default value of 14. You will be able to change RSI_PERIOD within the MT4 terminal instead of needing to edit the code each time.

PRICE_CLOSE bases the RSI calculation off the 60 minute close price, and 0 means you are getting the current (last bar) bar’s data with no delay. Setting the last value to 5 instead would give the 60 minute RSI as it would have been 5 bars back. Hope that helps! Let me know if you have more questions.

Patrick

I need to get the real time value. If I use PRICE_CLOSE wouldn’t it takes the value from the last closed bar? If current time is 3:24 how to I get the value of 1 hour RSI at 3:24 and not the RSI at 3:00 or 4:00.

PRICE_CLOSE just references the last close price on a bar, as opposed to PRICE_HIGH or PRICE_LOW. The zero means it is the last bar’s close price, or in other words real time data.

Try my suggestions and see for yourself!

Good luck!