Download MetaTrader 5
To add comments, please log in or register
Did you like the article? Share it with your friends!
qgmql
131
qgmql 2014.12.17 19:56 
int total;
    total  = OrdersTotal(); 

    for(cnt = 0; cnt < total; cnt++)
      {
        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
        if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) // Isn't it a less than, equal to sign?
          {
            if(OrderType() == OP_BUY)   // Why checking BUY while checking a SELL?
              {
                        OrderClose(OrderTicket(),Lots,Bid,3,Green);
                        return(0);
                      }
    
JC
1389
JC 2014.12.17 20:02  

It's quite common in MQL4 code. OP_BUY is zero; OP_SELL is 1; all pending orders such as OP_BUYLIMIT have IDs from 2 upwards. Therefore, OrderType() <= OP_SELL is a way of saying "if the selected ticket is an open position rather than a pending order..."

It's simpler, though less clear, than  (OrderType() == OP_BUY || OrderType() == OP_SELL) 

JC
1389
JC 2014.12.17 20:07  
qgmql:
( BTW, the code you are quoting is very bad. See the countless posts on this forum about looping from OrdersTotal()-1 downwards, rather than from zero upwards, when closing orders. )
whroeder1
13602
whroeder1 2014.12.18 17:54  
jjc: It's simpler, though less clear, than  (OrderType() == OP_BUY || OrderType() == OP_SELL)
Write self-documenting code and it's very clear.
bool OrderIsPending(void){ return OrderType() >= OP_BUYLIMIT; }
bool OrderIsOpen(void){    return !OrderIsPending();          }
qgmql: less than, equal to sign? Why checking BUY while checking a SELL?
  1. Less/equal - open order vs pending like jjc
  2. You have to check the order type if you want to specify the proper closing price (Bid/Ask.) Instead just use OCP
  3. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
    for(int pos = OrdersTotal() - 1; pos > 0; --pos) if(
       OrderSelect(pos, SELECT_BY_POS)
    && OrderIsOpen()
    && OrderSymbol() == Symbol()
    ){
       tbd OrderClose(OrderTicket(),Lots,OrderClosePrice(),3,Green);
    }
  4. Check your return codes (OrderSelect and OrderClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
JC
1389
JC 2014.12.18 18:16  
WHRoeder:

Surely...  

bool OrderIsPending(void){ return OrderType() >= OP_BUYLIMIT; }
bool OrderIsOpen(void){    return !OrderIsPending();             }
qgmql
131
qgmql 2014.12.23 16:45  
WHRoeder:
jjc: It's simpler, though less clear, than  (OrderType() == OP_BUY || OrderType() == OP_SELL)
Write self-documenting code and it's very clear.
qgmql: less than, equal to sign? Why checking BUY while checking a SELL?
  1. Less/equal - open order vs pending like jjc
  2. You have to check the order type if you want to specify the proper closing price (Bid/Ask.) Instead just use OCP
  3. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  4. Check your return codes (OrderSelect and OrderClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
"attached file" was given to me to check. if you please highlight the things, for which i must find topics in book. or this EA is having a bad code in all? (BTW its giving positive results to a trader, except one thing that it opens an other trade right after a trade is closed)
Files:
whroeder1
13602
whroeder1 2014.12.23 22:17  
  1.     total  = OrdersTotal(); 
        if(total < 1) 
    Means the EA is incompatible with every other, including itself on other charts and manual trading. order accounting - MQL4 forum
  2. for(cnt = 0; cnt < total; cnt++)
    You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
/
To add comments, please log in or register