Finding the price for market orders

If I placed a market order from RESTful API, how do I find out how much the filled price was?

For spot, let’s say place the market order via POST /api/v3/order, the return json will look something like this:

{
    "symbol": "BNBUSDT",
    "orderId": 168,
    "orderListId": -1,
    "clientOrderId": "xxxx",
    "transactTime": 1590967624991,
    "price": "0.00000000", // original price from client, market order
    "origQty": "11.00000000",
    "executedQty": "11.00000000",
    "cummulativeQuoteQty": "220.00000000",
    "status": "FILLED",
    "timeInForce": "GTC",
    "type": "MARKET",
    "side": "BUY",
    "fills": [
        {
            "price": "20.00000000", // filled price
            "qty": "1.00000000",
            "commission": "0.00000000",
            "commissionAsset": "BNB",
            "tradeId": 109
        },
        {
            "price": "20.00000000",
            "qty": "1.00000000",
            "commission": "0.00000000",
            "commissionAsset": "BNB",
            "tradeId": 110
        },
        {
            "price": "20.00000000",
            "qty": "9.00000000",
            "commission": "0.00000000",
            "commissionAsset": "BNB",
            "tradeId": 111
        }
    ]
}

The price in the fills from response data will give the matched price on each trades. The price may not be the same depending on the time of the fill.

This can also be found via the USER_DATA websocket, in the executionReport, there is a L field also returns the filled price.

{
    "e": "executionReport",
    "E": 1590967624994,
    "s": "BNBUSDT",
    "c": "2uD5fKxrgPpKxG3kxuuNNu",
    "S": "SELL",
    "o": "LIMIT",
    "f": "GTC",
    "q": "10.00000000",
    "p": "20.00000000",
    "P": "0.00000000",
    "F": "1.00000000",
    "g": -1,
    "C": "",
    "x": "TRADE",
    "X": "PARTIALLY_FILLED",
    "r": "NONE",
    "i": 166,
    "l": "1.00000000",
    "z": "1.00000000",
    "L": "20.00000000", // filled price
    "n": "0.00000000",
    "N": "USDT",
    "T": 1590967624991,
    "t": 110,
    "I": 424,
    "w": false,
    "m": true,
    "M": true,
    "O": 1590580465764,
    "Z": "20.00000000",
    "Y": "20.00000000",
    "Q": "0.00000000"
}

In futures, it’s a little different. In the endpoint POST /fapi/v1/order, for the parameter newOrderRespType:

  • if set to ACK, the API server will return the response immedicately after the order is “accepted” even it’s a market order. For example the response could be like:
{
    "orderId": 2413122813,
    "symbol": "BTCUSDT",
    "status": "NEW",
    "clientOrderId": "35wZAHMuvMSgmKKnTOA8Yy",
    "price": "0",
    "avgPrice": "0.00000",
    "origQty": "1",
    "executedQty": "0",
    "cumQty": "0",
    "cumQuote": "0",
    "timeInForce": "GTC",
    "type": "MARKET",
    "reduceOnly": false,
    "closePosition": false,
    "side": "BUY",
    "positionSide": "BOTH",
    "stopPrice": "0",
    "workingType": "CONTRACT_PRICE",
    "origType": "MARKET",
    "updateTime": 1590968491388
}

From the websocket connection, server will push 2 messages:


# first message
{
    "e": "ORDER_TRADE_UPDATE",
    "T": 1590968491388,
    "E": 1590968491390,
    "o": {
        "s": "BTCUSDT",
        "c": "35wZAHMuvMSgmKKnTOA8Yy",
        "S": "BUY",
        "o": "MARKET",
        "f": "GTC",
        "q": "1",
        "p": "0",
        "ap": "0",
        "sp": "0",
        "x": "NEW",
        "X": "NEW",
        "i": 2413122813,
        "l": "0",
        "z": "0",
        "L": "0", // it's 0 here
        "T": 1590968491388,
        "t": 0,
        "b": "0",
        "a": "0",
        "m": false,
        "R": false,
        "wt": "CONTRACT_PRICE",
        "ot": "MARKET",
        "ps": "BOTH",
        "cp": false
    }
}

