iCustom applied price: new bar value modify the previous one?

Paolo
10
Paolo  

Hi,

while developing a non banal custom indicator I met a strange behaviour which I cannot grasp fully. The indicator is intended not to be calculated for the open bar. In other words, it is supposed to calculate position J only when position J+1 appears (= bar J completed)

Below you can find the main loop which should allow for that.

My problem is: price[J]  value doesn't get frozen when a new bar appears (rates_total > prev_calculated) but it assumes a avalue which:

                    a) does not match the value plotted on the chart

                    b) it gets its final value only 1-2 ticks after the new bar appearance

I wish to understand better the logic beneath in order to code properly


double  Buf[];

int OnInit()
 {
  SetIndexBuffer(0,Buf,INDICATOR_DATA);

  return(INIT_SUCCEEDED);
 }
//--------------------------------------------------------
int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])
 {
  int J,jSTART;

// NO NEW BAR -> RETURNS (**))
  if(rates_total==prev_calculated) return(rates_total);

// FIRST CALL OR NEW BARS -> EXEC CALCULATIONS 
  if(prev_calculated==0)
   {
    jSTART=prev_calculated;   // 1st call: starts from bar 0 
   }     
  else                  
   {
    jSTART=prev_calculated-1; // other calls: starts from the bar before the open bar (see **)  
   } 
  
// MAIN CALCULATING CYCLE 
  for(J=jSTART;J<(rates_total-1);J++) //calculation stops before the open bar
   {        
    Buf[J]=......;   
   }

   Buf[rates_total-1]=EMPTY_VALUE; //Empties open bar value 

  return(rates_total);
 }













William Roeder
23730
William Roeder  
Paolo: My problem is: price[J]  value doesn't get frozen when a new bar appears (rates_total > prev_calculated) but it assumes a avalue which:

                    a) does not match the value plotted on the chart

                    b) it gets its final value only 1-2 ticks after the new bar appearance

Price[J] is the problem, but you don't show it.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code.
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem

We can't see your broken code.

Fix your broken code.

With the information you've provided — we can only guess. And you haven't provided any useful information for that.