-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add market order example * Implement standardized order chain helper functions * Revise market order example to use new chain helpers * Update order examples to reflect new API structure
- Loading branch information
Showing
8 changed files
with
149 additions
and
29 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
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,64 @@ | ||
from dydx_v4_client.indexer.rest.constants import ( | ||
OrderType, | ||
OrderExecution, | ||
OrderTimeInForce, | ||
) | ||
from v4_proto.dydxprotocol.clob.order_pb2 import Order | ||
|
||
|
||
class OrderHelper: | ||
@staticmethod | ||
def calculate_time_in_force( | ||
order_type: OrderType, | ||
time_in_force: Order.TimeInForce, | ||
post_only: bool = False, | ||
execution: OrderExecution = OrderExecution.DEFAULT, | ||
) -> Order.TimeInForce: | ||
if order_type == OrderType.MARKET: | ||
return Order.TimeInForce.TIME_IN_FORCE_IOC | ||
elif order_type == OrderType.LIMIT: | ||
if post_only: | ||
return Order.TimeInForce.TIME_IN_FORCE_POST_ONLY | ||
else: | ||
return time_in_force | ||
elif order_type in [OrderType.STOP_LIMIT, OrderType.TAKE_PROFIT_LIMIT]: | ||
if execution == OrderExecution.DEFAULT: | ||
return Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED | ||
elif execution == OrderExecution.POST_ONLY: | ||
return Order.TimeInForce.TIME_IN_FORCE_POST_ONLY | ||
elif execution == OrderExecution.FOK: | ||
return Order.TimeInForce.TIME_IN_FORCE_FILL_OR_KILL | ||
elif execution == OrderExecution.IOC: | ||
return Order.TimeInForce.TIME_IN_FORCE_IOC | ||
elif order_type in [OrderType.STOP_MARKET, OrderType.TAKE_PROFIT_MARKET]: | ||
if execution in [OrderExecution.DEFAULT, OrderExecution.POST_ONLY]: | ||
raise ValueError( | ||
f"Execution value {execution.value} not supported for {order_type.value}" | ||
) | ||
elif execution == OrderExecution.FOK: | ||
return Order.TimeInForce.TIME_IN_FORCE_FILL_OR_KILL | ||
elif execution == OrderExecution.IOC: | ||
return Order.TimeInForce.TIME_IN_FORCE_IOC | ||
raise ValueError( | ||
"Invalid combination of order type, time in force, and execution" | ||
) | ||
|
||
@staticmethod | ||
def calculate_client_metadata(order_type: OrderType) -> int: | ||
return ( | ||
1 | ||
if order_type | ||
in [OrderType.MARKET, OrderType.STOP_MARKET, OrderType.TAKE_PROFIT_MARKET] | ||
else 0 | ||
) | ||
|
||
@staticmethod | ||
def calculate_condition_type(order_type: OrderType) -> Order.ConditionType: | ||
if order_type in [OrderType.LIMIT, OrderType.MARKET]: | ||
return Order.ConditionType.CONDITION_TYPE_UNSPECIFIED | ||
elif order_type in [OrderType.STOP_LIMIT, OrderType.STOP_MARKET]: | ||
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 | ||
else: | ||
raise ValueError("Invalid order type") |
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,51 @@ | ||
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.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 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, | ||
order_type=OrderType.MARKET, | ||
side=Order.Side.SIDE_SELL, | ||
size=size, | ||
price=0, # Set to 0 for market orders | ||
time_in_force=Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED, | ||
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)) |
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