diff --git a/src/lib_spark/spark/api/service_helpers/AdminServiceUtils.cpp b/src/lib_spark/spark/api/service_helpers/AdminServiceUtils.cpp index 3820c8f7b..24ea2c4e7 100644 --- a/src/lib_spark/spark/api/service_helpers/AdminServiceUtils.cpp +++ b/src/lib_spark/spark/api/service_helpers/AdminServiceUtils.cpp @@ -6,6 +6,7 @@ #include "spark/config/protos/jetfix.pb.h" #include "spark/domain/DomainSecurityUtils.h" #include "spark/domain/SecurityCriteria.h" +#include "spark/domain_model/RakuraiVenues.h" #include "spark/domain_model/Security.pb.h" #include "spark/md/SymbolLoader.h" @@ -161,6 +162,15 @@ AdminServiceUtils::resolve_connection( const bts::api::admin::BTSAccount &account, const bts::api::Security &sec) { const bts::api::admin::ExchangeConnection *rv = nullptr; + if (domain::is_rakurai_venue(sec.exchange())) { + for (const auto &name : account.connections().exchange_connection_names()) { + const auto *conn = find_connection(data, name); + if (conn != nullptr && conn->has_general_connection() && + conn->general_connection().exchange() == sec.exchange()) + return conn; + } + return nullptr; + } switch (sec.exchange()) { case api::Venue::VENUE_GLBX: { //CME EXECUTION NEEDS TO BE UPDATED, probably merge with VENUE_BINANCE_USDS_FUTURES @@ -593,6 +603,12 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record( out_result.set_error_text("Could not find connection"); return rv; } + if (domain::is_rakurai_venue(sec.exchange())) { + rv.set_session(conn_ptr->name()); + rv.set_account(account_ptr->name()); + rv.set_operator_id(user_ptr->name()); + return rv; + } switch (sec.exchange()) { case api::Venue::VENUE_CBOE:{//subaccount not added rv.set_session("cboe_1"); diff --git a/src/lib_spark/spark/domain_model/RakuraiVenues.h b/src/lib_spark/spark/domain_model/RakuraiVenues.h new file mode 100644 index 000000000..8a1a0f8c7 --- /dev/null +++ b/src/lib_spark/spark/domain_model/RakuraiVenues.h @@ -0,0 +1,20 @@ +// Generated by tools/generate_venues.py. Do not edit. +#pragma once + +#include "spark/domain_model/Security.pb.h" + +namespace bts::domain { + +// Shared execution metadata must not depend on the Rakurai connector library. +inline bool is_rakurai_venue(api::Venue venue) { + switch (venue) { + case api::VENUE_ORCA: + case api::VENUE_RAYDIUM: + case api::VENUE_PHOENIX: + return true; + default: + return false; + } +} + +} // namespace bts::domain diff --git a/src/lib_spark/spark/domain_model/VENUES.md b/src/lib_spark/spark/domain_model/VENUES.md index 9fe605f1c..43f1c853b 100644 --- a/src/lib_spark/spark/domain_model/VENUES.md +++ b/src/lib_spark/spark/domain_model/VENUES.md @@ -54,6 +54,22 @@ The generator creates the Rakurai provider/handler mapping, so a new supported Rakurai connector should not require another venue-specific C++ switch. The remaining extra work is its runtime symbol configuration. +## Simulated execution for Rakurai venues + +The generator also creates `RakuraiVenues.h` in the shared Spark library. +Every venue with a `rakurai` entry is recognized by order routing, generic +security validation, and fractional spreader quantity handling. + +To use simulated execution, configure a `JetFIX.connection` with the new venue +and assign that connection to the trading account. Routing selects an assigned +general connection whose exchange matches the security, and uses its configured +name as the session (custom names are supported). The usual risk limits and +permissions still apply. Simulation is selected by the existing gateway factory +unless the venue appears in `NotSimExecution`. + +Adding a registry entry does not implement live execution for a new connector. +Live gateway support must be supplied separately before disabling simulation. + ## Native market-data venues A native provider also declares its configuration message in the registry: diff --git a/src/lib_spark/spark/orderman/orderman_utils/simple_validation.cpp b/src/lib_spark/spark/orderman/orderman_utils/simple_validation.cpp index 7645792bf..c67ec924d 100644 --- a/src/lib_spark/spark/orderman/orderman_utils/simple_validation.cpp +++ b/src/lib_spark/spark/orderman/orderman_utils/simple_validation.cpp @@ -1,5 +1,6 @@ #include "simple_validation.h" #include "spark/domain_model/Security.h" +#include "spark/domain_model/RakuraiVenues.h" #include "spark/domain_model/Security.pb.h" #include "spark/domain_model/Venue.h" #include "spark/orderman/orderman_utils/ExecutionReport.h" @@ -51,6 +52,9 @@ bool validate( } break; default: + if (domain::is_rakurai_venue(venue) && sec->exchange() == venue && + sec->has_generic()) + return true; utils::ErrorList::append(error, fmt::format("Invalid venue {}", venue)); return false; } diff --git a/src/lib_spark/spark/orderman/orders/EMNativeOrder.cpp b/src/lib_spark/spark/orderman/orders/EMNativeOrder.cpp index 89ff2a6ea..def454c07 100644 --- a/src/lib_spark/spark/orderman/orders/EMNativeOrder.cpp +++ b/src/lib_spark/spark/orderman/orders/EMNativeOrder.cpp @@ -30,9 +30,7 @@ #include "spark/utils/bts_assert.h" #include "spark/utils/bts_chrono.h" -#include #include -#include #include #include #include @@ -509,49 +507,6 @@ OrderOpResult EMNativeOrder::_on_activate( me->state.set_avg_price(0); me->session_services = &me->services->get_session_services( get_native_state(me->state).routing_record().session()); - // TODO: just to get going - std::string session_name; - switch (me->exchange) { - case api::VENUE_CBOE: - session_name = "cboe_1"; - break; - case api::VENUE_TAIFEX: - session_name="taifex_1"; - break; - case api::VENUE_OKX: - session_name="okx_1"; - break; - case api::VENUE_RAYDIUM: - session_name="raydium_1"; - break; - case api::VENUE_ORCA: - session_name="orca_1"; - break; - case api::VENUE_BINANCE_USDS_FUTURES: - session_name = "binance_usds_futures_1"; - break; - case api::VENUE_BINANCE: - session_name = "binance_1"; - break; - case api::VENUE_BYBIT: - session_name = "bybit_1"; - break; - case api::VENUE_GATEIO: - session_name = "gateio_1"; - break; - default: { - session_name = api::Venue_Name(me->exchange).substr(6); - std::transform( - session_name.begin(), session_name.end(), session_name.begin(), - [](char value) { - return static_cast( - std::tolower(static_cast(value))); - }); - session_name += "_1"; - break; - } - } - me->session_services = &me->services->get_session_services(session_name); // Checks for the following: // 1. Gateway connection diff --git a/src/lib_spark/spark/orderman/orders/spreader_orders/spreader/SpreaderFractionalQtyHelper.cpp b/src/lib_spark/spark/orderman/orders/spreader_orders/spreader/SpreaderFractionalQtyHelper.cpp index 2d99121b9..2ef957420 100644 --- a/src/lib_spark/spark/orderman/orders/spreader_orders/spreader/SpreaderFractionalQtyHelper.cpp +++ b/src/lib_spark/spark/orderman/orders/spreader_orders/spreader/SpreaderFractionalQtyHelper.cpp @@ -2,6 +2,7 @@ #include "spark/config/config.h" #include "spark/config/protos/spreader.pb.h" #include "spark/domain_model/Security.pb.h" +#include "spark/domain_model/RakuraiVenues.h" #include "spark/domain_model/SecurityKey.h" #include "spark/domain_model/common.h" #include "spark/logging/logging.h" @@ -28,7 +29,7 @@ bool is_crypto_leg(const LegPropertyData &l) { v == api::VENUE_BINANCE_COIN_FUTURES || v == api::Venue::VENUE_BINANCE_US || v == api::VENUE_DERIBIT || v == api::Venue::VENUE_DERIBIT || v == api::Venue::VENUE_OKX || - v == api::Venue::VENUE_ORCA || v == api::Venue::VENUE_RAYDIUM; + domain::is_rakurai_venue(v); } bool has_crypto_leg(const std::vector &legs) { diff --git a/src/lib_spark/spark/orderman/tests/rakurai_sim_routing_tests.cpp b/src/lib_spark/spark/orderman/tests/rakurai_sim_routing_tests.cpp new file mode 100644 index 000000000..04504624a --- /dev/null +++ b/src/lib_spark/spark/orderman/tests/rakurai_sim_routing_tests.cpp @@ -0,0 +1,130 @@ +#include "spark/api/service_helpers/AdminServiceUtils.h" +#include "spark/config/config.h" +#include "spark/domain_model/RakuraiVenues.h" +#include "spark/orderman/gateways/sim/SimExecutionGateway.h" +#include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h" +#include "spark/orderman/orderman_utils/ExecutionReport.h" +#include "spark/orderman/orderman_utils/simple_validation.h" + +#include + +namespace bts::orderman { + +TEST_CASE( + "Rakurai routing uses the account's matching venue session", + "[rakurai_sim]") { + const auto venue = + GENERATE(api::VENUE_ORCA, api::VENUE_RAYDIUM, api::VENUE_PHOENIX); + api::admin::AdminData data; + auto *account = data.add_accounts(); + account->set_id(1); + account->set_is_present(true); + account->set_name("ACC A"); + auto *user = data.add_users(); + user->set_id(1); + user->set_is_present(true); + user->set_name("bts"); + + // An unrelated general connection must never satisfy a Rakurai route. + auto *other = data.add_connections(); + other->set_name("okx_1"); + other->mutable_general_connection()->set_exchange(api::VENUE_OKX); + account->mutable_connections()->add_exchange_connection_names("okx_1"); + api::Security sec; + sec.set_exchange(venue); + api::admin::AdminServiceOperationResult result; + admin::AdminServiceUtils::get_routing_record(data, 1, 1, sec, result); + REQUIRE_FALSE(result.is_success()); + REQUIRE(result.error_text() == "Could not find connection"); + + auto *connection = data.add_connections(); + connection->set_name("custom_hedging_session"); + connection->mutable_general_connection()->set_exchange(venue); + // A configured session still requires account assignment. + admin::AdminServiceUtils::get_routing_record(data, 1, 1, sec, result); + REQUIRE_FALSE(result.is_success()); + account->mutable_connections()->add_exchange_connection_names( + connection->name()); + + const auto route = + admin::AdminServiceUtils::get_routing_record(data, 1, 1, sec, result); + REQUIRE(result.is_success()); + REQUIRE(route.session() == "custom_hedging_session"); + REQUIRE(route.account() == "ACC A"); + REQUIRE(route.operator_id() == "bts"); +} + +TEST_CASE( + "Phoenix generic securities pass execution report validation", + "[rakurai_sim]") { + api::Security sec; + sec.set_exchange(api::VENUE_PHOENIX); + sec.mutable_generic(); + std::string error; + REQUIRE(validate(&sec, api::VENUE_PHOENIX, error)); + REQUIRE(error.empty()); + + SECTION("the venue must match") { sec.set_exchange(api::VENUE_OKX); } + SECTION("generic metadata is required") { sec.clear_generic(); } + REQUIRE_FALSE(validate(&sec, api::VENUE_PHOENIX, error)); +} + +TEST_CASE( + "Phoenix sim fills fractional buy and sell limit orders", "[rakurai_sim]") { + const auto side = GENERATE(api::SIDE_BUY, api::SIDE_SELL); + api::Security sec; + sec.set_id(46); + sec.set_exchange(api::VENUE_PHOENIX); + sec.mutable_generic()->set_security_desc("SOL-USD-1"); + api::orderman::RoutingRecord route; + route.set_session("phoenix_1"); + route.set_account("ACC A"); + route.set_operator_id("bts"); + + md::GeneralSecurityIndex producers; + SimExecutionGateway gateway(api::VENUE_PHOENIX); + gateway.set_producers(&producers); + std::vector> reports; + gateway.on_exe_report() = [&](auto report, const auto &) { + reports.push_back(std::move(report)); + }; + CreateOrderMsg order{}; + order.key = OrderKey(66006); + order.security = &sec; + order.side = side; + order.price = Price(side == api::SIDE_BUY ? 101 : 99); + order.quantity = Quantity::create_new(0.25); + order.order_type = api::orderman::NATIVE_ORDER_TYPE_LIMIT; + order.time_in_force = api::orderman::TIME_IN_FORCE_GTC; + order.routing_record = &route; + gateway.send_msgs({order}, {}); + std::array bids{}, asks{}; + bids[0] = md::PriceSize_from_double(99, 1); + asks[0] = md::PriceSize_from_double(101, 1); + REQUIRE(gateway.get_matched_book(SecurityKey(46), bids, asks)); + gateway.dispatch_incoming_fix_messages(); + + REQUIRE(reports.size() == 2); + REQUIRE(reports.front()->exec_type == api::orderman::EXEC_TYPE_NEW); + const auto &fill = *reports.back(); + REQUIRE(fill.exec_type == api::orderman::EXEC_TYPE_FILL); + REQUIRE(fill.order_key == OrderKey(66006)); + REQUIRE(fill.exchange == api::VENUE_PHOENIX); + REQUIRE(fill.side == side); + REQUIRE(fill.filled_quantity.full_value() == 0.25); + std::string error; + REQUIRE(validate(fill.security, error)); +} + +TEST_CASE("Rakurai simulation obeys NotSimExecution", "[rakurai_sim]") { + config::internal::initialize_for_test(""); + REQUIRE(domain::is_rakurai_venue(api::VENUE_PHOENIX)); + REQUIRE(SimExecutionGatewayFactory::is_sim_execution_on(api::VENUE_PHOENIX)); + config::internal::initialize_for_test( + "NotSimExecution { value { entries { venue: VENUE_PHOENIX } } }"); + REQUIRE_FALSE( + SimExecutionGatewayFactory::is_sim_execution_on(api::VENUE_PHOENIX)); + config::internal::initialize_for_test(""); +} + +} // namespace bts::orderman diff --git a/tools/generate_venues.py b/tools/generate_venues.py index 6d9158ce8..847acd959 100755 --- a/tools/generate_venues.py +++ b/tools/generate_venues.py @@ -17,6 +17,7 @@ DOMAIN_PROTO = ROOT / "src/lib_spark/spark/api/domain/Venue.proto" MARKET_DATA_CONFIG = ROOT / "src/lib_spark/spark/api/md/MarketDataConfig.proto" ADMIN_TYPES = ROOT / "src/Spark.Admin.WebApp/src/Accounts/Types.fs" RAKURAI_REGISTRY = ROOT / "src/app_sparkd/sparkd/rakurai/RakuraiVenueRegistry.h" +RAKURAI_VENUES = ROOT / "src/lib_spark/spark/domain_model/RakuraiVenues.h" BEGIN = "// BEGIN GENERATED VENUE ENUM" END = "// END GENERATED VENUE ENUM" ADMIN_BEGIN = "// BEGIN GENERATED ADMIN VENUES" @@ -349,6 +350,32 @@ inline std::optional make_connector_dex_config( ''' +def rakurai_venues(venues: list[dict[str, object]]) -> str: + cases = "\n".join( + f" case api::VENUE_{venue['name']}:" + for venue in venues if venue.get("rakurai") + ) + return f'''// Generated by tools/generate_venues.py. Do not edit. +#pragma once + +#include "spark/domain_model/Security.pb.h" + +namespace bts::domain {{ + +// Shared execution metadata must not depend on the Rakurai connector library. +inline bool is_rakurai_venue(api::Venue venue) {{ + switch (venue) {{ +{cases} + return true; + default: + return false; + }} +}} + +}} // namespace bts::domain +''' + + def write_generated_file(path: Path, content: str, check: bool) -> bool: if path.exists() and path.read_text(encoding="utf-8") == content: return True @@ -417,6 +444,9 @@ def main() -> int: ok = write_generated_file( RAKURAI_REGISTRY, rakurai_registry(venues), args.check ) and ok + ok = write_generated_file( + RAKURAI_VENUES, rakurai_venues(venues), args.check + ) and ok return 0 if ok else 1 except (OSError, ValueError, json.JSONDecodeError) as error: print(f"venue generation failed: {error}", file=sys.stderr)