Skip to content

Commit

Permalink
feat: make drift term available (#701)
Browse files Browse the repository at this point in the history
* feat: make drift term available

* feat: address comments

* feat: add more asset to risky_trader

* feat: tweaks for consistent AMM runs

* feat: add cumulated trade volume plot

* feat: reverse trade order

* chore: tidy up

* feat: address comments from Charlie

* chore: run linter

---------

Co-authored-by: Charlie <[email protected]>
  • Loading branch information
Jiajia-Cui and cdummett authored Sep 5, 2024
1 parent 682044b commit cde315a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 29 deletions.
10 changes: 7 additions & 3 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,8 +96,8 @@ 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(
aggregated_balance.balance
data[timestamp_to_datetime(aggregated_balance.timestamp)][account_key] = (
int(aggregated_balance.balance)
)

df = pd.DataFrame.from_dict(data, orient="index").sort_index().ffill()
Expand Down
35 changes: 35 additions & 0 deletions vega_query/visualisations/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,41 @@ def overlay_volume(
ax.step(x, y, label="volume", where="post")


def overlay_cumulative_volume(
ax: Axes,
trades: List[protos.vega.vega.Trade],
price_decimals: int,
size_decimals: int,
**kwargs,
):
x = [] # List to store timestamps
y = [] # List to store cumulative volume
cumulative_volume = 0 # Initialize cumulative volume

for trade in reversed(trades):
# Convert timestamp to datetime for x-axis
timestamp = timestamp_to_datetime(trade.timestamp, nano=True)
x.append(timestamp)

# Ensure price and size are available
if hasattr(trade, "price") and hasattr(trade, "size"):
price = padded_int_to_float(trade.price, price_decimals)
size = padded_int_to_float(trade.size, size_decimals)

# Calculate traded volume (price * size)
volume = price * size if price != 0 else 0

# Accumulate volume (ensure volume is positive)
cumulative_volume += abs(volume)
y.append(cumulative_volume)
else:
# If price or size is missing, assume no change in cumulative volume
y.append(cumulative_volume)

# Plot the cumulative volume data using step plot
ax.step(x, y, label="Cumulative Volume", where="post", **kwargs)


def overlay_maker_fee(
ax: Axes,
trades: List[protos.vega.vega.Trade],
Expand Down
35 changes: 18 additions & 17 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 All @@ -80,13 +74,13 @@ def create(
ymin = ymax = 0
axes: List[Axes] = []

axn0l = fig.add_subplot(gs[:, 0])
axn0l = fig.add_subplot(gs[0, 0])
axn0r: Axes = axn0l.twinx()
if market_data_history is not None:
overlay_mark_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)
# overlay_auction_starts(axn0r, market_data_history)
# overlay_auction_ends(axn0r, market_data_history)

axn0l.set_ylabel("USDT")
axn0l.set_title(
Expand All @@ -100,6 +94,10 @@ def create(
axn0r.legend(loc="upper right", framealpha=1)
axn0r.add_artist(leg)

ax10 = fig.add_subplot(gs[1, 0])
ax10.set_title("Cumulated traded notional", loc="left")
ax10.set_ylabel("Market traded notional")

ax11 = fig.add_subplot(gs[0, 1])
ax11.set_title("AMM: Position", loc="left")
ax11.set_ylabel("Open Volume")
Expand Down Expand Up @@ -130,6 +128,13 @@ def create(
date_range_start_timestamp=start_timestamp,
date_range_end_timestamp=end_timestamp,
)

overlay_cumulative_volume(
ax=ax10,
trades=trades,
price_decimals=market.decimal_places,
size_decimals=market.position_decimal_places,
)
overlay_position(
ax=ax11,
trades=trades,
Expand All @@ -139,17 +144,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
6 changes: 4 additions & 2 deletions vega_sim/scenario/amm/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
initial_price=70000,
annualised_volatility=0.5,
notional_trade_volume=100,
process_theta=0.02,
process_theta=0.01,
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=7000,
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: 2 additions & 0 deletions vega_sim/scenario/benchmark/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def __init__(
notional_trade_volume: int,
risky_trader_funds: int = 1_000,
process_theta: float = 0,
process_drift: float = 0,
):
self.market_config = market_config
self.initial_price = initial_price
self.process_theta = process_theta
self.process_drift = process_drift
self.annualised_volatility = annualised_volatility
self.notional_trade_volume = notional_trade_volume
self.risky_trader_funds = risky_trader_funds
Expand Down
2 changes: 1 addition & 1 deletion vega_sim/scenario/benchmark/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def configure_agents(
x0=benchmark_config.initial_price,
mu=benchmark_config.initial_price,
theta=benchmark_config.process_theta,
drift=benchmark_config.process_drift,
sigma=benchmark_config.annualised_volatility
* np.sqrt(self.step_length_seconds / (365.25 * 24 * 60 * 60))
* benchmark_config.initial_price,
drift=0,
)
self.agents.append(
ConfigurableMarketManager(
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 cde315a

Please sign in to comment.