# Position closing

Once a position is open, a trader could choose to close it before expiry. If that's the case, the protocol needs to exit the lending position, which at expiry would have represented an amount
$L$
(principal + interest), and repay the owed debt, which at expiry would have represented an amount
$D$
(principal + interest).

## Pricing

The table below presents the price at which a trader could close a long position at a price
$P_{C,L}$
or a short position at a price
$P_{C,S}$
(other notations have been introduced in theoretical pricing).
Side
Price to close a position
Long
$P_{C,L}= \dfrac{S_{S}}{{(1+r_{B,b})}^T} + D \bigg( 1 - \dfrac{1}{{(1+r_{Q,l})}^T} \bigg)$
Short
$P_{C,S}= \dfrac{S_{L}}{{(1+r_{B,l})}^T} + L \bigg( 1 - \dfrac{1}{{(1+r_{Q,b})}^T} \bigg)$

## Example

Let's consider the close of the long and short positions presented in the numerical example in position opening:
• A trader wants to immediately close the long position with an open price
$P_{O,L}= 100.59 \: DAI$
. Given one could borrow ETH at a yearly fixed rate of
$r_{B,b}=3.10\%$
​ and lend DAI at a yearly fixed rate
$r_{Q,l}=9.90\%$
, and given the total debt to reimburse at expiry is
$D=50.59 \: DAI$
, the price at which a trader could immediately close the position is:
$P_{C,L}= \dfrac{99.90}{{(1+0.0310)}^{0.25}} + 50.59 * \bigg( 1 - \dfrac{1}{{(1+0.0990)}^{0.25}} \bigg) = 100.32 \:DAI$
• A trader wants to immediately close the short position with an entry price
$P_{O,S} =102.70 \: DAI$
. Given one could lend ETH at a yearly fixed rate of
$r_{B,l}=2.90\%$
​ and borrow DAI at a yearly fixed rate
$r_{Q,b}=10.10\%$
, and given the total money to get back from lending (principal + interest) is
$L=152.70 \:DAI$
, the price at which a trader could immediately close the position is:
$P_{C,S}= \dfrac{100.10}{{(1+0.0290)}^{0.25}} + 152.70 * \bigg( 1 - \dfrac{1}{{(1+0.1010)}^{0.25}} \bigg) = 103.02 \:DAI$

## Demonstration

### Long

Let's consider a trader who wants to immediately close a long position of
$1 \:ETH$
(numerical applications rely on the above example):
1. The protocol gets back the base currency which was lent,
$\dfrac{1}{{(1+r_{B,b})}^T}$
, i.e.
$0.9924 \:ETH$
.
2. This base currency is swapped back to the quote currency,
$\dfrac{S_{S}}{{(1+r_{B,b})}^T}$
, i.e.
$99.14 \:DAI$
.
3. The protocol buys back the debt
$D$
, today worth
$\dfrac{D}{{(1+r_{Q,l})}^T}$
, i.e.
$49.41 \: DAI$
.
4. For closing the position earlier, the trader will get back money on the debt,
$D - \dfrac{D}{{(1+r_{Q,l})}^T}$
or
$D \bigg( 1 - \dfrac{1}{{(1+r_{Q,l})}^T} \bigg)$
, i.e.
$1.18 \: DAI$
.
5. Hence the money the trader gets back for closing the long position is
$\dfrac{S_{S}}{{(1+r_{B,b})}^T} + D \bigg( 1 - \dfrac{1}{{(1+r_{Q,l})}^T} \bigg)$
, i.e
$100.32 \:DAI$
.

### Short

Let's consider a trader who wants to immediately close a short position of
$1 \:ETH$
(numerical applications rely on the above example):
1. The protocol needs
$\dfrac{1}{{(1+r_{B,l})}^T} \: ETH$
to close the debt, i.e.
$0.9929 \:ETH$
.
2. Hence the protocol needs
$\dfrac{S_{L}}{{(1+r_{B,l})}^T} \: DAI$
to close the debt, i.e.
$99.39 \: DAI$
.
3. On the other hand, the protocol gets back
$\dfrac{L}{{(1+r_{Q,b})}^T} \: DAI$
from lending, i.e.
$149.07 \: DAI$
.
4. The money lost in lending for closing the position earlier is
$L-\dfrac{L}{{(1+r_{Q,b})}^T} \: DAI$
or
$L \bigg(1-\dfrac{1}{{(1+r_{Q,b})}^T} \bigg) \: DAI$
, i.e.
$3.63 \: DAI$
.
5. Hence the money the trader gets back for closing the short position is
$\dfrac{S_{L}}{{(1+r_{B,l})}^T} + L \bigg( 1 - \dfrac{1}{{(1+r_{Q,b})}^T} \bigg)$
, i.e.
$103.02\:DAI$
.