ðŸŒ How the Sausage Gets Made ðŸŒ

### Data Acquisition

All data used in these simulations comes directly from the DyDx API in the browser. There is no reliance on third-party architecture, so you can fork this site and make one of your own!

### Quick Formulas

Here are some quick formulas for the simpler metrics that were put to use here. In the following

- Position Size: \(s_\alpha\) is the position size of perpetual \(\alpha\) that one has in their portfolio
- Price: \(p_\alpha\) is the price of perpetual \(\alpha\)
- E: account equity
- Position Open Interest: \(I_\alpha = |s_\alpha| p_\alpha \) is the amount of notional value for perpetual \(\alpha\)
- Account Open Interest: \(I = \sum_{\alpha} I_\alpha \)
- Maintenance Margin Requirement: \(M_\alpha\) is the maintenance margin requirement; to avoid liquidation, ensure that \(E > \sum_{\alpha} M_\alpha I_\alpha\)
- Collateralization Ratio: \(C = E / I\) Amount of equity you have, divided by your open intest
- Crypto Net Exposure: \(X = \sum_\alpha s_\alpha p_\alpha\), is the number of notional dollars one is exposed to of crypto. This is useful for someone who wants a fixed dollar amount of exposure to crypto.
- Crypto Net Leverage: \(X_l = X/E\), is how leveraged your DyDx account is on crypto
- Leverage Ratio: \(L = 1/C\) is the leverage that one has in their perpetual account
- Returns (log): return from time 0 to time T is calculated as \( \log (p_T / p_0) \)
- Drift (\(\mu\)): calculated as the mean of log returns for a given window; here, the window is the previous 30 days; this is from hourly data in the single-asset calculator, daily data in the multi-asset calculator
- Returns Volatility (\(\sigma\)): the standard deviation of log returns for a given window; here, the window is the previous 30 days; this is from hourly data in the single-asset calculator, daily data in the multi-asset calculator
- Sharpe Ratio: mean of log-returns divided by stdev of log-returns, i.e. \(\mu/\sigma\). A higher Sharpe ratio is good, assuming one has risk-averse preferences.
- Probability of Passing a Threshold: see sections below for in-depth description

### Probability of Liquidation

#### A Primer on Brownian Motion

The probability of liquidation comes from a **strong** assumption that the price of perpetual contracts can be accurately modeled as a discretized "Geometric Brownian Motion". In simple terms, a Geometric Brownian Motion (GBM for short) is a process where a variable \(S\), which represents a perpetual's price, changes over time, and at each tiny time interval \(\Delta t \), \(S\) changes by \( \mu + \sigma \mathcal{N}(0, 1) \) percent. Here, \(\mu \) is called the "percentage drift", and we can think of it like the driving force that pushes \(S\) up (if \(\mu\) is positive) or down (if \(\mu\) is negative) over time; \(\sigma\) is called the "percentage volatility", and you can think of it as the typical amount of deviation that you see from in change in each time step (i.e. the standard deviation of the change in \(S\) in each time step); \( \mathcal{N}(a, b)\) denotes a normal distribution with mean \(a\) and standard deviation \(b\), and since this is a random variable in our formula, its value *is not* constant on each time-step.

#### Probability of Liquidation via Geometric Brownian Motion

In order to find the probability of liquidation for a perpetual future, we can take the following steps:

- Model the price of the perpetual future as a geometric brownian motion \(M(\mu, \sigma)\), where \(\mu\) and \(\sigma\) are approximated with historical data.
- Find the probability \(p\) that \(M\) would pass the liquidation threshold before an input time \(\tau\).
- Approximate that the perpetual future's probability of passing the liquidation threshold before time \(\tau\) is equal to \(p\).

**This approach is not perfect!**It is merely

*one way*of approximating the price trajectory of an asset over a fixed time interval into the future, and it is not robust enough to incorporate black-swan like events. However, it should be noted that this model is very common among traders who deal in volatility, and it is one of the fundamental hypotheses present in the Black-Scholes-Merton model, a widely-used model for pricing options.

##### Estimating \(\mu\) and \(\sigma\)

To estimate \( \mu \), we take the following steps:

