<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.spiretrading.com/index.php?action=history&amp;feed=atom&amp;title=Risk_Server</id>
	<title>Risk Server - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.spiretrading.com/index.php?action=history&amp;feed=atom&amp;title=Risk_Server"/>
	<link rel="alternate" type="text/html" href="https://wiki.spiretrading.com/index.php?title=Risk_Server&amp;action=history"/>
	<updated>2026-04-07T13:01:08Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.1</generator>
	<entry>
		<id>https://wiki.spiretrading.com/index.php?title=Risk_Server&amp;diff=241&amp;oldid=prev</id>
		<title>Kamal: Created page with &quot;= Risk Server = The Risk Server monitors account profit and loss, enforces risk limits, and manages account state transitions based on trading activity. It tracks each account...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.spiretrading.com/index.php?title=Risk_Server&amp;diff=241&amp;oldid=prev"/>
		<updated>2025-12-01T22:18:57Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Risk Server = The Risk Server monitors account profit and loss, enforces risk limits, and manages account state transitions based on trading activity. It tracks each account...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Risk Server =&lt;br /&gt;
The Risk Server monitors account profit and loss, enforces risk limits, and manages account state transitions based on trading activity. It tracks each account's net position, buying power consumption, and realized/unrealized profit and loss. When an account exceeds its configured loss threshold, the server automatically transitions the account through risk states—from active trading to closed orders mode, and ultimately to disabled or liquidation if losses continue. By centralizing risk monitoring and enforcement, the server protects both individual accounts and the overall system from excessive exposure.&lt;br /&gt;
&lt;br /&gt;
The Risk Server integrates with the Service Locator for authentication, the Order Execution service to monitor trades, and a MySQL database to persist risk parameters and inventory snapshots.&lt;br /&gt;
&lt;br /&gt;
== Configuration ==&lt;br /&gt;
The Risk 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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
---&lt;br /&gt;
server:&lt;br /&gt;
  # Primary network interface and port the Risk Server binds to.&lt;br /&gt;
  interface: &amp;quot;0.0.0.0:20600&amp;quot;&lt;br /&gt;
  &lt;br /&gt;
  # List of addresses the server is reachable at (for registration with Service Locator).&lt;br /&gt;
  # Typically includes both public-facing and local addresses.&lt;br /&gt;
  addresses: [&amp;quot;198.51.100.5:20600&amp;quot;, &amp;quot;10.0.0.5:20600&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
data_store:&lt;br /&gt;
  # The address of the MySQL server (host:port).&lt;br /&gt;
  address: &amp;quot;127.0.0.1:3306&amp;quot;&lt;br /&gt;
  &lt;br /&gt;
  # The username for authenticating with MySQL.&lt;br /&gt;
  username: spireadmin&lt;br /&gt;
  &lt;br /&gt;
  # The password for the MySQL user.&lt;br /&gt;
  password: 1234&lt;br /&gt;
  &lt;br /&gt;
  # The name of the database schema where data is stored.&lt;br /&gt;
  schema: spire&lt;br /&gt;
&lt;br /&gt;
service_locator:&lt;br /&gt;
  # The address of the Service Locator (host:port).&lt;br /&gt;
  address: &amp;quot;10.0.0.5:20000&amp;quot;&lt;br /&gt;
  &lt;br /&gt;
  # The account username used by the Risk Server to authenticate with the Service Locator.&lt;br /&gt;
  username: risk_service&lt;br /&gt;
  &lt;br /&gt;
  # The password for the Risk Server's Service Locator account.&lt;br /&gt;
  password: admin_password&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Risk Parameters ==&lt;br /&gt;
Each account is assigned risk parameters that govern its trading limits and risk enforcement behavior:&lt;br /&gt;
&lt;br /&gt;
* '''Currency''' - The currency used for risk calculations and loss limits&lt;br /&gt;
* '''Buying Power''' - Maximum amount of capital available for trading&lt;br /&gt;
* '''Allowed State''' - The risk state the account is permitted to operate in&lt;br /&gt;
* '''Net Loss Threshold''' - Maximum net loss before transitioning to closed orders mode&lt;br /&gt;
* '''Transition Time''' - Duration allowed in closed orders mode before automatic liquidation&lt;br /&gt;
&lt;br /&gt;
=== Risk States ===&lt;br /&gt;
Accounts progress through the following risk states based on their profit and loss:&lt;br /&gt;
&lt;br /&gt;
* '''Active''' - Normal trading with full permissions&lt;br /&gt;
* '''Closed Orders''' - No new orders allowed; existing positions may be reduced&lt;br /&gt;
* '''Disabled''' - Trading prohibited; positions must be manually managed&lt;br /&gt;
* '''Liquidation''' - Account positions are automatically liquidated&lt;br /&gt;
&lt;br /&gt;
== Functionality ==&lt;br /&gt;
&lt;br /&gt;
=== Real-Time Risk Monitoring ===&lt;br /&gt;
The Risk Server continuously tracks:&lt;br /&gt;
&lt;br /&gt;
* Net position by security and currency&lt;br /&gt;
* Realized and unrealized profit and loss&lt;br /&gt;
* Buying power consumption&lt;br /&gt;
* Order and execution activity&lt;br /&gt;
&lt;br /&gt;
When an account's net loss exceeds its configured threshold, the server automatically transitions the account to closed orders mode and starts a timer. If the account does not return to profitability within the transition time, the server escalates to disabled or liquidation state.&lt;br /&gt;
&lt;br /&gt;
=== Inventory Management ===&lt;br /&gt;
The server maintains real-time inventory snapshots for each account, tracking:&lt;br /&gt;
&lt;br /&gt;
* Open positions by security&lt;br /&gt;
* Average cost basis&lt;br /&gt;
* Current market value&lt;br /&gt;
* Unrealized profit and loss&lt;br /&gt;
&lt;br /&gt;
Inventory updates occur on every execution report, ensuring accurate position tracking across all trading venues.&lt;br /&gt;
&lt;br /&gt;
=== Regional Risk Control ===&lt;br /&gt;
Risk parameters and state transitions can be scoped by region (country, venue, or security). This enables fine-grained control over risk exposure in specific markets or asset classes.&lt;br /&gt;
&lt;br /&gt;
== Utility Scripts ==&lt;br /&gt;
&lt;br /&gt;
=== positions.py ===&lt;br /&gt;
Reports current positions for accounts in CSV format:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python positions.py&lt;br /&gt;
  --config config.yml       # Configuration file&lt;br /&gt;
  --account trader1         # Specific account (optional; reports all if omitted)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Output format:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Account,Security,Currency,Side,Open Quantity,Cost Basis&lt;br /&gt;
trader1,AAPL.NASDAQ,USD,Long,100,15000.00&lt;br /&gt;
trader1,TSLA.NASDAQ,USD,Short,50,12500.00&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== moe.py ===&lt;br /&gt;
Manual Order Entry (MOE) utility for opening or closing positions without market execution. These orders are synthetic and do not interact with external venues:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python moe.py&lt;br /&gt;
  --config config.yml           # Configuration file&lt;br /&gt;
  --positions positions.csv     # CSV file with positions to MOE&lt;br /&gt;
  --account trader1             # Specific account (optional)&lt;br /&gt;
  --region CA                   # Region filter (country/venue/security)&lt;br /&gt;
  --open                        # Open positions&lt;br /&gt;
  --close                       # Close positions&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The positions CSV file must have the format:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Account,Security,Currency,Side,Open Quantity,Cost Basis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use cases:&lt;br /&gt;
* Opening initial positions for testing&lt;br /&gt;
* Closing positions during liquidation&lt;br /&gt;
* Adjusting positions after system recovery&lt;br /&gt;
&lt;br /&gt;
=== reset.py ===&lt;br /&gt;
Resets risk state for a specific region, clearing accumulated profit/loss tracking:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python reset.py&lt;br /&gt;
  --config config.yml       # Configuration file&lt;br /&gt;
  --region CA               # Region to reset (country code, venue, or security)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This utility is typically used at the start of a trading day or after maintenance periods to reset risk calculations.&lt;br /&gt;
&lt;br /&gt;
== Installation &amp;amp; Setup ==&lt;br /&gt;
A &amp;lt;code&amp;gt;setup.py&amp;lt;/code&amp;gt; script is provided to generate the final &amp;lt;code&amp;gt;config.yml&amp;lt;/code&amp;gt; from the &amp;lt;code&amp;gt;config.default.yml&amp;lt;/code&amp;gt; template.&lt;br /&gt;
&lt;br /&gt;
The script supports the following arguments:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
python setup.py&lt;br /&gt;
  --local 0.0.0.0           # Local interface (default: auto-detected IP)&lt;br /&gt;
  --world 198.51.100.5      # Global/public interface (optional)&lt;br /&gt;
  --address 10.0.0.5:20000  # Service Locator address (default: local_interface:20000)&lt;br /&gt;
  --password [REQUIRED]     # Service password for authentication&lt;br /&gt;
  --mysql_address 127.0.0.1:3306  # MySQL server address&lt;br /&gt;
  --mysql_username spireadmin     # MySQL username&lt;br /&gt;
  --mysql_password secretpw       # MySQL password (default: --password if omitted)&lt;br /&gt;
  --mysql_schema spire            # MySQL schema&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Operations ==&lt;br /&gt;
The Risk Server is controlled using three operational scripts: &amp;lt;code&amp;gt;start.sh&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;stop.sh&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;check.sh&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Log files are generated in the format:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
srv_YYYYMMDD_HH_MM_SS.log&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Upon startup, older log files are moved into the &amp;lt;code&amp;gt;logs/&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
=== check.sh ===&lt;br /&gt;
The &amp;lt;code&amp;gt;check.sh&amp;lt;/code&amp;gt; script verifies whether the server is currently running by inspecting the PID recorded in &amp;lt;code&amp;gt;pid.lock&amp;lt;/code&amp;gt; and testing whether the associated process exists.&lt;br /&gt;
&lt;br /&gt;
If the server is not running, it prints:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
RiskServer is not running.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== start.sh ===&lt;br /&gt;
The &amp;lt;code&amp;gt;start.sh&amp;lt;/code&amp;gt; script:&lt;br /&gt;
* Exits immediately if the server is already running&lt;br /&gt;
* Creates a &amp;lt;code&amp;gt;logs/&amp;lt;/code&amp;gt; directory if necessary&lt;br /&gt;
* Moves any existing &amp;lt;code&amp;gt;srv_*.log&amp;lt;/code&amp;gt; files into &amp;lt;code&amp;gt;logs/&amp;lt;/code&amp;gt;&lt;br /&gt;
* Starts the &amp;lt;code&amp;gt;RiskServer&amp;lt;/code&amp;gt; process in the background&lt;br /&gt;
* Writes the PID to &amp;lt;code&amp;gt;pid.lock&amp;lt;/code&amp;gt;&lt;br /&gt;
* Reads network interfaces from &amp;lt;code&amp;gt;config.yml&amp;lt;/code&amp;gt; and waits until the server is listening on at least one configured address&lt;br /&gt;
&lt;br /&gt;
This ensures the server is fully initialized before the script exits.&lt;br /&gt;
&lt;br /&gt;
=== stop.sh ===&lt;br /&gt;
The &amp;lt;code&amp;gt;stop.sh&amp;lt;/code&amp;gt; script:&lt;br /&gt;
* Reads the PID from &amp;lt;code&amp;gt;pid.lock&amp;lt;/code&amp;gt;&lt;br /&gt;
* Sends &amp;lt;code&amp;gt;SIGINT&amp;lt;/code&amp;gt; to request a graceful shutdown&lt;br /&gt;
* Waits for termination using exponential backoff (up to 300 seconds)&lt;br /&gt;
* Sends &amp;lt;code&amp;gt;SIGKILL&amp;lt;/code&amp;gt; if the server fails to stop cleanly&lt;br /&gt;
* Appends a forced-termination message to the most recent log file (if applicable)&lt;br /&gt;
* Removes the &amp;lt;code&amp;gt;pid.lock&amp;lt;/code&amp;gt; file&lt;br /&gt;
&lt;br /&gt;
This guarantees consistent shutdown behavior across normal and exceptional conditions.&lt;/div&gt;</summary>
		<author><name>Kamal</name></author>
		
	</entry>
</feed>