core.test_formatters
Tests for Response Formatting Functions
Tests the pure functions in f.domains.hedge.core.formatters that transform internal data structures into frontend-compatible API responses.
MATHEMATICAL BACKGROUND
These formatters handle the conversion of Decimal-based calculations to float-based API responses, ensuring numerical precision is preserved where needed while providing human-readable output.
KEY FORMULAS USED
Impermanent Loss vs HODL:
IL_hodl = V_LP / V_HODL - 1Where: V_LP = Current LP position value V_HODL = Value if tokens were held (not deposited)
Example: V_LP = 89,857.74, V_HODL = 93,454.00 IL_hodl = 89857.74 / 93454.00 - 1 = -0.0385 (-3.85%)
Impermanent Loss vs Deposit:
IL_deposit = V_LP / I_initial - 1Where: V_LP = Current LP position value I_initial = Initial deposit value at entry
Example: V_LP = 89,857.74, I_initial = 100,000.00 IL_deposit = 89857.74 / 100000.00 - 1 = -0.1014 (-10.14%)
Relationship Between IL Metrics:
- IL_hodl: Measures loss compared to passive holding
- IL_deposit: Measures absolute loss from entry point
- When price returns to entry: IL_hodl ≈ IL_deposit ≈ 0
- IL_deposit includes price change effect; IL_hodl isolates IL only
Put Option Payoff at Expiry:
hedge_payoff = max(K - S_expiry, 0) × quantityWhere: K = Strike price S_expiry = Spot price at expiration quantity = Number of put contracts
ROI and APR:
ROI = (capital / total_investment) - 1 APR = (1 + ROI)^(365/days) - 1
DERIBIT INSTRUMENT NAMING
Options on Deribit follow the format: {UNDERLYING}-{EXPIRY}-{STRIKE}-{TYPE}
Example: ETH-28MAR26-2600-P - UNDERLYING: ETH - EXPIRY: 28MAR26 (28 March 2026) - STRIKE: 2600 USD - TYPE: P (Put) or C (Call)
FUNCTIONS TESTED
format_scenario_point_as_il_entry()
- Converts ScenarioPoint to frontend IL table row
- Keys: variation, spot_price, il_vs_hodl, il_vs_deposit
format_il_table()
- Batch conversion of multiple ScenarioPoints
- Preserves ordering
format_scenario_result_as_expiry_entry()
- Converts ScenarioResult to expiry analysis row
- Calculates hedge_payoff based on positions
format_scenarios_at_expiry()
- Batch conversion with payoff calculation
- Aggregates across all positions
format_deribit_order()
- Converts HedgePosition to Deribit API format
- Generates instrument name from strike + expiry
format_deribit_orders()
- Batch conversion with quantity filtering
- Filters out positions below min_quantity
TYPE CONVERSION
All Decimal values are converted to float for JSON serialization: Decimal("0.0385") → 0.0385 (float)
This allows direct frontend consumption without Decimal handling.
Sample scenario point for testing.
Sample scenario result for testing.
Sample hedge position for testing.
Sample expiry date.
Test: Format IL entry with negative variation.
NUMERICAL EXAMPLE
Input: variation = -0.20 price = 2440 il_vs_hodl = -0.0385
Expected: All Decimal values converted to float Keys match frontend interface
Test IL entry with positive variation.
Test IL entry with zero variation (base case).
Test: Format IL table with multiple scenario points.
NUMERICAL EXAMPLE
Input: 3 scenario points with variations -0.20, 0.00, 0.20
Expected: 3 entries in order, all properly formatted
Test IL table with single point.
Test IL table with empty list.
Test: Format expiry entry with hedge payoff calculation.
NUMERICAL EXAMPLE
Input: scenario: price = 2440, variation = -0.20 position: Put 2600 × 5 fees = 500
Calculation: hedge_payoff = max(2600 - 2440, 0) × 5 = 160 × 5 = 800
Expected: hedge_payoff = 800
Test expiry entry when put is OTM (no payoff).
Test expiry entry at break-even scenario.
Test: Format multiple scenarios at expiry.
NUMERICAL EXAMPLE
Input: 2 scenarios: -20% and +20% variation
Expected: 2 entries with calculated payoffs
Test scenarios at expiry with empty list.
Test scenarios at expiry with no hedge positions.
Test: Format buy order for Deribit.
NUMERICAL EXAMPLE
Input: position: Put 2600 × 5, direction = BUY expiry = 2026-03-28 p0 = 3050
Expected: instrument = "ETH-28MAR26-2600-P" side = "BUY" quantity = 5.0
Test format sell order.
Test format order with large quantity.
Test: Format multiple Deribit orders.
NUMERICAL EXAMPLE
Input: 2 positions: Put 2600 × 5, Put 2200 × 10
Expected: 2 orders formatted correctly
Test that small quantities are filtered out.
Test empty positions list.