Can somebody explain this ?

 

This is a simple expert to check indicators values. The expert get a iSAR indicator, send its output to a function. The function doesn't change it, just print it and returns same value back to start(). Back in start() the value is print it again. The same is done for Open() in a second function. How come the values change inside the functions and then change again after returned?. Here are the expert and its output.

//+------------------------------------------------------------------+
//|                                                   Test1 Indy.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#include <log.mq4>

extern int    BarToCheck = 1;

int init()
  {
    log_open("Parabolic");
    log( "The log file has been successfully opened, the expert starts working..." );
    log_set_bar(BarToCheck);      
   return(0);
  }
  
int deinit()
  {
    log( "Close the log file, the expert finishes work..." );
    log_close();    
   return(0);
  } 

double dFunction1(double op,double val)
{
   log("Function dOpen="+DoubleToStr(op,Digits) +" dValue="+DoubleToStr(val,Digits));
   return(val);
}     

double dFunction2(double OP,double VAL)
{
   log("Function dOpen="+DoubleToStr(OP,Digits) +" dValue="+DoubleToStr(VAL,Digits));
   return(OP);
}    
   
int start()
{
   double dValue, dOpen, dVal, dOp;
   string sTime;
    bool New_Bar;
   New_Bar=IsNewBar();
   if (New_Bar)
    {      
      dValue = NormalizeDouble(iSAR(NULL, 0, 0.02, 0.2, BarToCheck), Digits);
      dOpen  = NormalizeDouble(iOpen(NULL, 0, BarToCheck), Digits);
      sTime  = TimeToStr(iTime(NULL, 0, BarToCheck), TIME_DATE|TIME_SECONDS);
      dVal = dFunction1(dOpen, dValue);
      dOp = dFunction2(dOpen, dValue);

    log("===New Bar @:"+ sTime +"====");  
    log("Open :" +DoubleToStr(dOpen, Digits) +" VALUE :" +DoubleToStr(dValue, Digits) + " Time["+ BarToCheck +"]:" + sTime);
    log("dVal="+DoubleToStr(dVal,Digits) +"  dOp="+DoubleToStr(dOp,Digits));
    }
  return(0);
}
//+------------------------------------------------------------------+
bool IsNewBar()
{
   static datetime New_Time = 0;
   bool New_Bar = false;
     if (New_Time!= Time[0])
       {
         New_Time = Time[0];
         New_Bar = true;
       }
      return(New_Bar);
} 

Here is the output

2010.09.17 - 01:30:00 - - - The log file has been successfully opened, the expert starts working...
2010.09.17 - 01:25:00 - - - Function dOpen=1.30678 dValue=1.30607
2010.09.17 - 01:25:00 - - - Function dOpen=1.30678 dValue=1.30607
2010.09.17 - 01:25:00 - - - ===New Bar @:2010.09.17 01:25:00====
2010.09.17 - 01:25:00 - - - Open :1.30678 VALUE :1.30607 Time[1]:2010.09.17 01:25:00
2010.09.17 - 01:25:00 - - - dVal=1.30607  dOp=1.30678
2010.09.17 - 01:30:00 - - - Function dOpen=1.30664 dValue=1.30609
2010.09.17 - 01:30:00 - - - Function dOpen=1.30664 dValue=1.30609
2010.09.17 - 01:30:00 - - - ===New Bar @:2010.09.17 01:30:00====
2010.09.17 - 01:30:00 - - - Open :1.30664 VALUE :1.30609 Time[1]:2010.09.17 01:30:00
2010.09.17 - 01:30:00 - - - dVal=1.30609  dOp=1.30664
2010.09.17 - 01:35:00 - - - Function dOpen=1.30672 dValue=1.30611
2010.09.17 - 01:35:00 - - - Function dOpen=1.30672 dValue=1.30611
2010.09.17 - 01:35:00 - - - ===New Bar @:2010.09.17 01:35:00====
2010.09.17 - 01:35:00 - - - Open :1.30672 VALUE :1.30611 Time[1]:2010.09.17 01:35:00
2010.09.17 - 01:35:00 - - - dVal=1.30611  dOp=1.30672
2010.09.17 - 01:40:00 - - - Function dOpen=1.30729 dValue=1.30616
2010.09.17 - 01:40:00 - - - Function dOpen=1.30729 dValue=1.30616
2010.09.17 - 01:40:00 - - - ===New Bar @:2010.09.17 01:40:00====
2010.09.17 - 01:40:00 - - - Open :1.30729 VALUE :1.30616 Time[1]:2010.09.17 01:40:00
2010.09.17 - 01:40:00 - - - dVal=1.30616  dOp=1.30729
2010.09.17 - 01:45:00 - - - Function dOpen=1.30756 dValue=1.30626
2010.09.17 - 01:45:00 - - - Function dOpen=1.30756 dValue=1.30626
2010.09.17 - 01:45:00 - - - ===New Bar @:2010.09.17 01:45:00====
2010.09.17 - 01:45:00 - - - Open :1.30756 VALUE :1.30626 Time[1]:2010.09.17 01:45:00
2010.09.17 - 01:45:00 - - - dVal=1.30626  dOp=1.30756
2010.09.17 - 01:50:00 - - - Function dOpen=1.30752 dValue=1.30635
2010.09.17 - 01:50:00 - - - Function dOpen=1.30752 dValue=1.30635
2010.09.17 - 01:50:00 - - - ===New Bar @:2010.09.17 01:50:00====
2010.09.17 - 01:50:00 - - - Open :1.30752 VALUE :1.30635 Time[1]:2010.09.17 01:50:00
2010.09.17 - 01:50:00 - - - dVal=1.30635  dOp=1.30752
2010.09.17 - 01:55:00 - - - Function dOpen=1.30741 dValue=1.30644
2010.09.17 - 01:55:00 - - - Function dOpen=1.30741 dValue=1.30644
2010.09.17 - 01:55:00 - - - ===New Bar @:2010.09.17 01:55:00====
2010.09.17 - 01:55:00 - - - Open :1.30741 VALUE :1.30644 Time[1]:2010.09.17 01:55:00
2010.09.17 - 01:55:00 - - - dVal=1.30644  dOp=1.30741
2010.09.17 - 02:00:00 - - - Function dOpen=1.30776 dValue=1.30657
2010.09.17 - 02:00:00 - - - Function dOpen=1.30776 dValue=1.30657
2010.09.17 - 02:00:00 - - - ===New Bar @:2010.09.17 02:00:00====
2010.09.17 - 02:00:00 - - - Open :1.30776 VALUE :1.30657 Time[1]:2010.09.17 02:00:00
2010.09.17 - 02:00:00 - - - dVal=1.30657  dOp=1.30776
 

