Skip to content

Commit

Permalink
fix: market maker order spam rejections (#699)
Browse files Browse the repository at this point in the history
* feat: add overlay methods

* feat: include expired markets

* fix: avoid duplicate keys

* fix: avoid batch spam rejections
  • Loading branch information
cdummett authored Aug 30, 2024
1 parent 3f85e31 commit 682044b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion vega_query/scripts/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
19 changes: 12 additions & 7 deletions vega_query/service/utils/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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
39 changes: 36 additions & 3 deletions vega_query/visualisations/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ def overlay_last_traded_price(
ax: Axes,
market_data_history: List[protos.vega.vega.MarketData],
price_decimals: int,
**kwargs,
):
x = []
y = []
for market_data in market_data_history:
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(
Expand All @@ -72,28 +73,60 @@ def overlay_best_bid_price(
ax: Axes,
market_data_history: List[protos.vega.vega.MarketData],
price_decimals: int,
**kwargs,
):
x = []
y = []
for market_data in market_data_history:
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 = []
for market_data in market_data_history:
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(
Expand Down
2 changes: 1 addition & 1 deletion vega_query/visualisations/plots/amm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vega_query/visualisations/plots/sla.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions vega_sim/vegahome/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions vega_sim/wallet/slim_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 682044b

Please sign in to comment.