Trouble getting histogram to work right

 

Hi gang, I'm trying to build an indicator for the first time in MT4 and myself and my programmer friend are stumped why the histogram is not working the way we want in this indicator.

We've been examining other indicators that use the histogram to create a cloud between two dynamic variables, and can't tell what we're missing.

This indicator has two RSIs of different speeds, with a moving average overlaid.

Currently both RSIs are plotting their histograms between the 0 level and the current RSI level.  I want the histogram to fill in the space between the two RSIs.

Any thoughts on what we're missing here?

Thanks in advance!

I've attached a link to a screenshot below.

#property copyright "Copyright © 2022 RRS"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 LightSeaGreen
#property indicator_color2 Silver
#property indicator_color3 Silver
#property indicator_color4 Purple
#property indicator_color5 Pink
#property indicator_style2 STYLE_SOLID
#property indicator_level1 50
#property indicator_levelcolor DimGray

//--- input parameters
extern int RSI_Slow = 11;
extern int RSI_Fast = 3;
extern int MA_Period = 17; 
extern int MA_Method = 0; //SMA

//--- buffers
double RSISlowBuffer[];
double RSIFastBuffer[];
double MABuffer[];
double H1[];
double H2[];


color tColor;
color tColor1;
color tColor3;
color tColor4;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
//--- buffers used for counting.
   IndicatorBuffers(5);
   SetIndexBuffer(0,MABuffer);
   SetIndexBuffer(1,RSISlowBuffer);
   SetIndexBuffer(2,RSIFastBuffer);
   SetIndexBuffer(3,H1);
   SetIndexBuffer(4,H2);
  
//--- indicator line
   SetIndexDrawBegin(0,RSI_Slow);
   SetIndexDrawBegin(1,RSI_Fast);
   
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 2);
   SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_SOLID, 1);
   SetIndexStyle(4, DRAW_HISTOGRAM, STYLE_SOLID, 1);
   
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("Speed Scooper");
   SetIndexLabel(0,"MA("+MA_Period+")");
   SetIndexLabel(1,"RSI Slow("+RSI_Slow+")");
   SetIndexLabel(2,"RSI Fast("+RSI_Fast+")");
   SetIndexLabel(3,"H1("+RSI_Slow+")");
   SetIndexLabel(4,"H2("+RSI_Fast+")");
   return(0);
  }
  
//+------------------------------------------------------------------+
//| RSI
//+------------------------------------------------------------------+

int start()
  {
   int i;
   int limit;
   int counted_bars=IndicatorCounted();
   
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   //--- main loops 1 and 2
   for(i=0; i < limit; i++)
      {
        RSISlowBuffer[i]=iRSI(Symbol(),0,RSI_Slow,PRICE_CLOSE,i);
        RSIFastBuffer[i]=iRSI(Symbol(),0,RSI_Fast,PRICE_CLOSE,i);
        H1[i]=RSISlowBuffer[i];
        H2[i]=RSIFastBuffer[i];
      }
         for(i=0; i < limit; i++)
      {
        MABuffer[i]=iMAOnArray(RSISlowBuffer,0,MA_Period,0,MA_Method,i);
      }
  }
Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
Files:
SpeedScooper.png  127 kb
 

Add 1 buffer, Mask[] (DRAW_HISTOGRAM, STYLE_SOLID, width 1, color Black) and add the following code in the first roop.

if (H1[i] < H2[i])
  Mask[i] = H1[i];
else if (H1[i] >= H2[i])
  Mask[i] = H2[i];
                
 
Nagisa Unada #:

Add 1 buffer, Mask[] (DRAW_HISTOGRAM, STYLE_SOLID, width 1, color Black) and add the following code in the first roop.

Thanks. That gets us somewhere, but it also masks the vertical graph bars on the chart too (see first attachment).

Why would this indicator require masking whereas other ones seem to do what I want automatically? Here's an MA crossover indicator that doesn't seem to require any additional calculations to fill in between the lines (shown in 2nd attachment)...

//+------------------------------------------------------------------+
//|                                                  MMA COLORED.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

#property copyright "pipsforever"
#property link      "http://www.trader-forex.fr/forum/programmation/30240-changer-la-couleur-dune-mm.html#post87444"

