I use this fuction : exchange.create_order(t[0][1], 'market', 'sell', 1, params={'reduceOnly': True}) can close positon on FTX, but I don't find the right way to deal on OKX exchange. Could you please let me know how to close postion on OKX?
Asked
Active
Viewed 992 times
1
-
Please edit your question to include a code block and/or error message in the body of question. [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting). Also what type of product are you trading? Futures? Margin? Spot? Please edit question to include as much code as possible in a code block. – Alex B May 25 '22 at 11:07
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 25 '22 at 12:23
-
I used this function :exchange.create_order(‘XRP-USDT’, "limit", "long", 1, 0.41, {"tdMode": "cross", "side": "sell", "ccy": "USDT"}) created a Perpetrual contract on OKX, but I don't know how close this position. – Kim May 25 '22 at 14:08
1 Answers
4
OKX requires some specific parameters in order to open or close a position. Please check out this simple example:
Opening a long position:
symbol = "LTC/USDT:USDT"
side = 'buy'
type = 'market'
amount = 1
price = None
exchange_params = {
'tdMode': 'cross' # margin: is required to be either "isolated" or "cross",
'posSide': 'long' # direction either long or short
}
open_position = exchange.create_order(symbol, type, side, amount, price, exchange_params)
After executing the snippet above a position in that market will be opened, if we want to close it we just need to issue an order in the opposite direction, something like this:
side = 'sell' # opposite side
exchange_params = {
'tdMode': 'cross' # margin: is required to be either "isolated" or "cross",
'posSide': 'long' # opposite direction now
}
# symbol, amount, type remain the same in this case
close_position = exchange.create_order(symbol, type, side, amount, price, exchange_params)
Let me know if you need any other clarification!

Carlos Gonçalves
- 86
- 1
- 9
-
2You can also do away with the `posSide` parameter by changing from `long/short mode` to `net mode`. In `net mode`, users can only have positions in one direction; In `long/short mode`, users can hold positions in long and short directions. [Set position mode](https://www.okx.com/docs-v5/en/#rest-api-account-get-account-configuration) – Alex B May 26 '22 at 20:25
-
Carlos, can you give me a hand (consegue me ajudar)? https://stackoverflow.com/questions/74690975/how-to-close-a-margin-order-in-okx-using-ccxt – Piero Costa Dec 05 '22 at 18:31
-
I'm having more or less the same issue, I can't find the way to close a margin market order. – Piero Costa Dec 05 '22 at 18:31