I changed the place of the print statements and now the three values are the same. Are the print statements ading a lag ?

Here is what I changed:(also switch the print order so the open and SAR stay in same colums at the printout):

   if (New_Bar)
    {      
      dValue = NormalizeDouble(iSAR(NULL, 0, 0.02, 0.2, BarToCheck), Digits);
      dOpen  = NormalizeDouble(iOpen(NULL, 0, BarToCheck), Digits);
      sTime  = TimeToStr(iTime(NULL, 0, BarToCheck), TIME_DATE|TIME_SECONDS);
    log("===New Bar @:"+ sTime +"====");  
    log("Open :" +DoubleToStr(dOpen, Digits) +" VALUE :" +DoubleToStr(dValue, Digits) + " Time["+ BarToCheck +"]:" + sTime);
    
      dVal = dFunction1(dOpen, dValue);
      dOp = dFunction2(dOpen, dValue);

    log("dOp="+DoubleToStr(dOp,Digits)+" dVal="+DoubleToStr(dVal,Digits));
    }
  return(0);

the output is now:

2010.09.17 - 01:45:00 - - - ===New Bar @:2010.09.17 01:45:00====
2010.09.17 - 01:45:00 - - - Open :1.30756 VALUE :1.30626 Time[1]:2010.09.17 01:45:00
2010.09.17 - 01:45:00 - - - Function dOpen=1.30756 dValue=1.30626
2010.09.17 - 01:45:00 - - - Function dOpen=1.30756 dValue=1.30626
2010.09.17 - 01:45:00 - - - dOp=1.30756 dVal=1.30626
2010.09.17 - 01:50:00 - - - ===New Bar @:2010.09.17 01:50:00====
2010.09.17 - 01:50:00 - - - Open :1.30752 VALUE :1.30635 Time[1]:2010.09.17 01:50:00
2010.09.17 - 01:50:00 - - - Function dOpen=1.30752 dValue=1.30635
2010.09.17 - 01:50:00 - - - Function dOpen=1.30752 dValue=1.30635
2010.09.17 - 01:50:00 - - - dOp=1.30752 dVal=1.30635
2010.09.17 - 01:55:00 - - - ===New Bar @:2010.09.17 01:55:00====
2010.09.17 - 01:55:00 - - - Open :1.30741 VALUE :1.30644 Time[1]:2010.09.17 01:55:00
2010.09.17 - 01:55:00 - - - Function dOpen=1.30741 dValue=1.30644
2010.09.17 - 01:55:00 - - - Function dOpen=1.30741 dValue=1.30644
2010.09.17 - 01:55:00 - - - dOp=1.30741 dVal=1.30644
2010.09.17 - 02:00:00 - - - ===New Bar @:2010.09.17 02:00:00====
2010.09.17 - 02:00:00 - - - Open :1.30776 VALUE :1.30657 Time[1]:2010.09.17 02:00:00
2010.09.17 - 02:00:00 - - - Function dOpen=1.30776 dValue=1.30657
2010.09.17 - 02:00:00 - - - Function dOpen=1.30776 dValue=1.30657
2010.09.17 - 02:00:00 - - - dOp=1.30776 dVal=1.30657
2010.09.17 - 02:05:00 - - - ===New Bar @:2010.09.17 02:05:00====
2010.09.17 - 02:05:00 - - - Open :1.30771 VALUE :1.30668 Time[1]:2010.09.17 02:05:00
2010.09.17 - 02:05:00 - - - Function dOpen=1.30771 dValue=1.30668
2010.09.17 - 02:05:00 - - - Function dOpen=1.30771 dValue=1.30668
2010.09.17 - 02:05:00 - - - dOp=1.30771 dVal=1.30668
 

Never mind, I just realized the print is placed in the wrong position. The actual time is in the left side of the print out.

I guess too much coffe!! .I need a break:)

Reason: