Reputation: 367
This question is about binance FUTURES api (not spot exchange, but futures).
GOAL : have the same behaviour that the button "market" under "close positions" that closes a position.
NOTE : please don't reply the endpoit : DELETE /fapi/v1/allOpenOrders ==>> this is CANCELLING only orders NOT FILLED / (not opened) positions.
I want to CLOSE a actual OPENED position.
(don't forget that the buttons buy/long and sell/short are opening positions) in futures, sell is not the same as a sell in spot trading. in futures, sell actually open a position. to take profit we have to close (not cancel) the position.
I search all over forums and it's very hard to find a correct working answer to this.
** 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 those parameters, I always get an error NOT matter what I try
symbol=BTCUSDT&side=SELL&type=STOP_MARKET&closePosition=true
I get Stop price less than zero.
symbol=BTCUSDT&side=SELL&type=STOP_MARKET&closePosition=true&stopPrice=30895.00
I get Order’s position side does not match user’s setting.
symbol=BTCUSDT&side=SELL&type=STOP&closePosition=true&stopPrice=30895.00
ProfitTarget strategy invalid for orderType STOP
symbol=BTCUSDT&side=SELL&quantity=0.01&type=MARKET
Order's position side does not match user's setting.
symbol=BTCUSDT&side=SELL&type=MARKET&closePosition=true
Target strategy invalid for orderType MARKET,closePosition true
symbol=BTCUSDT&side=SELL&type=STOP&closePosition=true
Target strategy invalid for orderType STOP,closePosition true
symbol=BTCUSDT&side=SELL&type=STOP_MARKET&closePosition=true
Stop price less than zero.
symbol=BTCUSDT&side=SELL&type=STOP_MARKET&closePosition=true&stopPrice=30158.30
Order's position side does not match user's setting.
symbol=BTCUSDT&side=SELL&type=TAKE_PROFIT_MARKET&closePosition=true&stopPrice=30131.30
Order's position side does not match user's setting.
symbol=BTCUSDT&side=SELL&positionSide=SHORT&type=TAKE_PROFIT_MARKET&closePosition=true&stopPrice=30271.60
Combination of optional parameters invalid.
!!!!!!!!!! what’s wrong or what parameter I’m missing ???
it's a bit frustrating....
does anyone know the correct parameters ???
Upvotes: 2
Views: 7851
Reputation: 11
I was running into the same issue and worked out that it can be done without the need for a socket connection to monitor whether or not one of the TP/SL has been filled in order to cancel the other.
It does require three separate calls to the API to create each as the solved answer points out, but the parameters sent in the calls can be set such that one will cancel the other two if it executes.
For example, the SL being hit will cancel the original order and the TP and the TP being hit will cancel the original order and the SL, etc. Here are the parameters I used in each call to achieve this (with example values):
Original order -
symbol=BNBUSDT
side=BUY
positionSide=BOTH
type=MARKET
quantity=1
reduceOnly=false
SL Order -
symbol=BNBUSDT
side=SELL
positionSide=BOTH
type=STOP_MARKET
timeInForce= GTE_GTC
quantity=1
reduceOnly=true
stopPrice=(your stop price)
workingType= MARK_PRICE
TP Order -
symbol=BNBUSDT
side=SELL
positionSide=BOTH
type=TAKE_PROFIT_MARKET
timeInForce= GTE_GTC
quantity=1
reduceOnly=true
stopPrice=(your take profit price)
workingType= MARK_PRICE
Hopefully, this helps anyone else running into this issue
Upvotes: 1
Reputation: 367
I mixed the BUY SELL stuff
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
Upvotes: 4