How to get the price of a market order that is already filled?

The approach in this post is perfect to get the price of a spot market order at the moment it is placed via the API.

Yet, it is sometimes required to retrieve this price for an existing market order, after the fact. There are several motivations for that:

  • The order might have been placed by a different process (manually via the web application, by a different application, …)
  • An application might crash after it sent the order via the REST API, but before it could get the response. At next start, the application might thus need to query the price for the order it sent before crashing. (Crashing might be due to a power or Internet outage, so better programmer discipline cannot eliminate this problem).

As far as we tested the query order endpoint:

  • it always returns 0 for the price field of market orders (even after they filled).
  • it does not accept the newOrderRespType = FULL parameter, and it does not return the fills by default.

Is there currently a way to find the price of filled market order via the REST API?

Hi. You can still query the order endpoint (the one you checked before) to know it. There is a parameter called cummulativeQuoteQty. This is the total quote quantity you spent. By dividing it with executedQty (another parameter in the return object), you can get the price. If you don’t want to calculate it, you can use this endpoint GET /api/v3/myTrades if you know the information about the corresponding trades. The returned data does not show the order type but the price value is the actual number instead of 0.

1 Like

Thank you very much, the division approach is very convenient! Yet I might also need to retrieve the fees, sadly I don’t think they are available outside of the trades list.

I suppose I could get them from the GET /api/v3/myTrades, yet I am not sure how to list all the trades corresponding to a given order, and only this order? Should I recursively retrieve all the trades for the account, and locally filter on the orderId? (I worry this could become very expensive when there has been a huge number of trades on the account.)

The approach you mentioned is a valid workaround but as you said, it may involve multiple API calls.
Another method is, you can call this endpoint GET /sapi/v1/asset/tradeFee( ) to get the commission rate and calculate it by yourself.
E.g. Assume that there is a maker order, buying 0.01BTC with USDT. The symbol is BTCUSDT, commission rate is 0.001, and the commission asset is BTC. So the commission fee is commission asset quantity * commission rate = 0.01 * 0.001 = 0.00001BTC

But, of course, the simplest approach is to either get the returned data when placing order or subscribe the user data stream.

1 Like