-
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.
[PY] [CLOB-894] [CLOB-895]
v0.6.8
Quick python example for cancelin…
…g short term and long term orders (#42)
- Loading branch information
1 parent
6aca7bd
commit 1a84b50
Showing
11 changed files
with
375 additions
and
49 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,83 @@ | ||
'''Example for trading with human readable numbers | ||
Usage: python -m examples.composite_example | ||
''' | ||
import asyncio | ||
import logging | ||
from random import randrange | ||
from v4_client_py.chain.aerial.wallet import LocalWallet | ||
from v4_client_py.clients import CompositeClient, Subaccount | ||
from v4_client_py.clients.constants import BECH32_PREFIX, Network | ||
|
||
from v4_client_py.clients.helpers.chain_helpers import ( | ||
ORDER_FLAGS_LONG_TERM, | ||
OrderType, | ||
OrderSide, | ||
OrderTimeInForce, | ||
OrderExecution, | ||
) | ||
|
||
from tests.constants import DYDX_TEST_MNEMONIC, MAX_CLIENT_ID | ||
|
||
|
||
async def main() -> None: | ||
wallet = LocalWallet.from_mnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX) | ||
network = Network.staging() | ||
client = CompositeClient( | ||
network, | ||
) | ||
subaccount = Subaccount(wallet, 0) | ||
|
||
""" | ||
Note this example places a stateful order. | ||
Programmatic traders should generally not use stateful orders for following reasons: | ||
- Stateful orders received out of order by validators will fail sequence number validation | ||
and be dropped. | ||
- Stateful orders have worse time priority since they are only matched after they are included | ||
on the block. | ||
- Stateful order rate limits are more restrictive than Short-Term orders, specifically max 2 per | ||
block / 20 per 100 blocks. | ||
- Stateful orders can only be canceled after they’ve been included in a block. | ||
""" | ||
long_term_order_client_id = randrange(0, MAX_CLIENT_ID) | ||
try: | ||
tx = client.place_order( | ||
subaccount, | ||
market='ETH-USD', | ||
type=OrderType.LIMIT, | ||
side=OrderSide.SELL, | ||
price=40000, | ||
size=0.01, | ||
client_id=long_term_order_client_id, | ||
time_in_force=OrderTimeInForce.GTT, | ||
good_til_block=0, # long term orders use GTBT | ||
good_til_time_in_seconds=60, | ||
execution=OrderExecution.DEFAULT, | ||
post_only=False, | ||
reduce_only=False | ||
) | ||
print('** Long Term Order Tx**') | ||
print(tx.tx_hash) | ||
except Exception as error: | ||
print('**Long Term Order Failed**') | ||
print(str(error)) | ||
|
||
# cancel a long term order. | ||
try: | ||
tx = client.cancel_order( | ||
subaccount, | ||
long_term_order_client_id, | ||
'ETH-USD', | ||
ORDER_FLAGS_LONG_TERM, | ||
good_til_time_in_seconds=120, | ||
good_til_block=0, # long term orders use GTBT | ||
) | ||
print('**Cancel Long Term Order Tx**') | ||
print(tx.tx_hash) | ||
except Exception as error: | ||
print('**Cancel Long Term Order Failed**') | ||
print(str(error)) | ||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(level=logging.INFO) | ||
asyncio.get_event_loop().run_until_complete(main()) |
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,73 @@ | ||
'''Example for trading with human readable numbers | ||
Usage: python -m examples.composite_example | ||
''' | ||
import asyncio | ||
import logging | ||
from random import randrange | ||
from v4_client_py.chain.aerial.wallet import LocalWallet | ||
from v4_client_py.clients import CompositeClient, Subaccount | ||
from v4_client_py.clients.constants import BECH32_PREFIX, Network | ||
|
||
from v4_client_py.clients.helpers.chain_helpers import ( | ||
ORDER_FLAGS_SHORT_TERM, | ||
Order_TimeInForce, | ||
OrderSide, | ||
) | ||
from tests.constants import DYDX_TEST_MNEMONIC, MAX_CLIENT_ID | ||
|
||
|
||
async def main() -> None: | ||
wallet = LocalWallet.from_mnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX) | ||
network = Network.staging() | ||
client = CompositeClient( | ||
network, | ||
) | ||
subaccount = Subaccount(wallet, 0) | ||
|
||
# place a short term order. | ||
short_term_client_id = randrange(0, MAX_CLIENT_ID) | ||
# Get the expiration block. | ||
current_block = client.get_current_block() | ||
next_valid_block_height = current_block + 1 | ||
# Note, you can change this to any number between `next_valid_block_height` to `next_valid_block_height + SHORT_BLOCK_WINDOW` | ||
good_til_block = next_valid_block_height + 10 | ||
|
||
try: | ||
tx = client.place_short_term_order( | ||
subaccount, | ||
market='ETH-USD', | ||
side=OrderSide.SELL, | ||
price=40000, | ||
size=0.01, | ||
client_id=short_term_client_id, | ||
good_til_block=good_til_block, | ||
time_in_force=Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED, | ||
reduce_only=False | ||
) | ||
print('**Short Term Order Tx**') | ||
print(tx.tx_hash) | ||
except Exception as error: | ||
print('**Short Term Order Failed**') | ||
print(str(error)) | ||
|
||
# cancel a short term order. | ||
try: | ||
tx = client.cancel_order( | ||
subaccount, | ||
short_term_client_id, | ||
'ETH-USD', | ||
ORDER_FLAGS_SHORT_TERM, | ||
good_til_time_in_seconds=0, # short term orders use GTB. | ||
good_til_block=good_til_block, # GTB should be the same or greater than order to cancel | ||
) | ||
print('**Cancel Short Term Order Tx**') | ||
print(tx.tx_hash) | ||
except Exception as error: | ||
print('**Cancel Short Term Order Failed**') | ||
print(str(error)) | ||
|
||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(level=logging.INFO) | ||
asyncio.get_event_loop().run_until_complete(main()) |
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,10 +1,32 @@ | ||
|
||
from enum import Enum | ||
import json | ||
import os | ||
from typing import Tuple | ||
|
||
from v4_client_py.clients.helpers.chain_helpers import Order_TimeInForce, is_order_flag_stateful_order | ||
|
||
def loadJson(filename): | ||
current_directory = os.path.dirname(os.path.abspath(__file__)) | ||
json_file_path = os.path.join(current_directory, filename) | ||
|
||
with open(json_file_path, "r") as file: | ||
return json.load(file) | ||
|
||
class HumanReadableOrderTimeInForce(Enum): | ||
DEFAULT = "DEFAULT" | ||
FOK = "FOK" | ||
IOC = "IOC" | ||
POST_ONLY = "POST_ONLY" | ||
|
||
def orderExecutionToTimeInForce(orderExecution: HumanReadableOrderTimeInForce) -> Order_TimeInForce: | ||
if orderExecution == HumanReadableOrderTimeInForce.DEFAULT.value: | ||
return Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED | ||
elif orderExecution == HumanReadableOrderTimeInForce.FOK.value: | ||
return Order_TimeInForce.TIME_IN_FORCE_FILL_OR_KILL | ||
elif orderExecution == HumanReadableOrderTimeInForce.IOC.value: | ||
return Order_TimeInForce.TIME_IN_FORCE_IOC | ||
elif orderExecution == HumanReadableOrderTimeInForce.POST_ONLY.value: | ||
return Order_TimeInForce.TIME_IN_FORCE_POST_ONLY | ||
else: | ||
raise ValueError('Unrecognized order execution') |
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
Oops, something went wrong.