Help me in my problem with #property strict

 

Hello my friends,

I've an indicator that I wrote several years ago with MT4. I want to update my indicator to the new MQL4 Compiler. I already wrote the new code but I've on problem. When I add #property strict the indicator vanish but when I delete it the indicator appears.

Here is the code & I hope any pro here could help me.

//+------------------------------------------------------------------+
//|                                                        PZO__.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_plots   1
//--- plot PZO_Line
#property indicator_label1  "PZO"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//---plot indicator levels
#property indicator_level1  -60
#property indicator_level2  -40
#property indicator_level3   0
#property indicator_level4   40
#property indicator_level5   60
//--- input parameters
input int      BARS=14;
//--- indicator buffers
double PZO[];//---Price Zone Oscillator
double R[];             //---Close Refrence
double CP[];            //---Closing Postions
double TC[];            //---Total Close
//--- right input parameters flag
bool   ExtParameters=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(4);
   SetIndexBuffer(0,PZO);
   SetIndexBuffer(1,R);
   SetIndexBuffer(2,CP);
   SetIndexBuffer(3,TC);
      
//----indicator style
   SetIndexStyle(0,DRAW_LINE);   
  
//----Data window & Index Label   
   IndicatorShortName("PZO("+IntegerToString(BARS)+")");
   SetIndexLabel(0,"PZO("+IntegerToString(BARS)+")");
      
//----indicator begin & digit number used at calculation
        SetIndexDrawBegin(0,BARS);
        IndicatorDigits(Digits-1);
      
//--- check for input parameters
   if(BARS<=0 )
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Prics Zone Oscillator - PZO 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[])
  {
   RefreshRates();
    int i,limit;
//---
   if(rates_total<=BARS || !ExtParameters)
      return(0); 
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
         
//---- main loop
//----R
   for( i=0; i<limit;i++)
   {
   if(Close[i]>Close[i+1])
   R[i]=Close[i];
   else
   R[i]= -1*Close[i];
   }
//----CP   
   for( i=0; i<limit;i++)
   {
   CP[i]=iMAOnArray(R,Bars,BARS,0,MODE_EMA,i);
   }
      
//----TC
   for( i=0; i<limit;i++)
   {
   TC[i]=iMA(NULL,0,BARS,0,MODE_EMA,0,i);
   }
 
//----PZO  
   for( i=0; i<limit;i++)
   {
   PZO[i]=100*(CP[i]/TC[i]);
   }
         
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
PZO__.mq4  4 kb
 

Is the compiler throwing any errors or warnings? Once you turn on property strict your code will need to be squeaky clean. Errors that used to slip past the compiler will no longer do so

I haven't been through all of your code but one thing that stands out is you set

#property indicator_buffers   1


But then in your Init you attempt to assign 4 indicator buffers

 
Stuart Browne:

Is the compiler throwing any errors or warnings? Once you turn on property strict your code will need to be squeaky clean. Errors that used to slip past the compiler will no longer do so

I haven't been through all of your code but one thing that stands out is you set


But then in your Init you attempt to assign 4 indicator buffers

- When I compile the code it give me Zero Errors which mean that my error is logical.

- When I  increase number of buffers from 1 to 4 I got this:


I don't understand as the indicator should look like this:


 

If all you want is that red PZO line, then you can leave the buffers at 1 and change line 99 from

for( i=0; i<limit;i++)

to

for( i=0; i<limit-1;i++)

 
But if you want the other 3 indicator buffers in there to print (R, CP & TC), you need to have a harder look at your code as there are bugs in there that need to be fixed.

With that one change, PZO line prints fine:

 

 
Stuart Browne:

If all you want is that red PZO line, then you can leave the buffers at 1 and change line 99 from

to

 
But if you want the other 3 indicator buffers in there to print (R, CP & TC), you need to have a harder look at your code as there are bugs in there that need to be fixed.

With that one change, PZO line prints fine:

 

I tried your solution for this problem and it worked good on recent data, but when I returned back on the chart (click Home button) it seems that the indicator give incorrect results.

Subtracting -1 from limit variable didn't give a compete answer to the problem. please look to the screenshot below:


 
Mohammad Al Bermaui:

I tried your solution for this problem and it worked good on recent data, but when I returned back on the chart (click Home button) it seems that the indicator give incorrect results.

Subtracting -1 from limit variable didn't give a compete answer to the problem. please look to the screenshot below:


I hope some pro could help me to fix this logical error.
 
Mohammad Al Bermaui:
I hope some pro could help me to fix this logical error.
//+------------------------------------------------------------------+
//|                                                        PZO__.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_plots   1
//--- plot PZO_Line
#property indicator_label1  "PZO"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//---plot indicator levels
#property indicator_level1  -60
#property indicator_level2  -40
#property indicator_level3   0
#property indicator_level4   40
#property indicator_level5   60
//--- input parameters
input int      BARS=14;
//--- indicator buffers
double PZO[];//---Price Zone Oscillator
double R[];             //---Close Refrence
double CP[];            //---Closing Postions
double TC[];            //---Total Close
//--- right input parameters flag
bool   ExtParameters=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(4);
   SetIndexBuffer(0,PZO);
   SetIndexBuffer(1,R);
   SetIndexBuffer(2,CP);
   SetIndexBuffer(3,TC);
      
//----indicator style
   SetIndexStyle(0,DRAW_LINE);   
  
//----Data window & Index Label   
   IndicatorShortName("PZO("+IntegerToString(BARS)+")");
   SetIndexLabel(0,"PZO("+IntegerToString(BARS)+")");
      
//----indicator begin & digit number used at calculation
        SetIndexDrawBegin(0,BARS);
        IndicatorDigits(Digits-1);
      
//--- check for input parameters
   if(BARS<=0 )
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Prics Zone Oscillator - PZO 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[])
  {
   RefreshRates();

//---
   if(rates_total<=BARS || !ExtParameters)
      return(0); 
//--- last counted bar will be recounted
   int i, limit = rates_total-prev_calculated;
   if(prev_calculated > 0) limit ++;
//---
   if(limit == rates_total)
      limit = rates_total-1;

//---- main loop
//----R
   for( i=0; i<limit;i++)
   {
   if(close[i]>close[i+1])
   R[i]=close[i];
   else
   R[i]= -1*close[i];
   }
//----CP   
   for( i=0; i<limit;i++)
   {
   CP[i]=iMAOnArray(R,Bars,BARS,0,MODE_EMA,i);
   }
      
//----TC
   for( i=0; i<limit;i++)
   {
   TC[i]=iMA(NULL,0,BARS,0,MODE_EMA,0,i);
   }
 
//----PZO  
   for( i=0; i<limit;i++)
   {
   PZO[i]=100*(CP[i]/TC[i]);
   }
         
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
  
 
Alexander Voronkov:
  

Sorry, I tried your solution but it didn't help, But I have worked this morning on solving the problem by cutting the code into pieces then studding it step by step. I hope to find the problem.

 

I think the problem is in this function:

iMAOnArray(R,Bars,BARS,0,MODE_EMA,i);
 

That is what is written about iMAOnArray in MQL4 Help:

The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries() function.

I hope some body to help me in this.