# second one
{
    "e": "ORDER_TRADE_UPDATE",
    "T": 1590968491388,
    "E": 1590968491390,
    "o": {
        "s": "BTCUSDT",
        "c": "35wZAHMuvMSgmKKnTOA8Yy",
        "S": "BUY",
        "o": "MARKET",
        "f": "GTC",
        "q": "1",
        "p": "0",
        "ap": "9402.88000",
        "sp": "0",
        "x": "TRADE",
        "X": "FILLED",
        "i": 2413122813,
        "l": "1",
        "z": "1",
        "L": "9402.88", // the filled price
        "n": "3.76115199",
        "N": "USDT",
        "T": 1590968491388,
        "t": 133344561,
        "b": "0",
        "a": "0",
        "m": false,
        "R": false,
        "wt": "CONTRACT_PRICE",
        "ot": "MARKET",
        "ps": "BOTH",
        "cp": false
    }
}

The second message with FILLED status tells the matching price.

  • if set to RESULT, this endpoint acts similar like the spot one. It returns the FILLED status with avgPrice instantly. e.g.
{
    "orderId": 2413175154,
    "symbol": "BTCUSDT",
    "status": "FILLED",
    "clientOrderId": "Lp12Nq0OoBYTF7zyqZGl3n",
    "price": "0",
    "avgPrice": "9452.71000", // avg filled price
    "origQty": "1",
    "executedQty": "1",
    "cumQty": "1",
    "cumQuote": "9452.71000",
    "timeInForce": "GTC",
    "type": "MARKET",
    "reduceOnly": false,
    "closePosition": false,
    "side": "BUY",
    "positionSide": "BOTH",
    "stopPrice": "0",
    "workingType": "CONTRACT_PRICE",
    "origType": "MARKET",
    "updateTime": 1590969041003
}

and there will be only one message from websocket connection:

{
    "e": "ORDER_TRADE_UPDATE",
    "T": 1590969041003,
    "E": 1590969041006,
    "o": {
        "s": "BTCUSDT",
        "c": "Lp12Nq0OoBYTF7zyqZGl3n",
        "S": "BUY",
        "o": "MARKET",
        "f": "GTC",
        "q": "1",
        "p": "0",
        "ap": "9452.71000", // avg price
        "sp": "0",
        "x": "TRADE",
        "X": "FILLED",
        "i": 2413175154,
        "l": "1",
        "z": "1",
        "L": "9452.71", // last filled price
        "n": "3.78108399",
        "N": "USDT",
        "T": 1590969041003,
        "t": 133350643,
        "b": "0",
        "a": "0",
        "m": false,
        "R": false,
        "wt": "CONTRACT_PRICE",
        "ot": "MARKET",
        "ps": "BOTH",
        "cp": false
    }
}

Hi, can someone please note what the fields represent above? what does “wt”, “ps”, “cp”, etc. mean?

TIA

The approach in the first 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 find 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?

2 Likes

‘wt’ - Stop Price Working Type (only for conditional orders)
‘ps’ - Position side
‘cp’ - closePosition (only works with stop_market/take_profit_market)

Please read https://binance-docs.github.io/apidocs/futures/en/#event-order-update for more details

Hi and thanks so much for the response. :slightly_smiling_face:

I was looking at the documentation here (just as you said to do):

1, I am trying to record changes to the Position. To do this, should I only log changes that have a pa > 0 for the positions listed under “P”? For example, down below (in the record listing), there are 3 entries listed under “P”. Only one of them has a pa> 0 Why are these other 2 records (under “P”) listed even when they have amounts = 0? What is the purpose of the fields “cr”?
2. I am also trying to determine when the position is closed. What rules can I follow to make sure I do this correctly?

{'E': 1609378092165,
 'T': 1609378092162,
 'a': {'B': [{'a': 'USDT', 'cw': '9982.90119401', 'wb': '9999.96580239'}],
       'P': [{'cr': '43.59957000',       <----- what does this mean
              'ep': '0.00000',
              'iw': '0',
              'ma': 'USDT',
              'mt': 'isolated',
              'pa': '0',   <--- why is this record listed in the results under "P"
              'ps': 'BOTH',
              's': 'BTCUSDT',
              'up': '0'},
             {'cr': '780.48126002',
              'ep': '28498.01000',
              'iw': '17.06460838',
              'ma': 'USDT',
              'mt': 'isolated',
              'pa': '0.003',
              'ps': 'LONG',
              's': 'BTCUSDT',
              'up': '0.10569000'},
             {'cr': '-673.46186999',   <---- what does this mean
              'ep': '0.00000',
              'iw': '0',
              'ma': 'USDT',
              'mt': 'isolated',
              'pa': '0',      <---- why is this record listed in the results under "P"
              'ps': 'SHORT',
              's': 'BTCUSDT',
              'up': '0'}],
       'm': 'ORDER'},
 'e': 'ACCOUNT_UPDATE'}

Please open a separate topic for your questions. This one is a Q&A for market order executing price

OK - thanks - will do so