Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate stop orders into market-sim #521

Merged
merged 17 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VEGA_SIM_VEGA_TAG=29a40af72e2cb96330e488efd6efc3c4c14879fb
VEGA_SIM_VEGA_TAG=347011f9a22f34ee9cbcf5b9def9f6b68dc33c8b
VEGA_SIM_CONSOLE_TAG=develop
VEGA_DEFAULT_KEY_NAME='Key 1'
VEGA_SIM_NETWORKS_INTERNAL_TAG=main
Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pipeline {
disableConcurrentBuilds(abortPrevious: true)
}
parameters {
string( name: 'VEGA_VERSION', defaultValue: '29a40af72e2cb96330e488efd6efc3c4c14879fb',
string( name: 'VEGA_VERSION', defaultValue: '347011f9a22f34ee9cbcf5b9def9f6b68dc33c8b',
description: 'Git branch, tag or hash of the vegaprotocol/vega repository')
string( name: 'VEGACAPSULE_VERSION', defaultValue: 'main',
description: 'Git branch, tag or hash of the vegaprotocol/vegacapsule repository')
Expand Down
70 changes: 70 additions & 0 deletions tests/integration/test_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,3 +1738,73 @@ def test_teams(vega_service_with_market: VegaServiceNull):
]
assert team_a_id in team_referee_history
assert team_b_id in team_referee_history


@pytest.mark.integration
def test_stop_order(vega_service_with_market: VegaServiceNull):
vega = vega_service_with_market
# Party A places the stop order
create_and_faucet_wallet(vega=vega, wallet=PARTY_A, amount=1e9)
vega.wait_for_total_catchup()
# Parties B and C move the mark price, triggering the stop
create_and_faucet_wallet(vega=vega, wallet=PARTY_B, amount=1e9)
vega.wait_for_total_catchup()
create_and_faucet_wallet(vega=vega, wallet=PARTY_C, amount=1e9)
vega.wait_for_total_catchup()
market_id = vega.all_markets()[0].id

# Party A opens a position
vega.submit_order(
trading_key=PARTY_A.name,
market_id=market_id,
order_type="TYPE_LIMIT",
time_in_force="TIME_IN_FORCE_GTC",
side="SIDE_BUY",
price=0.30,
volume=1,
)
vega.submit_order(
trading_key=PARTY_B.name,
market_id=market_id,
order_type="TYPE_LIMIT",
time_in_force="TIME_IN_FORCE_GTC",
side="SIDE_SELL",
price=0.30,
volume=1,
)
vega.wait_fn(1)
vega.wait_for_total_catchup()

# Build a stop order and submit it
order_submission = vega.build_order_submission(
market_id=market_id,
size=1,
side=vega_protos.vega.SIDE_SELL,
order_type=vega_protos.vega.Order.TYPE_LIMIT,
price=5.0,
time_in_force=vega_protos.vega.Order.TIME_IN_FORCE_IOC,
reduce_only=True,
)
stop_order_setup = vega.build_stop_order_setup(
market_id=market_id,
order_submission=order_submission,
price=0.4,
)
stop_orders_submission = vega.build_stop_orders_submission(
rises_above=stop_order_setup,
falls_below=None,
)
vega.submit_stop_order(
stop_orders_submission=stop_orders_submission, key_name=PARTY_A.name
)
vega.wait_fn(1)
vega.wait_for_total_catchup()

assert (
len(
vega.list_stop_orders(
statuses=[vega_protos.vega.StopOrder.STATUS_PENDING],
)
)
> 0
)
149 changes: 146 additions & 3 deletions tests/vega_sim/api/test_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest.mock import MagicMock, patch

import pytest
import datetime
import vega_sim.proto.data_node.api.v2 as data_node_protos_v2
import vega_sim.proto.vega as vega_protos
import vega_sim.proto.vega.events.v1.events_pb2 as events_protos
Expand All @@ -27,10 +28,16 @@
ReferralSetStats,
PartyAmount,
ReferrerRewardsGenerated,
MakerFeesGenerated,
FeesStats,
Team,
TeamReferee,
TeamRefereeHistory,
StopOrderEvent,
OrderSubmission,
PeggedOrder,
IcebergOpts,
StopOrder,
get_asset_decimals,
find_asset_id,
get_trades,
Expand All @@ -49,6 +56,7 @@
list_teams,
list_team_referees,
list_team_referee_history,
list_stop_orders,
)
from vega_sim.grpc.client import (
VegaTradingDataClientV2,
Expand Down Expand Up @@ -1057,7 +1065,7 @@ def GetFeesStats(self, request, context):
market=request.market_id,
asset=request.asset_id,
epoch_seq=1,
total_rewards_paid=[
total_rewards_received=[
vega_protos.events.v1.events.PartyAmount(
party="referrer1", amount="1000"
)
Expand All @@ -1082,6 +1090,21 @@ def GetFeesStats(self, request, context):
party="referrer1", amount="1000"
)
],
total_maker_fees_received=[
vega_protos.events.v1.events.PartyAmount(
party="referrer1", amount="1000"
)
],
maker_fees_generated=[
vega_protos.events.v1.events.MakerFeesGenerated(
taker="taker1",
maker_fees_paid=[
vega_protos.events.v1.events.PartyAmount(
party="referrer1", amount="1000"
)
],
)
],
)
)

