ChartIndicatorAdd() in MT4 - page 2

 
Jhojan Alberto Tobon Monsalve:

Thank you Seng, but if I do that the BB values dosen´t match the RSI values Do you know what I mean? So the indicator is not shown properly.

You mean the value range? Just remove the #property indicator_maximum and #property indicator_minimum lines will do. 
 
Seng Joo Thio:
You mean the value range? Just remove the  #property indicator_maximum and #property indicator_minimum lines will do. 

If I do that nothing happens and it is because by default the RSI goes from 0 to 100.

 
Seng Joo Thio:
You mean the value range? Just remove the  #property indicator_maximum and #property indicator_minimum lines will do. 

BB values are prices and RSI values are percentages from 0 to 100. Any idea how can I convert BB values to percentages between 0 to 100.?

 
Jhojan Alberto Tobon Monsalve:

If I do that nothing happens and it is because by default the RSI goes from 0 to 100.

Reload the indicator, or go to its property and reset - just have to make it 'forget' the range. 
 
Jhojan Alberto Tobon Monsalve:

BB values are prices and RSI values are percentages from 0 to 100. Any idea how can I convert BB values to percentages between 0 to 100.?

The bands take on values of the source data. 
 
Seng Joo Thio:
The bands take on values of the source data. 
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
//#property indicator_minimum 0
//#property indicator_maximum 100
#property indicator_buffers 4
#property indicator_plots   4
//--- plot Rsi
#property indicator_label1  "Rsi"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot BandUpper
#property indicator_label2  "BandUpper"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDeepSkyBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot BandMiddle
#property indicator_label3  "BandMiddle"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDeepSkyBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot BandLower
#property indicator_label4  "BandLower"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrDeepSkyBlue
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- input parameters

//--- indicator buffers
double         RsiBuffer[];
double         BandUpperBuffer[];
double         BandMiddleBuffer[];
double         BandLowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,RsiBuffer);
   SetIndexBuffer(1,BandUpperBuffer);
   SetIndexBuffer(2,BandMiddleBuffer);
   SetIndexBuffer(3,BandLowerBuffer);
//---
   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[])
  {
   int iLimit = MathMin(rates_total-1,rates_total-prev_calculated);
   for (int i=iLimit; i>=0; i--)
   {
      RsiBuffer[i] = iRSI(Symbol(),Period(),14,PRICE_CLOSE,i);      
     
      BandUpperBuffer[i] = iBandsOnArray(close,0,20,2,0,MODE_UPPER,i);
      BandMiddleBuffer[i] = iMAOnArray(close,0,20,0,MODE_SMA,i);
      BandLowerBuffer[i] = iBandsOnArray(close,0,20,2,0,MODE_LOWER,i);
      
   }
   return(rates_total);
  }

This is what I have done, simply changed RsiBuffer for close buffer. Nothing changed.


 
Jhojan Alberto Tobon Monsalve:

This is what I have done, simply changed RsiBuffer for close buffer. Nothing changed.

Two issues:

(1) When you insist on putting RSI and BB (on close prices) together, you're bound to see meaningless results because they are of different ranges (will be different if you run BB based on RSI data instead).

(2) You should check _LastError to find out the error generated by iBandsOnArray(), since it obviously gave you all zeros. And if you checked, the error was 4029, which implies the array you supplied were not acceptable (so you cannot send a const double close[] into a function which only accepts double &close[]). To fix that, just create a 5th buffer to store close[], then feed that new buffer to iBandsOnArray(), and you'll get (red line is close price):


Now, if you still insist on showing RSI together, it'll just be a simple case of scale the values - that's basic arithmetic which I'm sure you are capable of.

 
Seng Joo Thio:

Two issues:

(1) When you insist on putting RSI and BB (on close prices) together, you're bound to see meaningless results because they are of different ranges (will be different if you run BB based on RSI data instead).

(2) You should check _LastError to find out the error generated by iBandsOnArray(), since it obviously gave you all zeros. And if you checked, the error was 4029, which implies the array you supplied were not acceptable (so you cannot send a const double close[] into a function which only accepts double &close[]). To fix that, just create a 5th buffer to store close[], then feed that new buffer to iBandsOnArray(), and you'll get (red line is close price):


Now, if you still insist on showing RSI together, it'll just be a simple case of scale the values - that's basic arithmetic which I'm sure you are capable of.

Hi, Seng.

Sorry for the delay, I was in other stuffs. As you mention I have tested what you said about error 4029, but still not working. I also tried what I mentioned before about scaling and this is my result 

Now they are the same,the next, I would like to move the BB when I scroll or drag the chart rightwards just like MT5 does, because the BB values overflow the RSI values, I mean, they are greater than 100 or less than 0. I would like to keep the BB  between 0 and 100.


 
Jhojan Alberto Tobon Monsalve:

Sorry for the delay, I was in other stuffs. As you mention I have tested what you said about error 4029, but still not working. I also tried what I mentioned before about scaling and this is my result 

Now they are the same,the next, I would like to move the BB when I scroll or drag the chart rightwards just like MT5 does, because the BB values overflow the RSI values, I mean, they are greater than 100 or less than 0. I would like to keep the BB  between 0 and 100.

On error 4029, if you show your code, I can see where the bug is.

As for scaling, your results were expected. In fact, your results are better than on MT5... because if you look at MT5 - the relative positions of BB and RSI changes not only as you scroll, but also when you zoom in/out. Here's an example:

   

First picture shows USDJPY-M1 when I zoom all the way in - notice the upper bands (in green) and rsi. In second picture I zoom all the way out - notice the bands shifted.

So I really don't see any benefit in showing them in the same sub-window, since they're ranged differently to serve their original purposes. But if your rationale is to save screen space... hmm... how about getting a bigger screen? (LOL)

Reason: