From 64098729d3095a5e8989e1db757b6b819987966b Mon Sep 17 00:00:00 2001 From: Jiajia-Cui Date: Wed, 4 Sep 2024 12:53:50 +0100 Subject: [PATCH] feat: add cumulated trade volume plot --- vega_query/visualisations/overlay.py | 37 ++++++++++++++++++++++++++ vega_query/visualisations/plots/amm.py | 13 ++++++++- vega_sim/scenario/amm/registry.py | 4 +-- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/vega_query/visualisations/overlay.py b/vega_query/visualisations/overlay.py index 42b216b3b..79b7371ff 100644 --- a/vega_query/visualisations/overlay.py +++ b/vega_query/visualisations/overlay.py @@ -895,6 +895,43 @@ 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, +): + x = [] + y = [] + cumulative_volume = 0 # Initialize cumulative volume + + for trade in trades: + # Convert timestamp to datetime for x-axis + x.append(timestamp_to_datetime(trade.timestamp, nano=True)) + + # 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 + + # Debugging: Print current values + print(f"Trade timestamp: {trade.timestamp}, Price: {price}, Size: {size}, Volume: {volume}") + + # Accumulate volume + cumulative_volume += abs(volume) # Ensure volume is positive + y.append(cumulative_volume) + else: + # If price or size is missing, assume no change in cumulative volume + y.append(cumulative_volume) + print(f"Missing price or size for trade at {trade.timestamp}") + + # Plot the cumulative volume data using step plot + ax.step(x, y, label="Cumulative Volume", where="post") + + def overlay_maker_fee( ax: Axes, trades: List[protos.vega.vega.Trade], diff --git a/vega_query/visualisations/plots/amm.py b/vega_query/visualisations/plots/amm.py index 3c1511d22..e94d5af09 100644 --- a/vega_query/visualisations/plots/amm.py +++ b/vega_query/visualisations/plots/amm.py @@ -74,7 +74,7 @@ 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) @@ -94,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("traded notional") + ax11 = fig.add_subplot(gs[0, 1]) ax11.set_title("AMM: Position", loc="left") ax11.set_ylabel("Open Volume") @@ -124,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, diff --git a/vega_sim/scenario/amm/registry.py b/vega_sim/scenario/amm/registry.py index ba9d17598..14fb30436 100644 --- a/vega_sim/scenario/amm/registry.py +++ b/vega_sim/scenario/amm/registry.py @@ -11,9 +11,9 @@ BenchmarkConfig( market_config=configs.mainnet.BTCUSDT.CONFIG, initial_price=70000, - annualised_volatility=1, + annualised_volatility=0.5, notional_trade_volume=100, - process_theta=0.05, + process_theta=0.01, process_drift=-10, ), ],