dydx-v3-python icon indicating copy to clipboard operation
dydx-v3-python copied to clipboard

No intuitive and easy way to close a position (as available in the dydx UI)

Open gabrielfior opened this issue 3 years ago • 6 comments

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.

gabrielfior avatar Apr 20 '22 08:04 gabrielfior

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 avatar May 02 '22 16:05 Bennch

@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
        )

hbeijeman avatar May 15 '22 10:05 hbeijeman

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.

Bennch avatar May 15 '22 13:05 Bennch

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.

hbeijeman avatar May 15 '22 13:05 hbeijeman

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.

mevquant avatar Oct 08 '22 04:10 mevquant

... 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)

traderblakeq avatar Apr 05 '23 15:04 traderblakeq