-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheconomic-simulation.py
1208 lines (999 loc) · 48.6 KB
/
economic-simulation.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
import math
from multiprocessing import Pool
pseudoZero = 0.1 ** 10
pseudoMaxInt = 10 ** 10
def getRandomChange(vol, timespan, priceJumpPerDay, priceJumpMagnitude):
if random.random() * 2 < timespan * 365 * priceJumpPerDay:
if random.random() > 0.5:
changeRate = 1 + priceJumpMagnitude
else:
changeRate = 1 - priceJumpMagnitude
else:
changeRate = 1.0 + random.gauss(0, 1) * vol * (timespan ** 0.5)
return changeRate
def sortOrderPrice(value):
return value["orderPrice"]
def setPoolReservePlain(numberOfReserveTokens):
tokenReserves = []
for i in range(0, numberOfReserveTokens):
tokenReserves.append({
"tokenID": i,
"amount": 10000 * (i + 1) ^ 2
})
return tokenReserves
def setTokenWeights(tokenReserves):
tokenWeights = []
numberOfReserveTokens = len(tokenReserves)
for token in tokenReserves:
tokenWeights.append(1 / numberOfReserveTokens)
return tokenWeights
def getPoolPrice(X, Y, Wx, Wy):
poolPrice = (X / Wx) / (Y / Wy)
return poolPrice
def getSwapPrice(X, Y, EX, EY, Wx, Wy, swapFunction):
if swapFunction == "CPMM":
swapPrice = (Wy * X + 0.5 * (Wy * EX + Wx * EX)) / (Wx * Y + 0.5 * (Wx * EY + Wy * EY))
elif swapFunction == "ESPM":
swapPrice = (Wy * X + Wy * EX + Wx * EX) / (Wx * Y + Wx * EY + Wy * EY)
return swapPrice
def getInitialGlobalPrice(tokenReserves, tokenWeights):
numberOfReserveTokens = len(tokenReserves)
globalPriceList = []
for firstToken in range(0, numberOfReserveTokens - 1):
for secondToken in range(firstToken + 1, numberOfReserveTokens):
tokenPair = str(firstToken) + "/" + str(secondToken)
X = tokenReserves[firstToken]["amount"]
Y = tokenReserves[secondToken]["amount"]
Wx = tokenWeights[firstToken]
Wy = tokenWeights[secondToken]
poolPrice = getPoolPrice(X, Y, Wx, Wy)
globalPriceList.append({
"tokenPair": tokenPair,
"globalPrice": poolPrice
})
return globalPriceList
def getArbOrders(X, Y, poolPrice, globalPrice, arbTrigger, swapFunction, arbCompetitionGauge):
XtoYNewOrders = [] # buying Y from X
YtoXNewOrders = [] # selling Y for X
arbProfit = 0
if swapFunction == "CPMM":
orderAmtMultiplier = 0.5
elif swapFunction == "ESPM":
orderAmtMultiplier = 0.25 + (0.5 - 0.25) * arbCompetitionGauge
# add arbitrage order
if poolPrice < globalPrice * (1 - arbTrigger):
# XtoY arbitrage
orderPrice = globalPrice
orderAmt = X * (globalPrice / (poolPrice) - 1) * orderAmtMultiplier
if swapFunction == "CPMM":
arbProfit += (globalPrice / ((X + orderAmt) / Y) - 1) * orderAmt
elif swapFunction == "ESPM":
arbProfit += (globalPrice / ((X + 2 * orderAmt) / Y) - 1) * orderAmt
newOrder = {
"orderPrice": orderPrice,
"orderAmt": orderAmt,
}
XtoYNewOrders.append(newOrder)
elif poolPrice > globalPrice * (1 + arbTrigger):
# YtoX arbitrage
orderPrice = globalPrice
orderAmt = Y * (1 - globalPrice / (poolPrice)) * orderAmtMultiplier
if swapFunction == "CPMM":
arbProfit += (1 - globalPrice / (X / (Y + orderAmt))) * orderAmt * globalPrice
elif swapFunction == "ESPM":
arbProfit += (1 - globalPrice / (X / (Y + 2 * orderAmt))) * orderAmt * globalPrice
newOrder = {
"orderPrice": orderPrice,
"orderAmt": orderAmt,
}
YtoXNewOrders.append(newOrder)
return XtoYNewOrders, YtoXNewOrders, arbProfit
def getRandomOrders(X, Y, poolPrice, globalPrice, randomOrderSize, numberOfRandomOrderPerDay, simBlockSize,
secondsPerBlock):
orderProbability = numberOfRandomOrderPerDay / (24 * 60 * 60 / (secondsPerBlock * simBlockSize))
if orderProbability < 1:
if random.random() < orderProbability:
randomOrderNum = 1
else:
randomOrderNum = 0
else:
randomOrderNum = round(random.random() * orderProbability * 2)
if poolPrice <= globalPrice:
XtoYOrderNum = randomOrderNum
YtoXOrderNum = 0
else:
XtoYOrderNum = 0
YtoXOrderNum = randomOrderNum
XtoYNewOrders = [] # buying Y from X
YtoXNewOrders = [] # selling Y for X
for i in range(0, XtoYOrderNum):
orderPrice = globalPrice * (1 + 0.1)
orderAmt = X * random.random() * randomOrderSize * 2 * 2
newOrder = {
"orderPrice": orderPrice,
"orderAmt": orderAmt,
}
XtoYNewOrders.append(newOrder)
for i in range(0, YtoXOrderNum):
orderPrice = globalPrice * (1 - 0.1)
orderAmt = Y * random.random() * randomOrderSize * 2 * 2
newOrder = {
"orderPrice": orderPrice,
"orderAmt": orderAmt,
}
YtoXNewOrders.append(newOrder)
return XtoYNewOrders, YtoXNewOrders
def addOrders(XtoY, YtoX, XtoYNewOrders, YtoXNewOrders, lastOrderID, height, orderLifeSpanHeight, feeRate):
i = 0
for order in XtoYNewOrders:
i += 1
orderPrice = order["orderPrice"]
orderAmt = order["orderAmt"]
newOrder = {
"orderID": lastOrderID + 1,
"orderHeight": height,
"orderCancelHeight": height + orderLifeSpanHeight,
"orderPrice": orderPrice,
"orderAmt": orderAmt,
"matchedXAmt": 0,
"receiveYAmt": 0,
"feeXAmtPaid": 0,
"feeXAmtReserve": orderAmt * feeRate * 0.5,
"feeYAmtPaid": 0,
}
XtoY.append(newOrder)
lastOrderID = lastOrderID + 1
i = 0
for order in YtoXNewOrders:
i += 1
orderPrice = order["orderPrice"]
orderAmt = order["orderAmt"]
newOrder = {
"orderID": lastOrderID + 1,
"orderHeight": height,
"orderCancelHeight": height + orderLifeSpanHeight,
"orderPrice": orderPrice,
"orderAmt": orderAmt,
"matchedYAmt": 0,
"receiveXAmt": 0,
"feeYAmtPaid": 0,
"feeYAmtReserve": orderAmt * feeRate * 0.5,
"feeXAmtPaid": 0,
}
YtoX.append(newOrder)
lastOrderID = lastOrderID + 1
XtoY = sorted(XtoY, key=sortOrderPrice, reverse=True)
YtoX = sorted(YtoX, key=sortOrderPrice)
return XtoY, YtoX, lastOrderID
def getOrderbook(XtoY, YtoX):
XtoY = sorted(XtoY, key=sortOrderPrice, reverse=True)
YtoX = sorted(YtoX, key=sortOrderPrice)
orderPriceList = []
orderbook = []
for order in XtoY:
if order["orderPrice"] not in orderPriceList:
orderPriceList.append(order["orderPrice"])
for order in YtoX:
if order["orderPrice"] not in orderPriceList:
orderPriceList.append(order["orderPrice"])
orderPriceList = sorted(orderPriceList)
for orderPrice in orderPriceList:
orderbook.append({"orderPrice": orderPrice, "buyOrderAmt": 0, "sellOrderAmt": 0})
for order in orderbook:
for buyOrder in XtoY:
if buyOrder["orderPrice"] == order["orderPrice"]:
order["buyOrderAmt"] += buyOrder["orderAmt"]
for sellOrder in YtoX:
if sellOrder["orderPrice"] == order["orderPrice"]:
order["sellOrderAmt"] += sellOrder["orderAmt"]
return orderbook
def cancelEndOfLifeSpanOrders(XtoY, YtoX, height):
cancelOrderListXtoY = []
cancelOrderListYtoX = []
for order in XtoY:
if height >= order["orderCancelHeight"]:
cancelOrderListXtoY.append(order)
XtoY.remove(order)
for order in YtoX:
if height >= order["orderCancelHeight"]:
cancelOrderListYtoX.append(order)
YtoX.remove(order)
return XtoY, YtoX, cancelOrderListXtoY, cancelOrderListYtoX
def getPriceDirection(currentPrice, orderbook):
buyAmtOverCurrentPrice = 0
buyAmtAtCurrentPrice = 0
sellAmtUnderCurrentPrice = 0
sellAmtAtCurrentPrice = 0
for order in orderbook:
if order["orderPrice"] > currentPrice:
buyAmtOverCurrentPrice += order["buyOrderAmt"]
elif order["orderPrice"] == currentPrice:
buyAmtAtCurrentPrice += order["buyOrderAmt"]
sellAmtAtCurrentPrice += order["sellOrderAmt"]
elif order["orderPrice"] < currentPrice:
sellAmtUnderCurrentPrice += order["sellOrderAmt"]
if buyAmtOverCurrentPrice - (sellAmtUnderCurrentPrice + sellAmtAtCurrentPrice) * currentPrice > 0:
direction = "increase"
elif sellAmtUnderCurrentPrice * currentPrice - (buyAmtOverCurrentPrice + buyAmtAtCurrentPrice) > 0:
direction = "decrease"
else:
direction = "stay"
return direction
def getExecutableAmt(orderPrice, orderbook):
executableBuyAmtX = 0
executableSellAmtY = 0
for order in orderbook:
if order["orderPrice"] >= orderPrice:
executableBuyAmtX += order["buyOrderAmt"]
if order["orderPrice"] <= orderPrice:
executableSellAmtY += order["sellOrderAmt"] # in Y coins
return executableBuyAmtX, executableSellAmtY
def calculateMatchStay(X, Y, poolPrice, orderbook):
executableBuyAmtX, executableSellAmtY = getExecutableAmt(poolPrice, orderbook)
EX = executableBuyAmtX
EY = executableSellAmtY
PoolX = 0
PoolY = 0
originalEX = EX
originalEY = EY
if min(EX + PoolX, EY + PoolY) == 0:
matchType = "noMatch"
EX = 0
EY = 0
elif EX == EY * poolPrice:
matchType = "exactMatch"
else:
matchType = "fractionalMatch"
if EX > EY * poolPrice:
EX = EY * poolPrice
elif EX < EY * poolPrice:
EY = EX / poolPrice
return matchType, poolPrice, EX, EY, originalEX, originalEY, PoolX, PoolY
def calculateSwapIncrease(X, Y, Wx, Wy, orderbook, orderPrice, lastOrderPrice, swapFunction):
matchType = ""
# simulation range : (lastOrderPrice,orderPrice)
if matchType == "":
EX, EY = getExecutableAmt((lastOrderPrice + orderPrice) / 2, orderbook)
originalEX = EX
originalEY = EY
swapPrice = getSwapPrice(X, Y, EX, EY, Wx, Wy, swapFunction) # choose function for this pool type!
PoolY = (EX - swapPrice * EY) / swapPrice # any pool type!
if lastOrderPrice < swapPrice < orderPrice and PoolY >= 0: # swapPrice within given price range?
if EX == 0 and EY == 0:
matchType = "noMatch"
else:
matchType = "exactMatch" # all orders are exactly matched
# simulation for orderPrice
if matchType == "":
EX, EY = getExecutableAmt(orderPrice, orderbook)
originalEX = EX
originalEY = EY
swapPrice = orderPrice
PoolY = (swapPrice * Y - X) / (2 * swapPrice) # any pool type!
EX = min(EX, (EY + PoolY) * swapPrice)
EY = max(min(EY, EX / swapPrice - PoolY), 0)
matchType = "fractionalMatch"
if swapPrice < X / Y or PoolY < 0:
transactAmt = 0
else:
transactAmt = int(min(EX, (EY + PoolY) * swapPrice) * pseudoMaxInt) / pseudoMaxInt
return matchType, EX, EY, originalEX, originalEY, swapPrice, PoolY, transactAmt
def calculateMatchIncrease(X, Y, Wx, Wy, orderbook, swapFunction):
# variable initialization
currentPrice = X / Y
lastOrderPrice = currentPrice
PoolX = 0
PoolY = 0
matchType = ""
matchScenario = []
# iterate orderbook from current price to upwards(increase)/downwards(decrease)
for order in orderbook:
if order["orderPrice"] < currentPrice:
pass
else:
orderPrice = order["orderPrice"]
matchType, EX, EY, originalEX, originalEY, swapPrice, PoolY, transactAmt = calculateSwapIncrease(X, Y, Wx,
Wy,
orderbook,
orderPrice,
lastOrderPrice,
swapFunction)
matchScenario.append([matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY, transactAmt])
# update last variables
lastOrderPrice = orderPrice
maxScenario = ["noMatch", currentPrice, 0, 0, 0, 0, 0, 0, 0]
for scenario in matchScenario:
# print(scenario)
if scenario[0] == "exactMatch" and scenario[8] > 0:
maxScenario = scenario
break
else:
if scenario[8] > maxScenario[8]:
maxScenario = scenario
# print(maxScenario)
matchType = maxScenario[0]
swapPrice = maxScenario[1]
EX = maxScenario[2]
EY = maxScenario[3]
originalEX = maxScenario[4]
originalEY = maxScenario[5]
PoolX = maxScenario[6]
PoolY = maxScenario[7]
return matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY
def calculateSwapDecrease(X, Y, Wx, Wy, orderbook, orderPrice, lastOrderPrice, swapFunction):
matchType = ""
# simulation range : (lastOrderPrice,orderPrice)
if matchType == "":
EX, EY = getExecutableAmt((lastOrderPrice + orderPrice) / 2, orderbook)
originalEX = EX
originalEY = EY
swapPrice = getSwapPrice(X, Y, EX, EY, Wx, Wy, swapFunction) # # choose function for this pool type!
PoolX = (EY - EX / swapPrice) * swapPrice # any pool type!
if orderPrice < swapPrice < lastOrderPrice and PoolX >= 0: # swapPrice within given price range?
if EX == 0 and EY == 0:
matchType = "noMatch"
else:
matchType = "exactMatch" # all orders are exactly matched
# simulation for fractional match
if matchType == "":
EX, EY = getExecutableAmt(orderPrice, orderbook)
originalEX = EX
originalEY = EY
swapPrice = orderPrice
PoolX = (EY - EX / swapPrice) * swapPrice # any pool type!
EY = min(EY, (EX + PoolX) / swapPrice)
EX = max(min(EX, EY * swapPrice - PoolX), 0)
matchType = "fractionalMatch"
if swapPrice > X / Y or PoolX < 0:
transactAmt = 0
else:
transactAmt = int(min(EY, (EX + PoolX) / swapPrice) * pseudoMaxInt) / pseudoMaxInt
return matchType, EX, EY, originalEX, originalEY, swapPrice, PoolX, transactAmt
def calculateMatchDecrease(X, Y, Wx, Wy, orderbook, swapFunction):
# variable initialization
currentPrice = X / Y
lastOrderPrice = currentPrice
PoolX = 0
PoolY = 0
matchType = ""
matchScenario = []
# iterate orderbook from current price to upwards(increase)/downwards(decrease)
for order in orderbook:
if order["orderPrice"] > currentPrice:
pass
else:
orderPrice = order["orderPrice"]
matchType, EX, EY, originalEX, originalEY, swapPrice, PoolX, transactAmt = calculateSwapDecrease(X, Y, Wx,
Wy,
orderbook,
orderPrice,
lastOrderPrice,
swapFunction)
matchScenario.append([matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY, transactAmt])
# update last variables
lastOrderPrice = orderPrice
maxScenario = ["noMatch", currentPrice, 0, 0, 0, 0, 0, 0, 0]
for scenario in matchScenario:
# print(scenario)
if scenario[0] == "exactMatch" and scenario[8] > 0:
maxScenario = scenario
break
else:
if scenario[8] > maxScenario[8]:
maxScenario = scenario
# print(maxScenario)
matchType = maxScenario[0]
swapPrice = maxScenario[1]
EX = maxScenario[2]
EY = maxScenario[3]
originalEX = maxScenario[4]
originalEY = maxScenario[5]
PoolX = maxScenario[6]
PoolY = maxScenario[7]
return matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY
def computePriceDirection(X, Y, Wx, Wy, poolPrice, orderbook, swapFunction):
orderbook = sorted(orderbook, key=sortOrderPrice)
currentPrice = poolPrice
priceDirection = getPriceDirection(currentPrice, orderbook)
# print("priceDirection: " + str(priceDirection))
# print("\n")
if priceDirection == "stay":
matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY = calculateMatchStay(X, Y, poolPrice,
orderbook)
stayResult = [matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY]
return stayResult
elif priceDirection == "increase":
matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY = calculateMatchIncrease(X, Y, Wx, Wy,
orderbook,
swapFunction)
increaseResult = [matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY]
return increaseResult
elif priceDirection == "decrease":
orderbook = sorted(orderbook, key=sortOrderPrice, reverse=True)
matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY = calculateMatchDecrease(X, Y, Wx, Wy,
orderbook,
swapFunction)
decreaseResult = [matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY]
return decreaseResult
def swapCalculation(X, Y, Wx, Wy, poolPrice, XtoY, YtoX, swapFunction):
# sort XtoY, YtoX
XtoY = sorted(XtoY, key=sortOrderPrice, reverse=True)
YtoX = sorted(YtoX, key=sortOrderPrice)
# get orderbook
orderbook = getOrderbook(XtoY, YtoX)
# calculate each case
result = computePriceDirection(X, Y, Wx, Wy, poolPrice, orderbook, swapFunction)
matchType = result[0]
swapPrice = result[1]
EX = result[2]
EY = result[3]
originalEX = result[4]
originalEY = result[5]
PoolX = result[6]
PoolY = result[7]
return matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY
def findOrderMatch(X, Y, XtoY, YtoX, EX, EY, swapPrice, feeRate):
# sort XtoY, YtoX
XtoY = sorted(XtoY, key=sortOrderPrice, reverse=True)
YtoX = sorted(YtoX, key=sortOrderPrice)
# initiate variables
matchResultXtoY = []
accumMatchAmt = 0
matchAmt = 0
matchOrderList = []
i = -1
for order in XtoY:
i += 1
breakFlag = False
appendFlag = False
# include the order in matchAmt, matchOrderList
if order["orderPrice"] >= swapPrice:
matchAmt += order["orderAmt"]
matchOrderList.append(order)
### case check ###
if len(XtoY) > i + 1: # next order exist?
if XtoY[i + 1]["orderPrice"] == order["orderPrice"]: # next orderPrice is same?
breakFlag = False
appendFlag = False
else: # next orderPrice is new?
appendFlag = True
if XtoY[i + 1]["orderPrice"] >= swapPrice: # next price is matchable?
breakFlag = False
else: # next orderPrice is unmatchable?
breakFlag = True
else: # next order does not exist
breakFlag = True
appendFlag = True
if appendFlag:
# append orders
if matchAmt > pseudoZero:
# print(order)
if accumMatchAmt + matchAmt >= EX: # fractional match
fractionalMatchRatio = (EX - accumMatchAmt) / matchAmt
else:
fractionalMatchRatio = 1
for matchOrder in matchOrderList:
if fractionalMatchRatio > 0:
tempFeeXAmtPaid = matchOrder["feeXAmtReserve"] * fractionalMatchRatio
tempFeeXAmtReserve = matchOrder["feeXAmtReserve"] - tempFeeXAmtPaid
matchResultXtoY.append({
"orderID": matchOrder["orderID"],
"orderHeight": matchOrder["orderHeight"],
"orderCancelHeight": matchOrder["orderCancelHeight"],
"orderPrice": matchOrder["orderPrice"],
"orderAmt": matchOrder["orderAmt"],
"matchedXAmt": matchOrder["orderAmt"] * fractionalMatchRatio,
"receiveYAmt": matchOrder["orderAmt"] * fractionalMatchRatio / swapPrice,
"feeXAmtPaid": tempFeeXAmtPaid,
"feeXAmtReserve": tempFeeXAmtReserve,
"feeYAmtPaid": matchOrder["orderAmt"] * fractionalMatchRatio / swapPrice * feeRate * 0.5
})
# update accumMatchAmt and initiate matchAmt and matchOrderList
accumMatchAmt += matchAmt
matchAmt = 0
matchOrderList = []
if breakFlag:
break
# initiate variables
matchResultYtoX = []
accumMatchAmt = 0
matchAmt = 0
matchOrderList = []
i = -1
for order in YtoX:
i += 1
breakFlag = False
appendFlag = False
# include the order in matchAmt, matchOrderList
if order["orderPrice"] <= swapPrice:
matchAmt += order["orderAmt"]
matchOrderList.append(order)
### case check ###
if len(YtoX) > i + 1: # next order exist?
if YtoX[i + 1]["orderPrice"] == order["orderPrice"]: # next orderPrice is same?
breakFlag = False
appendFlag = False
else: # next orderPrice is new?
appendFlag = True
if YtoX[i + 1]["orderPrice"] <= swapPrice: # next price is matchable?
breakFlag = False
else: # next orderPrice is unmatchable?
breakFlag = True
else: # next order does not exist
breakFlag = True
appendFlag = True
if appendFlag:
# append orders
if matchAmt > pseudoZero:
if accumMatchAmt + matchAmt >= EY: # fractional match
fractionalMatchRatio = (EY - accumMatchAmt) / matchAmt
else:
fractionalMatchRatio = 1
for matchOrder in matchOrderList:
if fractionalMatchRatio > 0:
tempFeeYAmtPaid = matchOrder["feeYAmtReserve"] * fractionalMatchRatio
tempFeeYAmtReserve = matchOrder["feeYAmtReserve"] - tempFeeYAmtPaid
matchResultYtoX.append({
"orderID": matchOrder["orderID"],
"orderHeight": matchOrder["orderHeight"],
"orderCancelHeight": matchOrder["orderCancelHeight"],
"orderPrice": matchOrder["orderPrice"],
"orderAmt": matchOrder["orderAmt"],
"matchedYAmt": matchOrder["orderAmt"] * fractionalMatchRatio,
"receiveXAmt": matchOrder["orderAmt"] * fractionalMatchRatio * swapPrice,
"feeYAmtPaid": tempFeeYAmtPaid,
"feeYAmtReserve": tempFeeYAmtReserve,
"feeXAmtPaid": matchOrder["orderAmt"] * fractionalMatchRatio * swapPrice * feeRate * 0.5
})
# update accumMatchAmt and initiate matchAmt and matchOrderList
accumMatchAmt += matchAmt
matchAmt = 0
matchOrderList = []
if breakFlag:
break
return matchResultXtoY, matchResultYtoX
def clearBlankOrder(XtoY, YtoX):
for order in XtoY:
if order["orderAmt"] < pseudoZero:
XtoY.remove(order)
for order in YtoX:
if order["orderAmt"] < pseudoZero:
YtoX.remove(order)
return XtoY, YtoX
def updateState(X, Y, XtoY, YtoX, matchResultXtoY, matchResultYtoX):
# sort XtoY, YtoX
XtoY = sorted(XtoY, key=sortOrderPrice, reverse=True)
YtoX = sorted(YtoX, key=sortOrderPrice)
PoolXdelta = 0
PoolYdelta = 0
UserXdelta = 0
UserYdelta = 0
for match in matchResultXtoY:
for order in XtoY:
if match["orderID"] == order["orderID"]:
PoolXdelta += match["matchedXAmt"]
UserXdelta += -match["matchedXAmt"]
PoolYdelta += -match["receiveYAmt"]
UserYdelta += match["receiveYAmt"]
PoolXdelta += match["feeXAmtPaid"]
UserXdelta += -match["feeXAmtPaid"]
PoolYdelta += match["feeYAmtPaid"]
UserYdelta += -match["feeYAmtPaid"]
if abs(order["orderAmt"] - order["matchedXAmt"]) < pseudoZero: # full match
PoolXdelta += order["orderAmt"] - order["matchedXAmt"]
UserXdelta += -(order["orderAmt"] - order["matchedXAmt"])
PoolXdelta += order["feeXAmtReserve"]
UserXdelta += -order["feeXAmtReserve"]
XtoY.remove(order)
else:
order["orderAmt"] = match["orderAmt"] - match["matchedXAmt"]
order["matchedXAmt"] = 0
order["receiveYAmt"] = 0
order["feeXAmtPaid"] = 0
order["feeXAmtReserve"] = match["feeXAmtReserve"]
order["feeYAmtPaid"] = 0
break
for match in matchResultYtoX:
for order in YtoX:
if match["orderID"] == order["orderID"]:
PoolXdelta += -match["receiveXAmt"]
UserXdelta += match["receiveXAmt"]
PoolYdelta += match["matchedYAmt"]
UserYdelta += -match["matchedYAmt"]
PoolYdelta += match["feeYAmtPaid"]
UserYdelta += -match["feeYAmtPaid"]
PoolXdelta += match["feeXAmtPaid"]
UserXdelta += -match["feeXAmtPaid"]
if abs(order["orderAmt"] - order["matchedYAmt"]) < pseudoZero: # full match
PoolYdelta += order["orderAmt"] - order["matchedYAmt"]
UserYdelta += -(order["orderAmt"] - order["matchedYAmt"])
PoolYdelta += order["feeYAmtReserve"]
UserYdelta += -order["feeYAmtReserve"]
YtoX.remove(order)
else:
order["orderAmt"] = match["orderAmt"] - match["matchedYAmt"]
order["matchedYAmt"] = 0
order["receiveXAmt"] = 0
order["feeYAmtPaid"] = 0
order["feeYAmtReserve"] = match["feeYAmtReserve"]
order["feeXAmtPaid"] = 0
break
X += PoolXdelta
Y += PoolYdelta
# remove orders with negligible amount
XtoY, YtoX = clearBlankOrder(XtoY, YtoX)
return X, Y, XtoY, YtoX, PoolXdelta, PoolYdelta
def checkOrderbookValidity(XtoY, YtoX, currentPrice):
currentOrderbook = getOrderbook(XtoY, YtoX)
currentOrderbook = sorted(currentOrderbook, key=sortOrderPrice, reverse=True)
maxBuyOrderPrice = 0
minSellOrderPrice = 1000000000000
for order in currentOrderbook:
if order["buyOrderAmt"] > 0 and order["orderPrice"] > maxBuyOrderPrice:
maxBuyOrderPrice = order["orderPrice"]
if order["sellOrderAmt"] > 0 and order["orderPrice"] < minSellOrderPrice:
minSellOrderPrice = order["orderPrice"]
if maxBuyOrderPrice > minSellOrderPrice + pseudoZero or maxBuyOrderPrice / currentPrice > 1 + 0.1 ** 5 or minSellOrderPrice / currentPrice < 1 - 0.1 ** 5:
return False, maxBuyOrderPrice, minSellOrderPrice, currentPrice, currentOrderbook
else:
return True, maxBuyOrderPrice, minSellOrderPrice, currentPrice, currentOrderbook
def getGlobalPriceList(vol, simSeconds, secondsPerBlock, simBlockSize, priceJumpPerDay, priceJumpMagnitude):
# initialize states
height = 1
tokenReserves = setPoolReservePlain(paramNumberOfReserveTokens)
numberOfReserveTokens = len(tokenReserves)
tokenWeights = setTokenWeights(tokenReserves)
initialGlobalPrice = getInitialGlobalPrice(tokenReserves, tokenWeights)
globalPriceList = []
for globalPrice in initialGlobalPrice:
globalPriceList.append({
"tokenPair": globalPrice["tokenPair"],
"globalPrice": [globalPrice["globalPrice"]]
})
# get globalPriceList
while height < int(simSeconds / secondsPerBlock / simBlockSize):
# get next random global prices
for secondToken in range(1, numberOfReserveTokens):
for globalPrice in globalPriceList:
if globalPrice["tokenPair"] == "0/" + str(secondToken):
globalPrice["globalPrice"].append(globalPrice["globalPrice"][-1] * getRandomChange(vol,
secondsPerBlock * simBlockSize / (
365 * 24 * 60 * 60),
priceJumpPerDay,
priceJumpMagnitude))
break
for firstToken in range(1, numberOfReserveTokens - 1):
for secondToken in range(firstToken + 1, numberOfReserveTokens):
for globalPrice in globalPriceList:
if globalPrice["tokenPair"] == str(0) + "/" + str(firstToken):
firstPairPrice = globalPrice["globalPrice"][-1]
if globalPrice["tokenPair"] == str(0) + "/" + str(secondToken):
secondPairPrice = globalPrice["globalPrice"][-1]
globalPrice["globalPrice"].append(secondPairPrice / firstPairPrice)
# update height
height += 1
return globalPriceList
def simulation(swapFunction, params, globalPriceList):
arbTradingVolume = 0
arbTotalProfit = 0
feeRate, arbTrigger, tradingVolumePerDay, randomOrderSize, vol, priceJumpPerDay, priceJumpMagnitude, arbCompetitionGauge = params
# initialize states
height = 1
tokenReserves = setPoolReservePlain(paramNumberOfReserveTokens)
numberOfReserveTokens = len(tokenReserves)
tokenWeights = setTokenWeights(tokenReserves)
lastOrderID = 0
orderbookList = []
orderbookValidity = True
# simulation
while height < int(simSeconds / secondsPerBlock / simBlockSize):
# get newOrdersList
newOrdersList = []
# get newArbOrders
for firstToken in range(0, numberOfReserveTokens - 1):
for secondToken in range(firstToken + 1, numberOfReserveTokens):
# get information from tokenReserves
tokenPair = str(firstToken) + "/" + str(secondToken)
X = tokenReserves[firstToken]["amount"]
Y = tokenReserves[secondToken]["amount"]
Wx = tokenWeights[firstToken]
Wy = tokenWeights[secondToken]
poolPrice = getPoolPrice(X, Y, Wx, Wy)
# get information from globalPriceList
for price in globalPriceList:
if price["tokenPair"] == tokenPair:
globalPrice = price["globalPrice"][height - 1]
break
# get arbitrage orders : only the arb profit maximizer
XtoYNewOrders, YtoXNewOrders, arbProfit = getArbOrders(X, Y, poolPrice, globalPrice, arbTrigger,
swapFunction, arbCompetitionGauge)
arbTotalProfit += arbProfit
if len(XtoYNewOrders) + len(YtoXNewOrders) > 0:
newOrdersList = [{
"tokenPair": tokenPair,
"XtoYNewOrders": XtoYNewOrders,
"YtoXNewOrders": YtoXNewOrders
}]
for order in XtoYNewOrders:
arbTradingVolume += order["orderAmt"]
for order in YtoXNewOrders:
arbTradingVolume += order["orderAmt"] * order["orderPrice"]
# get newRandomOrders
for firstToken in range(0, numberOfReserveTokens - 1):
for secondToken in range(firstToken + 1, numberOfReserveTokens):
# get information from tokenReserves
tokenPair = str(firstToken) + "/" + str(secondToken)
X = tokenReserves[firstToken]["amount"]
Y = tokenReserves[secondToken]["amount"]
Wx = tokenWeights[firstToken]
Wy = tokenWeights[secondToken]
poolPrice = getPoolPrice(X, Y, Wx, Wy)
# get information from globalPriceList
for price in globalPriceList:
if price["tokenPair"] == tokenPair:
globalPrice = price["globalPrice"][height - 1]
break
# get random orders
XtoYNewOrders, YtoXNewOrders = getRandomOrders(X, Y, poolPrice, globalPrice, randomOrderSize,
(tradingVolumePerDay / randomOrderSize), simBlockSize,
secondsPerBlock)
if len(XtoYNewOrders) + len(YtoXNewOrders) > 0:
newOrdersListExist = False
for newOrders in newOrdersList:
if newOrders["tokenPair"] == tokenPair:
newOrdersListExist = True
newOrders["XtoYNewOrders"].extend(XtoYNewOrders)
newOrders["YtoXNewOrders"].extend(YtoXNewOrders)
break
if newOrdersListExist == False:
newOrdersList.append({
"tokenPair": tokenPair,
"XtoYNewOrders": XtoYNewOrders,
"YtoXNewOrders": YtoXNewOrders
})
# print when arbitrage happens
"""
if max(abs(tokenReserves[0]["amount"]/tokenReserves[1]["amount"]/globalPriceList[0]["globalPrice"]-1),abs(tokenReserves[0]["amount"]/tokenReserves[2]["amount"]/globalPriceList[1]["globalPrice"]-1),abs(tokenReserves[1]["amount"]/tokenReserves[2]["amount"]/globalPriceList[2]["globalPrice"]-1)) > 0.01:
print(str(height)+"/"+str(tokenReserves[0]["amount"]/tokenReserves[1]["amount"]/globalPriceList[0]["globalPrice"]-1)+"/"+str(tokenReserves[0]["amount"]/tokenReserves[2]["amount"]/globalPriceList[1]["globalPrice"]-1)+"/"+str(tokenReserves[1]["amount"]/tokenReserves[2]["amount"]/globalPriceList[2]["globalPrice"]-1)+"/"+str(tokenReserves[0]["amount"])+"/"+str(tokenReserves[1]["amount"])+"/"+str(tokenReserves[2]["amount"])+"/"+str(len(newOrdersList))+"/"+str(newOrdersList))
"""
for firstToken in range(0, numberOfReserveTokens - 1):
for secondToken in range(firstToken + 1, numberOfReserveTokens):
# get information from tokenReserves
tokenPair = str(firstToken) + "/" + str(secondToken)
X = tokenReserves[firstToken]["amount"]
Y = tokenReserves[secondToken]["amount"]
Wx = tokenWeights[firstToken]
Wy = tokenWeights[secondToken]
poolPrice = getPoolPrice(X, Y, Wx, Wy)
# get information from orderbookList
XtoY = []
YtoX = []
for orderbook in orderbookList:
if orderbook["tokenPair"] == tokenPair:
XtoY = orderbook["XtoY"]
YtoX = orderbook["YtoX"]
break
# get information from newOrdersList
XtoYNewOrders = []
YtoXNewOrders = []
for newOrder in newOrdersList:
if newOrder["tokenPair"] == tokenPair:
XtoYNewOrders = newOrder["XtoYNewOrders"]
YtoXNewOrders = newOrder["YtoXNewOrders"]
break
# add new orders
XtoY, YtoX, lastOrderID = addOrders(XtoY, YtoX, XtoYNewOrders, YtoXNewOrders, lastOrderID, height,
orderLifeSpanHeight, feeRate)
# swap pre-calculation
matchType, swapPrice, EX, EY, originalEX, originalEY, PoolX, PoolY = swapCalculation(X, Y, Wx, Wy,
poolPrice, XtoY,
YtoX, swapFunction)
# find order matching
matchResultXtoY, matchResultYtoX = findOrderMatch(X, Y, XtoY, YtoX, EX, EY, swapPrice, feeRate)
# swap execution
X, Y, XtoY, YtoX, PoolXdelta, PoolYdelta = updateState(X, Y, XtoY, YtoX, matchResultXtoY,
matchResultYtoX)
# cancel end of life span orders
XtoY, YtoX, cancelOrderListXtoY, cancelOrderListYtoX = cancelEndOfLifeSpanOrders(XtoY, YtoX, height)
XtoY, YtoX = clearBlankOrder(XtoY, YtoX)
orderbookValidity = checkOrderbookValidity(XtoY, YtoX, getPoolPrice(X, Y, Wx, Wy))
if orderbookValidity[0] == False:
# print(orderbookValidity)
# wait = input("pause")
pass
# update tokenReserves
tokenReserves[firstToken]["amount"] = X
tokenReserves[secondToken]["amount"] = Y
# update orderbookList
for orderbook in orderbookList:
if orderbook["tokenPair"] == tokenPair:
orderbook["XtoY"] = XtoY
orderbook["YtoX"] = YtoX
break
# update height
height += 1
return tokenReserves, arbTradingVolume, arbTotalProfit
def simulateAllParams(simNum):
printStr = ""