My IF isn't working

 
I can't figure out why my if statement isn't working properly. Here is the segment in question:
Print("1-last_sar = ", last_sar, " current_SAR = ", current_SAR);
if(last_sar != current_SAR)
{
Print("2-last_sar = ", last_sar, " current_SAR = ", current_SAR);
mod_results = OrderModify(ticket,0,current_SAR,0,0,Red);
Print("3-last_sar = ", last_sar, " current_SAR = ", current_SAR);
last_current_time = current_time;
last_sar = current_SAR;
Print("4-last_sar = ", last_sar, " current_SAR = ", current_SAR);
}
Print("5-last_sar = ", last_sar, " current_SAR = ", current_SAR);


I would think that Print statements #2,3 and 4 would only print when last_sar is not equal to current_SAR. According to my journal, I am getting this:
2007.02.26 22:37:18 1999.01.29 19:49 BB-SAR GBPUSD,M15: 5-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:49 BB-SAR GBPUSD,M15: 1-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:45 BB-SAR GBPUSD,M15: 5-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:45 BB-SAR GBPUSD,M15: 4-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:45 BB-SAR GBPUSD,M15: 3-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:45 BB-SAR GBPUSD,M15: modify #1 buy 0.10 GBPUSD at 1.6622 sl: 1.6258 tp: 0.0000 ok
2007.02.26 22:37:18 1999.01.29 19:45 BB-SAR GBPUSD,M15: 2-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:45 BB-SAR GBPUSD,M15: 1-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:44 BB-SAR GBPUSD,M15: 5-last_sar = 1.6258 current_SAR = 1.6258
2007.02.26 22:37:18 1999.01.29 19:44 BB-SAR GBPUSD,M15: 1-last_sar = 1.6258 current_SAR = 1.6258


Can anyone help me figure this out?
Thanks,
Bennett
 
