transform_open_position_to_order_parameters

Documentation for eth_defi.gmx.utils.transform_open_position_to_order_parameters function.

transform_open_position_to_order_parameters(config, positions, market_symbol, is_long, slippage_percent, out_token, amount_of_position_to_close, amount_of_collateral_to_remove)

Transform existing position data into precise order parameters for strategic position closure.

This function implements sophisticated data transformation logic that bridges the gap between high-level trading intentions (“close 50% of my ETH position”) and the precise technical parameters required by the GMX protocol for order execution. It handles complex address resolution, swap path calculation, and mathematical precision for financial calculations.

Data Transformation Architecture:

The transformation process involves multiple complex steps: position identification using human-readable keys, address resolution for multiple token types, swap path determination for asset conversion, and precise mathematical calculations for partial position closure. Each step includes validation to ensure data integrity and prevent execution errors.

Mathematical Precision Requirements:

Financial calculations require absolute precision to prevent rounding errors that could cause transaction failures or unexpected results. The function uses Decimal arithmetic for position size calculations and properly scales values to match protocol requirements for order parameters.

Swap Path Intelligence:

When the desired output token differs from the position’s collateral token, the function automatically determines the optimal swap path through available markets. This enables strategic asset selection upon position closure without requiring manual path configuration.

Error Prevention and Validation:

Comprehensive validation ensures that all required position data is available and properly formatted before attempting transformation. Clear error messages help identify configuration issues or missing position data that would prevent successful order creation.

Example:

# Strategic position closure with precise control
positions = get_positions(config)

# Close 75% of ETH long position, convert to USDC
close_params = transform_open_position_to_order_parameters(
    config=config,
    positions=positions,
    market_symbol="ETH",
    is_long=True,
    slippage_percent=0.005,  # 0.5% slippage
    out_token="USDC",  # Convert to stable asset
    amount_of_position_to_close=0.75,  # Close 75% of position
    amount_of_collateral_to_remove=0.5,  # Remove 50% of collateral
)

# Parameters ready for order execution
order = DecreaseOrder(**close_params)
Parameters
  • config (GMXConfigManager) – GMX configuration object containing network settings and token information required for address resolution and market data access

  • positions (dict[str, Any]) – Dictionary containing all current open positions with human-readable keys and comprehensive position data for transformation

  • market_symbol (str) – Symbol identifying the market containing the position to close (e.g., “ETH”, “BTC”). Must match an existing position

  • is_long (bool) – Direction of the position to close - True for long positions, False for short positions. Must match existing position direction

  • slippage_percent (float) – Maximum acceptable slippage for the closure operation as decimal (0.005 = 0.5%). Higher values enable faster execution in volatile markets but may result in worse prices

  • out_token (str) – Symbol of the token to receive upon position closure. May differ from collateral token, triggering automatic swap path calculation

  • amount_of_position_to_close (float) – Fraction of total position size to close, expressed as decimal (0.5 = 50%). Enables precise partial position management strategies

  • amount_of_collateral_to_remove (float) – Fraction of position collateral to withdraw, expressed as decimal. Independent of position closure amount for flexible capital management

Returns

Dictionary containing all parameters required for order execution, formatted according to GMX protocol requirements with proper address resolution and mathematical precision

Return type

dict[str, Any]

Raises

Exception – When the specified position cannot be found in the positions dictionary, indicating invalid market/direction combination