From 682044babcc209350d5431d07ea979c86207fb5d Mon Sep 17 00:00:00 2001 From: Charlie <99198652+cdummett@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:15:09 +0100 Subject: [PATCH] fix: market maker order spam rejections (#699) * feat: add overlay methods * feat: include expired markets * fix: avoid duplicate keys * fix: avoid batch spam rejections --- vega_query/scripts/plots.py | 2 +- vega_query/service/utils/market.py | 19 +++++++----- vega_query/visualisations/overlay.py | 39 +++++++++++++++++++++++-- vega_query/visualisations/plots/amm.py | 2 +- vega_query/visualisations/plots/sla.py | 2 +- vega_sim/scenario/benchmark/scenario.py | 2 +- vega_sim/vegahome/genesis.json | 2 ++ vega_sim/wallet/slim_wallet.py | 3 ++ 8 files changed, 57 insertions(+), 14 deletions(-) diff --git a/vega_query/scripts/plots.py b/vega_query/scripts/plots.py index 58547076a..3216a6a1e 100644 --- a/vega_query/scripts/plots.py +++ b/vega_query/scripts/plots.py @@ -77,7 +77,7 @@ if args.start_time is None else int(args.start_time.timestamp() * 1e9) ) - market = service.utils.market.find_market([args.market]) + market = service.utils.market.find_market([args.market], include_settled=True) if market.tradable_instrument.instrument.spot != protos.vega.markets.Spot(): asset = service.utils.market.find_quote_asset([args.market]) if market.tradable_instrument.instrument.future != protos.vega.markets.Future(): diff --git a/vega_query/service/utils/market.py b/vega_query/service/utils/market.py index 98135d302..1887371ff 100644 --- a/vega_query/service/utils/market.py +++ b/vega_query/service/utils/market.py @@ -31,7 +31,7 @@ def find_market( def find_asset(self, substrings: List[str]) -> protos.vega.assets.Asset: instrument = self.find_market( - substrings=substrings + substrings=substrings, include_settled=True ).tradable_instrument.instrument if instrument.spot != protos.vega.markets.Spot(): return self.find_quote_asset(substrings) @@ -42,7 +42,8 @@ def find_asset(self, substrings: List[str]) -> protos.vega.assets.Asset: def find_base_asset(self, substrings: List[str]) -> protos.vega.assets.Asset: instrument = self.find_market( - substrings=substrings + substrings=substrings, + include_settled=True, ).tradable_instrument.instrument asset_id = None if instrument.spot != protos.vega.markets.Spot(): @@ -53,7 +54,8 @@ def find_base_asset(self, substrings: List[str]) -> protos.vega.assets.Asset: def find_quote_asset(self, substrings: List[str]) -> protos.vega.assets.Asset: instrument = self.find_market( - substrings=substrings + substrings=substrings, + include_settled=True, ).tradable_instrument.instrument asset_id = None if instrument.spot != protos.vega.markets.Spot(): @@ -65,11 +67,10 @@ def find_quote_asset(self, substrings: List[str]) -> protos.vega.assets.Asset: def find_settlement_asset( self, substrings: List[str], - include_settled: bool = False, ) -> protos.vega.assets.Asset: instrument = self.find_market( substrings=substrings, - include_settled=include_settled, + include_settled=True, ).tradable_instrument.instrument asset_id = None if instrument.future != protos.vega.markets.Future(): @@ -81,7 +82,11 @@ def find_settlement_asset( raise Exception("Market is not a future or perpetual market.") def find_size_decimals(self, substrings: List[str]) -> int: - return self.find_market(substrings=substrings).position_decimal_places + return self.find_market( + substrings=substrings, include_settled=True + ).position_decimal_places def find_price_decimals(self, substrings: List[str]) -> int: - return self.find_market(substrings=substrings).decimal_places + return self.find_market( + substrings=substrings, include_settled=True + ).decimal_places diff --git a/vega_query/visualisations/overlay.py b/vega_query/visualisations/overlay.py index 721acc00e..42b216b3b 100644 --- a/vega_query/visualisations/overlay.py +++ b/vega_query/visualisations/overlay.py @@ -44,6 +44,7 @@ def overlay_last_traded_price( ax: Axes, market_data_history: List[protos.vega.vega.MarketData], price_decimals: int, + **kwargs, ): x = [] y = [] @@ -51,7 +52,7 @@ def overlay_last_traded_price( x.append(timestamp_to_datetime(market_data.timestamp, nano=True)) price = padded_int_to_float(market_data.last_traded_price, price_decimals) y.append(price if price != 0 else np.nan) - ax.step(x, y, label="last_traded_price", where="post") + ax.step(x, y, label="last_traded_price", where="post", **kwargs) def overlay_indicative_price( @@ -72,6 +73,7 @@ def overlay_best_bid_price( ax: Axes, market_data_history: List[protos.vega.vega.MarketData], price_decimals: int, + **kwargs, ): x = [] y = [] @@ -79,13 +81,29 @@ def overlay_best_bid_price( x.append(timestamp_to_datetime(market_data.timestamp, nano=True)) price = padded_int_to_float(market_data.best_bid_price, price_decimals) y.append(price if price != 0 else np.nan) - ax.step(x, y, label="best_bid_price", where="post") + ax.step(x, y, label="best_bid_price", where="post", **kwargs) + + +def overlay_best_bid_size( + ax: Axes, + market_data_history: List[protos.vega.vega.MarketData], + size_decimals: int, + **kwargs, +): + x = [] + y = [] + for market_data in market_data_history: + x.append(timestamp_to_datetime(market_data.timestamp, nano=True)) + size = padded_int_to_float(market_data.best_bid_volume, size_decimals) + y.append(size if size != 0 else np.nan) + ax.step(x, y, label="best_bid_size", where="post", **kwargs) def overlay_best_ask_price( ax: Axes, market_data_history: List[protos.vega.vega.MarketData], price_decimals: int, + **kwargs, ): x = [] y = [] @@ -93,7 +111,22 @@ def overlay_best_ask_price( x.append(timestamp_to_datetime(market_data.timestamp, nano=True)) price = padded_int_to_float(market_data.best_offer_price, price_decimals) y.append(price if price != 0 else np.nan) - ax.step(x, y, label="best_ask_price", where="post") + ax.step(x, y, label="best_ask_price", where="post", **kwargs) + + +def overlay_best_ask_size( + ax: Axes, + market_data_history: List[protos.vega.vega.MarketData], + size_decimals: int, + **kwargs, +): + x = [] + y = [] + for market_data in market_data_history: + x.append(timestamp_to_datetime(market_data.timestamp, nano=True)) + size = padded_int_to_float(market_data.best_offer_volume, size_decimals) + y.append(size if size != 0 else np.nan) + ax.step(x, y, label="best_ask_size", where="post", **kwargs) def overlay_price_bounds( diff --git a/vega_query/visualisations/plots/amm.py b/vega_query/visualisations/plots/amm.py index e02c89802..05551b325 100644 --- a/vega_query/visualisations/plots/amm.py +++ b/vega_query/visualisations/plots/amm.py @@ -40,7 +40,7 @@ def create( ): # Get market, asset, and network parameter information - market = service.utils.market.find_market([market_code]) + market = service.utils.market.find_market([market_code], include_settled=True) asset = service.utils.market.find_asset([market_code]) epoch_length = duration_str_to_int( service.api.data.get_network_parameter(key="validators.epoch.length").value diff --git a/vega_query/visualisations/plots/sla.py b/vega_query/visualisations/plots/sla.py index 391894239..97b2d8062 100644 --- a/vega_query/visualisations/plots/sla.py +++ b/vega_query/visualisations/plots/sla.py @@ -43,7 +43,7 @@ def create( ): # Get market, asset, and network parameter information - market = service.utils.market.find_market([market_code]) + market = service.utils.market.find_market([market_code], include_settled=True) asset = service.utils.market.find_asset([market_code]) epoch_length = duration_str_to_int( service.api.data.get_network_parameter(key="validators.epoch.length").value diff --git a/vega_sim/scenario/benchmark/scenario.py b/vega_sim/scenario/benchmark/scenario.py index 833fde216..c2def24bf 100644 --- a/vega_sim/scenario/benchmark/scenario.py +++ b/vega_sim/scenario/benchmark/scenario.py @@ -109,7 +109,7 @@ def configure_agents( self.agents.append( ExponentialShapedMarketMaker( wallet_name="ExponentialShapedMarketMaker", - key_name="ExponentialShapedMarketMaker", + key_name=f"ExponentialShapedMarketMaker_{benchmark_config.market_config.instrument.code}", price_process_generator=iter(benchmark_config.price_process), initial_asset_mint=1e9, market_name=market_name, diff --git a/vega_sim/vegahome/genesis.json b/vega_sim/vegahome/genesis.json index 02f5f18d3..4ada5b409 100644 --- a/vega_sim/vegahome/genesis.json +++ b/vega_sim/vegahome/genesis.json @@ -212,6 +212,8 @@ "spam.protection.proposal.min.tokens": "2000000000000000000000", "spam.protection.voting.min.tokens": "1000000000000000000", "spam.protection.applyReferral.min.funds": "0", + "spam.order.minimumMarginQuantumMultiple": "0", + "spam.order.minimumHoldingQuantumMultiple": "0", "transfer.fee.factor": "0.001", "transfer.minTransferQuantumMultiple": "0", "transfer.fee.maxQuantumAmount": "100", diff --git a/vega_sim/wallet/slim_wallet.py b/vega_sim/wallet/slim_wallet.py index 71dc2d83f..55566c96a 100644 --- a/vega_sim/wallet/slim_wallet.py +++ b/vega_sim/wallet/slim_wallet.py @@ -119,6 +119,9 @@ def create_key( self.pub_keys[wallet_name] = {} if self.vega_wallet is None: + if name in self.keys[wallet_name] and name in self.pub_keys[wallet_name]: + return + self.keys[wallet_name][name] = SigningKey.generate() self.pub_keys[wallet_name][name] = ( self.keys[wallet_name][name]