| Title: | Calculate Impermanent Loss in Automated Market Maker (AMM) Liquidity Pools |
| Version: | 0.1.0 |
| Description: | Computes the key metrics for assessing the performance of a liquidity provider (LP) position in a weighted multi-asset Automated Market Maker (AMM) pool. Calculates the nominal and percentage impermanent loss (IL) by comparing the portfolio value inside the pool (based on the weighted geometric mean of price ratios) against the value of simply holding the assets outside the pool (based on the weighted arithmetic mean). The primary function, ‘impermanent_loss()', incorporates the effect of earned trading fees to provide the LP’s net profit and loss relative to a holding strategy, using a methodology derived from Tiruviluamala, N., Port, A., and Lewis, E. (2022) <doi:10.48550/arXiv.2203.11352>. |
| License: | GPL-3 |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.1 |
| Imports: | ggplot2 |
| Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0) |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| NeedsCompilation: | no |
| Packaged: | 2025-12-06 16:06:01 UTC; amber |
| Author: | Amber Krause |
| Maintainer: | Amber Krause <amber32k@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2025-12-11 13:40:11 UTC |
Impermanent Loss Calculator for Weighted AMM Pools
Description
Calculates the Nominal Impermanent Loss (IL), Percentage IL, and Net Profit and Loss (PnL) for a liquidity position in a weighted Automated Market Maker (AMM) pool. This function is crucial for assessing the performance of liquidity provider positions in protocols like Balancer and Uniswap.
Usage
impermanent_loss(
prices_old,
prices_new,
weights,
investment,
fees,
plot = FALSE
)
Arguments
prices_old |
A numeric vector of initial prices for each asset. |
prices_new |
A numeric vector of new prices for each asset. |
weights |
A numeric vector of weights (must sum to 1) corresponding to the assets. |
investment |
The initial total dollar value invested in the pool. |
fees |
The total dollar value of trading fees earned by the LP during the period. |
plot |
A logical value. If TRUE, generates a bar chart comparing the value if held, value in the pool, and the net value (pool + fees). |
Value
A list containing the core metrics:
-
Value if held: Total current value if assets were held outside the pool. -
Value in pool: Total current value of the tokens in the pool. -
impermanent_loss_percent: The IL as a percentage of the held portfolio value. -
Nominal impermanent loss: The IL as a dollar-value opportunity loss. -
Fee offset ($): The total dollar fees earned. -
Net gain: The LP's net PnL (Fees MINUS Nominal IL) relative to holding.
References
Tiruviluamala, N., Port, A., & Lewis, E. (2022). A general framework for impermanent loss in automated market makers. arXiv preprint arXiv:2203.11352.
Examples
library(impermanentlosscalc)
library(ggplot2)
# Example 1: 3-Asset Unbalanced Pool (Weights: 30/20/50)
impermanent_loss(
prices_old = c(10, 20, 40),
prices_new = c(9, 22, 35),
weights = c(0.3, 0.2, 0.5),
investment = 1000,
fees = 10,
plot = TRUE
)
# Example 2: No price change, demonstrating IL is zero.
impermanent_loss(
prices_old = c(3, 3),
prices_new = c(3, 3),
weights = c(0.5, 0.5),
investment = 500,
fees = 0
)