Coverage for evaluation/impact_model.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""V5.4.2 - Almgren-style sqrt market-impact model. 

2 

3Replaces flat bps-per-side costs with a volume-participation-dependent 

4impact estimate when enabled. The core formula follows Almgren et al. 

5(2005): temporary price impact scales with the square root of the 

6participation rate (trade size / average daily volume), modulated by 

7the asset's own daily volatility. 

8 

9 impact_bps = eta × sigma_daily × sqrt(|trade_size / ADV|) 

10 

11where: 

12- eta: calibrated impact coefficient (higher = more illiquid asset class) 

13- sigma_daily: rolling daily volatility of the asset (fraction, not %) 

14- trade_size / ADV: participation rate (fraction of daily volume consumed) 

15 

16Pure: numpy only, no I/O, deterministic given inputs.""" 

17 

18from __future__ import annotations 

19 

20import math 

21 

22 

23def compute_sqrt_impact_bps( 

24 *, 

25 trade_size_fraction: float, 

26 daily_volatility: float, 

27 avg_daily_dollar_volume: float, 

28 portfolio_value: float, 

29 eta: float = 0.5, 

30) -> float: 

31 """Computes temporary market impact in basis points. 

32 

33 Args: 

34 trade_size_fraction: absolute weight change (e.g. 0.05 = 5% of book). 

35 daily_volatility: rolling daily return std (e.g. 0.02 = 2%). 

36 avg_daily_dollar_volume: asset's ADV in dollars (e.g. 50_000_000). 

37 portfolio_value: total book value in dollars. 

38 eta: impact coefficient — higher for less liquid assets. 

39 Calibrated defaults: equity ~0.5, crypto ~1.0, bond ETF ~0.3. 

40 

41 Returns: 

42 Impact cost in basis points (always >= 0). Zero when any input is 

43 degenerate (zero size, zero ADV, zero vol).""" 

44 if trade_size_fraction <= 0 or avg_daily_dollar_volume <= 0 or portfolio_value <= 0: 

45 return 0.0 

46 if daily_volatility <= 0: 

47 return 0.0 

48 

49 trade_dollars = trade_size_fraction * portfolio_value 

50 participation_rate = abs(trade_dollars / avg_daily_dollar_volume) 

51 

52 impact_fraction = eta * daily_volatility * math.sqrt(participation_rate) 

53 return max(0.0, impact_fraction * 1e4) 

54 

55 

56def compute_participation_rate( 

57 *, 

58 trade_size_fraction: float, 

59 portfolio_value: float, 

60 avg_daily_dollar_volume: float, 

61) -> float: 

62 """Fraction of the asset's ADV consumed by this trade (0 when degenerate).""" 

63 if avg_daily_dollar_volume <= 0 or portfolio_value <= 0: 

64 return 0.0 

65 return abs(trade_size_fraction * portfolio_value / avg_daily_dollar_volume) 

66 

67 

68# Per-security-type default eta values (calibrated from literature ranges; 

69# refine per-asset once real fill data exists). 

70DEFAULT_ETA_BY_SECURITY_TYPE: dict[str, float] = { 

71 "equity": 0.5, 

72 "crypto": 1.0, 

73 "bond": 0.3, 

74 "forex": 0.1, 

75 "future": 0.4, 

76 "option": 0.8, 

77}