How To Explain the difference between this indicators draw and the line chart

To add comments, please log in or register
Salomon Kabongo Kabenamualu
144
Salomon Kabongo Kabenamualu  

#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- indicator buffers 
double         MABuffer[]; 
//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
void OnInit() 
  { 
//--- Bind the Array to the indicator buffer with index 0 
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA); 
//--- Set the line drawing 
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); 
//--- Set the style line 
   PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_DOT); 
//--- Set line color 
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrRed); 
//--- Set line thickness 
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1); 
//--- Set labels for the line 
   PlotIndexSetString(0,PLOT_LABEL,"Moving Average"); 
//--- 
  } 
//+------------------------------------------------------------------+ 
//| 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[]) 
  { 
//---  
   for(int i=prev_calculated;i<rates_total;i++) 
     { 
      MABuffer[i]=close[i]; 
     } 
//--- return value of prev_calculated for next call 
   return(rates_total); 
  }






It is starting good but at the end this indicator draw with 1 shift ahead Why ?



Thanks for your interest.

Files:
Question.png 31 kb
kypa
976
kypa  
for(int i=(int)MathMax(0,prev_calculated-1); i<(int)MathMax(1,rates_total); i++)
prev_calculate==rates_total most of the time, that's why it's not updating the last bar after the first tick of it so it looks like it's shifted (actually it shows the open, not the close)
Alain Verleyen
36050
Alain Verleyen  
kypa:
A function call on each iteration is not efficient. Better to use variables.
kypa
976
kypa  
Doesn't 'for' function calculate start and end values only once at the beginning? I could be wrong, of course, I'm often wrong.
Alain Verleyen
36050
Alain Verleyen  
kypa:
Doesn't 'for' function calculate start and end values only once at the beginning? I could be wrong, of course, I'm often wrong.

Right for the start, but this condition is evaluated on each iteration :

i<(int)MathMax(1,rates_total)
kypa
976
kypa  

I'm totally wrong.

This:

int prev_total_calculated_rates(int index, int value)
  {
   Print(IntegerToString(index));
   return(value);
  }

instead of rates_total and prev_calculated if what I though is right shouldn't print more than once but it does.

About the second condition - I don't remember the reason I wrote it so, but it's not really necessary, just i<rates_total should be fine.


The fix for OP's test should be this:

int rates_zero=(int)MathMax(0,prev_calculated-1);
for(int i=rates_zero; i<rates_total; i++)
Salomon Kabongo Kabenamualu
144
Salomon Kabongo Kabenamualu  
kypa:

I'm totally wrong.

This:

instead of rates_total and prev_calculated if what I though is right shouldn't print more than once but it does.

About the second condition - I don't remember the reason I wrote it so, but it's not really necessary, just i<rates_total should be fine.


The fix for OP's test should be this:

Okey, let me try it 

Salomon Kabongo Kabenamualu
144
Salomon Kabongo Kabenamualu  
kypa:

I'm totally wrong.

This:

instead of rates_total and prev_calculated if what I though is right shouldn't print more than once but it does.

About the second condition - I don't remember the reason I wrote it so, but it's not really necessary, just i<rates_total should be fine.


The fix for OP's test should be this:

It's working thanks

Salomon Kabongo Kabenamualu
144
Salomon Kabongo Kabenamualu  
Alain Verleyen:
A function call on each iteration is not efficient. Better to use variables.

thanks for your interest 

To add comments, please log in or register