My indicator disappear anytime I change to new time-frame chart - page 2

 

Thank you for your explanation. 

I see it better now.

 SCFX 

 

Hi,

I scratch my head with that crazy error.

Still this simple indicator of 4 rows code disappear when I change timeframe.

Crazily enough, I applied the suggestions posted here already but still failed.

I feel so bad...

Please help me.

Many thanks,

SCFX

//+------------------------------------------------------------------+
//|                                                        H_roc.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//#property indicator_minimum -3.5
//#property indicator_maximum 3.5
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "boring"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
//--- indicator buffers
double         boring[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,boring);
   
          
//---
   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 limit=rates_total-prev_calculated;
   if(limit<=0) limit=300;
//---
 
 for(int i=1;i<=limit  ;i++)
{  if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50
     ) 
      boring[i]=Close[i];
    
}  
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
scfx:

Hi,

I scratch my head with that crazy error.

Still this simple indicator of 4 rows code disappear when I change timeframe.

Crazily enough, I applied the suggestions posted here already but still failed.

I feel so bad...

Please help me.

Many thanks,

SCFX



2014.06.15 11:26:39.908    zero divide in 'test.mq4' (59,44)
 
angevoyageur:
2014.06.15 11:26:39.908    zero divide in 'test.mq4' (59,44)


Thanks for replying.

On my journal, there is no such notice but when I change TF, this indicator is removed.

I still cannot fix it yet.

SCFX

2014.05.18 08:41:31.080 Custom indicator H_889_boring GBPUSD,H1: removed
2014.05.18 08:41:25.441 Custom indicator H_889_boring GBPUSD,H4: loaded successfully

 
scfx:

I still cannot fix it yet.

SCFX

That is because you are not trying very hard.

You will not learn anything if you get other people to do it for you, the only way is to do it yourslf so you understand it, the only way to do it yourself is to examine each line of code, break it down and figure out exactly what it is doing and there are several ways of doing that. 

for(int i=1;i<=limit  ;i++)
{  if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50
     ) // what is this parentheses doing here ? How can you expect to write code that works if you are this sloppy about it ?
      boring[i]=Close[i];
    
}  

 

These are all things you could do that would help you find the cause of the problem

  • You said you checked the journal tab, did you look in the experts tab next to it ?
  • I already told you last week your code was causing array out of range did you look that up to find out what it means ?
  • There is a debugger in metaeditor you could try to learn how to use it to debug your code. 
  • You could use Print() so you could check what the values of your variables were when the code was running.

If you had clicked on the experts tab it would tells you your indicator did zero divide on this line, changing timeframes had nothing to do with it.

if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50

So that has to mean this  High[i]-Low[i]  was zero for some reason. If you look at the indicator on the chart you can see the indicator drew some of its values then stops at a bar where there is only one tick. If there is only one tick high[i] == low[i] so that would cause a zero divide

Now make sure high[i]-low[i] does not get used if it is zero.

   int limit=rates_total-prev_calculated;
   if(limit<=0) limit=300;
//---
   for(int i=1;i<=limit  ;i++)
   {
    if(High[i] - Low[i] == 0) continue; // --------------- skip this bar
    
    if( MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50 ) 
    {boring[i]=Close[i];
   }}
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 

Now you will see the experts tab tells you the code did not zero divide anymore but it generates array out of range error on this line. 

if(high[i] - low[i] == 0)  

 

So what happens if you take out all the calculations and condidtions to test the loop ?

   int limit=rates_total-prev_calculated;
   if(limit<=0) limit=300;
//---

   for(int i=1;i<=limit  ;i++)
   { 
    boring[i]=Close[i];
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 

Now the experts tab reports array out of range again, this time on this line:

boring[i]=Close[i];

 

So now you know your code causes array out of range every time a price array is used in the loop. So figure out what is wrong with the loop and why Close[i], Low[i], High[i] is out of range. You can seee the indicator draws all the way to the end of the chart so the error must be at the end, the highest array indexes.

   int limit=rates_total-prev_calculated; // what is the value of limit in this calculation when prev_calulated == 0 ?
   if(limit<=0) limit=300;
//---

   for(int i=1;i<=limit;i++) // how does the value of limit compare with the highest available Close[] array index ?

It appears you are trying to make an indicator without learning how to code a loop that works correctly first. There are plenty of examples of how to do this. Look at the code for the included indicators and experiment with them until you understand exactly how they work then try to make an indicator to draw a simple single line that works properly before you try to make it perform calculations.
 

@ scfx

The only person who can fix is you. Or go to https://www.mql5.com/en/job

and post the job there.

 
SDC:
...

Please SDC, no need to be so harsh, even if you are right.
 
angevoyageur:

Please SDC, no need to be so harsh, even if you are right.

lol I reworded my post a little bit ;)

 

Sorry, I misguided you, I apolgize, indicator can be coded like that :

int limit = -1;
   if( prev_calculated == 0 ) limit =  rates_total - 3000;// will calculate 3000 Bars
  if( prev_calculated > 0 )   limit = rates_total-prev_calculated;

 for(int i=limit; i>=0; i--)
   {
    if(High[i] - Low[i] == 0) continue; // --------------- skip this bar
    

// Please test it, check if it's OK;
// Put the indicator in a backtest EA, say MACDSample, to check it
 

If the chart has less than 3000 bars it will still be out of range.

Reason: