-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Impermanent loss calculator python code
- Loading branch information
1 parent
7efc5b3
commit 56a59df
Showing
2 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Uniswap math functions\n", | ||
"# https://youtu.be/_asFkMz4zhw?t=25 - Uniswap V3 - Liquidity | DeFi\n", | ||
"\n", | ||
"def get_liquidity_0(x, sp, sb):\n", | ||
" return x * sp * sb / (sb - sp)\n", | ||
"\n", | ||
"def get_liquidity_1(y, sp, sa):\n", | ||
" return y / (sp - sa)\n", | ||
"\n", | ||
"def get_liquidity(x, y, sp, sa, sb):\n", | ||
" if sp <= sa:\n", | ||
" liquidity = get_liquidity_0(x, sp, sb)\n", | ||
" elif sp < sb:\n", | ||
" liquidity0 = get_liquidity_0(x, sp, sb)\n", | ||
" liquidity1 = get_liquidity_1(y, sp, sa)\n", | ||
" liquidity = min(liquidity0, liquidity1)\n", | ||
" else:\n", | ||
" liquidity = get_liquidity_1(y, sp, sa)\n", | ||
" return liquidity\n", | ||
"\n", | ||
"\n", | ||
"#\n", | ||
"# Calculate x and y given liquidity and price range\n", | ||
"#\n", | ||
"def calculate_x(L, sp, sa, sb):\n", | ||
" sp = max(min(sp, sb), sa) # if the price is outside the range, use the range endpoints instead\n", | ||
" return L * (sb - sp) / (sp * sb)\n", | ||
"\n", | ||
"def calculate_y(L, sp, sa, sb):\n", | ||
" sp = max(min(sp, sb), sa) # if the price is outside the range, use the range endpoints instead\n", | ||
" return L * (sp - sa)\n", | ||
"\n", | ||
"def calculate_a1(L, sp, sb, x, y):\n", | ||
" # https://www.wolframalpha.com/input/?i=solve+L+%3D+y+%2F+%28sqrt%28P%29+-+a%29+for+a\n", | ||
" # sqrt(a) = sqrt(P) - y / L\n", | ||
" return (sp - y / L) ** 2\n", | ||
"\n", | ||
"def calculate_a2(sp, sb, x, y):\n", | ||
" # https://www.wolframalpha.com/input/?i=solve+++x+sqrt%28P%29+sqrt%28b%29+%2F+%28sqrt%28b%29++-+sqrt%28P%29%29+%3D+y+%2F+%28sqrt%28P%29+-+a%29%2C+for+a\n", | ||
" # sqrt(a) = (y/sqrt(b) + sqrt(P) x - y/sqrt(P))/x\n", | ||
" # simplify:\n", | ||
" # sqrt(a) = y/(sqrt(b) x) + sqrt(P) - y/(sqrt(P) x)\n", | ||
" sa = y / (sb * x) + sp - y / (sp * x)\n", | ||
" return sa ** 2\n", | ||
"\n", | ||
"#\n", | ||
"# Two different ways how to calculate p_b. calculate_b1() uses liquidity as an input, calculate_b2() does not.\n", | ||
"#\n", | ||
"def calculate_b1(L, sp, sa, x, y):\n", | ||
" # https://www.wolframalpha.com/input/?i=solve+L+%3D+x+sqrt%28P%29+sqrt%28b%29+%2F+%28sqrt%28b%29+-+sqrt%28P%29%29+for+b\n", | ||
" # sqrt(b) = (L sqrt(P)) / (L - sqrt(P) x)\n", | ||
" return ((L * sp) / (L - sp * x)) ** 2\n", | ||
"\n", | ||
"def calculate_b2(sp, sa, x, y):\n", | ||
" # find the square root of b:\n", | ||
" # https://www.wolframalpha.com/input/?i=solve+++x+sqrt%28P%29+b+%2F+%28b++-+sqrt%28P%29%29+%3D+y+%2F+%28sqrt%28P%29+-+sqrt%28a%29%29%2C+for+b\n", | ||
" # sqrt(b) = (sqrt(P) y)/(sqrt(a) sqrt(P) x - P x + y)\n", | ||
" P = sp ** 2\n", | ||
" return (sp * y / ((sa * sp - P) * x + y)) ** 2\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"def calculate_P(x,y,sa,sb):\n", | ||
" p = sb*x\n", | ||
" q = y-x*sa*sb\n", | ||
" r = -sb*y\n", | ||
" return (math.pow(-1*q+(q**2 - 4*p*r),0.5)/(2*p) )\n", | ||
"\n", | ||
"def tick_price(t):\n", | ||
" return 1.0001**t\n", | ||
"\n", | ||
"def price_tick(p):\n", | ||
" return math.log(p,1.0001)\n", | ||
"\n", | ||
"def getYFromX(x,sa,sb,sp):\n", | ||
" return x * (sp - sa) * sp *sb /(sb - sp)\n", | ||
"\n", | ||
"# ? P is Y/X\n", | ||
"def simulator_calc_L(x,pa,pb,p):\n", | ||
" sa = pa ** 0.5\n", | ||
" sb = pb ** 0.5\n", | ||
" sp = p ** 0.5\n", | ||
" if p >= pb:\n", | ||
" y=x\n", | ||
" x=0\n", | ||
" L = get_liquidity(x, y, sp, sa, sb)\n", | ||
" elif p <= pa:\n", | ||
" y = 0\n", | ||
" else:\n", | ||
" print(\"sim2\", sa, sb, sp, x)\n", | ||
" y = getYFromX(x, sa, sb, sp)\n", | ||
" L = get_liquidity(x, y, sp, sa, sb)\n", | ||
"\n", | ||
" return L,y\n", | ||
"\n", | ||
"# ! Requires X token to be a stable token\n", | ||
"def getXY(capitalDollar, token0Decimals, token1Decimals, Pxusd, Pyusd, sp, sa, sb):\n", | ||
" # captal = x * Pxusd / xdecimals + y * Pyusd / yDecimals ----- eq1\n", | ||
" # replace y with x from getYFromX\n", | ||
"\n", | ||
" factor1 = Pxusd / (10 ** token0Decimals)\n", | ||
" factor2 = Pyusd / (10 ** token1Decimals)\n", | ||
" factor3 = (sp - sa) * sp * sb / (sb - sp)\n", | ||
"\n", | ||
" x = capitalDollar / (factor1 + factor3 * factor2)\n", | ||
" y = getYFromX(x, sa, sb, sp)\n", | ||
" return x, y" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.11" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.