Futures API how to close at market price (like the "market" button uder the section "close")

Hi,

I just simply want to CLOSE a opened position at market price without extra features…
Example in the futures website portion : there is a section “close” and a button “market”
when we hit the market button => it instantly close the position no matter what…

I want the EXCAT behaviour that this market close button via the API.

** I can OPEN a position at market price with this:
symbol=BTCUSDT&side=SELL&positionSide=SHORT&type=MARKET&quantity=0.01

** But when I try to close it with this

without stopPrice:
symbol=BTCUSDT&side=SELL&type=STOP_MARKET&closePosition=true
I get Stop price less than zero.

with stopPrice :
symbol=BTCUSDT&side=SELL&type=STOP_MARKET&closePosition=true&stopPrice=30895.00
I get Order’s position side does not match user’s setting.

different type:
symbol=BTCUSDT&side=SELL&type=STOP&closePosition=true&stopPrice=30895.00
ProfitTarget strategy invalid for orderType STOP

what’s wrong or what parameter I’m missing ???

After discussion with support I found out i mixed the BUY, SELL …

OPEN SHORT
symbol=BTCUSDT&side=SELL&positionSide=SHORT&type=MARKET&quantity=0.01

CLOSE SHORT
symbol=BTCUSDT&side=BUY&positionSide=SHORT&type=MARKET&quantity=0.01

OPEN LONG
symbol=BTCUSDT&side=BUY&positionSide=LONG&type=MARKET&quantity=0.01

CLOSE LONG
symbol=BTCUSDT&side=SELL&positionSide=LONG&type=MARKET&quantity=0.01

The issue with this workaround, is 1. if the quantity is larger than the max quantity i get an errror from binance (-4005) and 2. if the quantity is lower than the min order quantity i get also an error (-1013 or -4164) anyhow better would be the funktion as requested would work, but it does not.
Any idea what we can do when we want to use the app “close” funktionality?

what is the exactly error did you receive?

How do you mean? This are the exactly errors. Is there any approach to close a future position (trade) without setting the quantity and side? As in the app, just push close by market and the complete position will be closed, no matter the size of position.

When you have an open position normally you close that position by placing a new order of opposite direction. For example, if you have an open position with the side ‘BUY’, you can close that by placing a new order with the side ‘SELL’. Params may vary to close an opposite position depending on your current open position.
Following is the situation that works for me
Open Position Params

    "side": "SELL",
    "positionSide": "BOTH",
    "symbol": "BTCUSDT",
    "type": "LIMIT",
    "timeInForce": "GTC",
    "quantity": "0.003",
    "price": "42342.0",
    "reduceOnly": "False"
  }

Close Position Params

{
  "symbol": "BTCUSDT",
  "type": "MARKET",
  "side": "BUY",
  "quantity": 0.003,
  "positionSide": "BOTH",
  "reduceOnly": true,
  "newOrderRespType": "RESULT"
}

I will recommend using the inspect element → network tab
if you have an open position and close that by clicking the market button you will see a place_order request. Open the payload and see all the parameters required to close the current open position. You might see some useless params but most of the parameters are useful. I have attached the image of my network tab that might be helpful. I have closed an open position of side ‘SELL’.

Here are the best steps:

  1. Get Open positions of symbol. Endpoint “/fapi/v2/positionRisk” Binance API Documentation
  2. itterate until you find your symbol
  3. get positionAmt as float
  4. If it’s less than zero BUY same qty in the market
  5. If it’s more than zero SELL same qty in the market

Your position will be closed in market!