Skip to content

Commit

Permalink
feat: update estimate position tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdummett committed Jan 22, 2024
1 parent 214fb40 commit c8a9e46
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 28 deletions.
98 changes: 74 additions & 24 deletions tests/integration/test_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,19 @@ def test_estimate_position(vega_service_with_market: VegaServiceNull):
vega = vega_service_with_market
market_id = vega.all_markets()[0].id

margin, liquidation = vega.estimate_position(
margin, collateral_increase_estimate, liquidation = vega.estimate_position(
market_id=market_id,
open_volume=-1,
average_entry_price=1,
margin_account_balance=1000,
general_account_balance=1000,
order_margin_account_balance=1000,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
margin_factor=0.5,
side=["SIDE_SELL", "SIDE_SELL"],
price=[1.01, 1.02],
remaining=[1, 1],
is_market_order=[False, False],
collateral_available=1,
)


Expand Down Expand Up @@ -535,23 +540,31 @@ def test_liquidation_price_witin_estimate_position_bounds_AC002(
collateral = account_TRADER_B.margin + account_TRADER_B.general
market_data = vega.get_latest_market_data(market_id=market_id)

_, estimate_liquidation_price_initial = vega.estimate_position(
_, _, estimate_liquidation_price_initial = vega.estimate_position(
market_id,
open_volume=-100,
side=["SIDE_SELL"],
price=[market_data.mark_price],
remaining=[0],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=500,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)
_, estimate_liquidation_price_MO = vega.estimate_position(
_, _, estimate_liquidation_price_MO = vega.estimate_position(
market_id,
open_volume=0,
side=["SIDE_SELL"],
price=[market_data.mark_price],
remaining=[100],
is_market_order=[True],
collateral_available=collateral,
average_entry_price=500,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)

for price in [519, 520, 543, 543.5]:
Expand Down Expand Up @@ -768,14 +781,18 @@ def test_liquidation_price_witin_estimate_position_bounds_AC005(
PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin

_, estimate_liquidation_price_1 = vega.estimate_position(
_, _, estimate_liquidation_price_1 = vega.estimate_position(
market_id,
open_volume=-10,
side=["SIDE_SELL", "SIDE_SELL"],
price=[1005, 1060],
remaining=[2, 2],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)

# #AC 0012-NP-LIPE-005: The estimated liquidation price with sell orders is lower than that for the open volume only.
Expand All @@ -801,14 +818,18 @@ def test_liquidation_price_witin_estimate_position_bounds_AC005(
vega.wait_for_total_catchup()
PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin
_, estimate_liquidation_price_2 = vega.estimate_position(
_, _, estimate_liquidation_price_2 = vega.estimate_position(
market_id,
open_volume=-12,
side=["SIDE_SELL"],
price=[1060],
remaining=[2],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)
assert (
estimate_liquidation_price_2.best_case.open_volume_only
Expand Down Expand Up @@ -851,14 +872,18 @@ def test_liquidation_price_witin_estimate_position_bounds_AC005(

PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin
_, estimate_liquidation_price_3 = vega.estimate_position(
_, _, estimate_liquidation_price_3 = vega.estimate_position(
market_id,
open_volume=-13,
side=["SIDE_SELL"],
price=[1060],
remaining=[1],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)
assert (
estimate_liquidation_price_3.best_case.open_volume_only
Expand Down Expand Up @@ -900,11 +925,16 @@ def test_liquidation_price_witin_estimate_position_bounds_AC005(
vega.wait_for_total_catchup()

PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin

_, estimate_liquidation_price_4 = vega.estimate_position(
_, _, estimate_liquidation_price_4 = vega.estimate_position(
market_id,
open_volume=-14,
collateral_available=PARTY_A_account.general + PARTY_A_account.margin,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)

vega.submit_order(
Expand Down Expand Up @@ -1125,14 +1155,18 @@ def test_estimated_liquidation_price_AC004(vega_service: VegaServiceNull):

PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin
_, estimate_liquidation_price_1 = vega.estimate_position(
_, _, estimate_liquidation_price_1 = vega.estimate_position(
market_id,
open_volume=10,
side=["SIDE_SELL"],
price=[1001, 1002],
remaining=[1, 2],
is_market_order=[False, False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)
# AC 0012-NP-LIPE-004: An estimate is obtained for a long position with multiple limit sell order with the absolute value of the total remaining size of the orders less than the open volume. The estimated liquidation price with sell orders is lower than that for the open volume only.
assert (
Expand All @@ -1157,14 +1191,18 @@ def test_estimated_liquidation_price_AC004(vega_service: VegaServiceNull):
PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin

_, estimate_liquidation_price_2 = vega.estimate_position(
_, _, estimate_liquidation_price_2 = vega.estimate_position(
market_id,
open_volume=9,
side=["SIDE_SELL"],
price=[1002],
remaining=[2],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)
assert (
estimate_liquidation_price_2.best_case.including_sell_orders
Expand Down Expand Up @@ -1204,14 +1242,18 @@ def test_estimated_liquidation_price_AC004(vega_service: VegaServiceNull):

PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin
_, estimate_liquidation_price_3 = vega.estimate_position(
_, _, estimate_liquidation_price_3 = vega.estimate_position(
market_id,
open_volume=7,
side=["SIDE_SELL"],
price=[1002],
remaining=[0],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)
assert (
estimate_liquidation_price_2.best_case.open_volume_only
Expand Down Expand Up @@ -1389,14 +1431,18 @@ def test_estimated_liquidation_price_AC001003(vega_service: VegaServiceNull):
vega.wait_for_total_catchup()
PARTY_A_account = vega.party_account(key_name=PARTY_A.name, market_id=market_id)
collateral = PARTY_A_account.general + PARTY_A_account.margin
_, estimate_liquidation_price_1 = vega.estimate_position(
_, _, estimate_liquidation_price_1 = vega.estimate_position(
market_id,
open_volume=0,
side=["SIDE_BUY"],
price=[970],
remaining=[initial_volume],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)

# AC 0012-NP-LIPE-003: An estimate is obtained for a position with no open volume and a single limit buy order, after the order fills the mark price keeps going down in small increments and the actual liquidation takes place within the obtained estimated range.
Expand All @@ -1413,14 +1459,18 @@ def test_estimated_liquidation_price_AC001003(vega_service: VegaServiceNull):
vega.wait_for_total_catchup()

# 0012-NP-LIPE-001:An estimate is obtained for a long position with no open orders, mark price keeps going down in small increments and the actual liquidation takes place within the estimated range.
_, estimate_liquidation_price_2 = vega.estimate_position(
_, _, estimate_liquidation_price_2 = vega.estimate_position(
market_id,
open_volume=10,
side=["SIDE_BUY"],
price=[],
remaining=[],
is_market_order=[False],
collateral_available=collateral,
average_entry_price=1000,
margin_account_balance=collateral,
general_account_balance=0,
order_margin_account_balance=0,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_CROSS_MARGIN,
)

vega.amend_order(
Expand Down
10 changes: 8 additions & 2 deletions tests/vega_sim/api/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,16 +908,22 @@ def EstimatePosition(self, request, context):
add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
margin, liquidation = estimate_position(
margin, collateral_increase_estimate, liquidation = estimate_position(
data_client=data_client,
market_id=expected_market_id,
open_volume=1,
average_entry_price=100,
margin_account_balance=10000,
general_account_balance=10000,
order_margin_account_balance=10000,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_ISOLATED_MARGIN,
orders=[(vega_protos.vega.SIDE_BUY, "500.00", 1, False)],
collateral_available="100",
margin_factor=0.5,
asset_decimals={"asset": 1},
)

assert margin == expected_margin
assert collateral_increase_estimate == collateral_increase_estimate
assert liquidation == expected_liquidation


Expand Down
21 changes: 19 additions & 2 deletions tests/vega_sim/api/test_data_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,11 @@ def test_estimate_position(trading_data_v2_servicer_and_port):
timestamp=0000000000000000000,
),
)
expected_collateral_increase_estimate = (
data_node_protos_v2.trading_data.CollateralIncreaseEstimate(
worst_case="0", best_case="100"
)
)
expected_liquidation = data_node_protos_v2.trading_data.LiquidationEstimate(
best_case=data_node_protos_v2.trading_data.LiquidationPrice(
open_volume_only="1000",
Expand Down Expand Up @@ -910,6 +915,10 @@ def EstimatePosition(self, request, context):
timestamp=0000000000000000000,
),
),
collateral_increase_estimate=data_node_protos_v2.trading_data.CollateralIncreaseEstimate(
worst_case="0",
best_case="100",
),
liquidation=data_node_protos_v2.trading_data.LiquidationEstimate(
best_case=data_node_protos_v2.trading_data.LiquidationPrice(
open_volume_only="1000",
Expand All @@ -930,10 +939,15 @@ def EstimatePosition(self, request, context):
add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
margin, liquidation = estimate_position(
margin, collateral_increase_estimate, liquidation = estimate_position(
data_client=data_client,
market_id=expected_market_id,
open_volume=1,
average_entry_price=100,
margin_account_balance=10000,
general_account_balance=10000,
order_margin_account_balance=10000,
margin_mode=vega_protos.vega.MarginMode.MARGIN_MODE_ISOLATED_MARGIN,
orders=[
data_node_protos_v2.trading_data.OrderInfo(
side=vega_protos.vega.SIDE_BUY,
Expand All @@ -942,9 +956,12 @@ def EstimatePosition(self, request, context):
is_market_order=True,
)
],
collateral_available="1000",
margin_factor=0.1,
include_collateral_increase_in_available_collateral=False,
scale_liquidation_price_to_market_decimals=False,
)
assert margin == expected_margin
assert collateral_increase_estimate == expected_collateral_increase_estimate
assert liquidation == expected_liquidation


Expand Down

0 comments on commit c8a9e46

Please sign in to comment.