-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplace_stateful_orders.py
151 lines (129 loc) · 4.51 KB
/
place_stateful_orders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Script that places stateful orders every N seconds
and writes the time that it was placed along with the order info to BigQuery
Usage: python place_stateful_orders.py
"""
import asyncio
import logging
from logging.handlers import RotatingFileHandler
from random import randrange
from v4_client_py.chain.aerial.wallet import LocalWallet
from v4_client_py.clients import Subaccount
from v4_client_py.clients.constants import BECH32_PREFIX
from v4_client_py.clients.helpers.chain_helpers import OrderSide
from v4_client_py.clients.helpers.chain_helpers import (
Order_TimeInForce,
ORDER_FLAGS_LONG_TERM,
)
# Import helpers
from bq_helpers import (
create_table,
BatchWriter,
PLACE_ORDER_CLUSTERING_FIELDS,
PLACE_ORDER_SCHEMA,
PLACE_ORDER_TIME_PARTITIONING,
)
from client_helpers import (
get_markets_data,
load_config,
place_orders,
precompute_order,
setup_clients,
)
# The idea of this experiment is to see what is the lag between placing a stateful order and when it shows up in a stream
# Dataset configuration
DATASET_ID = "latency_experiments"
TABLE_ID = "long_running_stateful_orders"
# Batch settings for BQ writes
BATCH_SIZE = 1 # write every order to BQ as soon as it is placed
BATCH_TIMEOUT = 10
WORKER_COUNT = 1
config = load_config()
# Constants on how to place orders
TIME_IN_FORCE = Order_TimeInForce.TIME_IN_FORCE_POST_ONLY
BUY_PRICE = 0.01
SELL_PRICE = 10
SIZE = 1
MARKET = "AXL-USD"
NUM_BLOCKS = 10_000
DYDX_MNEMONIC = config["stateful_mnemonic"]
GTBT_DELTA = 5
PLACE_INTERVAL = 13
async def listen_to_block_stream_and_place_orders(batch_writer):
# Setup clients to broadcast
wallet = LocalWallet.from_mnemonic(DYDX_MNEMONIC, BECH32_PREFIX)
client, ledger_client = setup_clients(config["full_node_submission_address"])
market = await asyncio.to_thread(get_markets_data, client, MARKET)
subaccount = Subaccount(wallet, 0)
account = await asyncio.to_thread(
ledger_client.query_account,
subaccount.wallet.address()
)
client_id = randrange(0, 2**32 - 1)
num_blocks_placed = 0
sequence = account.sequence
while num_blocks_placed < NUM_BLOCKS:
# only place one order each time, due to Stateful order rate limit
logging.info(f"Presigning orders {num_blocks_placed}")
order = precompute_order(
client,
ledger_client,
market,
subaccount,
OrderSide.BUY,
BUY_PRICE,
client_id,
0,
client.calculate_good_til_block_time(GTBT_DELTA),
SIZE,
ORDER_FLAGS_LONG_TERM,
TIME_IN_FORCE,
sequence,
account.number
)
orders = [order]
logging.info(f"Placing orders {num_blocks_placed}")
current_block = await asyncio.to_thread(client.get_current_block)
# Check errors, because if block rate limit is exceeded, we need to
# wait and also the sequence number might be off
errs = await place_orders(
ledger_client,
current_block,
orders,
batch_writer,
)
if len([x for x in errs if x is not None]) > 0:
logging.info("Saw errors placing orders, refreshing seq and sleeping longer...")
account = await asyncio.to_thread(
ledger_client.query_account,
subaccount.wallet.address()
)
await asyncio.sleep(PLACE_INTERVAL * 2)
sequence = account.sequence - 1
client_id += 1
num_blocks_placed += 1
sequence += 1
# stay under the stateful order rate limit
await asyncio.sleep(PLACE_INTERVAL)
logging.info("Finished placing orders")
async def main():
batch_writer = BatchWriter(
DATASET_ID, TABLE_ID, WORKER_COUNT, BATCH_SIZE, BATCH_TIMEOUT
)
batch_writer_task = asyncio.create_task(batch_writer.batch_writer_loop())
listen_task = asyncio.create_task(
listen_to_block_stream_and_place_orders(batch_writer)
)
await asyncio.gather(batch_writer_task, listen_task)
if __name__ == "__main__":
handler = RotatingFileHandler(
"place_stateful_orders.log",
maxBytes=5 * 1024 * 1024, # 5 MB
backupCount=5
)
logging.basicConfig(
handlers=[handler],
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
create_table(DATASET_ID, TABLE_ID, PLACE_ORDER_SCHEMA, PLACE_ORDER_TIME_PARTITIONING, PLACE_ORDER_CLUSTERING_FIELDS)
asyncio.run(main())