Market Order consuming book to fill the total volume

 

Hello!

What is the best way to send a market order and ensure that the order is done successfully, even if order volume needs to consume several levels of price of the book?

I am using orders ORDER_TYPE_BUY/ORDER_TYPE_SELL, action TRADE_ACTION_DEAL and filling ORDER_FILLING_FOK, because I want to ensure that the whole volume be dealt, and not only partial.

What happens when the liquidity is not high is that, for example, if first ask price is $80,00 and has only 1 volume and after that ask price $80,01 have more 5 volume on the book. If I send a market buy order, as I described, of 2 volume, MT5 executes the order but it is canceled, because there is no 2 volume available on first level of price, and I chose filling FOK (Fill or Kill). And market orders don't accept input price. In the code it accepts but in the documentation it says that if price is informed, it will be ignored for market orders.

But the general concept of market order is to deal the requested volume at all needed price levels, which in this case would be at $80,00 and $80,01.

How do I do for the market order consume all needed price levels of the book to liquidate all order volume, instead of cancelling the order due to lack of volume on first level?

Thanks

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
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...