MQL4 to MQL5? Why this histogram does not draw?

To add comments, please log in or register
joshatt
20
joshatt  

Hi all,

It's been a long time, suddenly I find everything automatically turned into MQL5?

The following code is a three MA indicator I've been using for a long time.(In MT5 first bar is 0? but this indicator still runs fine. MQL5 running in MT4 is really confusing.) Today I'm trying to add a MA Cross signal, draw this signal as histogram. So I added the 4th buffer "crossBuffer". In the main loop, I added the second "for" structure, in which the first "if" structure is merely for debug purpose(forget 2nd and 3rd "if"), showing the histogram does not draw.

Could anybody please help?

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   1 

#property indicator_color1 Yellow
#property indicator_color2 Orange
#property indicator_color3 Red


#property indicator_label4  "Histogram" 
#property indicator_type4   DRAW_HISTOGRAM 
#property indicator_color4  Yellow 
#property indicator_style4  STYLE_SOLID 
#property indicator_width4  1 


extern int MA1=30;
extern int MA1Shift=0;
extern int MA2=50;
extern int MA2Shift=0;
extern int MA3Period=100;
extern int MA3Shift=0;
//--- buffers


double ExtMA1Buffer[];
double ExtMA2Buffer[];
double ExtMA3Buffer[];
double crossBuffer[];


color colors[]={clrRed,clrBlue,clrGreen}; 
 
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT}; 
//---
int    ExtBegin;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorDigits(Digits);
//---

//---- line shifts when drawing
   SetIndexShift(0,MA1Shift);
   SetIndexShift(1,MA2Shift);
   SetIndexShift(2,MA3Shift);

//---- first positions skipped when drawing
   SetIndexDrawBegin(0,MA1Shift+MA1);
   SetIndexDrawBegin(1,MA2Shift+MA2);
   SetIndexDrawBegin(2,MA3Shift+MA3Period);

//---- 4 indicator buffers mapping
   SetIndexBuffer(0,ExtMA1Buffer);
   SetIndexBuffer(1,ExtMA2Buffer);
   SetIndexBuffer(2,ExtMA3Buffer);
   SetIndexBuffer(3,crossBuffer,INDICATOR_DATA); 

//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_HISTOGRAM);

//---- index labels
   SetIndexLabel(0,"MA1");
   SetIndexLabel(1,"MA2");
   SetIndexLabel(2,"MA3");

//--- initialization done
return(INIT_SUCCEEDED);

  }
  
  

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 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 loop
   for(int ii=0; ii<limit; ii++)
     {
      //---- ma_shift set to 0 because SetIndexShift called abowe
      ExtMA1Buffer[ii]=iMA(NULL,0,MA1,0,MODE_SMA,PRICE_CLOSE,ii);
      ExtMA2Buffer[ii]=iMA(NULL,0,MA2,0,MODE_SMA,PRICE_CLOSE,ii);
      ExtMA3Buffer[ii]=iMA(NULL,0,MA3Period,0,MODE_SMA,PRICE_CLOSE,ii);

     }
     
   for( int ii=0; ii<limit; ii++)         // newly added lines
     {
         if(ii==5) crossBuffer[ii]=ExtMA3Buffer[ii];     
              
              //---- up trend and cross
         if( ExtMA3Buffer[ii-1]<ExtMA3Buffer[ii] && ExtMA1Buffer[ii]>ExtMA2Buffer[ii] && ExtMA2Buffer[ii]>ExtMA3Buffer[ii] && ( ExtMA1Buffer[ii-1]<ExtMA2Buffer[ii-1] || ExtMA2Buffer[ii-1]<ExtMA3Buffer[ii-1] ) )
         {
         crossBuffer[ii]=ExtMA3Buffer[ii];
         }
         
              //---- down trend and cross
         if( ExtMA3Buffer[ii-1]>ExtMA3Buffer[ii] && ExtMA1Buffer[ii]<ExtMA2Buffer[ii] && ExtMA2Buffer[ii]<ExtMA3Buffer[ii] && ( ExtMA1Buffer[ii-1]>ExtMA2Buffer[ii-1] || ExtMA2Buffer[ii-1]>ExtMA3Buffer[ii-1] ) )
         {
         crossBuffer[ii]=ExtMA3Buffer[ii];
         }
     }  
//---- done



return(rates_total); 

  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
35802
Alain Verleyen  

Why are you talking about mql5 ? It's clearly mql4.

ExtMA3Buffer[ii-1]

You are looking in the future.

What is happening when ii=0 ?

whroeder1
17991
whroeder1  
#property indicator_chart_window
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_HISTOGRAM);
                                   
Also

In the chart window, histograms are drawn via pairs of buffers. If the first is higher than the second, it uses the first's color.

In separate window, histograms are drawn from the buffer value to zero. They are drawn from the first to the last. Of you want one to go the back, it must be the first buffer.

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

joshatt
20
joshatt  
Alain Verleyen:

Why are you talking about mql5 ? It's clearly mql4.

You are looking in the future.

What is happening when ii=0 ?

Thanks for your help.

I'm still in MQL4?  Some of my indicators have OnInit and Oncalculate.

My bad --- yesterday I read that "In MT5 first bar is 0, last one is rates_total-1", so I changed "ii+1" into "ii-1", then realized that it's still MT4 , but then forget to change them back.

joshatt
20
joshatt  
whroeder1:
Also

Thank you for your help, sir.

Sorry, I don't understand your advice, do you mean move SetIndexStyle lines outside of OnInit into front? that will cause "'SetIndexStyle' - declaration without type" .

And, does it have to be via pairs of buffers, in the chart window?  I want it to draw from zero. In fact, I was planning to use verticle dot_line, instead of histogram, just don't know how to draw verticle lines NOT as objects.

To add comments, please log in or register