-
Notifications
You must be signed in to change notification settings - Fork 57
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
add batch cancel example #223
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few code lines about how to create this
short_term_cancels
would be great