- Retrieve the past 7 days' hourly open-high-low-close candle data from DyDx's public api, and also get the past 7 days' hourly funding rates.
- Compute the hourly percentage return for all 7-days worth of hours, according to: \[ R_t = (C_t - C_{t-1}) + F_t \] , where \(R_t\) is the return at the end of time \(t\), \(C_t\) is the close at the end of time \(t\), \(C_{t-1}\) is the close at the end of time \(t-1\), and \(F_t\) is the funding rate percentage that pays out at the end of time \(t\).
- Compute the mean of these hourly returns, and call it \(\hat{\mu}\). This is our approximation of the mean of hourly returns.

##### Probability of \(M\)\((\hat{\mu}, \hat{\sigma}) \) Passing Liquidation Threshold

To calculate the probability that \(M\) passes the liquidation price $\(K\) before a time \(\tau\) can be computed by summing up the probability that the \(M\) crosses $\(K\) for the first time at time \(t\) for all \(t < \tau\). In other words, if \(f(t; \mu, \sigma)\) is the probability density of \(M\) passing $\(K\) for the first time at time \(t\), then we know that the probability of \(M\) crossing $\(K\) at some point before time \(\tau\) is given by \[\Pr(\text{passing before } \tau) = \int_{t=0}^{t=\tau} f(t; \mu, \sigma) dt .\] Great! But how do we calculate \(f\)? It turns out that the first passage time of \(M\) is given by an inverse gaussian distribution (ref)! That is, \(f\) is the probability density function of the Inverse Gaussian (IG) distribution with mean parameter \(\log \left(K/\mu \right)\) and shape parameter \(\left(\log\left(K/\sigma\right)\right)^2\). When \(\log(\mu)\) is 0, we simply take the limit of the IG as mean tends toward infinity, which is well-defined. Furthermore, for those familiar with statistics, our formula above gives us the cumulative distribution function (CDF) associated with \(f,\) and that CDF is known (ref): \[ CDF(x; \psi, \lambda) = \Phi \left( \sqrt{\frac{\lambda}{x}} \left(\frac{x}{\psi} - 1 \right) \right) + \exp \left(\frac{2\lambda}{\psi} \right) \Phi \left( -\sqrt{\frac{\lambda}{x}} \left(\frac{x}{\psi}+1 \right) \right), \] where \(\psi\) is the mean parameter, \(\lambda\) is the shape parameter, and \(\Phi\) is the standard normal CDF (cumulative distribution function of a random distribution with mean 0 and standard deviation 1). We estimate the probability of passing the threshold before time \(\tau\) by calculating \(CDF(\tau; \psi, \lambda) \) with the substitution \(\psi = \log \left(K/\hat{\mu} \right)\ \) and \(\lambda = \left(\log\left(K/\hat{\sigma}\right)\right)^2 \).

### Generalizing to a Portfolio

#### The Problem

In the previous section, we were able to find the probability of liquidation for a single-position portfolio by simply referring to the Brownian motion of the underlying perpetual's price changes and funding rates. In doing so, we were able to assume that since the perpetual follows a GBM, then so does the equity account of our single-asset portfolio. But can we make the same claim about the equity account in a portfolio?

Well, the answer is.. sorta? Essentially, a portfolio is composed of sum of open positions \(S_\alpha\) in a sequence of perpetuals \alpha. That is, if we call the portfolio value at time \(t\) \(P_t\), then \(P_t = \sum_{\alpha} S_\alpha p_{\alpha,t}\), where \(p_{\alpha,t}\) is the price of perpetual \(\alpha\) at time \(t\). The question is: can the change in this portfolio price be modeled by a GBM, given that we assume \(p_{\alpha,t}\) follows a GBM?

If we are looking for 0 approximation, then the clear answer is "no". A GBM has a distribution of returns which follow a log normal distribution. \(P_t\), on the other hand, is a weighted sum of items with log normally distributed returns, so its returns come from a sum-of-log-normal distribution. And, as it turns out, we know approximately jack shit about the closed-form of sums of log-normal distributions ref. However, we *do* know that the sum of log-normal distributions comes out to be well-approximated by a log-normal distribution! This is an interesting fact that also happens to apply to normal distributions, but not to distributions in general. This approximation works best for the sum of a small number of log-normal distributions, all with similar variance and mean parameters. In our case, we are taking a weighted sum of log-normal distributions of returns from perpetuals, so I believe the approximation is kosher. For this reason, I decided that it would be OK to model the distribution of portfolio returns as following a log-normal distribution, and thus apply the techniques from the single-asset portfolio case for volatility parameter estimation and then probability of liquidation for a given portfolio.