Stop loss not triggered timely


I am debugging an EA in strategy tester visual mode and found cases where stop loss (and also take profits( are not triggered timely.

As you can see in the attached screen capture:
  1- A sell order was executed on Jun 1srt, with a SL of  1.11285.   
  2- On the next day/candle,  the price  exceeded the SL value 
. 3- The price moved unfavourable during the next  two candles
 .4- The SL was triggered on the third.

I am using Metatrader 5 build 3550, Dec 16th 2022.

Any clues?

Thank you very much in advance.
MQL5SLTP.png  115 kb

Forum on trading, automated trading systems and testing trading strategies

My position stopped unjustly

Fernando Carreiro, 2023.01.11 00:43

I will repeat ...

    • Buy positions open on the Ask price and close at the Bid price.
    • Sell positions open on the Bid price and close at the Ask price.

    Charts only show Bid prices, not Ask prices.

    So ask yourself, and find out, what the Spread and Ask price were at the time that the stop-loss was hit.


    I add the EA log.

    20230119.log  34 kb


    Thank you very much for your answer. 

    Unfortunately, ASK prices are indeed greter than SL. I compiled a modified version which writes to the log, bid and ask prices of each tick (see attached image file). But in fact this is not neccesary as ASK PRICES SHOULD ALWAYS BE GRETER THAN BID ONES. So if bid prices which charts show are greter than the SL, ask prices also are.

    Am I wrong or missing something?

    MQL5SLTP2.png  150 kb

    As was mentioned above – the trades are closed on respective prices: sells are closed by Ask price and Buys are closed by Bid price. And charts are mostly displayed by Bid price - so the close price wont; be matching perfectly for Sell trades (it would be above the real chart price at that moment. You can see in the logs the prices are managed perfectly – sell is opened with SL 1.11285, and it’s closed a few days later on the same price (by Buy position).

    2020.06.01 00:00:00   CTrade::OrderSend: market sell 1.00 EURUSD sl: 1.11285 tp: 1.10285 [done at 1.11085]

    2020.06.08 00:00:00   stop loss triggered #18 sell 1 EURUSD 1.11085 sl: 1.11285 tp: 1.10285 [#19 buy 1 EURUSD at 1.11285]

    On the screenshot the arrow and trendline (trail of the position) seems to be moved earlier – but there might be possibility that on mq5 the objects on charts are not so precise – so just look to the prices. It’s important what is the real result not exactly how it look on the chart.


    I understand your point. But have you seen the last screenshot attached? I am  basing my argument on bid and ask prices received on each tick by OnTick procedure.

    And there you can see, that on 2020.06.02 the values are  bid=1.11349 ask=11374, both greter then SL (1.11285)

    Based on these values. SL should have been triggered on Jun 2nd, instead of Jun 8th, should't it?


    Please, anyone willing to help? 

    I may of course be wrong and overlooking something. But I think a picture (or two, from MT5)  is worth a thousand words.

    1- Buy Jan 30th  at 1,10107;    SL 1.09907).

    2- Modify SL on Feb 3rd ; new SL 1,10730

    3- The price goes sharply down (below horizontal line is SL).

    What am I missing or is wrong here?

    I apologize again if I am missing something obvious, but I think I am not.

    Thank you very much in advance.

    P.D. MT5 Jan 13th 2023, build 3559


    MT5-main.png  29 kb
    Omittted the seconf picture with the exact numbers
    MT5-DEBUG.png  51 kb

    I have to say the Metaquotes support sucks. No one will answered. I understand that this problem  might  be recurrent, and most of the time because a user has completely not taken all aspect into account. Anyway not answering at all is very rude, to say the least.

    I have further investigated the problem, and it seems to occur when using the modeling option "Open prices only" in the strategy tester. This is a simple way to verify the problem:

    1- Compiile the source code attached (I have also attached the compiled code). The EA simply opens a long position if there is no one, and waits for a SL.

    2- In strategy tester, choose EURUSD, Daily,  custom period 2020.03.10 yo 2020.03.31, and default EA inputs.

    3- Run the EA in strategy tester, with modeling options "1 minute OHLC" or "Every tick". Stop losses  do indeed trigger. No problem

    4- BUT, run it again with modelling option "Open prices only". Voila Not stop losses triggered. SL was 200 points, lost  8051 points in only one deal, after being stopped out, event tough 200 points SL was specified.

    I hope I will get and answer.

    Have a good day.

    P.D. Again, MT5 build 3559, Jan 13rd 2023.

    kk.mq5  6 kb
    kk.ex5  21 kb
    @Mario Adrián #: I have to say the Metaquotes support sucks. No one will answered. I understand that this problem  might  be recurrent, and most of the time because a user has completely not taken all aspect into account. Anyway not answering at all is very rude, to say the least.

    This is not "MetaQuotes support". This is a user forum, where normal users freely discuss things amongst themselves under no obligations or responsibilities.

    "Very rude" is you expecting that others are somehow under some kind of obligation to respond and answer your questions. We are not your slaves.

    If that is your attitude, then I suggest you hire someone. That way they will be obliged to give you an answer.



    My comment was not of course meant to to you or any other users who kindly try to answer questions such as mine.

    My complaint is, as written in my comment, to Metaquotes, for their lack of support. I really do not know if they read they forum or not, but I have not found any way to contact theiir support if there really is one for normal users.

    I apologize if my comment was understood as address to you or any other users. Quite on the contrary, I am very grateful to all  to you, as I see that you very often share your knowledge in this forum.

    Kind regards