Can't figure out why I'm getting an invalid request on my sell stop order.

 

Here's my code


const double myVol = 0.1;
ulong myTicket = 0;

MqlTradeRequest request = {};
MqlTradeResult result = {};

ulong SendTradeRequest() {
    if(OrderSend(request, result)) {
        return result.order;
    } else {
        Print("Error: ", GetLastError());
        return 0;
    }
}


void QuestionFunction() {
    ZeroMemory(request);
    ZeroMemory(result);
    request.action = TRADE_ACTION_PENDING;
    request.type = ORDER_TYPE_SELL_STOP;
    request.volume = myVol;
    request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID) - (200 * _Point);
    myTicket = SendTradeRequest();

}


void OnInit() {
   QuestionFunction();
}

/*
   OUTPUT IN JOURNAL TAB
   2023.10.19 09:48:40.088      Experts expert n7 (EURUSD,M1) loaded successfully
   2023.10.19 09:48:41.639      Trades  '75043970': failed sell stop 0.1  at 1.05217 [Invalid request]
   
   OUTPUT IN EXPERTS TAB
   Error: 4756

*/

But when I place such an order manually, i.e. a sell stop on EURUSD that is 0.002 below the the bid, it goes through and gets filled.

I'm using a MetaQuotes demo account.

Thank you.

 

So, changing the request to make it similar to the one made in the documentation ( https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions#trade_action_pending) made it work.


void QuestionFunction() {

    ZeroMemory(request);
    ZeroMemory(result);
    request.action = TRADE_ACTION_PENDING;
    request.symbol = Symbol();
    request.volume = myVol;
    request.deviation = 2;
    double price;
    double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
    int digits = SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
    request.type = ORDER_TYPE_SELL_STOP;
    price = SymbolInfoDouble(Symbol(), SYMBOL_BID) - 200 * point;
    request.price = NormalizeDouble(price,digits);
    
    myTicket = SendTradeRequest();

}


I don't really understand why though.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trade Operation Types - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
absconditus87 #:

So, changing the request to make it similar to the one made in the documentation ( https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions#trade_action_pending) made it work.



I don't really understand why though.

Because now it knows on which symbol to place the pending order.
 
Alain Verleyen #:
Because now it knows on which symbol to place the pending order.
Thank you. I didn't know it was necessary to specify the symbol if it's the symbol of the chart the EA is running on.
Reason: