Skip to content

Commit

Permalink
exploratory uniswap agent to explore target grid price
Browse files Browse the repository at this point in the history
  • Loading branch information
msabvid committed Feb 28, 2024
1 parent 28638c8 commit f0ac227
Show file tree
Hide file tree
Showing 6 changed files with 9,908 additions and 3,958 deletions.
8,258 changes: 5,609 additions & 2,649 deletions simulations/aave/cache.json

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions simulations/aave/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ def runner(
# -------------------------
# Initialize Uniswap agent
# -------------------------
uniswap_agent_type = (
partial(DummyUniswapAgent, sim_n_steps=n_steps) if init_cache else UniswapAgent
)
uniswap_agent = uniswap_agent_type(
env=env,
dt=0.01,
Expand Down
2 changes: 1 addition & 1 deletion simulations/agents/borrow_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update(self, rng: np.random.Generator, env):
env, self.address, self.oracle_address, [self.token_b_address]
)[0][0]
coef = 10 ** (self.decimals_token_b - 4)
u = rng.integers(low=7000, high=9500)
u = rng.integers(low=7000, high=9300)
available_borrow = int(
coef * available_borrow_base * u / borrow_asset_price
)
Expand Down
29 changes: 23 additions & 6 deletions simulations/agents/uniswap_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,16 @@ def __init__(
sqrt_price_uniswap_x96 = self.get_sqrt_price_x96_uniswap(env)

if self.token_b == self.token1_address:
token_a_price = (sqrt_price_uniswap_x96 / 2**96) ** 2
self.init_token_a_price = (sqrt_price_uniswap_x96 / 2**96) ** 2
token_b_price = 1
else:
token_a_price = (2**96 / sqrt_price_uniswap_x96) ** 2
self.init_token_a_price = (2**96 / sqrt_price_uniswap_x96) ** 2
token_b_price = 1

self.external_market = Gbm(
mu=mu,
sigma=sigma,
token_a_price=token_a_price,
token_a_price=self.init_token_a_price,
token_b_price=token_b_price,
dt=dt,
)
Expand Down Expand Up @@ -511,19 +511,36 @@ def __init__(
token_a_address=token_a_address,
token_b_address=token_b_address,
mu=mu,
sigma=sigma,
sigma=0,
dt=dt,
)
self.sim_n_steps = sim_n_steps

# calibrate mu to explore the pool
upper_bound_price = 1.7 * self.init_token_a_price
lower_bound_price = 0.3 * self.init_token_a_price

self.mu0 = (
1
/ (dt * float(sim_n_steps // 3))
* np.log(upper_bound_price / self.init_token_a_price)
)
self.mu1 = (
1
/ (dt * sim_n_steps - dt * float(sim_n_steps // 3))
* np.log(lower_bound_price / upper_bound_price)
)

def update(self, rng: np.random.Generator, env):
"""
Update the state of the agent
Makes an exploratory update by manually changing
the drift of the external market
"""
if self.step < self.sim_n_steps // 3:
self.external_market.mu = self.mu0
else:
self.external_market.mu = self.mu1
tx = super().update(rng, env)
if self.step in [self.sim_n_steps // 4, 3 * self.sim_n_steps // 4]:
self.external_market.mu = -self.external_market.mu
return tx
5,570 changes: 4,275 additions & 1,295 deletions simulations/uniswap/cache.json

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions simulations/uniswap/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ def runner(
# ------------------------
# Initialize Uniswap agent
# ------------------------
uniswap_agent_type = (
partial(DummyUniswapAgent, sim_n_steps=n_steps) if init_cache else UniswapAgent
)

agent = uniswap_agent_type(
env=env,
dt=0.01,
Expand Down

0 comments on commit f0ac227

Please sign in to comment.