Indicator display different in strategy tester visual mode

 

Hello

I have this indicator:

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 bar1, bar2;
//---
    if (rates_total <= 4) return(0);

    int limit;    
    
    limit = rates_total - prev_calculated;
    if (prev_calculated > 0) limit++;
    
    for (int i=limit-2; i>=0; i--) {
        bar1 = sign(Close[i]-Open[i]);
        bar2 = sign(Close[i+1]-Open[i+1]);        
        
        if (bar1+bar2==0)
            ind[i] = Open[i+1];
        else
            ind[i] = (Close[i+1]+Open[i+1]) * 0.5;
    }
  
//--- return value of prev_calculated for next call
   return(rates_total);
}


When I add it to an EA running in strategy tester, the indicator line varies at certain places. If I then stop the ea and reset the indicator, it corrects itself. In the screen shot the yellow line is when the indicator is dropped on the running strategy tester, the purple is shows when I just add it to the chart.

What am I doing wrong?

Screenshot

 

    limit = rates_total - prev_calculated;
    if (prev_calculated > 0) limit++;
    
    for (int i=limit-2; i>=0; i--) {

When rates_total-pre_calculated=0, limit =1 and the for loop will not execute

When rates_total-pre_calculated=1, when a new bar opens, limit =2 and the for loop will only calculate with bar[0]

So in real time, the code will only execute for the first tick of a new bar. When you attach it it has access to bar close values

 
Keith Watford:

    limit = rates_total - prev_calculated;
    if (prev_calculated > 0) limit++;
    
    for (int i=limit-2; i>=0; i--) {

When rates_total-pre_calculated=0, limit =1 and the for loop will not execute

When rates_total-pre_calculated=1, when a new bar opens, limit =2 and the for loop will only calculate with bar[0]

So in real time, the code will only execute for the first tick of a new bar. When you attach it it has access to bar close values

Thank you, sir, for your explanation
 
Exactly. Do your lookbacks correctly.
    limit = rates_total - prev_calculated;
    if (prev_calculated > 0) limit++;
    
    for (int i=limit-2; i>=0; i--) {
Reason: