Download MetaTrader 5

OrderClose Error 138

To add comments, please log in or register
cgidds
10
cgidds  

I have an EA that opens BUY and SELL orders with a stoploss and takeprofit based on certain conditions. However, when the moving average turns positive or negative over a certain number of bars, I want to close the order. When I do this, I keep getting error code 138 on the OrderClose. Does anyone know what I'm doing wrong?


if (is_position_open())

{

    RefreshRates();

    double ma_c, ma_p, delta;

        

    ma_c = iMA(NULL, 0 ,ma_period, 0, MODE_SMA, PRICE_CLOSE, shift-1);

    ma_p = iMA(NULL, 0, ma_period, 0, MODE_SMA, PRICE_CLOSE, shift+3);

    delta = (ma_c - ma_p);


    current_order        = OrderSelect(0, SELECT_BY_POS); 

    ticket_number        = OrderTicket(); 

    order_price          = OrderOpenPrice();


    switch(OrderType())

    {

        case OP_BUY:

            if (delta < 0)

            {

                // MA has decreased too fast; close the order

                if (!OrderClose(ticket_number, OrderLots(), Ask, slippage, Violet))

                {

                    Print("Error closing buy order from MA indicator: ", GetLastError());

                } else {

                    Print("MA decreasing too fast; Successfully closed buy order: ", ticket_number);

                }

            }

            break;

        case OP_SELL:

            if (delta > 0)

            {

                // MA has increased too fast; close the order

                if (!OrderClose(ticket_number, OrderLots(), Bid, slippage, Violet))

                {

                    Print("Error closing sell order from MA indicator: ", GetLastError());

                } else {

                    Print("MA increasing too fast; Successfully closed sell order: ", ticket_number);

                }

            }

            break;

        default:

            break;

    }

}

Alain Verleyen
Moderator
35039
Alain Verleyen  

Moved to right section.

whroeder1
17050
whroeder1  
cgidds: I keep getting error code 138 on the OrderClose. Does anyone know what I'm doing wrong?
  1. Post your MT4 question in the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2.        case OP_BUY:
    
                if (delta < 0){
                    // MA has decreased too fast; close the order
                    if (!OrderClose(ticket_number, OrderLots(), Ask, slippage, Violet))
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  3. You can not use any Trade Functions until you select an order. If you have, you can use OrderClosePrice() instead of Bid/Ask.
              www.mql5.com/en/forum/158890/page3#comment_3812636
cgidds
10
cgidds  
whroeder1:
  1. Post your MT4 question in the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  3. You can not use any Trade Functions until you select an order. If you have, you can use OrderClosePrice() instead of Bid/Ask.
              www.mql5.com/en/forum/158890/page3#comment_3812636

Wow, that was it (your #2). Thanks for the quick reply!

The point of me using the moving average in the first place is to close the order before it hits the SL when it's already on a downward trend (in a BUY scenario) and the opposite for a SELL scenario. Is there a better way to do this? I tried to look for the MODE_SPREAD in the link you sent, but I couldn't find it on the 4 pages of the forum topic. 

To add comments, please log in or register