Order Execution Server

From Spire Trading Inc.
Revision as of 17:25, 1 December 2025 by Kamal (talk | contribs) (Created page with "= Order Execution Server = The Order Execution Server receives order submissions from clients, routes them to appropriate execution venues or brokers, and tracks the lifecycle...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Order Execution Server

The Order Execution Server receives order submissions from clients, routes them to appropriate execution venues or brokers, and tracks the lifecycle of each order from submission through completion. It publishes execution reports to subscribed clients, enforces compliance rules and risk parameters, and maintains a persistent record of all order activity. By centralizing order routing and monitoring, the server ensures consistent application of trading controls while providing real-time visibility into order status across the system.

The Order Execution Server integrates with the Service Locator for authentication, the Compliance Server for rule validation, the Risk Server for position and loss monitoring, and a MySQL database to persist order records and execution reports.

Configuration

The Order Execution Server is configured via a YAML file that defines network interfaces, database connection, and Service Locator integration. Below is the structure of the configuration file with example values:

---
server:
  # Primary network interface and port the Order Execution Server binds to.
  interface: "0.0.0.0:20700"
  
  # List of addresses the server is reachable at (for registration with Service Locator).
  # Typically includes both public-facing and local addresses.
  addresses: ["198.51.100.5:20700", "10.0.0.5:20700"]

data_store:
  # The address of the MySQL server (host:port).
  address: "127.0.0.1:3306"
  
  # The username for authenticating with MySQL.
  username: spireadmin
  
  # The password for the MySQL user.
  password: 1234
  
  # The name of the database schema where data is stored.
  schema: spire

service_locator:
  # The address of the Service Locator (host:port).
  address: "10.0.0.5:20000"
  
  # The account username used by the Order Execution Server to authenticate with the Service Locator.
  username: order_execution_service
  
  # The password for the Order Execution Server's Service Locator account.
  password: admin_password
...

Functionality

Order Routing

The server routes orders to their specified destinations based on:

  • Destination configuration (venue, broker, or internal order type like MOE)
  • Account permissions and entitlements
  • Compliance rule validation
  • Risk parameter enforcement

Orders that violate compliance rules or risk limits are rejected before routing.

Order Monitoring

Clients can subscribe to receive execution reports for:

  • Orders submitted by a specific account
  • Individual orders by order ID

Subscriptions enable real-time tracking of order activity across portfolios or trading desks.

Compliance and Risk Enforcement

Before accepting an order, the server validates:

  • Compliance rules defined for the account or group
  • Risk parameters including buying power and loss limits
  • Account state (active, closed orders, disabled)
  • Trading permissions and entitlements

Orders that fail validation are immediately rejected with an appropriate error message.

Order Cancellation

Clients can request order cancellation. The server:

  1. Validates cancel permission
  2. Routes cancel request to the destination
  3. Updates order state based on destination response
  4. Publishes final execution report

Utility Scripts

cancel_order.py

A utility for bulk order cancellation supporting multiple use cases:

Cancel by order ID:

python cancel_order.py
  --config config.yml
  --order 12345
  --message "Manual cancellation"

Cancel by account and region:

python cancel_order.py
  --config config.yml
  --account trader1
  --region CA                      # Country code, venue, or security
  --begin "2024-01-01"
  --end "2024-12-31"
  --message "End of day cancel"

Cancel by region (all accounts):

python cancel_order.py
  --config config.yml
  --region XTSE                    # Venue code
  --begin "2024-01-01"
  --end "2024-12-31"
  --connections 8                  # Parallel connections for performance
  --message "Market close"

The script supports parallel processing using multiple service connections to efficiently cancel large numbers of orders.

Installation & Setup

A setup.py script is provided to generate the final config.yml from the config.default.yml template.

The script supports the following arguments:

python setup.py
  --local 0.0.0.0           # Local interface (default: auto-detected IP)
  --world 198.51.100.5      # Global/public interface (optional)
  --address 10.0.0.5:20000  # Service Locator address (default: local_interface:20000)
  --password [REQUIRED]     # Service password for authentication
  --mysql_address 127.0.0.1:3306  # MySQL server address
  --mysql_username spireadmin     # MySQL username
  --mysql_password secretpw       # MySQL password (default: --password if omitted)
  --mysql_schema spire            # MySQL schema

Operations

The Order Execution Server is controlled using three operational scripts: start.sh, stop.sh, and check.sh.

Log files are generated in the format:

srv_YYYYMMDD_HH_MM_SS.log

Upon startup, older log files are moved into the logs/ directory.

check.sh

The check.sh script verifies whether the server is currently running by inspecting the PID recorded in pid.lock and testing whether the associated process exists.

If the server is not running, it prints:

SimulationOrderExecutionServer is not running.

start.sh

The start.sh script:

  • Exits immediately if the server is already running
  • Creates a logs/ directory if necessary
  • Moves any existing srv_*.log files into logs/
  • Starts the SimulationOrderExecutionServer process in the background
  • Writes the PID to pid.lock
  • Reads network interfaces from config.yml and waits until the server is listening on at least one configured address

This ensures the server is fully initialized before the script exits.

stop.sh

The stop.sh script:

  • Reads the PID from pid.lock
  • Sends SIGINT to request a graceful shutdown
  • Waits for termination using exponential backoff (up to 300 seconds)
  • Sends SIGKILL if the server fails to stop cleanly
  • Appends a forced-termination message to the most recent log file (if applicable)
  • Removes the pid.lock file

This guarantees consistent shutdown behavior across normal and exceptional conditions.