delvtech / hyperdrive

An automated market maker for fixed and variable yield with on-demand terms.
Apache License 2.0
25 stars 3 forks source link

Add sdk method for `calculate_implied_rate_for_open_short` #996

Closed DannyDelott closed 2 months ago

DannyDelott commented 2 months ago

Adding a calculate_implied_rate_for_open_short to get the implied rate for opening a short will be very useful to show in the UI.

https://github.com/delvtech/hyperdrive/blob/main/crates/hyperdrive-math/src/short/open.rs

From @jrhea:

it would be really helpful to show the implied rate when opening a short. it would be good bc it could inform users that shorts (at the current rate) are not profitable and it would also communicate the potential for multiplied exposure when the fixed is less than the variable

From @MazyGio:

there's currently no indication about how shorting at 21.90% fixed rate vs 3.6% variable rate seems like a very risky bet to take

https://discord.com/channels/754739461707006013/799343723086479421/1230937901823692881

dpaiton commented 2 months ago

I'm confused about how it is different from calculate_spot_rate_after_short https://github.com/delvtech/hyperdrive/blob/main/crates/hyperdrive-math/src/short/open.rs#L100

edit: ok I get it; this is the implied rate a short has to pay which is different from the spot rate after the short is opened. It'll be a function of base paid, fees, and the interest adjustment.

jalextowle commented 2 months ago

We talked about the algorithm to compute this offline. Let's assume that the amount of bonds that we're shorting is given by $\Delta y$.

  1. Preview the short to get the amount of base the short needs to deposit $\Delta x$. This preview should include the max loss and the fees, but it shouldn't include the variable interest that is prepaid (the short will receive this back at maturity).
  2. Calculate the effective price as $p_{effective} = \tfrac{\Delta y - \Delta x}{\Delta y}$.
  3. Calculate the effective fixed rate as $r{effective} = \tfrac{1 - p{effective}}{p_{effective} \cdot t}$, where $t$ is the position duration in year fractions.
  4. Let $r{variable}$ be the variable rate. We can calculate the implied rate as $r{implied} = \tfrac{r{variable} - r{effective}}{r_{effective}}$.

For example, let's say that $r{effective} = 1%$ and $r{variable} = 10%$. The short will pay a deposit 1 base for every 100 bonds they short. They will receive 10 base at maturity for every 100 bonds that they short. This means that their implied rate is 900% which matches the formula of $\tfrac{0.1 - 0.01}{0.01} = 9$.