do while loop using &&

 

I am building an EA program but I am having an unexpected result.


I want to make sure the market is going in the right direction.  So I want to check the price every 1/2 second.   The problem I have is that the code accepts the same price as true.  for some reason  the "greater than" operator (<) actually means "greater than or equal to"  (<=) .  which is not standard to C++.    I am either going nuts or MQL5 is not the same as C++.

      do
      {
         //do something
      }
       while((price1>price2);


this example above SHOULD WORK just fine....but it does not.  I tried a work around and add a != to the while statement so it looks like       

my while statement looks like this

     do {
          //do something
       }
      while((price1>price2) && (price1!=price2));  


want to eliminate this by adding a not equal statement.   The code is not doing what I want it to do.


Here is my actual code,,,,with lots of detail.


   if (Marketdirection == true)         // check to see if I selected a buy direction or a sell direction
   {
      double price1;
      double price2;
      string s_price1;
      string s_price2;
      do {
         price1 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);                                  //get the current price
         s_price1= DoubleToString(price1);
         PrintFormat("     Price1: %s",s_price1);
         Sleep(500);                                                                                             // wait 1/2 second
         price2 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);                                  //get the current price
         s_price2= DoubleToString(price2);
         PrintFormat("     Price2: %s",s_price2);
      }
      while((price1>price2) && (price1!=price2));                                               //spin if the first price is greater then or equal to the second price   (sell direction)
      PrintFormat("    BUY price direction: Price: %s < %s",s_price1, s_price2);
      }


The output of the code is as follows:

    Price1: 130.5930000

    Price2: 130.5930000

BUY price direction: Price: 130.5930000 < 130.5930000               <---  this is not right....it should just keep spinning!


If the two prices are different by 1 pip then it would work just fine....but the code exits the do while loop too soon.  Once the two number equal it exits.

the greater than  (>)  should not be accepting equal numbers....but it is.

In the while statement I attempted to fix this by adding the not equal statement but this has not worked.....

How can I do a greater than operation but not include the equal too?     

Is this is a bug in the C++ version used on MQL5?     It is very frustrating.


Crazy issue.....Thanks for your help.

Chris

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Chris PinterSo I want to check the price every 1/2 second. 
Nothing is changing. Return from OnTick and wait for something to change.
 
Maybe you should consider casting to an integer if comparing the price values.

Like this:
int iPrice = dPrice * _Point;

This way you get rid of any glitches by double values.

Else maybe you should try to print out the values and see if they are actually the same.

I've published a project in cloud storage, that might help you with the debugging.


 
William Roeder #:
Nothing is changing. Return from OnTick and wait for something to change.

What!?    This is about the while loop.....it does not work.  


Any comments on the issue at hand?.....about the "greater than " operator?

 
Chris Pinter #:

What!?    This is about the while loop.....it does not work.  


Any comments on the issue at hand?.....about the "greater than " operator?


See my post!

 

@Chris Pinter

Try to code it like this:

   do
     {
      price1 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);                             
      Sleep(500);                                                                
      price2 = SymbolInfoDouble(_Symbol,SYMBOL_ASK); 
     }
   while(price1>=price2);  
   PrintFormat("    BUY price direction: Price: %f < %f",price1,price2);
 
Chris Pinter:

I am building an EA program but I am having an unexpected result.
I want to make sure the market is going in the right direction. 
So I want to check the price every 1/2 second.  


The problem I have is that the code accepts the same price as true.

....

Crazy issue.....Thanks for your help.
Chris


Hello Chris,

What results can I expect/get from your code :

 if (Marketdirection == true)         // check to see if I selected a buy direction or a sell direction
   {
      double price1;
      double price2;
      string s_price1;
      string s_price2;
      do {
         price1 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    //get the current price
         s_price1= DoubleToString(price1);
         PrintFormat("     Price1: %s",s_price1);
         Sleep(500);                                       // wait 1/2 second
         price2 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    //get the current price
         s_price2= DoubleToString(price2);
         PrintFormat("     Price2: %s",s_price2);
      }
      while((price1>price2) && (price1!=price2));          //spin if the first price is greater then or equal to the second price   (sell direction)
      PrintFormat("    BUY price direction: Price: %s < %s",s_price1, s_price2);
      }

Steps of the process in words:

  1. Compare the number changes in every half second.
  2. At the beginning of the loop process, price1 contains the current price, and price2 is empty (or can be assumed as 0).
  3. Then in the next half second, price2 contains the latest current price.
  4. The expression "while" requires that
    4.1. as long as price1 > price2
    4.2 and price2 are not equal to price2,
    -- then for sure the loop will continue.
  5. When price1 is no longer greater than price2 or contains the same price, or price1 is less than price2,
    -- then the loop ends.
  6. After the loop ends, your EA will always write the result via PrintFormat() for 2 variables containing:
    6.1. the same price
    6.2 or price1 is less than price2 
  7. return to step 1

So, the final result after the "while" ends must be:
-- price1 is equal or less than price2.

Result as text " BUY price direction: Price: 130.5930000 < 130.5930000" is correct.

Q:

How can I do a greater than operation but not include the equal too?     

Is this is a bug in the C++ version used on MQL5?     It is very frustrating.

A: No, I am sure that's not a bug :)

- - -

If you expect the EA to do something about the price changes between price1 & price2 through the "while" loop,
-- I'll place the order here:

      do {
         price1 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    
         s_price1= DoubleToString(price1);
         PrintFormat("     Price1: %s",s_price1);
         Sleep(500);                                       
         price2 = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    
         s_price2= DoubleToString(price2);
         PrintFormat("     Price2: %s",s_price2);
         
         //-- 
         if(price1>price2)
         {
           //-- do something
         } else
         if(price1<price2)
         {
           //-- do something
         } 
      }
      while(price1!=price2 && !IsStopped());

Kind regards,
Yohana

PS. 
I apologize if I misunderstood your needs.

 
Chris Pinter #:

What!?    This is about the while loop.....it does not work.  


Any comments on the issue at hand?.....about the "greater than " operator?

William is correct. 
You have missed his point
When the price changes OnTick will be called again so your while loop is not required and will actually mean you may miss ticks or be processing them after the event

 

Thanks everyone,

That makes total sense now you explained it to me.   I will rethink and try your suggestion.  :)

Reason: