Indicator loop mql4 vs mql5

 

Hy,

I want to have an indicator which was originally coded in MQL4 in MQL5. Everyting is ok so far. Just one warning with data conversion. But that is ok.

My question is about the indicator loop in function OnCalculate(). I'am not sure with the new code for the old indicator loop:

Old version

//+------------------------------------------------------------------+
int start() {

   int limit, i;
   int counted_bars=IndicatorCounted();
   //---
   if(counted_bars<0) return(-1);
   //----
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
mov[limit]=0;
trend[limit]=0;
wave[limit]=0;
vol[limit]=0;
up[limit]=0;
dn[limit]=0;

for(i=limit-1; i>=0; i--) {

if (Close[i]-Close[i+1]>0) mov[i]=1;
if (Close[i]-Close[i+1]==0) mov[i]=0;
if (Close[i]-Close[i+1]<0) mov[i]=-1; 

if ((mov[i]!=0) && (mov[i]!=mov[i+1])) {trend[i]=mov[i];} else {trend[i]=trend[i+1];} 

if ((trend[i]!=wave[i+1]) && (MathAbs(Close[i]-Close[i+1])*10000>=dif)) {wave[i]=trend[i];} else {wave[i]=wave[i+1];}    

if (wave[i]==wave[i+1]) {vol[i]=vol[i+1]+Volume[i];} else {vol[i]=Volume[i];}

if (wave[i]==1) {up[i]=vol[i]; dn[i]=0;}
if (wave[i]==-1) {dn[i]=vol[i]; up[i]=0;}
   
   }

 My new version for MQL5:

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, i;
   int counted_bars=prev_calculated;
   //---
   if(counted_bars<0) return(-1);
   //----
   if(counted_bars>0) counted_bars--;
   //----
   if(counted_bars==0) counted_bars=1;
   limit=rates_total-counted_bars;
   
mov[limit]=0;
trend[limit]=0;
wave[limit]=0;
vol[limit]=0;
up[limit]=0;
dn[limit]=0;

for(i=limit-1; i>=0; i--) {

if (close[i]-close[i+1]>0) mov[i]=1;
if (close[i]-close[i+1]==0) mov[i]=0;
if (close[i]-close[i+1]<0) mov[i]=-1; 

if ((mov[i]!=0) && (mov[i]!=mov[i+1])) {trend[i]=mov[i];} else {trend[i]=trend[i+1];} 

if ((trend[i]!=wave[i+1]) && (MathAbs(close[i]-close[i+1])*10000>=dif)) {wave[i]=trend[i];} else {wave[i]=wave[i+1];}    

if (wave[i]==wave[i+1]) {vol[i]=vol[i+1]+volume[i];} else {vol[i]=volume[i];}

if (wave[i]==1) {up[i]=vol[i]; dn[i]=0;}
if (wave[i]==-1) {dn[i]=vol[i]; up[i]=0;}
   
   }
   
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 The result in MT4

MT4 

  

 And the result for my MT5-Code :-(

 

 I'am looking for some help with the indicator loop above.

Thanks 

 
The problem is obviously not in the loop.
 
angevoyageur:
The problem is obviously not in the loop.

Any ideas what the problem is? Algorithm within the loop can't be the reason.

Source Code MQL4

#property indicator_separate_window
#property indicator_buffers 6

#property indicator_color1 Green
#property indicator_color2 Red

#property indicator_width1 2     
#property indicator_width2 2     

extern int dif=1;
double mov[],trend[],wave[],up[],dn[],vol[];
int firstrun;

int init() {

IndicatorBuffers(6);

SetIndexBuffer(0,up);
SetIndexBuffer(1,dn);
SetIndexBuffer(2,mov);
SetIndexBuffer(3,trend);
SetIndexBuffer(4,wave);
SetIndexBuffer(5,vol);

SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
SetIndexStyle(4,DRAW_NONE);
SetIndexStyle(5,DRAW_NONE);

   return(0); }
 
//+------------------------------------------------------------------+
int start() {

   int limit, i;
   int counted_bars=IndicatorCounted();
   //---
   if(counted_bars<0) return(-1);
   //----
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
mov[limit]=0;
trend[limit]=0;
wave[limit]=0;
vol[limit]=0;
up[limit]=0;
dn[limit]=0;

for(i=limit-1; i>=0; i--) {

if (Close[i]-Close[i+1]>0) mov[i]=1;
if (Close[i]-Close[i+1]==0) mov[i]=0;
if (Close[i]-Close[i+1]<0) mov[i]=-1; 

if ((mov[i]!=0) && (mov[i]!=mov[i+1])) {trend[i]=mov[i];} else {trend[i]=trend[i+1];} 

if ((trend[i]!=wave[i+1]) && (MathAbs(Close[i]-Close[i+1])*10000>=dif)) {wave[i]=trend[i];} else {wave[i]=wave[i+1];}    

if (wave[i]==wave[i+1]) {vol[i]=vol[i+1]+Volume[i];} else {vol[i]=Volume[i];}

if (wave[i]==1) {up[i]=vol[i]; dn[i]=0;}
if (wave[i]==-1) {dn[i]=vol[i]; up[i]=0;}
   
   }


return(0); 

Modified Code for MT5

//+------------------------------------------------------------------+
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   6
//--- plot up
#property indicator_label1  "up"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot dn
#property indicator_label2  "dn"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot mov
#property indicator_label3  "mov"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot trend
#property indicator_label4  "trend"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot wave
#property indicator_label5  "wave"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrRed
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot vol
#property indicator_label6  "vol"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrRed
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- input parameters
input int      dif=1;
//--- indicator buffers
double           up[];
double           dn[];
double         mov[];
double         trend[];
double         wave[];
double          vol[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,up,INDICATOR_DATA);
   SetIndexBuffer(1,dn,INDICATOR_DATA);
   SetIndexBuffer(2,mov,INDICATOR_DATA);
   SetIndexBuffer(3,trend,INDICATOR_DATA);
   SetIndexBuffer(4,wave,INDICATOR_DATA);
   SetIndexBuffer(5,vol,INDICATOR_DATA);
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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 limit, i;
   int counted_bars=prev_calculated;
   //---
   if(counted_bars<0) return(-1);
   //----
   if(counted_bars>0) counted_bars--;
   //----
   if(counted_bars==0) counted_bars=1;
   limit=rates_total-counted_bars;
   
mov[limit]=0;
trend[limit]=0;
wave[limit]=0;
vol[limit]=0;
up[limit]=0;
dn[limit]=0;

for(i=limit-1; i>=0; i--) {

if (close[i]-close[i+1]>0) mov[i]=1;
if (close[i]-close[i+1]==0) mov[i]=0;
if (close[i]-close[i+1]<0) mov[i]=-1; 

if ((mov[i]!=0) && (mov[i]!=mov[i+1])) {trend[i]=mov[i];} else {trend[i]=trend[i+1];} 

if ((trend[i]!=wave[i+1]) && (MathAbs(close[i]-close[i+1])*10000>=dif)) {wave[i]=trend[i];} else {wave[i]=wave[i+1];}    

if (wave[i]==wave[i+1]) {vol[i]=vol[i+1]+volume[i];} else {vol[i]=volume[i];}

if (wave[i]==1) {up[i]=vol[i]; dn[i]=0;}
if (wave[i]==-1) {dn[i]=vol[i]; up[i]=0;}
   
   }
   
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I think there is a problem with the implementation of the old  IndicatorCounted() function 

 

There are so many errors it is amazing that something is displayed. 

Have you read documentation on indicator with MQL5  ?

I draw your special attention to timeseries access.

P.S: Moreover, there are also small errors in the version MQL4.

 

I haven't check the entire code, but you should change 

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

into DRAW_HISTOGRAM.

 
phi.nuts:

I haven't check the entire code, but you should change 

into DRAW_HISTOGRAM.

Thank you phi.nuts for your info, but I think there is more than this. Same result.

I think I have to study all the stuff  that angevoyageur recommended. One day I will find the error even more...

Thank's for your suggestions.

Mike 

 
mm111:

Thank you phi.nuts for your info, but I think there is more than this. Same result.

I think I have to study all the stuff  that angevoyageur recommended. One day I will find the error even more...

Thank's for your suggestions.

Mike 

You are welcome. Keep us informed of your progress.

I post you what I have tried with your code, but I don't have more time to continue. May be that help you.

Files:
test.mq5  4 kb
 
maybe mql5's i is not the i in mql4 !!!