OrderClose, error 129 (INVALID_PRICE) again

 

Hi, a bit of my code:

void CheckPositions(void)
{
    int total_orders = OrdersTotal();
    for (int c = total_orders - 1; c >= 0; c--) {
        if (OrderSelect(c, SELECT_BY_POS) == false) {
            Alert("Can't select order!");
        } else {
            bool is_pending = (OrderType() != OP_BUY && OrderType() != OP_SELL);
            [...]
            bool close_condition = ...
            [...]
            if (close_condition) {
                if (is_pending) {
                    if (OrderDelete(OrderTicket()) == false) {
                        Alert("Can't delete pending order!");
                    }
                } else {
                    RefreshRates();
                    double close_price = (OrderType() == OP_BUY ? Bid : Ask);
                    if (OrderClose(OrderTicket(), OrderLots(), close_price, 1000) == false) {
                        Alert("Can't close position! ", GetLastError());
                    }
                }
            }
        }
    }
}

So I'm setting slippage as 1000 because I want to close position immediately. The position is being closed eventually, but sometimes it takes few tries with 129 error. Any ideas what am I doing wrong ??

Regards

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. On ECN brokers, slippage is usually ignored.
  3. 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 forum
  4. You don't need close_price. If there are multiple orders to close call RefreshRates() after each close/delete (before the next OrderSelect() ) and then use OrderClosePrice() instead.
  5. 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).
 

Thank you for very useful information (so that's what magic number is used for ;)).

I disagree with 5. though: ' == false' is much easier to catch for eyes than '!', so I use it on purpose to improve code readability.

Thanks

 
#5) A matter of personal styles. No problem.
  1. You should be able to read the code out loud and have it make sense.
    "If orderDelete is false" begs the question why is it false, what does false mean, not did it delete the order.
     if (OrderDelete(OrderTicket()) == false) {
    "if couldn't delete order" is clearer, in my opinion.
     if (!OrderDelete(OrderTicket()) {
  2. likewise rename boolean variables so the code is readable.
    "if is pending" (order) sounds reasonable.
    bool is_pending = (OrderType() != OP_BUY && OrderType() != OP_SELL);
    if (is_pending) {
    Close condition sounds like the order has close (e.g. stop loss) not that it is time to cancel (delete or close,) or not have signal.
     if (close_condition)