error with negative price as stop loss on BTCUSD chart

 

On MT5 I have an EA running on a BTCUSD chart.

When a new market or stoplimit order is opened by the user, the EA is supposed to automatically add SL and TP.

All good in general but on BTCUSD it may happen that when opening a sellstop or limitsell order that it's TP goes into the negtive price section of the chart.

and this always gives me a return code of 10016, which stands for invalid stops according to doc, and the modification of the order is not successful.

I added the caluclated SL and TP as horizontal lines to the order and they are technically correct. Both sit at the expected positions. 

It seems it is actually the negative price that makes the SL or TP invalid. On MT4 this is no issue at all, just on MT5.

the code for the actual modification is standard and works fine as long as SL/TP are positive.

            if (sl!=OrderGetDouble(ORDER_SL)) {
              if (!trade.OrderModify(ticket, priceOpen, sl, tp, ORDER_TIME_GTC, 0, 0)) {
                Comment("modify result: " + trade.ResultRetcode()); // this displays 10016 wehn SL or TP is negative
                errorTicket = ticketAsString;
                errorTitle = "ORDER: modification error ";
                errorMsg = "Ticket " + IntegerToString(ticket) +
                            "\n@ " +DoubleToString(priceOpen, Digits()) +            // correct stoplimit price as displayed in MT5's order list
                            "\nSL "+DoubleToString(sl, Digits()) +                   // correctly calculated SL    
                            "\nTP "+DoubleToString(tp, Digits()) + "\n\n";           // correctly calculated TP
                errorUtils.BuildErrorMessage(errorMsg, errorTitle, calcType);             
              }
            }

I tried afterwards to enter SL/TP manually for the order, through MT5's interface but there as well, a ngetive SL or TP is not allowed.

Sure, not so many charts have negative price areas. still ... is this a bug or am I missing something?

 

... add on ...

I just realized, if the price is too far in the positive area of the chart it throws as well an error: 10013 which is "invalid request" according to doc.

So I guess I need to limit possible SL/TPs to something like the price min/max of maybe the last 6 months or so.

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5