Hello,
I tested your if statement. It seems to work properly, or atleast the the script you included there. This is the code I used to test it. Try it and tell me if it works. You could have something else assigning values in your program. The other thing would be, your variables are not global, or they are being reset when the handler is run.
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
double last_sar;
double current_SAR;
double mod_results;
double last_current_time;
double ticket;
double current_time;
int init()
  {
//----
last_sar = 5;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
 
   if (last_sar != current_SAR)
   {
   Print("2-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   mod_results = OrderModify(ticket,0,current_SAR,0,0,Red);
   Print("3-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   last_current_time = current_time;
   last_sar = current_SAR;
   Print("4-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   }
   Print("5-last_sar = ", last_sar, " current_SAR = ", current_SAR);
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
Lord_Beast wrote:
Hello,
I tested your if statement. It seems to work properly, or atleast the the script you included there. This is the code I used to test it. Try it and tell me if it works. You could have something else assigning values in your program. The other thing would be, your variables are not global, or they are being reset when the handler is run.



Thanks for your help. I ran your code and then I added in a couple more lines of my original code until it broke. Here is what I ended up with:
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern double stepsize = 0.02;
extern double af = 0.2;
double last_sar;
double current_SAR;
double mod_results;
double last_current_time;
double ticket;
double current_time;
int init()
  {
//----
last_sar = 5;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double current_SAR = iSAR(NULL, 0, stepsize, af, 0);
  
//   Print("1-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   if (last_sar != current_SAR)
   {
   Print("2-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   mod_results = OrderModify(ticket,0,current_SAR,0,0,Red);
   Print("3-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   last_current_time = current_time;
   last_sar = current_SAR;
   Print("4-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   }
   last_sar = current_SAR;
//   Print("5-last_sar = ", last_sar, " current_SAR = ", current_SAR);
//----
   return(0);
  }
//+------------------------------------------------------------------+
And here is the journal where it shows print #2 when last_sar is equal to current_SAR:

2007.02.28 19:32:45 1999.03.23 06:45 BB GBPUSD,M15: 4-last_sar = 1. 6278 current_SAR = 1.6278
2007.02.28 19:32:45 1999.03.23 06:45 BB GBPUSD,M15: 3-last_sar = 1. 6279 current_SAR = 1.6278
2007.02.28 19:32:45 1999.03.23 06:45 BB GBPUSD,M15: ticket for OrderModify function must be an integer
2007.02.28 19:32:45 1999.03.23 06:45 BB GBPUSD,M15: 2-last_sar = 1. 6279 current_SAR = 1.6278
2007.02.28 19:32:45 1999.03.23 06:30 BB GBPUSD,M15: 4-last_sar = 1. 6279 current_SAR = 1.6279
2007.02.28 19:32:45 1999.03.23 06:30 BB GBPUSD,M15: 3-last_sar = 1. 6279 current_SAR = 1.6279
2007.02.28 19:32:45 1999.03.23 06:30 BB GBPUSD,M15: ticket for OrderModify function must be an integer
2007.02.28 19:32:45 1999.03.23 06:30 BB GBPUSD,M15: 2-last_sar = 1.6279 current_SAR = 1.6279
2007.02.28 19:32:45 1999.03.23 06:15 BB GBPUSD,M15: 4-last_sar = 1. 6279 current_SAR = 1.6279
2007.02.28 19:32:45 1999.03.23 06:15 BB GBPUSD,M15: 3-last_sar = 1. 628 current_SAR = 1.6279
2007.02.28 19:32:45 1999.03.23 06:15 BB GBPUSD,M15: ticket for OrderModify function must be an integer
2007.02.28 19:32:45 1999.03.23 06:15 BB GBPUSD,M15: 2-last_sar = 1. 628 current_SAR = 1.6279


 
That is quite an interesting problem. I found out why this happens:

2007.02.28 19:32:45 1999.03.23 06:15 BB GBPUSD,M15: 4-last_sar = 1. 6279 current_SAR = 1.6279

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   current_SAR = iSAR(NULL, 0, stepsize, af, 0); // already assigned as a variable so, this will just update the value.
  
//   Print("1-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   if (last_sar != current_SAR)
   {
   Print("2-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   mod_results = OrderModify(ticket,0,current_SAR,0,0,Red);
   Print("3-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   last_current_time = current_time;
   last_sar = current_SAR; // this will make last_sar the same value as current_SAR before printing
   Print("4-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   }
   last_sar = current_SAR;
//   Print("5-last_sar = ", last_sar, " current_SAR = ", current_SAR);
//----
   return(0);
  }
//+------------------------------------------------------------------+

this is what I get when I test your code:
2007.03.02 00:53:25 2007.01.02 12:05 test USDCHF,M5: 4-last_sar = 1. 2135 current_SAR = 1.2135
2007.03.02 00:53:25 2007.01.02 12:05 test USDCHF,M5: 3-last_sar = 1. 2136 current_SAR = 1.2135
2007.03.02 00:53:25 2007.01.02 12:05 test USDCHF,M5: ticket for OrderModify function must be an integer
2007.03.02 00:53:25 2007.01.02 12:05 test USDCHF,M5: 2-last_sar = 1. 2136 current_SAR = 1.2135

If you rem out the last_sar = current_SAR; just before the Print("4-last_sar = ", last_sar, " current_SAR = ", current_SAR); will not make the values equil until later. Also, you will see in my code, that I removed the double from infront of the current_SAR = iSAR(NULL, 0, stepsize, af, 0); Just in case re-creating the variable was triggering a glitch. Tell me how this tests for you.
 
Lord_Beast:
That is quite an interesting problem. I found out why this happens:

2007.02.28 19:32:45 1999.03.23 06:15 BB GBPUSD,M15: 4-last_sar = 1. 6279 current_SAR = 1.6279
Lord_Beast,
Well, I deleted one of the two lines that set last_sar = current_SAR. Then, I removed the double in front of the line that sets current_SAR. Then I added more debugging/
Here is what I ended up with:
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern double stepsize = 0.02;
extern double af = 0.2;
double last_sar;
double current_SAR;
double mod_results;
double last_current_time;
double ticket;
double current_time;
double diff;
int init()
  {
//----
last_sar = 5;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   current_SAR = iSAR(NULL, 0, stepsize, af, 0);
  
//   Print("1-last_sar = ", last_sar, " current_SAR = ", current_SAR);
   if (last_sar != current_SAR)
      {
      Print("2-last_sar = ", last_sar, " current_SAR = ", current_SAR);
      diff = current_SAR - last_sar;
      Print("3-current_SAR - last_sar = ", diff);
      //mod_results = OrderModify(ticket,0,current_SAR,0,0,Red);
      }
   last_current_time = current_time;
   last_sar = current_SAR;
//----
   return(0);
  }
//+------------------------------------------------------------------+
Which then gave me these unexplainable results:

I'm totally at a loss how this can happen. Most of them are correct and then there is an entry in the journal like above.

Thanks for your help.
 
I'm trying to figure out why it does that. All I can figure is that the values might be different past the 4 digits that it's showing. I tried to have it output like 5.12345 and I got 5.1234, so, that would mean it's testing to be not equal, but when it outputs it stops at 4 digits past the decimal point. I would think, well atleast that's the only reasonable explaination left. If you do get a duplicate in the calculations, does that cause an issue in your logic?
 
I can see how they could be different past four digits but if you look at the screenshot above, the example at 07:00 shows that the numbers are different but the calculated difference between them is 0 and it should be .0001. The one at 06:45 says .0001 is the difference but it should be 0. It's kinda like something is off by one.

I'm not sure I understand your question about the duplicate. What do you mean?
 
Thanks to your help Lord_Beast, I think I fixed it. I added this line:
current_sar2 = (MathRound(current_SAR * 10000)) / 10000;
which gets rid of any extraneous digits out past .xxxx

I need to test it some more but I think that is it.

Thanks for your help!

Bennett
Reason: