From c67c207af8c71fbbc917ac16ab6289993111a14a Mon Sep 17 00:00:00 2001 From: samtin0x <40127309+samtin0x@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:58:41 +0000 Subject: [PATCH] feat: add stop market order example --- .../dydx_v4_client/node/chain_helpers.py | 4 +- v4-client-py-v2/dydx_v4_client/node/market.py | 5 +- .../examples/stop_market_order_example.py | 51 +++++++++++++++++++ v4-client-py-v2/pyproject.toml | 2 +- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 v4-client-py-v2/examples/stop_market_order_example.py diff --git a/v4-client-py-v2/dydx_v4_client/node/chain_helpers.py b/v4-client-py-v2/dydx_v4_client/node/chain_helpers.py index baf91a7e..d415efb9 100644 --- a/v4-client-py-v2/dydx_v4_client/node/chain_helpers.py +++ b/v4-client-py-v2/dydx_v4_client/node/chain_helpers.py @@ -53,9 +53,9 @@ def calculate_client_metadata(order_type: OrderType) -> int: @staticmethod def calculate_condition_type(order_type: OrderType) -> Order.ConditionType: - if order_type in [OrderType.LIMIT, OrderType.MARKET]: + if order_type in [OrderType.LIMIT, OrderType.MARKET, OrderType.STOP_MARKET]: return Order.ConditionType.CONDITION_TYPE_UNSPECIFIED - elif order_type in [OrderType.STOP_LIMIT, OrderType.STOP_MARKET]: + elif order_type in [OrderType.STOP_LIMIT]: return Order.ConditionType.CONDITION_TYPE_STOP_LOSS elif order_type in [OrderType.TAKE_PROFIT_LIMIT, OrderType.TAKE_PROFIT_MARKET]: return Order.ConditionType.CONDITION_TYPE_TAKE_PROFIT diff --git a/v4-client-py-v2/dydx_v4_client/node/market.py b/v4-client-py-v2/dydx_v4_client/node/market.py index ee347bfc..7cfaaf6d 100644 --- a/v4-client-py-v2/dydx_v4_client/node/market.py +++ b/v4-client-py-v2/dydx_v4_client/node/market.py @@ -28,7 +28,10 @@ def calculate_quantums(self, size: float) -> int: result = max(quantums, self.market["stepBaseQuantums"]) return result - def calculate_subticks(self, price: float) -> int: + def calculate_subticks( + self, + price: float, + ) -> int: QUOTE_QUANTUMS_ATOMIC_RESOLUTION = -6 exponent = ( self.market["atomicResolution"] diff --git a/v4-client-py-v2/examples/stop_market_order_example.py b/v4-client-py-v2/examples/stop_market_order_example.py new file mode 100644 index 00000000..917c1e24 --- /dev/null +++ b/v4-client-py-v2/examples/stop_market_order_example.py @@ -0,0 +1,51 @@ +import random + +from dydx_v4_client import MAX_CLIENT_ID, OrderFlags +from v4_proto.dydxprotocol.clob.order_pb2 import Order + +from dydx_v4_client.indexer.rest.constants import OrderType, OrderExecution +from dydx_v4_client.indexer.rest.indexer_client import IndexerClient +from dydx_v4_client.network import TESTNET +from dydx_v4_client.node.client import NodeClient +from dydx_v4_client.node.market import Market +from dydx_v4_client.wallet import Wallet +from tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS + +MARKET_ID = "ETH-USD" + + +async def test_place_stop_market_order(): + size, trigger_price = 0.001, 1800 + 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() + print(current_block) + new_order = market.order( + order_id=order_id, + order_type=OrderType.STOP_MARKET, + side=Order.Side.SIDE_SELL, + size=size, + price=trigger_price, + time_in_force=Order.TimeInForce.TIME_IN_FORCE_IOC, + reduce_only=False, + execution=OrderExecution.IOC, + good_til_block=current_block + 10, + ) + print(new_order) + transaction = await node.place_order( + wallet=wallet, + order=new_order, + ) + + print(transaction) + wallet.sequence += 1 diff --git a/v4-client-py-v2/pyproject.toml b/v4-client-py-v2/pyproject.toml index b1e50aef..9fd52079 100644 --- a/v4-client-py-v2/pyproject.toml +++ b/v4-client-py-v2/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dydx-v4-client" -version = "1.1.1" +version = "1.1.2" description = "" authors = [ "Saul Martin ",