Forex Factory (https://www.forexfactory.com/forum.php)
-   Platform Tech (https://www.forexfactory.com/forumdisplay.php?f=69)
-   -   Function not finding lowest BuyPosition - why? (https://www.forexfactory.com/showthread.php?t=467534)

MikeFT Jan 21, 2014 7:31am | Post# 1

Function not finding lowest BuyPosition - why?
 
Hey folks.

I must have made a very stupid mistake, but I can not figure it out on my own.
I did make myself a function to find the Highest/Lowest Buy/Sell Orders.
Everything works fine, but the value for the "LowestBuy". LowestBuy always returns same value as HighestBuy. WHY?
This is driving me nuts!

Inserted Code
//Global variables needed:
/*
double            HighestBuy = 0;
double            LowestBuy = 0;
double            HighestSell = 0;
double            LowestSell = 0;
*/
void GetOpenOrderPriceForMostRecent ()
   {
   HighestBuy = 0;
   LowestBuy = 0;
   HighestSell = 0;
   LowestSell = 0;
   int               TotalOrders;
   TotalOrders = OrdersTotal();
   for(int iii=TotalOrders-1;iii>=0;iii--)
      {
      OrderSelect(iii, SELECT_BY_POS);
      if(OrderSymbol() != Symbol()) continue;
      if(OrderType() == OP_BUY && OrderOpenPrice() > HighestBuy)
         {
         HighestBuy = OrderOpenPrice();
         }
      if(OrderType() == OP_BUY && OrderOpenPrice() < LowestBuy || OrderType() == OP_BUY && LowestBuy == 0)
         {
         LowestBuy = OrderOpenPrice();
         }
      if (OrderType() == OP_SELL && OrderOpenPrice() > HighestSell)
         {
         HighestSell = OrderOpenPrice();
         }
      if (OrderType() == OP_SELL && OrderOpenPrice() < LowestSell  || OrderType() == OP_SELL && LowestSell == 0)
         {
         LowestSell = OrderOpenPrice();
         }
      }
   }

AimHigher Jan 21, 2014 7:59am | Post# 2

Change this

if(OrderType() == OP_BUY && OrderOpenPrice() < LowestBuy || OrderType() == OP_BUY && LowestBuy == 0)

to

if(OrderType() == OP_BUY && (OrderOpenPrice() < LowestBuy || OrderType() == OP_BUY && LowestBuy == 0))

and vice versa for sell orders.

MikeFT Jan 21, 2014 8:10am | Post# 3

Thank you! But that's not working!
Still, your idea seemed to make sense in some ways.

This is doing the trick:
Inserted Code
if(OrderType() == OP_BUY && (OrderOpenPrice() < LowestBuy || LowestBuy == 0))

The question now is: Why was the original code forfing for LowestSell, but not for LowestBuy? It's not making sense, even if the problem is solved...

AimHigher Jan 21, 2014 11:16am | Post# 4

Sorry, I don't quite understand.
What is and what isn't working?
What does each variable return and what do you want them to return?

AimHigher Jan 21, 2014 11:38am | Post# 5

Sorry, my bad. Missed a bracket.

Try this, it worked for me when I tested it.

if(OrderType() == OP_BUY && (OrderOpenPrice() < LowestBuy || (OrderType() == OP_BUY && LowestBuy == 0)))

and same for sell orders

if (OrderType() == OP_SELL && (OrderOpenPrice() < LowestSell || (OrderType() == OP_SELL && LowestSell == 0)))

MikeFT Jan 23, 2014 1:36pm | Post# 6

Sorry for not being clear.

My original code did give expected results for LowestSell, but not for LowestBuy.

Your suggestions with the brackets works with both!
I still am wondering, why "LowestBuy" was not working as I've written the code, but "LowestSell" was. I'd like to avoid such problems in the future!

Thank you for your help!

Gumrai Jan 24, 2014 5:42am | Post# 7

Sorry for not being clear. My original code did give expected results for LowestSell, but not for LowestBuy. Your suggestions with the brackets works with both! I still am wondering, why "LowestBuy" was not working as I've written the code, but "LowestSell" was. I'd like to avoid such problems in the future! Thank you for your help!
Are you sure that LowestSell was working as expected?
I believe that your problem was related to the precedence rules and to be honest, I always have difficulties with precedence when using && and ||.
http://docs.mql4.com/basis/operations/rules
That is why I will always use brackets, just to be sure that I will get the expected result.
I understand precedence quite well with multiplication, adding and subtracting.
Eg.
Inserted Code
      int number = 2*2 + 2*2+1;
      Alert(number);
will result in 9 because the +1 is calculated last
Inserted Code
      int number = 2*2 + 2*(2+1);
      Alert(number);
will result in 10 because the brackets ensure that (2+1) is given precedence

This suggest to me that in your code
Inserted Code
[color][font] if(OrderType() == OP_BUY && OrderOpenPrice() < LowestBuy || OrderType() == OP_BUY && LowestBuy == 0)[/font][/color]
that && LowestBuy == 0 is left out on its own, so is always part of the if condition. In other words the if condition will only be met if LowestBuy == 0
Best to be safe and get into the habit of always using brackets.

MikeFT Jan 29, 2014 5:21am | Post# 8

Thank you.

That makes sense! Will keep this in mind for the future.


© Forex Factory