#property indicator_chart_window
#property indicator_buffers 4

extern int Fast = 8;
extern int Slow = 24;
extern color Up = Yellow;
extern color Down=Red;
extern color Ma_1 = Yellow;
extern color Ma_2 = Red;
double H1[];
double H2[];
double M1[];
double M2[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
SetIndexBuffer(0,H1);
SetIndexStyle(0,DRAW_HISTOGRAM,0,2,Up);

SetIndexBuffer(1,H2);
SetIndexStyle(1, DRAW_HISTOGRAM,0,2,Down);

SetIndexBuffer(2,M1);
SetIndexStyle(2,DRAW_LINE,0,2,Ma_1);

SetIndexBuffer(3,M2);
SetIndexStyle(3,DRAW_LINE,0,2,Ma_2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   for (int i =0; i<Bars-Slow; i++)
   
   {
     H1[i] = iMA(Symbol(),0,Fast,0,MODE_SMA,PRICE_CLOSE,i);
     H2[i] = iMA(Symbol(),0,Slow,0,MODE_SMA,PRICE_CLOSE,i);
     
     M1[i] = iMA(Symbol(),0,Fast,0,MODE_SMA,PRICE_CLOSE,i);
     M2[i] = iMA(Symbol(),0,Slow,0,MODE_SMA,PRICE_CLOSE,i);
   
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
rrsch #: Why would this indicator require masking whereas other ones seem to do what I want automatically? Here's an MA crossover indicator that doesn't seem to require any additional calculations to fill in between the lines (shown in 2nd attachment)...

The difference lies here:

#property indicator_separate_window
#property indicator_chart_window

Depending on whether it is displayed on the main chart or on the subwindow, the Histogram style behaves differently. On the subwindow it displays the histogram growing from the zero-line.

 
rrsch #:

Thanks. That gets us somewhere, but it also masks the vertical graph bars on the chart too (see first attachment).

Another method is to display the histogram as objects. That way, the vertical lines will not be masked.

 
Fernando Carreiro #: Depending on whether it is displayed on the main chart or on the subwindow, the Histogram style behaves differently. On the subwindow it displays the histogram growing from the zero-line.

Exactly.

In a separate window, histograms are drawn from the buffer value to zero. They are drawn from the first to the last. If you want one to be the background, it must be the first buffer.

See also
          How to Draw Cnadle chart in indicator_separate_window ? (XDXD) - MQL4 programming forum (2016)

 
Thanks guys. Would the DRAW_FILLING function in MQL5 work the way I want it to?
 
rrsch #: Thanks guys. Would the DRAW_FILLING function in MQL5 work the way I want it to?

That is only available on MT5. It is not available for MT4.

 
Fernando Carreiro #:

That is only available on MT5. It is not available for MT4.

Yeah, I realized that after trying to get it to work in MQL4 the other night! Will that function work how I want it to though, in MT5?
 
rrsch #: Yeah, I realized that after trying to get it to work in MQL4 the other night! Will that function work how I want it to though, in MT5?

That depends on what YOUR definition of "work the way I want it to".

Also, you will have to totally rethink your indicator for MQL5 as there are differences, as you are using the old style code of MQL4 which is totally incompatible with MQL5. Had you been using the more modern MQL4+ style, it would have been easier for you to port it, but not as it is now. For one you will have to use the OnInit and OnCalculate event handlers, instead of the "init" and "start" handlers.

 
Fernando Carreiro #:

That depends on what YOUR definition of "work the way I want it to".

Also, you will have to totally rethink your indicator for MQL5 as there are differences, as you are using the old style code of MQL4 which is totally incompatible with MQL5. Had you been using the more modern MQL4+ style, it would have been easier for you to port it, but not as it is now. For one you will have to use the OnInit and OnCalculate event handlers, instead of the "init" and "start" handlers.

"Work the way I want it to" as in filling in between the two RSI lines like I spoke about in my original comment.

I just modified this indicator from one of the stock MQL4 ones, and I'm just figuring out how to move forward with it – whether to try it myself with my programmer friend or hire a developer. This is helpful for me to get clear on that.

Thanks!

Reason: