# 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](https://contango-2.gitbook.io/exchange/protocol/theoretical-pricing)).

<table><thead><tr><th width="150"> Side</th><th width="396.2857142857143">Price to close a position</th></tr></thead><tbody><tr><td>Long</td><td><span class="math">P_{C,L}=  \dfrac{S_{S}}{{(1+r_{B,b})}^T}   + D \bigg( 1 -  \dfrac{1}{{(1+r_{Q,l})}^T} \bigg)</span></td></tr><tr><td>Short</td><td><span class="math">P_{C,S}=  \dfrac{S_{L}}{{(1+r_{B,l})}^T}   + L \bigg( 1 -  \dfrac{1}{{(1+r_{Q,b})}^T} \bigg)</span></td></tr></tbody></table>

## Example

Let's consider the close of the long and short positions presented in the numerical example in [position opening](https://contango-2.gitbook.io/exchange/protocol/protocol-pricing/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$$.&#x20;

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$$.
