风险限额检查器
Risk Limit Checker
题目详情
在电子交易系统中,事前风控检查至关重要,防止错误订单到达交易所造成严重损失。事前风控限额检查器在微秒级评估每笔订单,确保其不超过持仓限制、订单大小限制和频率限制。
任务:实现 RiskLimitChecker 类,对每笔订单检查:最大订单大小、单标的最大持仓、总持仓限制和订单频率限制。所有检查在微秒内完成。
英文原题
In electronic trading systems, pre-trade risk checks are critical to prevent erroneous orders from reaching the exchange and causing severe financial loss. A pre-trade risk limit checker evaluates every incoming order in microseconds to ensure it complies with established trading limits such as maximum order size, notional value, position, and daily volume.
Task
Implement a RiskLimitChecker class that validates incoming orders against four global risk limits. The class must be initialized with
解析
问题分析
实时风险限额检查在订单提交前验证:账户净持仓、单产品集中度、日亏损上限等约束。超限时拒绝订单并发送告警。
实现
struct RiskLimits { double max_net_position, max_single_exposure, max_daily_loss; };
class RiskChecker {
std::unordered_map<std::string, double> positions_, daily_pnl_;
RiskLimits limits_;
public:
enum Decision { APPROVED, REJECTED_POSITION, REJECTED_EXPOSURE, REJECTED_LOSS };
Decision check(const std::string& account, const std::string& product,
int qty, double price, double current_pnl) {
double notional = std::abs(qty) * price;
if (std::abs(positions_[account] + qty * (qty > 0 ? 1.0 : -1.0)) > limits_.max_net_position)
return REJECTED_POSITION;
if (notional > limits_.max_single_exposure) return REJECTED_EXPOSURE;
if (daily_pnl_[account] + current_pnl < -limits_.max_daily_loss) return REJECTED_LOSS;
return APPROVED;
}
};复杂度与边界
- 时间复杂度:check O(1) 哈希表查找
- 空间复杂度:O(账户数 + 产品数)
- 边界条件:(1) 新账户自动创建限额记录 (2) 跨产品净额结算 (3) 日初重置 PnL
英文解析
Analysis
Real-time risk limit checks verify constraints before order submission: account net position, single-product concentration, daily loss cap, etc. Orders exceeding limits are rejected with alerts.
Solution
struct RiskLimits { double max_net_position, max_single_exposure, max_daily_loss; };
class RiskChecker {
std::unordered_map<std::string, double> positions_, daily_pnl_;
RiskLimits limits_;
public:
enum Decision { APPROVED, REJECTED_POSITION, REJECTED_EXPOSURE, REJECTED_LOSS };
Decision check(const std::string& account, const std::string& product,
int qty, double price, double current_pnl) {
double notional = std::abs(qty) * price;
if (std::abs(positions_[account] + qty * (qty > 0 ? 1.0 : -1.0)) > limits_.max_net_position)
return REJECTED_POSITION;
if (notional > limits_.max_single_exposure) return REJECTED_EXPOSURE;
if (daily_pnl_[account] + current_pnl < -limits_.max_daily_loss) return REJECTED_LOSS;
positions_[account] += qty * (qty > 0 ? 1.0 : -1.0);
daily_pnl_[account] += current_pnl;
return APPROVED;
}
};Complexity & Edge Cases
- Time complexity: O(1) for threshold comparison; O(N) for N portfolio positions aggregated
- Space complexity: O(N) for position risk array
- Edge cases: (1) Zero-position portfolios have no risk exposure. (2) Negative positions (shorts) contribute absolute exposure. (3) Real-time market price changes may exceed limits between check intervals.
Key Considerations
- Atomicity: Risk check and position update must be atomic — no order submission between check and update.
- Per-account tracking: Position and PnL tracked per account for accurate limit enforcement.
- Daily reset: Daily PnL counters reset at market open.
- Time complexity: check O(1); Space complexity: O(accounts + products).