No intuitive and easy way to close a position (as available in the dydx UI)
I am having trouble closing an existing position in the dydx-v3-python package, whereas this can be easily accomplished using the UI. Otherwise one has to manually calculate prices for closing the position, which can be tedious.
I'm searching for a solution too. Something like market sell, to sell imminently. To sell with limit orders does not work so often...
@Bennch , @gabrielfior , this is possible.
You need to fetch your open positions via a call to private_client.private.get_account() The open positions will have a market and a size (amount).
Then simply for that market / size create a sell order, for example,
r = private_client.private.create_order(
position_id=position_id,
market=MARKET_ETH_USD,
side=ORDER_SIDE_SELL,
order_type=ORDER_TYPE_MARKET,
post_only=False,
size='0.01',
price='2020.0',
limit_fee='0.015',
expiration_epoch_seconds=1652619576,
time_in_force=TIME_IN_FORCE_FOK
)
That is what i was trying all the time. But if the orderbook is low, the order will not be filled. I real MARKET order shout not need a price variable.
That is what i was trying all the time. But if the orderbook is low, the order will not be filled. I real MARKET order shout not need a price variable.
Yeah, I found that confusing as well. I traced the UI and I noticed that the price is +100 above or below the index price. And checking the result the order was executed for the actual index price. So I'm guessing the backend is doing BEST(price, index).
So far, my orders do get executed and never stuck.
Another possible way to do it as sugested by @hbeijeman could be tracking your current open positions with
all_positions = client.private.get_positions( market=MARKET_ETH_USD, status=POSITION_STATUS_OPEN, )
filter then by any desired conditions (such as entryPrice) then just getting the side (either LONG or SHORT) and creating an opposite order.
... Would be fantastic, if this were implemented in a simply way into the dydx-v3 python wrapper, any plans of doing that ?
something like:
response = client.private.close_position(order_id=order_id)