Skip to content

Commit fd9443f

Browse files
committed
refactor: improve readability of TWAP parameters and logging in market.py and example
1 parent 59048d4 commit fd9443f

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

v4-client-py-v2/dydx_v4_client/node/market.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ def order(
103103
condition_type=condition_type,
104104
conditional_order_trigger_subticks=conditional_order_trigger_subticks,
105105
builder_code_parameters=builder_code_parameters(builder_address, fee_ppm),
106-
twap_parameters=twap_parameters(
107-
twap_duration, twap_interval, twap_price_tolerance
108-
) if (twap_duration is not None and twap_interval is not None and twap_price_tolerance is not None) else None,
106+
twap_parameters=(
107+
twap_parameters(twap_duration, twap_interval, twap_price_tolerance)
108+
if (
109+
twap_duration is not None
110+
and twap_interval is not None
111+
and twap_price_tolerance is not None
112+
)
113+
else None
114+
),
109115
order_router_address=order_router_address,
110116
)

v4-client-py-v2/examples/twap_parameter_example.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# TWAP Configuration
1919
TWAP_DURATION = 300 # minimum is 300 seconds
2020
# Interval must be in range [30 (30 seconds), 3600 (1 hour)] AND must be a factor of duration
21-
TWAP_INTERVAL = 60 # minimum is 30 seconds, must be factor of duration
21+
TWAP_INTERVAL = 60 # minimum is 30 seconds, must be factor of duration
2222
TWAP_PRICE_TOLERANCE = 10000 # 1% tolerance (10,000 ppm) - valid range [0, 1_000_000)
2323
MONITORING_INTERVAL = 2 # Check position changes every 2 seconds
2424

@@ -84,7 +84,9 @@ async def place_and_track_twap_order(size: float):
8484
if initial_position:
8585
print(f" Market: {MARKET_ID}")
8686
print(f" Size: {initial_size:.6f}")
87-
print(f" Direction: {'LONG' if is_long else 'SHORT' if initial_size < 0 else 'NONE'}")
87+
print(
88+
f" Direction: {'LONG' if is_long else 'SHORT' if initial_size < 0 else 'NONE'}"
89+
)
8890
else:
8991
print(f" No open position for {MARKET_ID}")
9092
print()
@@ -95,9 +97,7 @@ async def place_and_track_twap_order(size: float):
9597
print()
9698

9799
# Create order ID
98-
order_id = market.order_id(
99-
TEST_ADDRESS, 0, unique_client_id, OrderFlags.SHORT_TERM
100-
)
100+
order_id = market.order_id(TEST_ADDRESS, 0, unique_client_id, OrderFlags.SHORT_TERM)
101101

102102
current_block = await node.latest_block_height()
103103

@@ -118,7 +118,8 @@ async def place_and_track_twap_order(size: float):
118118
price=0, # Market order
119119
time_in_force=Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED,
120120
reduce_only=False,
121-
good_til_block=current_block + 30, # Must be within ShortBlockWindow limit (40 blocks max)
121+
good_til_block=current_block
122+
+ 30, # Must be within ShortBlockWindow limit (40 blocks max)
122123
twap_duration=TWAP_DURATION,
123124
twap_interval=TWAP_INTERVAL,
124125
twap_price_tolerance=TWAP_PRICE_TOLERANCE,
@@ -160,7 +161,9 @@ async def place_and_track_twap_order(size: float):
160161
current_position = pos
161162
break
162163

163-
current_size = float(current_position.get("size", 0)) if current_position else 0.0
164+
current_size = (
165+
float(current_position.get("size", 0)) if current_position else 0.0
166+
)
164167

165168
# Check if position changed
166169
if abs(current_size - last_position_size) > 0.000001:
@@ -238,7 +241,9 @@ async def place_and_track_twap_order(size: float):
238241
f"Total Change: {change['total_change']:+.6f}"
239242
)
240243
else:
241-
print("No position changes detected (order may still be executing or no changes occurred)")
244+
print(
245+
"No position changes detected (order may still be executing or no changes occurred)"
246+
)
242247

243248
print()
244249
print("=" * 80)

0 commit comments

Comments
 (0)