Skip to content

Commit

Permalink
feat: tweaks for consistent AMM runs
Browse files Browse the repository at this point in the history
  • Loading branch information
cdummett committed Sep 3, 2024
1 parent 6113b41 commit 5399890
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
8 changes: 6 additions & 2 deletions vega_query/service/utils/party.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import pandas as pd

from vega_query.utils import timestamp_to_datetime

logger = getLogger(__name__)


Expand Down Expand Up @@ -42,7 +44,9 @@ def historic_positions(
data = defaultdict(lambda: defaultdict(int))
for trade in trades:
position_after_trade = positions.get(trade.market_id, 0)
data[trade.timestamp][trade.market_id] = position_after_trade
data[timestamp_to_datetime(trade.timestamp)][
trade.market_id
] = position_after_trade
match party_id:
case trade.buyer:
positions[trade.market_id] -= trade.size
Expand Down Expand Up @@ -92,7 +96,7 @@ def historic_balances(
)
if aggregated_balance.market_id is not "":
account_key += f" | {aggregated_balance.market_id[:7]}"
data[aggregated_balance.timestamp][account_key] = int(
data[timestamp_to_datetime(aggregated_balance.timestamp)][account_key] = int(
aggregated_balance.balance
)

Expand Down
18 changes: 4 additions & 14 deletions vega_query/visualisations/plots/amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ def create(
# Set unique colors for each party and request party specific information
amms = service.api.data.list_amms(market_id=market.id)
if party_ids is None:
# party_ids = __party_defaults_old(market_data_history=market_data_history)
party_ids = __party_defaults(amms=amms)
# party_ids = __party_defaults(amms=amms) + __party_defaults_old(
# market_data_history=market_data_history
# )

print(party_ids)

party_colors = __party_colors(party_ids)

Expand Down Expand Up @@ -139,17 +133,13 @@ def create(
ax11.get_lines()[-1].set_label(amm_party_id[:7])

# Reconstruct the AMMs aggregated balance from balance changes
aggregated_balances = service.api.data.list_balance_changes(
party_ids=[amm_party_id],
df = service.utils.party.historic_balances(
party_id=amm_party_id,
asset_id=asset.id,
date_range_start_timestamp=start_timestamp,
date_range_end_timestamp=end_timestamp,
)
overlay_aggregated_balances(
ax=ax21,
aggregated_balances=aggregated_balances,
asset_decimals=asset.details.decimals,
)
ax21.get_lines()[-1].set_label(amm_party_id[:7])
ax21.step(df.index, df.total, where="post", label="total")

ax11.legend()
ax21.legend()
Expand Down
9 changes: 5 additions & 4 deletions vega_sim/scenario/amm/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
BenchmarkConfig(
market_config=configs.mainnet.BTCUSDT.CONFIG,
initial_price=70000,
annualised_volatility=0.5,
annualised_volatility=1,
notional_trade_volume=100,
process_theta=0.02,
process_drift=-0.1,
process_theta=0.05,
process_drift=-10,
),
],
amm_liquidity_fee=0.001,
amm_liquidity_fee=0.0001,
amm_update_frequency=0,
initial_network_parameters={
"validators.epoch.length": "1h",
"market.fee.factors.makerFee": "0",
},
),
}
8 changes: 4 additions & 4 deletions vega_sim/scenario/amm/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ def configure_agents(
wallet_name="AutomatedMarketMaker",
key_name=f"AutomatedMarketMaker_{benchmark_config.market_config.instrument.code}_{str(i_agent).zfill(3)}",
market_name=benchmark_config.market_config.instrument.name,
initial_asset_mint=2e6,
commitment_amount=1e5,
initial_asset_mint=1e6,
commitment_amount=10_000,
slippage_tolerance=0.05,
proposed_fee=self.amm_liquidity_fee,
price_process=iter(benchmark_config.price_process),
lower_bound_scaling=1 - 0.05,
upper_bound_scaling=1 + 0.05,
lower_bound_scaling=1 - 0.02,
upper_bound_scaling=1 + 0.02,
leverage_at_lower_bound=20,
leverage_at_upper_bound=20,
update_bias=self.amm_update_frequency,
Expand Down
2 changes: 1 addition & 1 deletion vega_sim/scenario/benchmark/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(
initial_price: float,
annualised_volatility: float,
notional_trade_volume: int,
risky_trader_funds: int = 100_000,
risky_trader_funds: int = 1_000,
process_theta: float = 0,
process_drift: float = 0,
):
Expand Down
3 changes: 1 addition & 2 deletions vega_sim/scenario/common/utils/price_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,8 @@ def ou_price_process(n, theta=0.15, mu=0.0, sigma=0.2, x0=1.0, drift=0.0):
x[0] = x0
for t in range(1, n):
dx = (
theta * (mu - x[t - 1]) * dt
theta * ((mu + t * drift) - x[t - 1]) * dt
+ sigma * np.sqrt(dt) * np.random.normal()
+ drift * dt
)
x[t] = x[t - 1] + dx
return x
Expand Down

0 comments on commit 5399890

Please sign in to comment.