-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot_slippage.py
46 lines (37 loc) · 1.08 KB
/
plot_slippage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
from simulation_int_many import Curve, geometric_mean
N = 3
A = int(1000 * N**N * 10000)
gamma = int(4e-4 * 1e18)
fee = 0.7e-4
class PCurve(Curve):
def get_xcp(self):
# First calculate the ideal balance
# Then calculate, what the constant-product would be
D = self.D()
N = len(self.x)
X = [D * 10**18 // (N * p) for p in self.p]
return geometric_mean(X)
def f_slippage(A, gamma):
D = 3 * 10**18
c = PCurve(A, gamma, D, N)
p = []
amounts = []
for dDD in [1e-8] + list(np.logspace(-5, -0.5)):
dx = int(D * dDD)
x0 = c.x[0]
y0 = c.x[1]
y = c.y(x0 + dx, 0, 1)
amounts.append(dDD)
p.append(dx / (y0 - y))
p = np.array(p)
p = (p - p[0]) / p[0]
return amounts[1:], p[1:]
if __name__ == '__main__':
import pylab
for _A, _gamma in [(A, gamma), (A*10, gamma)]:
amounts, p = f_slippage(_A, _gamma)
pylab.loglog(amounts, fee + p)
pylab.xlabel('trade_size / pool_size')
pylab.ylabel('Relative price impact')
pylab.show()