Skip to content

Commit

Permalink
feat: add asset decimal scaling and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiajia-Cui committed Sep 23, 2024
1 parent dd8aae8 commit 64b59a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
3 changes: 3 additions & 0 deletions vega_query/service/utils/party.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def historic_balances(
self,
party_id: str,
asset_id: str,
asset_decimals: int,
market_id: Optional[str] = None,
account_types: Optional[List[protos.vega.vega.AccountType.Value]] = None,
date_range_start_timestamp: Optional[int] = None,
Expand Down Expand Up @@ -102,4 +103,6 @@ def historic_balances(

df = pd.DataFrame.from_dict(data, orient="index").sort_index().ffill()
df["total"] = df.sum(axis=1)
df["total"] = df["total"]* 10**-asset_decimals
return df

14 changes: 9 additions & 5 deletions vega_query/visualisations/plots/amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def create(

party_colors = __party_colors(party_ids)

fig = plt.figure(figsize=(15, 8))
fig = plt.figure(figsize=(16, 9))
gs = fig.add_gridspec(2, 2)
ymin = ymax = 0
axes: List[Axes] = []
Expand All @@ -78,13 +78,15 @@ def create(
axn0r: Axes = axn0l.twinx()
if market_data_history is not None:
overlay_mark_price(axn0l, market_data_history, market.decimal_places)
overlay_last_traded_price(axn0l, market_data_history, market.decimal_places)
overlay_trading_mode(axn0r, market_data_history)
# overlay_auction_starts(axn0r, market_data_history)
# overlay_auction_ends(axn0r, market_data_history)

axn0l.set_ylabel("USDT")
axn0l.set_title(
"Market Price: Ornstein–Uhlenbeck ($\\theta=0.01$, $\\sigma=0.5$)",
# "Market Price: Ornstein–Uhlenbeck ($\\theta=0.01$, $\\sigma=0.5$)",
"Market Price: Ornstein–Uhlenbeck",
loc="left",
)

Expand All @@ -106,7 +108,7 @@ def create(
axes.append(ax11)
ax21 = fig.add_subplot(gs[1, 1])
ax21.set_title("AMM: Aggregated Account Balances [USDT]", loc="left")
ax21.set_ylabel("$\\Delta$ USDT")
ax21.set_ylabel("USDT")
ax21.sharex(axn0l)
axes.append(ax21)

Expand Down Expand Up @@ -141,16 +143,18 @@ def create(
party_id=amm_party_id,
size_decimals=market.position_decimal_places,
)
ax11.get_lines()[-1].set_label(amm_party_id[:7])
# ax11.get_lines()[-1].set_label(amm_party_id[:7])
ax11.get_lines()[-1].set_label("AMM")

# Reconstruct the AMMs aggregated balance from balance changes
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,
asset_decimals=asset.details.decimals,
)
ax21.step(df.index, df.total, where="post", label="total")
ax21.step(df.index, df.total, where="post", label="AMM portfolio")

ax11.legend()
ax21.legend()
Expand Down
11 changes: 6 additions & 5 deletions vega_sim/scenario/amm/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
benchmark_configs=[
BenchmarkConfig(
market_config=configs.mainnet.BTCUSDT.CONFIG,
initial_price=70000,
annualised_volatility=0.5,
notional_trade_volume=100,
process_theta=0.01,
process_drift=-10,
initial_price=60000,
annualised_volatility=0.28,
notional_trade_volume=10,
process_theta=0.0001,
process_drift=-5,
risky_trader_funds=1,
),
],
amm_liquidity_fee=0.0001,
Expand Down
44 changes: 16 additions & 28 deletions vega_sim/scenario/amm/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ def configure_agents(
# Set commitment amount to 0 to avoid benchmark market maker from
# providing liquidity, they should only supply volume through orders.
agent.fee_amount = self.amm_liquidity_fee
agent.commitment_amount = 0.000001
agent.supplied_amount = 0

# if isinstance(agent, MarketOrderTrader):
# # Set commitment amount to 0 to avoid benchmark market maker from
# # providing liquidity, they should only supply volume through orders.
# agent.fee_amount = self.amm_liquidity_fee
# agent.commitment_amount =10
# agent.supplied_amount = 50

# if isinstance(agent, LimitOrderTrader):
# # Set commitment amount to 0 to avoid benchmark market maker from
# # providing liquidity, they should only supply volume through orders.
# agent.spread = 0
# agent.mean = -7.5
# agent.sigma = 0.3

# For each market, add an AMM agent.
for benchmark_config in self.benchmark_configs:
Expand All @@ -83,34 +99,6 @@ def configure_agents(
)
)

for i_agent, benchmark_config in enumerate(self.benchmark_configs):
extra_agents.append(
MarketOrderTrader(
wallet_name="MarketOrderTrader",
key_name=f"MarketOrderTrader_{benchmark_config.market_config.instrument.code}_{str(i_agent).zfill(3)}",
market_name=benchmark_config.market_config.instrument.name,
initial_asset_mint=1e6,
buy_intensity=1.0, # Set buy intensity for the trader
sell_intensity=1.0, # Set sell intensity for the trader
base_order_size=1.0, # Set base order size for market orders
tag=f"MarketOrderTrader_{benchmark_config.market_config.instrument.code}_{str(i_agent).zfill(3)}",
random_state=self.random_state,
)
)

extra_agents.append(
LimitOrderTrader(
wallet_name="LimitOrderTrader",
key_name=f"LimitOrderTrader_{benchmark_config.market_config.instrument.code}_{str(i_agent).zfill(3)}",
market_name=benchmark_config.market_config.instrument.name,
initial_asset_mint=1e6,
buy_intensity=1.0, # Set buy intensity for the trader
sell_intensity=1.0, # Set sell intensity for the trader
tag=f"LimitOrderTrader_{benchmark_config.market_config.instrument.code}_{str(i_agent).zfill(3)}",
random_state=self.random_state,
)
)

extra_agents = {agent.name(): agent for agent in extra_agents}
agents.update(extra_agents)
return agents
9 changes: 3 additions & 6 deletions vega_sim/scenario/benchmark/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ def configure_agents(
buy_intensity=100,
sell_intensity=100,
base_order_size=benchmark_config.notional_trade_volume
/ benchmark_config.initial_price
/ 100,
/ (benchmark_config.initial_price*100),
step_bias=1,
initial_asset_mint=1e8,
tag=f"{benchmark_config.market_config.instrument.code}_{str(i_agent).zfill(3)}",
Expand All @@ -171,11 +170,9 @@ def configure_agents(
market_name=market_name,
time_in_force_opts={"TIME_IN_FORCE_GTT": 1},
buy_volume=benchmark_config.notional_trade_volume
/ benchmark_config.initial_price
/ 100,
/ (benchmark_config.initial_price*100),
sell_volume=benchmark_config.notional_trade_volume
/ benchmark_config.initial_price
/ 100,
/ (benchmark_config.initial_price*100),
buy_intensity=100,
sell_intensity=100,
submit_bias=1,
Expand Down

0 comments on commit 64b59a5

Please sign in to comment.