Expand All @@ -1099,7 +1122,7 @@ def GetFeesStats(self, request, context):
market="market_id",
asset="",
epoch_seq=1,
total_rewards_paid=[PartyAmount(party="referrer1", amount=100.0)],
total_rewards_received=[PartyAmount(party="referrer1", amount=100.0)],
referrer_rewards_generated=[
ReferrerRewardsGenerated(
referrer="referrer1",
Expand All @@ -1108,6 +1131,13 @@ def GetFeesStats(self, request, context):
],
referees_discount_applied=[PartyAmount(party="referrer1", amount=100.0)],
volume_discount_applied=[PartyAmount(party="referrer1", amount=100.0)],
total_maker_fees_received=[PartyAmount(party="referrer1", amount=100.0)],
maker_fees_generated=[
MakerFeesGenerated(
taker="taker1",
maker_fees_paid=[PartyAmount(party="referrer1", amount=100.0)],
)
],
)
assert get_fees_stats(
data_client=data_client,
Expand All @@ -1117,7 +1147,7 @@ def GetFeesStats(self, request, context):
market="",
asset="asset",
epoch_seq=1,
total_rewards_paid=[PartyAmount(party="referrer1", amount=100.0)],
total_rewards_received=[PartyAmount(party="referrer1", amount=100.0)],
referrer_rewards_generated=[
ReferrerRewardsGenerated(
referrer="referrer1",
Expand All @@ -1126,6 +1156,13 @@ def GetFeesStats(self, request, context):
],
referees_discount_applied=[PartyAmount(party="referrer1", amount=100.0)],
volume_discount_applied=[PartyAmount(party="referrer1", amount=100.0)],
total_maker_fees_received=[PartyAmount(party="referrer1", amount=100.0)],
maker_fees_generated=[
MakerFeesGenerated(
taker="taker1",
maker_fees_paid=[PartyAmount(party="referrer1", amount=100.0)],
)
],
)


Expand Down Expand Up @@ -1253,3 +1290,109 @@ def ListTeamRefereeHistory(self, request, context):
joined_at_epoch=1,
)
]


def test_list_stop_orders(trading_data_v2_servicer_and_port):
def ListStopOrders(self, request, context):
return data_node_protos_v2.trading_data.ListStopOrdersResponse(
orders=data_node_protos_v2.trading_data.StopOrderConnection(
page_info=data_node_protos_v2.trading_data.PageInfo(
has_next_page=False,
has_previous_page=False,
start_cursor="",
end_cursor="",
),
edges=[
data_node_protos_v2.trading_data.StopOrderEdge(
cursor="cursor",
node=vega_protos.events.v1.events.StopOrderEvent(
submission=vega_protos.commands.v1.commands.OrderSubmission(
market_id="market_id",
price="30",
size=10000,
side=vega_protos.vega.SIDE_BUY,
time_in_force=vega_protos.vega.Order.TIME_IN_FORCE_IOC,
type=vega_protos.vega.Order.TYPE_LIMIT,
reference="reference",
pegged_order=vega_protos.vega.PeggedOrder(
reference=vega_protos.vega.PEGGED_REFERENCE_MID,
offset="1",
),
post_only=True,
reduce_only=True,
iceberg_opts=vega_protos.commands.v1.commands.IcebergOpts(
peak_size=1000,
minimum_visible_size=100,
),
),
stop_order=vega_protos.vega.StopOrder(
id="id",
oco_link_id="oco_link_id",
expires_at=1672531200000000000,
expiry_strategy=vega_protos.vega.StopOrder.EXPIRY_STRATEGY_CANCELS,
trigger_direction=vega_protos.vega.StopOrder.TRIGGER_DIRECTION_RISES_ABOVE,
status=vega_protos.vega.StopOrder.STATUS_PENDING,
created_at=1672531200000000000,
updated_at=1672531200000000000,
order_id="order_id",
party_id="party_id",
market_id="market_id",
rejection_reason=vega_protos.vega.StopOrder.REJECTION_REASON_TRADING_NOT_ALLOWED,
price="1000",
trailing_percent_offset=None,
),
),
),
],
)
)

server, port, mock_servicer = trading_data_v2_servicer_and_port
mock_servicer.ListStopOrders = ListStopOrders

add_TradingDataServiceServicer_v2_to_server(mock_servicer(), server)

data_client = VegaTradingDataClientV2(f"localhost:{port}")
stop_orders = list_stop_orders(
data_client=data_client,
market_price_decimals_map={"market_id": 0},
market_position_decimals_map={"market_id": 2},
)
assert stop_orders == [
StopOrderEvent(
submission=OrderSubmission(
market_id="market_id",
price=30.0,
size=100.0,
side=vega_protos.vega.SIDE_BUY,
time_in_force=vega_protos.vega.Order.TIME_IN_FORCE_IOC,
type=vega_protos.vega.Order.TYPE_LIMIT,
reference="reference",
pegged_order=PeggedOrder(
reference=vega_protos.vega.PEGGED_REFERENCE_MID,
offset=1.0,
),
post_only=True,
reduce_only=True,
iceberg_opts=IcebergOpts(
peak_size=10.0,
minimum_visible_size=1.0,
),
),
stop_order=StopOrder(
id="id",
oco_link_id="oco_link_id",
expires_at=datetime.datetime(2023, 1, 1, 0, 0),
expiry_strategy=vega_protos.vega.StopOrder.EXPIRY_STRATEGY_CANCELS,
trigger_direction=vega_protos.vega.StopOrder.TRIGGER_DIRECTION_RISES_ABOVE,
status=vega_protos.vega.StopOrder.STATUS_PENDING,
created_at=datetime.datetime(2023, 1, 1, 0, 0),
updated_at=datetime.datetime(2023, 1, 1, 0, 0),
order_id="order_id",
party_id="party_id",
market_id="market_id",
rejection_reason=vega_protos.vega.StopOrder.REJECTION_REASON_TRADING_NOT_ALLOWED,
price=1000.0,
),
)
]
Loading