Skip to content

Commit

Permalink
feat: converted market data
Browse files Browse the repository at this point in the history
* fix: correct tags in make file

* feat: add market data handler

* feat: rename market data methods

* feat: migrate methods from data to service

* feat: update state extraction

* fix: make run black

* fix: correct decimal mapping

* feat: update market_data uses

* fix: run make black

* feat: add mark_price too snitch

* fix: liquidity fee shares

* feat: Fix up when loading new market data too fast

* fix: run make black

* feat: Fix up handling when market_id is None

---------

Co-authored-by: Tom McLean <[email protected]>
  • Loading branch information
cdummett and TomMcL authored Mar 17, 2023
1 parent b588f91 commit a8d6941
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 318 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pull_deps_networks:
@echo "Downloading Git dependencies into " ${EXTERN_DIR}
@echo "Downloading Vega networks-internal"
@if [ ! -d ./extern/networks-internal ]; then mkdir ./extern/networks-internal; git clone https://github.com/vegaprotocol/networks-internal ${EXTERN_DIR}/networks-internal; fi
ifneq (${VEGA_SIM_NETWORKS_TAG},develop)
@git -C ${EXTERN_DIR}/networks-internal pull; git -C ${EXTERN_DIR}/networks-internal checkout ${VEGA_SIM_NETWORKS_TAG}
ifneq (${VEGA_SIM_NETWORKS_INTERNAL_TAG},develop)
@git -C ${EXTERN_DIR}/networks-internal pull; git -C ${EXTERN_DIR}/networks-internal checkout ${VEGA_SIM_NETWORKS_INTERNAL_TAG}
else
@git -C ${EXTERN_DIR}/networks-internal checkout develop; git -C ${EXTERN_DIR}/networks-internal pull
endif
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/feature_tests/0002-STTL-008.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
" print(general+margin+bond - mint_amount[wallet.name])\n",
" print(vega.order_status(order_id=buy_order_id))\n",
"\n",
"print(vega.market_data(market_id=market_id))"
"print(vega.get_latest_market_data(market_id=market_id))"
]
},
{
Expand Down Expand Up @@ -306,7 +306,7 @@
"# Check order status/ market state after amend\n",
"print(vega.order_status(order_id=buy_order_id))\n",
"\n",
"print(vega.market_data(market_id=market_id))"
"print(vega.get_latest_market_data(market_id=market_id))"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/feature_tests/0004-AMND-010.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"# Check order status/ market state\n",
"print(vega.order_status(order_id=buy_order_id))\n",
"\n",
"print(vega.market_data(market_id=market_id))"
"print(vega.get_latest_market_data(market_id=market_id))"
]
},
{
Expand Down Expand Up @@ -180,7 +180,7 @@
"# Check order status/ market state after amend\n",
"print(vega.order_status(order_id=buy_order_id))\n",
"\n",
"print(vega.market_data(market_id=market_id))"
"print(vega.get_latest_market_data(market_id=market_id))"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion examples/visualisations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def move_market(
Volume to be traded at new price.
"""

market_data = vega.market_data(market_id=market_id)
market_data = vega.get_latest_market_data(market_id=market_id)
market_info = vega.market_info(market_id=market_id)

curr_price = int(market_data.mid_price) * 10**-market_info.decimal_places
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_one_off_transfer(vega_service_with_high_volume_with_market: VegaService
to_account_type=vega_protos.vega.ACCOUNT_TYPE_GENERAL,
asset=asset_id,
amount=500,
delay=15,
delay=50,
)

vega.wait_fn(10)
Expand All @@ -256,7 +256,7 @@ def test_one_off_transfer(vega_service_with_high_volume_with_market: VegaService
assert party_a_accounts_t2.general == 500
assert party_b_accounts_t2.general == 1000

vega.wait_fn(10)
vega.wait_fn(100)
vega.wait_for_total_catchup()

party_a_accounts_t3 = vega.party_account(
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/test_transaction_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ def _test_transaction_store():
log_dir = vega.log_dir

market_id = vega.all_markets()[0].id
final_oi = vega.market_data(market_id=market_id).open_interest
final_oi = vega.get_latest_market_data(market_id=market_id).open_interest
time.sleep(1)

with replay.replay_run_context(replay_path=log_dir) as vega:
time.sleep(1)
vega.wait_for_core_catchup()
final_oi_replay = vega.market_data(market_id=market_id).open_interest
final_oi_replay = vega.get_latest_market_data(market_id=market_id).open_interest

with replay.replay_run_context(replay_path=log_dir) as vega:
time.sleep(1)
vega.wait_for_core_catchup()
final_oi_replay_2 = vega.market_data(market_id=market_id).open_interest
final_oi_replay_2 = vega.get_latest_market_data(
market_id=market_id
).open_interest

assert final_oi == final_oi_replay
assert final_oi_replay_2 == final_oi_replay
87 changes: 0 additions & 87 deletions tests/vega_sim/api/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
Trade,
AggregatedLedgerEntry,
get_asset_decimals,
best_prices,
price_bounds,
find_asset_id,
get_trades,
margin_levels,
Expand All @@ -30,7 +28,6 @@
open_orders_by_market,
party_account,
list_transfers,
get_liquidity_fee_shares,
list_ledger_entries,
)
from vega_sim.grpc.client import (
Expand Down Expand Up @@ -198,43 +195,6 @@ def test_asset_decimals(mkt_info_mock):
assert get_asset_decimals("ASSET", None) == 3


@patch("vega_sim.api.data_raw.market_data")
def test_best_prices(mkt_data_mock):
mkt_data = MagicMock()
mkt_data_mock.return_value = mkt_data

mkt_data.best_static_bid_price = "202"
mkt_data.best_static_offer_price = "212"

bid_res, ask_res = best_prices("mkt", None, 2)
assert bid_res == pytest.approx(2.02)
assert ask_res == pytest.approx(2.12)


@patch("vega_sim.api.data_raw.market_data")
def test_price_bounds(mkt_data_mock):
mkt_data = MagicMock()
mkt_data_mock.return_value = mkt_data
mkt_data.price_monitoring_bounds = [
vega_protos.vega.PriceMonitoringBounds(
min_valid_price="10000",
max_valid_price="20000",
),
vega_protos.vega.PriceMonitoringBounds(
min_valid_price="11000",
max_valid_price="19000",
),
vega_protos.vega.PriceMonitoringBounds(
min_valid_price="12000",
max_valid_price="18000",
),
]

min_valid_price, max_valid_price = price_bounds("mkt", None, 2)
assert min_valid_price == pytest.approx(120)
assert max_valid_price == pytest.approx(180)


def test_open_orders_by_market(trading_data_v2_servicer_and_port):
def ListOrders(self, request, context):
return data_node_protos_v2.trading_data.ListOrdersResponse(
Expand Down Expand Up @@ -723,53 +683,6 @@ def ListTransfers(self, request, context):
assert res == [expected]


def test_get_liquidity_fee_shares(trading_data_v2_servicer_and_port):
expected = {"party1": 0.75, "party2": 0.25}

def GetLatestMarketData(self, request, context):
return data_node_protos_v2.trading_data.GetLatestMarketDataResponse(
market_data=vega_protos.vega.MarketData(
liquidity_provider_fee_share=[
vega_protos.vega.LiquidityProviderFeeShare(
party="party1",
equity_like_share="0.75",
average_entry_valuation="75",
average_score="0.5",
),
vega_protos.vega.LiquidityProviderFeeShare(
party="party2",
equity_like_share="0.25",
average_entry_valuation="100",
average_score="0.5",
),
]
)
)

server, port, mock_servicer = trading_data_v2_servicer_and_port
mock_servicer.GetLatestMarketData = GetLatestMarketData

add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")

res1 = get_liquidity_fee_shares(
data_client=data_client, market_id="na", party_id="party1"
)
assert res1 == expected["party1"]

res2 = get_liquidity_fee_shares(
data_client=data_client, market_id="na", party_id="party2"
)
assert res2 == expected["party2"]

res3 = get_liquidity_fee_shares(
data_client=data_client,
market_id="na",
)
assert res3 == expected


def test_list_ledger_entries(trading_data_v2_servicer_and_port):
expected = data_node_protos_v2.trading_data.AggregatedLedgerEntry(
timestamp=10000000, quantity="540", asset_id="asset1"
Expand Down
6 changes: 3 additions & 3 deletions tests/vega_sim/api/test_data_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
infrastructure_fee_accounts,
liquidity_provisions,
market_accounts,
market_data,
get_latest_market_data,
market_data_history,
market_info,
order_status,
Expand Down Expand Up @@ -364,7 +364,7 @@ def ListAccounts(self, request, context):
assert res == expected


def test_market_data(trading_data_v2_servicer_and_port):
def test_get_latest_market_data(trading_data_v2_servicer_and_port):
expected = vega_protos.vega.MarketData(mid_price="100", market="foobar")

def GetLatestMarketData(self, request, context):
Expand All @@ -380,7 +380,7 @@ def GetLatestMarketData(self, request, context):
add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
res = market_data(market_id="foobar", data_client=data_client)
res = get_latest_market_data(market_id="foobar", data_client=data_client)

assert res == expected

Expand Down
Loading

0 comments on commit a8d6941

Please sign in to comment.