Add Support for Specifying Margin Type (Cross/Isolated) in Order Placement #313

Hello dYdX Team,

I noticed that the v4-chain repository does not appear to have a feature for specifying the margin type (Cross or Isolated) during order placement via the clob module.

Adding this capability as a parameter in the place-order command would greatly improve flexibility for traders managing their margin strategies.

Currently, I am using the following code to place an order. By default, it places orders with Cross Margin, but there is no option to switch to Isolated Margin:

import asyncio
import random

from dydx_v4_client import MAX_CLIENT_ID, NodeClient, OrderFlags, Wallet
from dydx_v4_client.indexer.rest.constants import OrderType
from dydx_v4_client.indexer.rest.indexer_client import IndexerClient
from dydx_v4_client.network import TESTNET
from dydx_v4_client.node.market import Market
from v4_proto.dydxprotocol.clob.order_pb2 import Order

MARKET_ID = "ETH-USD"
TEST_ADDRESS = ''
DYDX_TEST_MNEMONIC = (
)

async def place_market_order(size: float):
    try:
        node = await NodeClient.connect(TESTNET.node)
        indexer = IndexerClient(TESTNET.rest_indexer)

        market_data = await indexer.markets.get_perpetual_markets(MARKET_ID)
        market = Market(market_data["markets"][MARKET_ID])

        wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC, TEST_ADDRESS)

        account_info = await node.get_account(TEST_ADDRESS)
        wallet.sequence = account_info.sequence

        order_id = market.order_id(
            TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.SHORT_TERM
        )

        current_block = await node.latest_block_height()

        new_order = market.order(
            order_id=order_id,
            order_type=OrderType.LIMIT,
            side=Order.Side.SIDE_SELL,
            size=size,
            price=0,
            time_in_force=Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED,
            reduce_only=False,
            good_til_block=current_block + 10,
        )

        transaction = await node.place_order(
            wallet=wallet,
            order=new_order,
        )

        print(f"Transaction successful: {transaction}")
        wallet.sequence += 1

    except Exception as e:
        print(f"An error occurred: {e}")

asyncio.run(place_market_order(1.053))

As you can see, this code places orders with Cross Margin by default, and there is no option to switch to Isolated Margin.

Could you clarify whether this functionality is currently supported or if there are plans to include it in future updates?

Thank you for your time and consideration.

Best regards,

1 Like

Hi Soheil,

The margin type can be set using the designated subaccounts. If the order is placed on subaccount 0 to 127 then the order would be placed on cross-margin. From subaccount 128 or greater, the order will be placed using isolated margin. You can find further information in this link: How to integrate APIs with FE isolated positions · dYdX · v4

Hope that helps! Thank you.

Could you please provide an example code?

Here are a couple of examples with typescript:

1 Like