-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrikiddo.py
179 lines (143 loc) · 5.22 KB
/
rikiddo.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
from rikiddo_core_functions import fixFee, getVolumeRatio, z_r, eValue, lsdCostFunction, lsdPriceFunction_i, minRevenue
import pandas as pd
import numpy as np
import random
import time
random.seed(1)
symbol1= 'Will'
symbol2= 'Will_not'
fee = fixFee(0.04, 2)
k=1
traderMaxFee = 0.1
# %% Simulation Settings
b = 0.7
minRev = minRevenue(b, fee)
q_1 = 1000000 #max volume available for q_1
q_2 = 1000000 #max volume available for q_2
q_1_pool = 0 #amount of q_1 inside the pool
q_2_pool = 0 #amount of q_2 inside the pool
liquidityBounds = [q_1<10000, q_2<10000]
ratio_q_1 = q_1 / (q_1 + q_2)
ratio_q_2 = q_2 / (q_1 + q_2)
simulationRecord = pd.DataFrame([])
indexer = 0
#Initial state
account1 = f'account_{symbol1}'
account2 = f'account_{symbol2}'
account1_pool = f'account_{symbol1}_pool'
account2_pool = f'account_{symbol2}_pool'
data = {'transactionNumber': [0],
account1 : [q_1],
account2: [q_2],
account1_pool : [0],
account2_pool: [0],
'totalVolume': q_1+q_2,
'totalPoolVolume': [0],
'totalFee': [fee],
'whoBuy': ['Initial'],
'ratioVolume': [q_1/q_2],
'z': [0],
'order': ['Initial'],
'transactCost': [0],
'previousCost': [0],
'deltaQ': [0],
'costPerUnit': [0],
'r': 1}
simulationRecord = simulationRecord.append(pd.DataFrame.from_dict(data))
transaction = 0
start_time = time.time()
print('--------------------------------------LOOP PRINTS--------------------------------------')
#%% Loop simulation
symbols = [symbol1, symbol2]
previousStateCost = 0
while time.time() - start_time <4:
# transaction += 1
poolInventory= [q_1_pool, q_2_pool]
buysell = ['buy', 'sell']
signals = [0.5, 0.5]
buysellSignals = [0.5, 0.5]
#The signal changes every 500 transactions
if transaction%500 == 0:
signals[0] = random.random()
signals[1] = 1 - signals[0]
buysellSignals[0] = 1 - signals[0]
buysellSignals[1] = signals[0]
buyAsset = np.random.choice(symbols, 1, True, signals)
marketSignal = np.random.choice(buysell, 1, True, buysellSignals)
indexNum = symbols.index(buyAsset)
if indexNum == 0:
yElement = symbols[1]
else:
yElement = symbols[0]
if (q_1>=1000000) | (q_2>=1000000) | (q_1_pool<=0) | (q_2_pool<=0) & (marketSignal == 'sell'):
marketSignal = 'buy'
#establishing bounds to the buy or sell decission
asset_pool = f'account_{symbols[indexNum]}_pool'
r = getVolumeRatio('totalPoolVolume', simulationRecord, transaction)
z = z_r(r)
totalFee = fee + z
if totalFee < minRev:
print('Your fee is lower than expected. Bounding with minRevenue')
totalFee = minRev
#how much you want to buy or sell
deltaQ = random.uniform(0, 3000)
#Pool liquidity bounds
if (q_1<15000) | (q_2<15000):
print('LIQUIDITY WARNING: Rising fee dramatically')
totalFee = 0.15
if totalFee <= traderMaxFee:
#Cost function
eVal, dynamicFee = eValue(poolInventory, totalFee)
if marketSignal == 'buy':
if indexNum == 0:
q_1 -= deltaQ
q_1_pool += deltaQ
else:
q_2_pool += deltaQ
q_2 -= deltaQ
if (q_1<0) | (q_2<0):
print('no liquidity')
break
elif marketSignal == 'sell':
if indexNum == 0:
q_1 += deltaQ
q_1_pool -= deltaQ
else:
q_2_pool -= deltaQ
q_2 += deltaQ
# C_q = lsdCostFunction([q_1_pool, q_2_pool], eVal, dynamicFee)
C_q = lsdCostFunction([q_1_pool, q_2_pool], dynamicFee)
transactCost = C_q - previousStateCost
previousState =[data[f'account_{symbol1}'][-1], data[f'account_{symbol2}'][-1]]
P_q_1 = lsdPriceFunction_i(C_q, totalFee, previousState, poolInventory)
print(marketSignal, buyAsset, deltaQ, transactCost)
costPerUnit = transactCost/deltaQ
simdata = {'transactionNumber': [transaction],
account1 : [q_1],
account2: [q_2],
account1_pool : [q_1_pool],
account2_pool: [q_2_pool],
'totalVolume': [q_1+q_2],
'totalPoolVolume': [q_1_pool+q_2_pool],
'totalFee': [totalFee],
'whoBuy': [buyAsset],
'ratioVolume': [r],
'z': [z],
'order': [marketSignal],
'transactCost': [transactCost],
'previousCost': [previousStateCost],
'deltaQ': [deltaQ],
'costPerUnit': [costPerUnit],
'r': r}
df_simdata = pd.DataFrame(data=simdata)
simulationRecord = simulationRecord.append(df_simdata)
simulationRecord['profitCumSum'] = simulationRecord['transactCost'].cumsum()
previousStateCost = C_q
transaction += 1
else:
print(f'Fee is too high for the trader. Its value is {totalFee}')
###############################################################################################
finalTime= time.time() - start_time
print(f"--- {finalTime} seconds ---")
print(f'We made {transaction} transactions in {finalTime} seconds with LSD-LMSR')
simulationRecord.to_csv('simulationRecord.csv')