"for" loop doesn't update

 

Hi, I have an open order counter, which works, and I want to know the open price for the last opened order... so I made something like this


string MyComB=CountOrdersBuy()+1; //this value is ok

//Then, I use "MyComB" as the comment for next BUY order 


//Orders Counter BUY <-- This works ok
int CountOrdersBuy()
{
int NumberBuy=0;
for(int f=OrdersTotal()-1; f>=0; f--)
{
if(OrderSelect(f,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol())
if(OrderType()==OP_BUY)
{
NumberBuy=NumberBuy+1;
}
}
return NumberBuy;
}


//Last Price BUY <-- This one, only returns open price the first time.
double LastPriceBuy()
{
double PriceBuy=0;
for(int g=OrdersTotal()-1; g>=0; g--)
{
if(OrderSelect(g,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol())
if(OrderType()==OP_BUY)
if(OrderComment()==CountOrdersBuy())
{
PriceBuy=OrderOpenPrice();
}
}
return PriceBuy;
}

So, "LastPriceBuy()" only gives correct number when I open the first order. On the second one, still shows first order price,

But if I make a new order with the wrong number in comment, it shows "0" so, it does check on "if(OrderComment()==CountOrdersBuy())".

Any idea? Thanks!

 
NikoSeneca: So, "LastPriceBuy()" only gives correct number when I open the first order. On the second one, still shows first order price,
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Of course, it does. You find the second order and save the price, you continue looping and find the first order and save that price. Why does this surprise you?

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Of course, it does. You find the second order and save the price, you continue looping and find the first order and save that price. Why does this surprise you?

1. Sorry for that, I didn't saw it properly.

2. It surprise me because when there are two orders, first order doesn't match with "CountOrdersBuy()". So why it returns that price?
 
NikoSeneca #: 2. first order doesn't match with "CountOrdersBuy()". So why it returns that price?
if(OrderComment()==CountOrdersBuy())
{
PriceBuy=OrderOpenPrice();
}
  1. You selected an order and read the comment.
  2. Then you selected all orders and returned the count.
  3. The OrderOpenPrice is now position zero, any symbol, any type.
 
William Roeder #:
  1. You selected an order and read the comment.
  2. Then you selected all orders and returned the count.
  3. The OrderOpenPrice is now position zero, any symbol, any type.

Fixed! Thanks!

double LastPriceBuy()
{
double PriceBuy=0;
for(int g=OrdersTotal()-1; g>=0; g--)
{
if(OrderSelect(g,SELECT_BY_POS,MODE_TRADES))
PriceBuy=OrderOpenPrice();
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderComment()==CountOrdersBuy())
return PriceBuy;
}
return 0;
}
 
NikoSeneca #: Fixed! Thanks!

But you still have an OrderSelect loop inside an OrderSelect loop; a potential problem (e.g. move the assignment next to the return, and you've broken it), and is n² slower to execute. Eliminate it and simplify your code.

int nBuy=CountOrdersBuy();
for(int g=OrdersTotal()-1; g>=0; g--) if(
   OrderSelect(g,SELECT_BY_POS)
&& OrderType()   == OP_BUY 
&& OrderComment()== nBuy
&& OrderSymbol() == _Symbol
){
   return OrderOpenPrice();
}