-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
189 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Cancelling Orders | ||
|
||
This guide demonstrates how to cancel orders using the dYdX Python SDK. | ||
|
||
## Cancelling Orders | ||
|
||
There are two ways to cancel orders using the dYdX Python SDK: | ||
|
||
### 1. Cancel a Specific Order | ||
|
||
To cancel a specific order, you can use the `cancel_order` method. Here's an example: | ||
|
||
```python | ||
from dydx_v4_client.node.message import cancel_order | ||
|
||
cancel_order_msg = cancel_order( | ||
order_id, | ||
good_til_block, | ||
good_til_block_time | ||
) | ||
|
||
response = await node_client.cancel_order( | ||
wallet, | ||
order_id, | ||
good_til_block=good_til_block, | ||
good_til_block_time=good_til_block_time | ||
) | ||
``` | ||
|
||
### 2. Batch Cancel Orders | ||
For cancelling multiple orders at once, you can use the batch_cancel_orders method. Here's an example: | ||
pythonCopyfrom dydx_v4_client.node.message import batch_cancel | ||
from v4_proto.dydxprotocol.clob.order_pb2 import OrderBatch | ||
|
||
```python | ||
batch_cancel_msg = batch_cancel( | ||
subaccount_id, | ||
short_term_cancels, | ||
good_til_block | ||
) | ||
|
||
response = await node_client.batch_cancel_orders( | ||
wallet, | ||
subaccount_id, | ||
short_term_cancels, | ||
good_til_block | ||
) | ||
``` | ||
|
||
### Examples | ||
For more detailed examples of how to cancel orders: | ||
- **Cancelling a specific order**: See `examples/long_term_order_cancel_example.py` | ||
- **Batch cancelling orders**: See `examples/batch_cancel_example.py` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
from dydx_v4_client.indexer.rest.indexer_client import IndexerClient | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
from dydx_v4_client.indexer.socket.websocket import IndexerSocket | ||
from dydx_v4_client.indexer.candles_resolution import CandlesResolution | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import asyncio | ||
import random | ||
|
||
from dydx_v4_client import MAX_CLIENT_ID, NodeClient, Order, OrderFlags | ||
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 dydx_v4_client.wallet import Wallet | ||
from tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS | ||
from v4_proto.dydxprotocol.clob.tx_pb2 import OrderBatch | ||
from v4_proto.dydxprotocol.subaccounts.subaccount_pb2 import SubaccountId | ||
|
||
|
||
MARKET_ID = "BTC-USD" | ||
PERPETUAL_PAIR_BTC_USD = 0 | ||
|
||
|
||
async def test_batch_cancel(): | ||
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) | ||
|
||
# Place multiple orders | ||
orders = [] | ||
client_ids = [] | ||
for _ in range(3): | ||
client_id = random.randint(0, MAX_CLIENT_ID) | ||
order_id = market.order_id(TEST_ADDRESS, 0, client_id, OrderFlags.SHORT_TERM) | ||
client_ids.append(client_id) | ||
current_block = await node.latest_block_height() | ||
order = market.order( | ||
order_id, | ||
side=Order.Side.SIDE_SELL, | ||
order_type=OrderType.LIMIT, | ||
size=0.01, | ||
price=40000 + random.randint(-100, 100), | ||
time_in_force=Order.TIME_IN_FORCE_IOC, | ||
reduce_only=False, | ||
good_til_block=current_block + 20, | ||
) | ||
orders.append(order) | ||
|
||
# Place orders | ||
for order in orders: | ||
place = await node.place_order(wallet, order) | ||
print(f"Placed order: {place}") | ||
wallet.sequence += 1 | ||
|
||
# Prepare batch cancel | ||
subaccount_id = SubaccountId(owner=TEST_ADDRESS, number=0) | ||
order_batch = OrderBatch(clob_pair_id=PERPETUAL_PAIR_BTC_USD, client_ids=client_ids) | ||
cancellation_current_block = await node.latest_block_height() | ||
|
||
# Execute batch cancel | ||
batch_cancel_response = await node.batch_cancel_orders( | ||
wallet, subaccount_id, [order_batch], cancellation_current_block + 10 | ||
) | ||
print(f"Batch cancel response: {batch_cancel_response}") | ||
|
||
|
||
asyncio.run(test_batch_cancel()) |
Oops, something went wrong.