Strategy Tester version 5.0 build 4040. Errors of limit orders processing.

 
Strategy Tester version 5.0 build 4040. Errors of limit orders processing.

Testing mode all ticks, market ticks. The problem is as follows. My Expert Advisor places a limit order to sell or buy. I found that the orders are not triggering correctly. The orders are triggered when the bid crosses in the case of SellLimit and ask crosses in the case of BuyLimit. In some cases this is correct for small spreads. Further, when going tick by tick, I found out that there is a situation when the current value of Last is above SellLimit or below BuyLimit. This means that a market order, which my pending order was waiting for, has passed in the moment. We are not talking about execution priority or volume here. Only the fact that Last was above or below the limit order.  And orders at such moments did not work. Accordingly, my Expert Advisor does not give correct results when testing. I went further. I wrote a test in which I simply set a limit order to sell and immediately launched my market order to buy above the market price with a large volume. But my market buy order worked. But the limit sell order did not work. This is another question.  For myself, I found a way out of the situation. I wrote a section for the tester. I check if Last is above or below my limit order. If yes, I immediately throw a market order into the stack, and delete the limit order. The order triggers at the Bid price for Sell or Ask price for Buy.  The difference between the price of the limit order and the market order I record in the deposit. It is good that the strategy tester allows to do it in the Expert Advisor. Thus, I brought the result closer to the results on the real market.  Also, with the help of the TesterDeposit(); function, it is possible to record unaccounted broker's and stock exchange's commission in the tester.  Please, ask the developers to correct the processing of limit orders in the strategy tester. Since at large spreads they are processed incorrectly, which leads to unreliable testing on real tick data.

My sample code that solves this 

Translated with www.DeepL.com/Translator (free version)

     if(MQLInfoInteger(MQL_TESTER) == 1 || MQLInfoInteger(MQL_OPTIMIZATION) == 1)
        {
         // Check if the flipper is higher than our order.
         if(last_f_tick.last > OrderGetDouble(ORDER_PRICE_OPEN) && prevmymqltic.last <= OrderGetDouble(ORDER_PRICE_OPEN))
           {
            Print("Missed trade ",last_f_tick.last," ",OrderGetDouble(ORDER_PRICE_OPEN));
            if(!b_trade.Sell(1,futuer,OrderGetDouble(ORDER_PRICE_OPEN),0,0,DoubleToString(OrderGetDouble(ORDER_PRICE_OPEN),b_symbol.Digits())))
              {
               uint _result=a_trade.ResultRetcode();
               Print(__FUNCTION__+"no sell order set. Volume ",f_volume," Result=",_result);
               buysellrunning = false;
               return;
              }
            PrevSellOrderTicket = b_trade.ResultOrder();
            if(!a_trade.Buy(1,_Symbol,0,0,0,0,""))
              {
               uint _result=a_trade.ResultRetcode();
               Print(__FUNCTION__+"no Buy order is set. Volume ",b_volume," Result=",_result);
               buysellrunning = false;
               return;
              }
            PrevBuyOrderTicket = a_trade.ResultOrder();
            double diff = ((OrderGetDouble(ORDER_PRICE_OPEN)- last_f_tick.bid)/a_symbol.TickSize())*a_symbol.TickValue();
            TesterDeposit(diff);
            if(!b_trade.OrderDelete(PrevLimOrderTicket))
              {
               uint _result=a_trade.ResultRetcode();
               Print(__FUNCTION__+"limit order for sale has not been deleted. Volume ",1," Result=",_result);
               buysellrunning = false;
               return;
              }
            buysellrunning = false;
            prevmymqltic = last_f_tick;
            return;
           }
        }


Files:
Reason: