Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set leverage on new order opening by python library client? #2153

Open
BaseMax opened this issue Aug 26, 2024 · 2 comments
Open

How to set leverage on new order opening by python library client? #2153

BaseMax opened this issue Aug 26, 2024 · 2 comments

Comments

@BaseMax
Copy link

BaseMax commented Aug 26, 2024

Hi Team,

I'm Max, and I'm currently working on a Python script to automate order placement on the DYDX platform. At the moment, I'm handling limit orders, but my goal is to also support leveraged orders. However, I haven't been able to find any references or code examples on how to set leverage in orders. I've been going through the library's source code but couldn't locate anything related to "leverage."

Could you please provide any references or code snippets that might help?

Thank you!

one example:

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 = 'xxxxxxxxxxxxxxxxxxx'
DYDX_TEST_MNEMONIC = (
    "xxxxxxxxxxxxxxxx"
)


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,  # LIMIT , MARKET , STOP_MARKET , STOP_LIMIT , TAKE_PROFIT_LIMIT , TAKE_PROFIT_MARKET
            side=Order.Side.SIDE_SELL,  # SIDE_SELL , SIDE_BUY , SIDE_UNSPECIFIED , DESCRIPTOR
            size=size,
            price=0,
            time_in_force=Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED,  # TIME_IN_FORCE_UNSPECIFIED , TIME_IN_FORCE_IOC , TIME_IN_FORCE_POST_ONLY , TIME_IN_FORCE_FILL_OR_KILL , DESCRIPTOR
            reduce_only=False,
            good_til_block=current_block + 10,
        )

        transaction = await node.place_order(
            wallet=wallet,
            order=new_order,
        )
        print(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))
Copy link

linear bot commented Aug 26, 2024

@BaseMax
Copy link
Author

BaseMax commented Sep 15, 2024

Any updates or help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants
@BaseMax and others