Coverage for features/derivatives_macro_features.py: 100%

22 statements  

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

1"""Shared, pure futures-term-structure / options-sentiment "macro" feature 

2implementations - additive third sibling to features/macro_features.py and 

3features/bond_features.py, broadcast identically to EVERY asset's model 

4input (not just futures/options assets themselves). 

5 

6This is the concrete mechanism satisfying the "the model's cross-asset 

7macro features should include options/futures/bond-derived signals so any 

8asset's prediction can be informed by them" requirement - generalized well 

9beyond any single hardcoded pair (e.g. "predict SPY from QQQ options" was 

10only ever an illustrative example, not a special case to hardcode). 

11 

12Every function neutral-defaults to 0.0 on missing input - never raises - 

13same convention as features/macro_features.py. Since real futures 

14continuous-contract prices and options chain volumes only exist once IB is 

15connected (phase_v2.ib.enabled), these features are 0.0 for every asset, 

16every date, until then - a correctness property, not a bug: "no derivatives 

17data configured" must be indistinguishable in the model input from "the 

18signal happens to be neutral today," matching every other missing- 

19reference-ticker case in this codebase. 

20""" 

21 

22from __future__ import annotations 

23 

24FUTURES_TERM_STRUCTURE_SLOPE_NEUTRAL = 0.0 

25OPTIONS_PUT_CALL_RATIO_NEUTRAL = 0.0 

26OPTIONS_IMPLIED_VOL_SKEW_NEUTRAL = 0.0 

27 

28FUTURES_MACRO_FEATURE_NAMES = ["futures_term_structure_slope"] 

29OPTIONS_MACRO_FEATURE_NAMES = ["options_put_call_ratio", "options_implied_vol_skew"] 

30DERIVATIVES_MACRO_FEATURE_NAMES = FUTURES_MACRO_FEATURE_NAMES + OPTIONS_MACRO_FEATURE_NAMES 

31 

32 

33def futures_term_structure_slope(front_month_price: float | None, next_month_price: float | None) -> float: 

34 """(next_month - front_month) / front_month - positive = contango 

35 (next-month more expensive, typical/"normal" market), negative = 

36 backwardation (front-month more expensive, often a supply-stress or 

37 high-carry-cost signal). Returns 

38 FUTURES_TERM_STRUCTURE_SLOPE_NEUTRAL (0.0) when either input is 

39 missing or front_month_price is zero.""" 

40 if front_month_price is None or next_month_price is None or front_month_price == 0: 

41 return FUTURES_TERM_STRUCTURE_SLOPE_NEUTRAL 

42 return (next_month_price - front_month_price) / front_month_price 

43 

44 

45def options_put_call_ratio(put_volume: float | None, call_volume: float | None) -> float: 

46 """A bounded [-1, 1] put/call VOLUME skew - (put - call) / (put + 

47 call), not the raw put/call ratio - chosen specifically so 0.0 is a 

48 genuinely meaningful neutral value (balanced put/call activity OR no 

49 chain data available, both correctly represented as "no signal"), 

50 unlike a raw ratio where 0.0 would ambiguously also mean "zero puts, 

51 some calls" (a real, non-neutral market state). Positive = put-heavy 

52 (bearish/hedging-heavy sentiment), negative = call-heavy (bullish 

53 sentiment). Returns OPTIONS_PUT_CALL_RATIO_NEUTRAL (0.0) when either 

54 input is missing or both volumes are zero.""" 

55 if put_volume is None or call_volume is None: 

56 return OPTIONS_PUT_CALL_RATIO_NEUTRAL 

57 total_volume = put_volume + call_volume 

58 if total_volume <= 0: 

59 return OPTIONS_PUT_CALL_RATIO_NEUTRAL 

60 return (put_volume - call_volume) / total_volume 

61 

62 

63def options_implied_vol_skew(otm_put_iv: float | None, otm_call_iv: float | None) -> float: 

64 """OTM put implied vol minus OTM call implied vol (both at a matched 

65 delta, e.g. 25-delta each, per features/options_greeks.py's 

66 implied_volatility()) - the standard equity-index "smirk" measure; 

67 positive (the typical case) means downside protection carries a 

68 volatility premium over upside calls. Returns 

69 OPTIONS_IMPLIED_VOL_SKEW_NEUTRAL (0.0) when either input is missing.""" 

70 if otm_put_iv is None or otm_call_iv is None: 

71 return OPTIONS_IMPLIED_VOL_SKEW_NEUTRAL 

72 return otm_put_iv - otm_call_iv