Skip to content

Commit

Permalink
lpandarb: handle matured positions (#1536)
Browse files Browse the repository at this point in the history
  • Loading branch information
wakamex authored Jun 20, 2024
1 parent a1118cc commit e68e084
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/agent0/core/hyperdrive/policies/lpandarb.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,17 @@ def arb_fixed_rate_down(
FixedPoint((maturity_time - pool_state.block_time + 12))
/ FixedPoint(interface.pool_config.position_duration),
)
logging.info("curve portion is %s\nbonds needed is %s", curve_portion, bonds_needed)
reduce_short_amount = minimum(
short.balance, bonds_needed / curve_portion, interface.calc_max_long(max_trade_amount_base, pool_state)
)
if reduce_short_amount > min_trade_amount_bonds:
bonds_needed -= reduce_short_amount * curve_portion
logging.debug(
"reducing short by %s\nreduce_short_amount*curve_portion = %s",
reduce_short_amount,
reduce_short_amount * curve_portion,
if curve_portion > FixedPoint(0):
logging.info("curve portion is %s\nbonds needed is %s", curve_portion, bonds_needed)
reduce_short_amount = minimum(
short.balance, bonds_needed / curve_portion, interface.calc_max_long(max_trade_amount_base, pool_state)
)
action_list.append(
close_short_trade(
reduce_short_amount, maturity_time, slippage_tolerance, base_fee_multiple, priority_fee_multiple
if reduce_short_amount > min_trade_amount_bonds:
action_list.append(
close_short_trade(
reduce_short_amount, maturity_time, slippage_tolerance, base_fee_multiple, priority_fee_multiple
)
)
)
# Open a new long, if there's still a need, and we have money
if max_trade_amount_base >= min_trade_amount_bonds and bonds_needed > min_trade_amount_bonds:
max_long_shares = interface.calc_shares_in_given_bonds_out_down(
Expand Down Expand Up @@ -186,14 +181,19 @@ def arb_fixed_rate_up(
FixedPoint(maturity_time - pool_state.block_time + 12)
/ FixedPoint(interface.pool_config.position_duration),
)
logging.info("curve portion is %s\nbonds needed is %s", curve_portion, bonds_needed)
reduce_long_amount = minimum(
long.balance, bonds_needed / curve_portion, interface.calc_max_short(max_trade_amount_base, pool_state)
)
if reduce_long_amount > min_trade_amount_bonds:
bonds_needed -= reduce_long_amount * curve_portion
logging.debug("reducing long by %s", reduce_long_amount)
action_list.append(close_long_trade(reduce_long_amount, maturity_time, slippage_tolerance))
if curve_portion > FixedPoint(0):
logging.info("curve portion is %s\nbonds needed is %s", curve_portion, bonds_needed)
reduce_long_amount = minimum(
long.balance, bonds_needed / curve_portion, interface.calc_max_short(max_trade_amount_base, pool_state)
)
if reduce_long_amount > min_trade_amount_bonds:
bonds_needed -= reduce_long_amount * curve_portion
logging.debug("reducing long by %s", reduce_long_amount)
action_list.append(
close_long_trade(
reduce_long_amount, maturity_time, slippage_tolerance, base_fee_multiple, priority_fee_multiple
)
)
# Open a new short, if there's still a need, and we have money
if max_trade_amount_base >= min_trade_amount_bonds and bonds_needed > min_trade_amount_bonds:
max_short = interface.calc_max_short(max_trade_amount_base, pool_state)
Expand Down
36 changes: 36 additions & 0 deletions src/agent0/core/hyperdrive/policies/lpandarb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,39 @@ def test_safe_short_trading(interactive_hyperdrive: LocalHyperdrive, manual_agen
assert len(action_result) == 2 # LP & Arb (no closing trades)
assert isinstance(action_result[0], AddLiquidity) # LP first
assert isinstance(action_result[1], OpenShort) # then arb


@pytest.mark.anvil
def test_matured_long(interactive_hyperdrive: LocalHyperdrive, arbitrage_andy: LocalHyperdriveAgent):
"""Don't touch the matured long."""
# report starting fixed rate
logging.info("starting fixed rate is %s", interactive_hyperdrive.interface.calc_spot_rate())

# arbitrage it back
arbitrage_andy.open_long(base=FixedPoint(100_000))

interactive_hyperdrive.chain.advance_time(int(YEAR_IN_SECONDS * 2), create_checkpoints=False)

# check Andy's trades to make sure he doesn't CloseLong
event = arbitrage_andy.execute_policy_action()
event = event[0] if isinstance(event, list) else event
logging.info("event is %s", event)
assert not isinstance(event, CloseLong)


@pytest.mark.anvil
def test_matured_short(interactive_hyperdrive: LocalHyperdrive, arbitrage_andy: LocalHyperdriveAgent):
"""Don't touch the matured short."""
# report starting fixed rate
logging.info("starting fixed rate is %s", interactive_hyperdrive.interface.calc_spot_rate())

# arbitrage it back
arbitrage_andy.open_short(bonds=FixedPoint(100_000))

interactive_hyperdrive.chain.advance_time(int(YEAR_IN_SECONDS * 2), create_checkpoints=False)

# check Andy's trades to make sure he doesn't CloseShort
event = arbitrage_andy.execute_policy_action()
event = event[0] if isinstance(event, list) else event
logging.info("event is %s", event)
assert not isinstance(event, CloseShort)

0 comments on commit e68e084

Please sign in to comment.