@@ -77,29 +77,42 @@ async def close_btc_position_if_exists(
7777 return attempt > 1 # Return True if we closed it, False if it never existed
7878
7979 position_size = float (btc_position .get ('size' ))
80+ is_long = position_size > 0
81+ abs_size = abs (position_size )
82+
8083 if attempt == 1 :
81- print (f"Found open BTC position: { position_size } BTC" )
82-
84+ position_type = "long" if is_long else "short"
85+ print (f"Found open BTC position: { abs_size } BTC ({ position_type } )" )
86+
8387 print (f"Closing BTC position... (attempt { attempt } /{ max_attempts } )" )
84-
85- # Create order to close position (SELL for long positions)
88+
89+ # Create order to close position
8690 order_id = market .order_id (
8791 address , subaccount_number , random .randint (0 , MAX_CLIENT_ID ), OrderFlags .SHORT_TERM
8892 )
89-
93+
9094 current_block = await node .latest_block_height ()
91-
92- # Calculate price from oracle price with slippage (for SELL orders, subtract slippage)
95+
96+ # Calculate price from oracle price with slippage
97+ # For SELL orders (closing long): subtract slippage
98+ # For BUY orders (closing short): add slippage
9399 slippage_pct = 10 # Same as open_btc_position default
94100 oracle_price = float (market .market ["oraclePrice" ])
95- price = oracle_price * ((100 - slippage_pct ) / 100.0 )
96-
97- # Use SELL side to close a long position
101+
102+ if is_long :
103+ # Long position: use SELL to close, subtract slippage
104+ side = Order .Side .SIDE_SELL
105+ price = oracle_price * ((100 - slippage_pct ) / 100.0 )
106+ else :
107+ # Short position: use BUY to close, add slippage
108+ side = Order .Side .SIDE_BUY
109+ price = oracle_price * ((100 + slippage_pct ) / 100.0 )
110+
98111 new_order = market .order (
99112 order_id = order_id ,
100113 order_type = OrderType .MARKET ,
101- side = Order . Side . SIDE_SELL ,
102- size = position_size ,
114+ side = side ,
115+ size = abs_size ,
103116 price = price ,
104117 time_in_force = None ,
105118 reduce_only = True ,
@@ -195,7 +208,7 @@ async def open_btc_position(
195208 Open a BTC position with the specified size.
196209
197210 Args:
198- size: Position size in BTC (e.g., 0.01 )
211+ size: Position size in BTC (e.g., 0.001 )
199212 slippage_pct: Percentage to add to oracle price for BUY orders (default: 10)
200213
201214 Returns:
@@ -319,14 +332,17 @@ async def test():
319332 wallet = await Wallet .from_mnemonic (node , DYDX_TEST_MNEMONIC , TEST_ADDRESS )
320333
321334 subaccount_number = 0
322- clob_pair_id = 0 # BTC-USD
323335 market_id = "BTC-USD"
324- position_size = 0.01 # BTC
336+ position_size = 0.001 # BTC
325337
326338 # Get BTC market
327339 market_data = await indexer .markets .get_perpetual_markets (market_id )
328340 market = Market (market_data ["markets" ][market_id ])
329341
342+ # Get the CLOB pair ID from the market data (not hardcoded)
343+ clob_pair_id = int (market .market ["clobPairId" ])
344+ print (f"Using CLOB pair ID: { clob_pair_id } for market { market_id } " )
345+
330346 # Step 1: Close initial BTC position if any
331347 print ("\n " + "=" * 60 )
332348 print ("Step 1: Closing initial BTC position (if any)" )
0 commit comments