forked from atiselsts/uniswap-lp-articles-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_article_6.py
executable file
·705 lines (536 loc) · 24.6 KB
/
plot_article_6.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#!/usr/bin/env python
#
# This plots the figures for the article on liquidity relocation.
#
import matplotlib.pyplot as pl
import numpy as np
from ing_theme_matplotlib import mpl_style
import v2_math
import v3_math
from math import sqrt
# Constants for the LP positions
INITIAL_PRICE = 100
RANGE_FACTOR = 1.1
# select the value such that at 50:50 HODL we have 1.0 of the volatile asset X
INITIAL_VALUE = 2 * INITIAL_PRICE
INITIAL_X = INITIAL_VALUE / INITIAL_PRICE / 2
INITIAL_Y = INITIAL_VALUE / 2
# Constants for the Gaussian LP
# should be an odd number; the more positions, the better the gaussian is simulated
NUM_GAUSSIAN_POSITIONS = 7
# Constants for simulations
# similar to the 1-day volatility for ETH-USD
SIGMA = 0.05
# set to 0.0 to get a martingale
ZERO_MU = +0.000
# set to nonzero to simulate directional price movements
POSITIVE_MU = +0.003
# assume 12 second blocks as in the mainnet
BLOCKS_PER_DAY = 86400 // 12
NUM_DAYS = 365
# assume 0.3% swap fee
SWAP_FEE = 0.3 / 100
NUM_SIMULATIONS = 10000
############################################################
#
# Use geometrical Brownian motion to simulate price evolution.
#
def get_price_path(sigma_per_day, mu, blocks_per_day=BLOCKS_PER_DAY, M=NUM_SIMULATIONS):
np.random.seed(123) # make it repeatable
T = NUM_DAYS
n = T * blocks_per_day
# calc each time step
dt = T/n
# simulation using numpy arrays
St = np.exp(
(mu - sigma_per_day ** 2 / 2) * dt
+ sigma_per_day * np.random.normal(0, np.sqrt(dt), size=(M, n-1)).T
)
# include array of 1's
St = np.vstack([np.ones(M), St])
# multiply through by S0 and return the cumulative product of elements along a given simulation path (axis=0).
St = INITIAL_PRICE * St.cumprod(axis=0)
return St
############################################################
def evaluate_static(all_prices, range_factor):
all_divloss = []
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
price_a = INITIAL_PRICE / range_factor
price_b = INITIAL_PRICE * range_factor
L = v3_math.get_liquidity(INITIAL_X, INITIAL_Y,
sqrt(INITIAL_PRICE),
sqrt(price_a), sqrt(price_b))
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
Vfinal = v3_math.position_value_from_liquidity(L, prices[-1], price_a, price_b)
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def evaluate_full_rebalancing(all_prices, range_factor):
all_divloss = []
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
price_a = INITIAL_PRICE / range_factor
price_b = INITIAL_PRICE * range_factor
L = v3_math.get_liquidity(INITIAL_X, INITIAL_Y,
sqrt(INITIAL_PRICE),
sqrt(price_a), sqrt(price_b))
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
for p in prices:
if not (price_a <= p <= price_b):
sp = sqrt(p)
sa = sqrt(price_a)
sb = sqrt(price_b)
# amounts before rebalancing
x = v3_math.calculate_x(L, sp, sa, sb)
y = v3_math.calculate_y(L, sp, sa, sb)
v_old = v3_math.position_value(x, y, p)
# amounts after rebalancing
y = v_old / 2
x = y / p
# sanity check
v_new = x * p + y
assert v_new - 1e-8 < v_old < v_new + 1e-8
price_a = p / range_factor
price_b = p * range_factor
sa = sqrt(price_a)
sb = sqrt(price_b)
L = v3_math.get_liquidity(x, y, sp, sa, sb)
# sanity check
v_new = v3_math.position_value_from_liquidity(L, p, price_a, price_b)
assert v_new - 1e-8 < v_old < v_new + 1e-8
Vfinal = v3_math.position_value_from_liquidity(L, prices[-1], price_a, price_b)
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def evaluate_two_sided(all_prices, range_factor, do_rebalance):
all_divloss = []
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
price_low_a = INITIAL_PRICE / (range_factor ** 2)
price_low_b = INITIAL_PRICE / range_factor
price_high_a = INITIAL_PRICE * range_factor
price_high_b = INITIAL_PRICE * (range_factor ** 2)
L_low = v3_math.get_liquidity(0, INITIAL_Y,
sqrt(INITIAL_PRICE),
sqrt(price_low_a), sqrt(price_low_b))
# since the ranges are symmetric, we expect L_low == L_high
L_high = L_low
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
if do_rebalance:
for p in prices:
if not (price_low_a <= p <= price_high_b):
sp = sqrt(p)
sa_low = sqrt(price_low_a)
sb_low = sqrt(price_low_b)
sa_high = sqrt(price_high_a)
sb_high = sqrt(price_high_b)
if p < price_low_a:
# amounts before rebalancing
x_low = v3_math.calculate_x(L_low, sp, sa_low, sb_low)
y_low = v3_math.calculate_y(L_low, sp, sa_low, sb_low)
v_old_low = v3_math.position_value(x_low, y_low, p)
# amounts after rebalancing
y_low = v_old_low / 2
x_low = y_low / p
price_low_a = p / range_factor
price_low_b = p * range_factor
sa_low = sqrt(price_low_a)
sb_low = sqrt(price_low_b)
L_low = v3_math.get_liquidity(x_low, y_low, sp, sa_low, sb_low)
x_high = v3_math.calculate_x(L_high, sp, sa_high, sb_high)
price_high_a = p * (range_factor ** 2)
price_high_b = p * (range_factor ** 3)
sa_high = sqrt(price_high_a)
sb_high = sqrt(price_high_b)
L_high = v3_math.get_liquidity(x_high, 0, sp, sa_high, sb_high)
else:
# amounts before rebalancing
x_high = v3_math.calculate_x(L_high, sp, sa_high, sb_high)
y_high = v3_math.calculate_y(L_high, sp, sa_high, sb_high)
v_old_high = v3_math.position_value(x_high, y_high, p)
# amounts after rebalancing
y_high = v_old_high / 2
x_high = y_high / p
price_high_a = p / range_factor
price_high_b = p * range_factor
sa_high = sqrt(price_high_a)
sb_high = sqrt(price_high_b)
L_high = v3_math.get_liquidity(x_high, y_high, sp, sa_high, sb_high)
y_low = v3_math.calculate_y(L_low, sp, sa_low, sb_low)
price_low_a = p / (range_factor ** 3)
price_low_b = p / (range_factor ** 2)
sa_low = sqrt(price_low_a)
sb_low = sqrt(price_low_b)
L_low = v3_math.get_liquidity(0, y_low, sp, sa_low, sb_low)
Vfinal_low = v3_math.position_value_from_liquidity(L_low, prices[-1], price_low_a, price_low_b)
Vfinal_high = v3_math.position_value_from_liquidity(L_high, prices[-1], price_high_a, price_high_b)
Vfinal = Vfinal_low + Vfinal_high
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def evaluate_fast_rebalancing(all_prices, range_factor):
all_divloss = []
sqrt_range_factor = sqrt(sqrt(range_factor))
n = 0
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
price_a = INITIAL_PRICE / range_factor
price_b = INITIAL_PRICE * range_factor
price_a_trigger = INITIAL_PRICE / sqrt_range_factor
price_b_trigger = INITIAL_PRICE * sqrt_range_factor
L = v3_math.get_liquidity(INITIAL_X, INITIAL_Y,
sqrt(INITIAL_PRICE),
sqrt(price_a), sqrt(price_b))
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
for p in prices:
if not (price_a_trigger <= p <= price_b_trigger):
n += 1
sp = sqrt(p)
sa = sqrt(price_a)
sb = sqrt(price_b)
# amounts before rebalancing
x = v3_math.calculate_x(L, sp, sa, sb)
y = v3_math.calculate_y(L, sp, sa, sb)
v_old = v3_math.position_value(x, y, p)
# amounts after rebalancing
y = v_old / 2
x = y / p
# sanity check
v_new = x * p + y
assert v_new - 1e-8 < v_old < v_new + 1e-8
price_a = p / range_factor
price_b = p * range_factor
price_a_trigger = p / sqrt_range_factor
price_b_trigger = p * sqrt_range_factor
sa = sqrt(price_a)
sb = sqrt(price_b)
old_L = L
L = v3_math.get_liquidity(x, y, sp, sa, sb)
# sanity check
v_new = v3_math.position_value_from_liquidity(L, p, price_a, price_b)
assert v_new - 1e-8 < v_old < v_new + 1e-8
Vfinal = v3_math.position_value_from_liquidity(L, prices[-1], price_a, price_b)
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def evaluate_partial_rebalancing(all_prices, range_factor):
all_divloss = []
sqrt_range_factor = sqrt(range_factor)
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
price_a = INITIAL_PRICE / range_factor
price_b = INITIAL_PRICE * range_factor
L = v3_math.get_liquidity(INITIAL_X, INITIAL_Y,
sqrt(INITIAL_PRICE),
sqrt(price_a), sqrt(price_b))
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
for p in prices:
if not (price_a <= p <= price_b):
sp = sqrt(p)
sa = sqrt(price_a)
sb = sqrt(price_b)
# amounts before rebalancing
x = v3_math.calculate_x(L, sp, sa, sb)
y = v3_math.calculate_y(L, sp, sa, sb)
v_old = v3_math.position_value(x, y, p)
# amounts after rebalancing
if p < price_a:
price_a /= sqrt_range_factor
price_b /= sqrt_range_factor
else:
price_a *= sqrt_range_factor
price_b *= sqrt_range_factor
sa = sqrt(price_a)
sb = sqrt(price_b)
x_per_unit = v3_math.calculate_x(1, sp, sa, sb) * p
y_per_unit = v3_math.calculate_y(1, sp, sa, sb)
total = y_per_unit + x_per_unit
x_prop = x_per_unit / total
y_prop = 1 - x_prop
x = v_old * x_prop / p
y = v_old * y_prop
# sanity check
v_new = x * p + y
assert v_new - 1e-8 < v_old < v_new + 1e-8
L = v3_math.get_liquidity(x, y, sp, sa, sb)
# sanity check
v_new = v3_math.position_value_from_liquidity(L, p, price_a, price_b)
assert v_new - 1e-8 < v_old < v_new + 1e-8
Vfinal = v3_math.position_value_from_liquidity(L, prices[-1], price_a, price_b)
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def evaluate_width_rebalancing(all_prices, range_factor):
all_divloss = []
initial_range_factor = range_factor
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
range_factor = initial_range_factor
price_a = INITIAL_PRICE / range_factor
price_b = INITIAL_PRICE * range_factor
L = v3_math.get_liquidity(INITIAL_X, INITIAL_Y,
sqrt(INITIAL_PRICE),
sqrt(price_a), sqrt(price_b))
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
for p in prices:
if not (price_a <= p <= price_b):
v_old = v3_math.position_value_from_liquidity(L, p, price_a, price_b)
if False:
# both directions
range_factor = range_factor ** 2
price_a = INITIAL_PRICE / range_factor
price_b = INITIAL_PRICE * range_factor
else:
# towards the direction where the price has moved
if p < price_a:
price_a /= range_factor
else:
price_b *= range_factor
sa = sqrt(price_a)
sb = sqrt(price_b)
sp = sqrt(p)
x_per_unit = v3_math.calculate_x(1, sp, sa, sb) * p
y_per_unit = v3_math.calculate_y(1, sp, sa, sb)
total = y_per_unit + x_per_unit
x_prop = x_per_unit / total
y_prop = 1 - x_prop
x = v_old * x_prop / p
y = v_old * y_prop
# sanity check
v_new = x * p + y
assert v_new - 1e-8 < v_old < v_new + 1e-8
L = v3_math.get_liquidity(x, y, sp, sa, sb)
# sanity check
v_new = v3_math.position_value_from_liquidity(L, p, price_a, price_b)
assert v_new - 1e-8 < v_old < v_new + 1e-8
Vfinal = v3_math.position_value_from_liquidity(L, prices[-1], price_a, price_b)
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def gaussian_liquidity_distribution(n):
n //= 2
# use STD such that it's halfway in the open positions
# e.g. num_pos = 7 -> sigma=1.5 -> the std is between the 1st and 2nd side positions
sigma = n / 2
mu = 0
liquidities = [0] * n
for i in range(n):
x = i + 1
value = np.exp(-1/2 * (x - mu)**2 / sigma ** 2)
liquidities[i] = value
# prepend with itself, reversed, and add 1.0 in the center
return liquidities[::-1] + [1] + liquidities
def gaussian_liq_from_values(distr, range_factor, total_value, center_price):
n = len(distr)
n = n // 2
half_distr = distr[n:]
price_a = center_price / range_factor
price_b = center_price * range_factor
scp = sqrt(center_price)
range_factor_2 = range_factor ** 2
value_0 = 2 * v3_math.calculate_y(half_distr[0], scp, sqrt(price_a), scp)
values = [0] * n
for i in range(n):
price_a /= range_factor_2
price_b /= range_factor_2
values[i] = v3_math.calculate_y(half_distr[i+1], scp, sqrt(price_a), sqrt(price_b))
unit_values = values[::-1] + [value_0] + values
total_unit_liquidity_value = sum(unit_values)
factor = total_value / total_unit_liquidity_value
return [u * factor for u in distr]
def gaussian_values_from_liq(liquidities, range_factor, center_price, current_price):
n = len(liquidities)
p = current_price
sp = sqrt(current_price)
range_factor_2 = range_factor ** 2
price_a = center_price / (range_factor ** n)
price_b = price_a * range_factor_2
values = [0] * n
for i in range(n):
if p < price_a:
# x only
values[i] = p * v3_math.calculate_x(liquidities[i], sp, sqrt(price_a), sqrt(price_b))
elif p > price_b:
# y only
values[i] = v3_math.calculate_y(liquidities[i], sp, sqrt(price_a), sqrt(price_b))
else:
# both
values[i] = p * v3_math.calculate_x(liquidities[i], sp, sp, sqrt(price_b)) \
+ v3_math.calculate_y(liquidities[i], sp, sqrt(price_a), sp)
price_b *= range_factor_2
price_a *= range_factor_2
return values
def evaluate_gaussian(all_prices, range_factor, do_rebalance):
assert NUM_GAUSSIAN_POSITIONS % 2 == 1
full_range_factor = range_factor
range_factor = full_range_factor ** (1/NUM_GAUSSIAN_POSITIONS)
all_divloss = []
for sim in range(NUM_SIMULATIONS):
prices = all_prices[:,sim]
trigger_low = INITIAL_PRICE / full_range_factor
trigger_high = INITIAL_PRICE * full_range_factor
distr = gaussian_liquidity_distribution(NUM_GAUSSIAN_POSITIONS)
liquidities = gaussian_liq_from_values(distr, range_factor, INITIAL_VALUE, INITIAL_PRICE)
center_price = INITIAL_PRICE
new_value = sum(gaussian_values_from_liq(liquidities, range_factor, INITIAL_PRICE, INITIAL_PRICE))
assert INITIAL_VALUE - 1e-8 < new_value < INITIAL_VALUE + 1e-8
if do_rebalance:
for p in prices:
if not (trigger_low <= p <= trigger_high):
value = sum(gaussian_values_from_liq(liquidities, range_factor, center_price, p))
old_total_liq = sum(liquidities)
center_price = p
liquidities = gaussian_liq_from_values(distr, range_factor, value, center_price)
# sanity check
new_total_liq = sum(liquidities)
assert new_total_liq < old_total_liq
# sanity check
new_value = sum(gaussian_values_from_liq(liquidities, range_factor, p, p))
assert value - 1e-8 < new_value < value + 1e-8
trigger_low = p / full_range_factor
trigger_high = p * full_range_factor
Vfinal = sum(gaussian_values_from_liq(liquidities, range_factor, center_price, prices[-1]))
Vhodl = prices[-1] * INITIAL_X + INITIAL_Y
divloss = (Vfinal - Vhodl) / Vhodl
all_divloss.append(divloss)
return np.mean(all_divloss)
############################################################
def compute_expected_divloss(sigma, mu):
initial_x = INITIAL_X
initial_y = INITIAL_Y
all_prices = get_price_path(sigma, mu, blocks_per_day=12)
final_prices = all_prices[-1,:]
median = sorted(final_prices)[NUM_SIMULATIONS // 2]
returns = final_prices / INITIAL_PRICE
print(f"sigma={sigma:.2f} mean={np.mean(final_prices):.4f} median={median:.4f} std={np.std(np.log(returns)):.4f}")
divloss_static = evaluate_static(all_prices, RANGE_FACTOR)
divloss_with_rebal = evaluate_full_rebalancing(all_prices, RANGE_FACTOR)
divloss_gaussian_static = evaluate_gaussian(all_prices, RANGE_FACTOR, False)
divloss_gaussian_rebal = evaluate_gaussian(all_prices, RANGE_FACTOR, True)
divloss_fast_rebal = evaluate_fast_rebalancing(all_prices, RANGE_FACTOR)
divloss_partial_rebal = evaluate_partial_rebalancing(all_prices, RANGE_FACTOR)
divloss_change_width = evaluate_width_rebalancing(all_prices, RANGE_FACTOR)
divloss_2sided_static = evaluate_two_sided(all_prices, RANGE_FACTOR, False)
divloss_2sided_rebal = evaluate_two_sided(all_prices, RANGE_FACTOR, True)
if False:
print("sigma=", sigma)
print(f"static LP: {divloss_static*100:.2f}")
print(f"with rebalancing: {divloss_with_rebal*100:.2f}")
print(f"with gaussian, static: {divloss_gaussian_static*100:.2f}")
print(f"with gaussian, rebal.: {divloss_gaussian_rebal*100:.2f}")
print(f"with width change: {divloss_change_width*100:.2f}")
print(f"with partial rebalancing: {divloss_partial_rebal*100:.2f}")
print(f"with fast rebalancing: {divloss_fast_rebal*100:.2f}")
print(f"two-sided static LP: {divloss_2sided_static*100:.2f}")
print(f"two-sided rebalancing LP: {divloss_2sided_rebal*100:.2f}")
print("****")
return {"static": divloss_static,
"full rebalancing": divloss_with_rebal,
"gaussian static": divloss_gaussian_static,
"gaussian rebalancing": divloss_gaussian_rebal,
"fast rebalancing": divloss_fast_rebal,
"partial rebalancing": divloss_partial_rebal,
"width change only": divloss_change_width,
"two-sided static": divloss_2sided_static,
"two-sided rebalancing": divloss_2sided_rebal
}
############################################################
# example with USDC/USD depeg, very narrow position
def depeg_example():
print("simulating 2% depeg with price reversion")
initial_price = 1.0
price_a = initial_price / 1.001
price_b = initial_price * 1.001
initial_value = 1000
x = initial_value / 2
y = initial_value / 2
L0 = v3_math.get_liquidity(x, y,
sqrt(initial_price),
sqrt(price_a), sqrt(price_b))
new_price = 0.98
x = v3_math.calculate_x(L0, sqrt(new_price), sqrt(price_a), sqrt(price_b))
new_y = new_price * x / 2
new_x = x / 2
price_a = new_price / 1.001
price_b = new_price * 1.001
L1 = v3_math.get_liquidity(new_x, new_y,
sqrt(new_price),
sqrt(price_a), sqrt(price_b))
new_price = initial_price
y = v3_math.calculate_y(L1, sqrt(new_price), sqrt(price_a), sqrt(price_b))
final_value = y
print(f"final_value={final_value:.2f} loss={100 * (1 - final_value / initial_value):.2f}%")
############################################################
# shows that 4x higher price -> 2x deeper liquidity
def plot_liquidity_from_price():
initial_price = 100
prices = np.arange(initial_price, initial_price * 16, 0.01)
liquidities = []
price_a = initial_price / 1.01
unit_x = 1.0
unit_y = initial_price
L0 = v3_math.get_liquidity_1(unit_y / 2, sqrt(price_a), sqrt(initial_price))
print("L0=", L0)
for price in prices:
price_a = price / 1.01
price_b = price * 1.01
if price < initial_price:
L = v3_math.get_liquidity_1(unit_y, sqrt(price_a), sqrt(price_b))
else:
L = v3_math.get_liquidity_0(unit_x, sqrt(price_a), sqrt(price_b))
liquidities.append(L / L0)
pl.figure(figsize=(5, 3))
pl.plot(prices / initial_price, liquidities)
pl.ylabel("Liquidity multiplier")
pl.xlabel("Price multiplier")
pl.savefig("article_6_price_vs_liquidity.png", bbox_inches='tight')
pl.close()
############################################################
def plot_values(sigmas, values, expected_value_hodl, selector, filename):
pl.figure()
# convert to yearly sigma to improve x axis appearance
sigmas = [s * sqrt(365) for s in sigmas]
for label in selector:
divloss = [experiment[label] for experiment in values]
v = [expected_value_hodl * (1.0 + d) for d in divloss]
pl.plot(sigmas, v, label=label, marker="o", linestyle="--")
pl.legend()
pl.xscale("log")
x = [0.1, 0.2, 0.4, 0.8, 0.6, 1.0, 1.4, 1.8]
pl.xticks(x, [str(u) for u in x])
pl.ylabel("Expected final value, $")
pl.xlabel("$\sigma$")
pl.savefig(f"article_6_{filename}.png", bbox_inches='tight')
pl.close()
############################################################
def main():
mpl_style(True)
depeg_example()
plot_liquidity_from_price()
sigmas = [SIGMA / 8, SIGMA / 4, SIGMA / 2, SIGMA, SIGMA * 2]
values = [compute_expected_divloss(sigma, mu=ZERO_MU) for sigma in sigmas]
plot_values(sigmas, values, 100.0, ("static", "full rebalancing"), "static_vs_full")
plot_values(sigmas, values, 100.0,
("static", "full rebalancing", "partial rebalancing", "fast rebalancing", "width change only"),
"full_vs_partial")
plot_values(sigmas, values, 100.0,
("static", "full rebalancing", "two-sided static", "two-sided rebalancing"),
"full_vs_twosided")
plot_values(sigmas, values, 100.0,
("static", "full rebalancing", "gaussian static", "gaussian rebalancing"),
"full_vs_gaussian")
# experiment with positive price drift
values = [compute_expected_divloss(sigma, mu=POSITIVE_MU) for sigma in sigmas]
plot_values(sigmas, values, 199,
("static", "full rebalancing", "partial rebalancing", "fast rebalancing", "width change only"),
"directional_full_vs_partial")
if __name__ == '__main__':
main()
print("all done!")