Skip to content

Commit

Permalink
Add market order example
Browse files Browse the repository at this point in the history
  • Loading branch information
samtin0x committed Jul 21, 2024
1 parent 74af544 commit 0215807
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions v4-client-py-v2/examples/market_order_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
import random

from dydx_v4_client import MAX_CLIENT_ID, NodeClient, OrderFlags, Wallet
from v4_proto.dydxprotocol.clob.order_pb2 import Order

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 tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS

MARKET_ID = "ETH-USD"


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

market = Market(
(await indexer.markets.get_perpetual_markets(MARKET_ID))["markets"][MARKET_ID]
)
wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC, TEST_ADDRESS)

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,
side=Order.Side.SIDE_SELL,
size=size,
price=0, # Set to 0 for market orders
time_in_force=Order.TimeInForce.TIME_IN_FORCE_FILL_OR_KILL, # Use IOC for market orders
reduce_only=False,
good_til_block=current_block + 10,
)

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

print(transaction)
wallet.sequence += 1


asyncio.run(place_market_order(0.00001))

0 comments on commit 0215807

Please sign in to comment.