返回题库

流动性压力测试

Stress Test Liquidity

专题
Finance / 金融
难度
L3
来源
MyntBit

题目详情

流动性空洞在市场波动激增时出现,流动性提供者扩大买卖价差以管理库存风险。模拟这些动态交易成本对真实回测至关重要。本题建模波动率依赖价差对策略表现的影响。

任务:实现流动性压力模拟器类,模拟流动性空洞期间的价差变化。当波动率超过阈值时价差自动扩大,策略须在扩大价差下仍能盈利。

英文原题

Liquidity holes occur when market volatility spikes, causing liquidity providers to widen bid-ask spreads to manage inventory risk. Simulating these dynamic transaction costs is crucial for realistic backtesting, as strategies that appear profitable in normal conditions may fail during stress regimes. This problem models the impact of volatility-dependent spreads on trading strategy performance.
Task
Implement a backtesting engine that calculates the total Profit and Loss (PnL) of a trading str

解析

问题分析

流动性压力测试模拟极端市场条件下的订单簿行为:买卖价差急剧扩大、深度消失、大规模订单导致价格滑点。核心是构建非线性的市场冲击模型。

实现

struct StressScenario { double spread_mult; double depth_pct; double vol_mult; };
class LiquidityStressTester {
    OrderBook book_;
public:
    double simulate(const StressScenario& s, int order_qty, bool is_buy) {
        OrderBook stressed = book_;  // 拷贝后施压
        stressed.scaleDepth(s.depth_pct / 100.0);     // 深度收缩
        stressed.widenSpread(s.spread_mult);           // 价差扩大
        double base_cost = book_.estimateCost(order_qty, is_buy);
        double stress_cost = stressed.estimateCost(order_qty, is_buy);
        return (stress_cost - base_cost) / base_cost * 100;  // 额外成本百分比
    }
};

复杂度与边界

  • 时间复杂度:simulate O(扫过档位数)
  • 空间复杂度:O(档位数)
  • 边界条件:(1) 深度为 0 时模拟完全无流动性 (2) 负价差可能短暂出现 (3) 不同产品需要不同压力参数

英文解析

Analysis

Liquidity stress testing simulates order book behavior under extreme market conditions: bid-ask spreads widening dramatically, depth disappearing, and large orders causing significant price slippage. The core challenge is constructing a non-linear market impact model that captures these dynamics.

Solution

struct StressScenario { double spread_mult; double depth_pct; double vol_mult; };
class LiquidityStressTester {
    OrderBook book_;
public:
    double simulate(const StressScenario& s, int order_qty, bool is_buy) {
        OrderBook stressed = book_;  // Copy and apply stress
        stressed.scaleDepth(s.depth_pct / 100.0);     // Depth contraction
        stressed.widenSpread(s.spread_mult);           // Spread widening
        double base_cost = book_.estimateCost(order_qty, is_buy);
        double stress_cost = stressed.estimateCost(order_qty, is_buy);
        return (stress_cost - base_cost) / base_cost * 100;  // Extra cost percentage
    }
};

Complexity & Edge Cases

  • Time complexity: simulate O(number of price levels scanned)
  • Space complexity: O(number of price levels)
  • Edge cases: (1) Depth of 0 simulates complete liquidity absence (2) Negative spreads may briefly appear in stressed scenarios (3) Different products require different stress parameters

Verification

Apply stress scenarios with known parameters, benchmark stressed vs normal execution costs. Verify depth scaling and spread widening produce expected cost increases. Test extreme scenario where no liquidity exists.

Key Considerations

Stress testing is essential for risk management in quantitative trading. The non-linear relationship between order size and market impact means that under stress, execution costs can increase by orders of magnitude. Stress scenarios must be calibrated to historical crisis events to provide meaningful risk estimates.