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

 

Hi SDC,

I got it. Many thanks.

if there is 100 bar, total rates starts from 0 to 99.

At the beginning, pre_cal =0 --> limit=100 bar - 0 =100.

for(int=1 to limit=100; I++)

at the very end, I=100(index value) and there is no such bar 100.

Therefore to make the max I=99, I put limit -1;

I wonder if there is any better way.

Moreover, what make me confused is that in many other indicator, I use same approach and there is no such out of range problem.

Also, thank you for the Expert tab.

SCFX


   int limit=rates_total-prev_calculated;
   if(limit<=0) limit=2;
//---
 
 for(int i=1;i<limit-1  ;i++)
{  if((High[i]-Low[i])==0) continue;

   if(   MathAbs(Close[i]-Open[i])/(High[i]-Low[i])<0.50     ) 
      boring[i]=Close[i];
 
scfx:

Moreover, what make me confused is that in many other indicator, I use same approach and there is no such out of range problem.



before B600 out of range wasn't a critical error
 
scfx:.

Therefore to make the max I=99, I put limit -1;

I wonder if there is any better way.

Your code will now draw the chart history from bar 1 up without error but it wont draw for any new bars. There are many ways to code it depending what you want it to do. Look at the included indicators in metaeditor to see how the MQ coders do it. When you can read their code and understand the reason for each line you will have no problem creating indicators.

 

I got it now SDC.

Personally speaking, it is difficult to imagine when I read the document. Still I am not sure why prev_calculated= Total_rates -1.

So I make numerical example here. I hope it could help someone new like me.

Normally we see:

Limit= rates_total- prev_calculated; //(no-1)

OR

for(i=1;i<limit;i++) or for(i=1;i<=limit;i++)

The important thing is to make sure LIMIT >=1. In my situation, LIMIT=0 and therefore, indicator is not REFESHED when new tick coming in.

Why, let see. (I think the cause is the prev_calculated at least in my case)

Assuming that I attach the indicator when there are 100 bar on chart. Here are variables values:

First table of value
Variable  
Total_rates 100
Index of bar 0-99
prev_calculated 0
Limit 100
Loop i value 1-99 

 Everything is good. Value will show up first time from bar 1 to the beginning of chart.How many bar already calculated? It is critical point to my mistake and I am not 100% clear.

 

When new bar start, on chart now 101 bar. Indicator is not updated on the already closed bar, which is now bar 1.

Second Table of value
Variable  
Total_rates 101
Index of bar 0-100
prev_calculated  99 OR 100 (see below)
Limit                 2 OR 1
Loop i value 1to1 OR 1 to 0 

 Based on the 1st table, indicator calculate 99 bar (as it loop from 1 to 99).
However, from the document it says: “BUT if it is not the first calling of start(), the value equal to Bars-1 will be returned). So it will return 101-1=100.
This one bar different cause trouble. If system return 100 as prev_calculated.
You see that, if prev_calculated=99, the loop will work.
However, look like prev_calculated = 100 and therefore loop will not work as limit =0 or -1 depends.
Can anyone help me see the logic behind the prev_calculated= Bars-1 in this case?

So the solution is quite clear: Make for(i=1; ___ this one here must be greater than 1 for (<) or greater than or equal to 1 for (<=)___; i++). In my last code, it equal to 0.

   int pcal=prev_calculated;
   
   if(prev_calculated>0)
   pcal--; 
      
   int limit=rates_total-pcal;
   for(int i=1;i<=limit  ;i++)	//it is <=


//--------------OR ------------------------

  int limit=rates_total-prev_calculated-1;
  if(limit<=0) limit=2; //NOT limit

for(int i=1;i<=limit  ;i++) //it is <=

  

I hope it helps.

 

 

SCFX 

 

prev_calculated == rates_total

scfx:

I got it now SDC.

Personally speaking, it is difficult to imagine when I read the document. Still I am not sure why prev_calculated= Total_rates -1. 

SCFX 

  Create a new indicator put this code in the start function, attach it to a 1 minute chart and watch the alerts as the ticks arrive.

Alert("rt = ",rates_total,"  pc = ",prev_calculated);

You will see rates_total is the current amount of bars.

prev_calculated is the amount of bars there were at the previous tick.

 
SDC:

prev_calculated == rates_total

  Create a new indicator put this code in the start function, attach it to a 1 minute chart and watch the alerts as the ticks arrive.

You will see rates_total is the current amount of bars.

prev_calculated is the amount of bars there were at the previous tick.

 


wow that's weird.

In the link posted before, they said that if :BUT if it is not the first calling of start(), the value equal to Bars-1 will be returned.

How come it return Bars (Rate_totals).

 

yes I know they say that but its not entirely accurate.

what actually happens is this. 

rates_total == total amount of bars when the current tick arrived. prev_calculated == total amount of bars when the previous tick arrived.

Bars in Chart  Indicator State rates-total prev-calculated rates_total-prev_calculated 
 1000 first run when loaded 1000 0  1000
 1000 next tick 1000 1000  0
 1000 next tick 1000 1000  0
 1001 1st tick of new bar 1001 1000  1
 1001 next tick 1001 1001  0
 1001 next tick  1001 1001  0

 

Therefore you have 3 main states of prev-calculated vs rates_total. Indicator loaded, mid bar ticks, new bar first tick.

prev_calculated == 0 on first run because there was no previous tick since the indicator loaded. 

Also if chart is changed or history added, prev_calculated resets back to zero.

The return value from OnCalculate is not used but it is best to use the default return(rates_total) in case they ever fix that.

 
Bars in Chart Indicator State rates-total prev-calculated rates_total-prev_calculated
1000 first run when loaded 1000 0 1000
And bar 1000 does not exist.

I know what the new docs say. If you do it their way you have to test prev_calculated for non-zero and adjust rates_total - prev_calculated down one. That is going back to decrement indicator_counted confusion.
int OnCalculate(const int rates_total,
                const int prev_calculated, ...){
   #define LOOKBACK 1 // iMA(... iBar+1)
   int indicator_counted = prev_calculated;
   if(indicator_counted < LOOKBACK) indicator_counted = LOOKBACK;
   for(iBar = rates_total - 1 - indicator_counted; i>=0 i--){
      Buffer[iBar] = ...;
   }
   return(rates_total - 1); // Recalculate bar zero next tick.
}
See Show progress of indicator - MQL4 forum
 

There is no point doing return( rates_total - 1 ) the value of prev_calculated is that as if it was return(rates_total) regardless of what you told it to return.

 
SDC: There is no point doing return( rates_total - 1 ) the value of prev_calculated is that as if it was return(rates_total) regardless of what you told it to return.
have you posted the problem at the service desk?
Reason: