Skip to content

Commit

Permalink
feat: add cumulated trade volume plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiajia-Cui committed Sep 4, 2024
1 parent 5399890 commit 6409872
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
37 changes: 37 additions & 0 deletions vega_query/visualisations/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
13 changes: 12 additions & 1 deletion vega_query/visualisations/plots/amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions vega_sim/scenario/amm/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
],
Expand Down

0 comments on commit 6409872

Please sign in to comment.