it is buy trade exist?

 

Im just learning mql4, but I have a problem. I want to know if it a buy trade exist, but the function always returned true. Whats the problem?


bool vaneBuy(){

bool buy=false;

   for(int x=OrdersTotal();x>=1;x--){

   if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true);{

   if(OrderType()==OP_BUY){ buy=true;

   }

   }}

   return(buy);

}


 
sorry, if one sell trade exist, the function returned true (buy position exist) because the OrderType() is allways 0 (zero). So what is the problem?
 
phil10:

Im just learning mql4, but I have a problem. I want to know if it a buy trade exist, but the function always returned true. Whats the problem?


bool vaneBuy(){

bool buy=false;

   for(int x=OrdersTotal();x>=1;x--){

   if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true);{ 

   if(OrderType()==OP_BUY){ buy=true;

   }

   }}

   return(buy);

}


Hi,


You have a semicolon after the first if, remove it and it will work as you want

 

bool vaneBuy()

{

   for(int x=0;x<OrdersTotal();x++)

 if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))

   if(OrderType()==OP_BUY)

   return(true);

return(false) ;

}

 
phil10: Im just learning mql4, but I have a problem. I want to know if it a buy trade exist, but the function always returned true. Whats the problem?
   for(int x=OrdersTotal();x>=1;x--){

   if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true);{
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. If there are n orders, their positions are [0 … n-1]. You are looping [n … 1] so your first OrderSelect always fails and you never process the oldest order.

  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  5. bool isBuyOpen(){ return (bool)countOrderType(OP_BUY); }
    int countOrderType(int type){
       int n=0;
       for(int iPos=OrdersTotal(); iPos > 0;) if(
          OrderSelect(--iPos,SELECT_BY_POS) 
       && OrderType()        == type
    // && OrderMagicNumber() == gMagic
    // && OrderSymbol()      == _Symbol
       ){ ++n; }
       return n;
    }
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  6. You can count up or down, in this instance as it doesn't matter (down is trivially faster — one call to OrdersTotal.) But understand:
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().