Cancel OrderSend after 5 sec of no response? MT4

najVilla  

Currently Im sending orders with a slippage as parameter, but when there is news or high volatility the advisor is not respecting it. Also in my strategy there I modify the SL in a certain point, and with the high volatility, again, it took a minute since I modify the order (minute that I spent without SL, and bring me big loss).

Here is a normal OrderSend

OrderSend(_Symbol, OP_SELL, loteSize, entryPrice, 10, stopLoss, takeProfit, "Sell", magicNumber);
And here the ModifyOrder
OrderModify(OrderTicket(),OrderOpenPrice(),optimalStopLoss,OrderTakeProfit(),0,Blue);

So to deal with that I thought in to make a setTimeOut (I don’t know if there is something like that in MQL4), if after 5 sec there is no answer I cancel "OrderSend" and for the "ModifyOrder" case I close the order (due I don’t have a SL).

It is a good idea?, can you help me to do it?. There is better ways to handle the high volatility?

I have a good internet and is not the problem.

Thank you.

William Roeder  
  1. OrderSend(_Symbol, OP_SELL, loteSize, entryPrice, 10, stopLoss, takeProfit, "Sell", magicNumber);

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. najVilla: if after 5 sec there is no answer I cancel "OrderSend" a

    There is no cancel. OrderSend will not return until it times out (~30 seconds).

  3. The microseconds the EA takes is irrelevant, compared to the 20-200 milliseconds the network takes, which is irrelevant compared to the minutes to do a trade because of the servers during news.

    OrderSend sends the request, network delivers, placed in the server queue. Once it reaches the top of the queue, you get filled or not, and the result delivered back to the terminal. Normally it is the 20-200 milliseconds the network takes to deliver is the limiting factor, but it can take minutes to do a trade because of the servers during news.

  4. If you get a no result, the order may or may not have opened on the server. You do not know. Log it, return and wait. After the terminal reconnects and you get a new tick you can see if it is open or you can retry.


    najVilla  
    William Roeder #:
    1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
                General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
      Next time, post in the correct place. The moderators will likely move this thread there soon.

    2. There is no cancel. OrderSend will not return until it times out (~30 seconds).

    3. The microseconds the EA takes is irrelevant, compared to the 20-200 milliseconds the network takes, which is irrelevant compared to the minutes to do a trade because of the servers during news.

      OrderSend sends the request, network delivers, placed in the server queue. Once it reaches the top of the queue, you get filled or not, and the result delivered back to the terminal. Normally it is the 20-200 milliseconds the network takes to deliver is the limiting factor, but it can take minutes to do a trade because of the servers during news.

    4. If you get a no result, the order may or may not have opened on the server. You do not know. Log it, return and wait. After the terminal reconnects and you get a new tick you can see if it is open or you can retry.


      1. Im sorry, Im new over here. I will try to move it by-myself.

      2. So how is the way to avoid this big delay that can take minutes on the servers during the news? Change to mt5 like Enrique Dangeroux says? (I design my expert advisor for mt4 and it would be a big deal to modify it for mt5)? or change a broker? (currently working with Axi that I think is a good one).

      Again my expert advisor change the SL after a certain point and for like 30 sec (like you said) I stayed without SL and bring me big loss... 

      Thank you for take the time to reply.

      William Roeder  
      najVilla #:

      1. Im sorry, Im new over here. I will try to move it by-myself.

      2. So how is the way to avoid this big delay that can take minutes on the servers during the news? Change to mt5 like Enrique Dangeroux says? (I design my expert advisor for mt4 and it would be a big deal to modify it for mt5)? or change a broker? (currently working with Axi that I think is a good one).

      Again my expert advisor change the SL after a certain point and for like 30 sec (like you said) I stayed without SL and bring me big loss...

      1. You can't move it. A Moderator already has.

        1. There is no avoid, unless you don't trade around news releases.
        2. Why do you think MT4 has a server delay but MT5 won't? It's the same broker's server.
        3. Why do you think other brokers won't have the same delay?

      2. Don't open an order without an SL. Like your OrderSend already does. Control your risk.

        Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

        1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

        2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

        3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                    MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                    Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                    Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

        4. You must normalize lots properly and check against min and max.

        5. You must also check Free Margin to avoid stop out

        6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

        Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.