Coverage for features/macro_features.py: 100%

16 statements  

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

1"""Shared, pure cross-asset "macro" feature implementations (Phase 1b of 

2the 5/10 -> 9/10 roadmap, see development/Changelog.md) - deliberate, 

3explicit signals computed once per bar/date from a small set of fixed 

4reference tickers (the Phase 1a bond ETF sleeve + the existing crypto 

5sleeve) and broadcast identically to every asset's model input that 

6bar/date, mirroring how regime state is already a shared, once-per-bar 

7feature. 

8 

9This is distinct from - and additive to - the existing generic 

10correlation-based peer mechanism (topology/market_topology.py's 

11top_peers/top_peer_returns): that mechanism already lets a bond or crypto 

12asset surface as an equity's correlated peer purely incidentally, with no 

13code change needed. These functions instead compute a small, fixed, 

14always-present macro state (yield-curve slope, credit spread, crypto risk 

15appetite) that every asset sees identically regardless of whether it 

16happens to correlate with any one specific reference ticker that day. 

17 

18Imported by BOTH train.py (offline, per-date lookup across the whole 

19universe) and main.py (runtime, once per bar) for parity by construction - 

20same convention as features/technical_indicators.py. 

21 

22Each function takes plain optional-float 20-day-momentum inputs (reusing 

23the existing momentum_20d column/computation already produced by 

24train.py::engineer_features()/main.py's topology-payload step, rather than 

25re-deriving returns from scratch) and returns a single scalar float. A 

26missing/unavailable reference ticker (still warming up, or absent from 

27this particular universe configuration) neutral-defaults its proxy to 0.0 

28- never raises - same "missing peer -> 0.0" convention peer-return 

29features already established. 

30""" 

31 

32from __future__ import annotations 

33 

34YIELD_CURVE_SLOPE_NEUTRAL = 0.0 

35CREDIT_SPREAD_NEUTRAL = 0.0 

36CRYPTO_RISK_APPETITE_NEUTRAL = 0.0 

37 

38 

39def yield_curve_slope_proxy( 

40 long_duration_momentum_20d: float | None, 

41 short_duration_momentum_20d: float | None, 

42) -> float: 

43 """Relative 20-day momentum spread between a long-duration Treasury 

44 ETF (e.g. TLT) and a short-duration one (e.g. SHY) - a proxy for 

45 curve-steepening (positive: long duration outperforming, typically 

46 falling long yields relative to short) vs. flattening/inverting 

47 (negative) sentiment, computable from price data alone with no 

48 separate yield-level data feed. Returns YIELD_CURVE_SLOPE_NEUTRAL (0.0) 

49 when either input is unavailable (reference ticker missing or still 

50 warming up).""" 

51 if long_duration_momentum_20d is None or short_duration_momentum_20d is None: 

52 return YIELD_CURVE_SLOPE_NEUTRAL 

53 return long_duration_momentum_20d - short_duration_momentum_20d 

54 

55 

56def credit_spread_proxy( 

57 high_yield_momentum_20d: float | None, 

58 investment_grade_momentum_20d: float | None, 

59) -> float: 

60 """Relative 20-day momentum spread between a high-yield corporate bond 

61 ETF (e.g. HYG) and an investment-grade one (e.g. LQD) - widens 

62 (negative: high-yield underperforming investment-grade) when credit 

63 risk appetite deteriorates, a standard risk-off signal. Returns 

64 CREDIT_SPREAD_NEUTRAL (0.0) when either input is unavailable.""" 

65 if high_yield_momentum_20d is None or investment_grade_momentum_20d is None: 

66 return CREDIT_SPREAD_NEUTRAL 

67 return high_yield_momentum_20d - investment_grade_momentum_20d 

68 

69 

70def crypto_risk_appetite_proxy(crypto_momentum_20d: float | None) -> float: 

71 """20-day momentum of a liquid crypto reference asset (e.g. BTCUSD) - 

72 a fast-moving risk-on/risk-off signal distinct from the slower-moving 

73 bond-based proxies above, given crypto's comparatively higher 

74 volatility and shorter mean-reversion cycles. Returns 

75 CRYPTO_RISK_APPETITE_NEUTRAL (0.0) when the reference asset's momentum 

76 is unavailable.""" 

77 if crypto_momentum_20d is None: 

78 return CRYPTO_RISK_APPETITE_NEUTRAL 

79 return crypto_momentum_20d