# Gamma Swap by Deri Protocol

## Introduction

In [the previous paper](/library/academy/gamma-swap/introducing-gamma-swap.md), we introduced a new type of derivative, Gamma Swap. As discussed, the trading of Gamma swap depends on a composite trading venue consisting of two (virtual) primitive trading venues for power perps and perpetual futures. This paper explains the implementation of such a composite trading venue by the [DPMM of Deri Protocol](https://github.com/deri-protocol/whitepaper/blob/master/deri_v3_whitepaper.pdf).

## Composite DPMM of Gamma Swap

Following the terminology in the previous paper, in this paper, $$i$$  refers to the index price while $$M$$  refers to the different kinds of mark prices. **The core role of DPMM is to determine the mark prices based on the index price input and the trading activities.** Everything else (e.g., PnL, funding fees) is then based on the mark prices.

Internally, 1 unit of Gamma swap is treated as a portfolio of powers and perps. DPMM simultaneously keeps track of the power and perp positions and calculates their respective mark prices. When 1 unit of long Gamma swap is entered, the DPMM adds 1 unit of power and $$-\frac{2i\_0}{1-h\_0T}$$  units of perp to the two positions, respectively. One could also enter a Gamma swap position with a specified entry price $$i\_s$$ . In that case, DPMM would add 1 unit of power and $$-\frac{2i\_s}{1-h\_0T}$$  units of perp to the two positions, respectively. Denote the total net position of power as $$n$$  and total net position of perp as $$m$$ , then DPMM calculates the mark price of perp as follows:

$$
\frac{M\_{perp}-i}{i}=k\_{perp}\left(n\cdot\frac{2i}{1-hT}+m\right)=k\_{perp}m'
$$

where $$i$$ is the index price of the underlying, and $$m'=n\cdot\frac{2i}{1-hT}+m$$. Please note that:

* This is just the classical DPMM for perp with $$m'=\left(n\cdot\frac{2i}{1-hT}+m\right)$$  as the total net position, instead of just $$m$$.
* Notice that $$m'=0$$   at a “balanced point”, at which the liquidity pool has no Delta exposure. For example, immediately after the first position was opened, we have $$n =1$$   and $$m=-\frac{2i\_0}{1-h\_0T}$$  , hence $$m'=0$$ . A “balanced point” is equivalent to the scenario of zero net position in a regular DPMM of perp.
* A trading action reducing $$|m'|$$ (and thus bringing the system to the “balanced point”) enjoys a negative slippage of $$M\_{perp}$$  . However, since opening a new position at the current price (without specifying entry price) does not change $$m'$$  , it does not enjoy a negative slippage. This is a bit different from the case of perp DPMM, in which a new position reducing the total net position always enjoys negative slippage.

And the mark price of power is:

$$
\frac{M\_{power}-i\_{power}}{i\_{power}}=k\_{power}n
$$

where $$i\_{power}$$  is the theoretical price of the power, calculated just like that in the [power perp DPMM](https://github.com/deri-protocol/whitepaper/blob/master/Pricing_Continuously_Funded_Power_Perpetuals.pdf):

$$
i\_{power}=\frac{i^2}{1-hT}
$$

Now we have a composite DPMM that, at any point, takes the state variables $$(n, m, i, \sigma)$$  to determine $$(M\_{perp}, M\_{power})$$  and hence gives$$M\_{gamma}$$  as

$$
M\_{gamma}=M\_{power}-\frac{2i\_0}{1-h\_0T}M\_{perp}+\frac{i\_0^2}{1-h\_0T}
$$

$$M\_{gamma}$$determines the trading cost of every trade and consequently determines PnL.

## Funding Calculation&#x20;

1 unit of Gamma swap has composite funding based on the power funding and the perp funding:

$$
F\_{gamma}=F\_{power}+\left(\frac{2i}{1-hT}-\frac{2i\_0}{1-h\_0T}\right)F\_{perp}
$$

where $$(i\_0, h\_0)$$  are the $$(i,h)$$   as of the position entry point, and

$$
\begin{align\*}
F\_{power}=&\frac{M\_{power}-i^2}{T}\\
F\_{perp}=&\frac{M\_{perp}-i}{T}=i\cdot\frac{k\_{perp}}{T}\left(n\cdot\frac{2i}{1-hT}+m\right)
\end{align\*}
$$

$$F\_{power}$$   is simply handled the same way as power perps.

Whereas the handling of $$F\_{perp}$$ is somewhat tricky, as the coefficient of the $$F\_{perp}$$ term is constantly changing with $$i$$ . Essentially, this can be understood as every unit of Gamma swap has $$-\frac{2i\_0}{1-h\_0T}$$   “real futures” as well as $$\frac{2i}{1-hT}$$   “virtual futures”, and hence totally $$\left(\frac{2i}{1-hT}-\frac{2i\_0}{1-h\_0T}\right)$$   effective futures. The tricky part is the constantly varying virtual part. Because of this constantly varying virtual part, the perp funding should be calculated **per Gamma swap, instead of per futures**.

By definition, the exact funding fee of 1 unit of Gamma swap accumulated over the time period $$(t\_1, t\_2)$$   should be the integral of $$F\_{perp}$$  over this period.

$$
\begin{align\*}
\text{Funding}*{perp}(t\_1,t\_2)=&\int*{t\_1}^{t\_2}\left(\frac{2i}{1-hT}-\frac{2i\_0}{1-h\_0T}\right) F\_{perp}dt
\\
\=&\int\_{t\_1}^{t\_2}\left(\frac{2i}{1-hT}\right) F\_{perp}dt-\frac{2i\_0}{1-h\_0T}\int\_{t\_1}^{t\_2} F\_{perp}dt
\\
\=&
A|*{t\_1}^{t\_2}-
\frac{2i\_0}{1-h\_0T}B|*{t\_1}^{t\_2}
\end{align\*}
$$

where $$A|*{t\_1}^{t\_2}$$  and $$B|*{t\_1}^{t\_2}$$  are the accumulated increment of $$A$$   and $$B$$   over $$(t\_i,t\_2]$$  :

$$
\begin{align\*}
A|*{t\_1}^{t\_2}=&\int*{t\_1}^{t\_2}\frac{2i}{1-hT}\cdot F\_{perp}dt=\int\_{t\_1}^{t\_2}\frac{2i}{1-hT}\cdot i\cdot\frac{k\_{perp}}{T}\left(n\cdot\frac{2i}{1-hT}+m\right)dt\\
B|*{t\_1}^{t\_2}=&\int*{t\_1}^{t\_2} F\_{perp}dt=\int\_{t\_1}^{t\_2} i\cdot\frac{k\_{perp}}{T}\left(n\cdot\frac{2i}{1-hT}+m\right)dt\\
\end{align\*}
$$

Please note that the two accumulating variables $$A$$   and $$B$$   are universal to all positions while $$\frac{i\_o}{1-h\_0T}$$   is position-specific. We need to track the accumulations of $$A$$ and $$B$$ separately and then for any position we can calculate its accumulated funding fee with its own value of $$\frac{i\_0}{1-h\_0T}$$  . That is, for a position of $$x$$ gamma swaps entered at $$t\_a$$   with entry values $$(i\_0, h\_0)$$  , its accumulated funding fee from $$t\_1$$ to $$t\_2$$ is:

$$
\left\[(A\_{t\_2}-A\_{t\_1})-(B\_{t\_2}-B\_{t\_1})\frac{2i\_0}{1-h\_0T}\right]\cdot x
$$

In practice, it is impossible to rigorously calculate $$A$$ and $$B$$ due to their path-dependencies. Hence, we need some numerical approximations.

#### Approximation Method 1

We assume the funding over the time period $$(t\_{i-1},t\_i]$$   is accumulated at the speed as of $$t\_i$$  . Essentially this assumes $$i=i(t\_2), \forall i \in(t\_{i-1}, t\_i]$$ . With this approximation, the two integrals over $$(t\_i,t\_2]$$   degenerate into simple multiplications:

$$
\begin{align\*}
A|*{t\_1}^{t\_2}=&\frac{2i}{1-hT}\cdot i \cdot\frac{k*{perp}}{T}\left(n\cdot\frac{2i}{1-hT}+m\right)\Delta t\\
B|*{t\_1}^{t\_2}=&
i \cdot\frac{k*{perp}}{T}\left(n\cdot\frac{2i}{1-hT}+m\right)\Delta t
\end{align\*}
$$

#### Approximation Method 2

A more reasonable approximation is to assume the index price $$i$$ changes linearly over $$(t\_1, t\_2]$$  :

$$
i(t)=i\_1+(i\_2-i\_1)\frac{t-t\_1}{t\_2-t\_1}, \forall i \in(t\_{i-1}, t\_i]
$$

Then we can calculate the accumulating variables $$A$$  and $$B$$   as follows:

$$
\begin{align\*}
A|*{t\_1}^{t\_2}=&\frac{2}{1-hT}\cdot\frac{k*{perp}}{T}\left(n\cdot\frac{2}{1-hT}\cdot\overline {i^3}+m\cdot\overline {i^2}\right)\Delta t\\

B|*{t\_1}^{t\_2}=&
\frac{k*{perp}}{T}\left(n\cdot\frac{2}{1-hT}\cdot\overline {i^2}+m\cdot\overline {i}\right)\Delta t
\end{align\*}
$$

where $$\overline {i^3}$$, $$\overline {i^2}$$ and $$\overline i$$   are the time-weighted average value of $$i^3$$  , $$i^2$$   and $$i$$   over $$(t\_1, t\_2]$$  , respectively:

$$
\begin{align\*}
\overline{i^3}&=\frac{1}{\Delta t}\int\_{t\_1}^{t\_2}i^3dt=\frac{(i\_1+i\_2)(i\_1^2+i\_2^2)}{4}\\

\overline{i^2}&=\frac{1}{\Delta t}\int\_{t\_1}^{t\_2}i^2dt=\frac{i\_1^2+i\_1i\_2+i\_2^2}{3}\\

\overline{i}&=\frac{1}{\Delta t}\int\_{t\_1}^{t\_2}idt=\frac{i\_1+i\_2}{2}\\
\end{align\*}
$$

With this approximation, the power funding component can be calculated similarly:

$$
\text{Funding}*{power}(t\_1,t\_2) = \frac{(k*{power}n+hT)}{(1-hT)T}\cdot \overline{i^2} \Delta t= A\_{power}|\_{t\_1}^{t\_2}
$$

where $$A\_{power}|\_{t\_1}^{t\_2}$$   is the accumulating variable tracking the accumulation of power funding. In practice, the two  $$A$$ variables of power and futures can be combined into a single variable.

## Summary

The [previous paper](/library/academy/gamma-swap/introducing-gamma-swap.md) introduces the concept of Gamma Swap, a new type of derivative depending on a composite trading venue consisting of two primitive trading venues for power perps and perpetual futures. This paper explains the implementation of such a composite trading venue by the DPMM of Deri Protocol. With the DPMM for Gamma Swap constructed, now we are ready to roll out a comprehensive solution for Gamm Swap.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.deri.io/library/academy/gamma-swap/gamma-swap-by-deri-protocol.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
