Coverage for features/fx_carry.py: 100%

27 statements  

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

1"""FX carry feature (V5.7.0): the 3-month interest-rate differential of a currency pair, in percentage points. 

2 

3Long the base currency of a pair earns (r_base - r_quote) of carry per year; the gap is the one slow, economically 

4grounded driver the forex rows of the model had no input for (they carried only price features and an all-zero asset 

5class). 0.0 for every non-forex row, so the column is constant outside the 15 pairs and the existing scaler / schema 

6handle it like any other feature. 

7 

8Rates come from FRED (OECD 3-month interbank, monthly, plus the daily US Treasury 3-month bill), looked up through the 

9publication-lag-aware `series_value_asof()` that every other FRED-backed feature shares: a monthly observation dated the 

101st of month M is published weeks later, so it is only used `FX_RATE_PUBLICATION_LAG_DAYS` after its date. One 

11definition for `train.py` and `main.py`: no formula is duplicated. 

12""" 

13 

14from __future__ import annotations 

15 

16from typing import TYPE_CHECKING 

17 

18from data_pipeline.fred_backfill import series_value_asof 

19 

20if TYPE_CHECKING: 

21 from datetime import date 

22 

23FX_CARRY_FEATURE_NAME = "fx_carry_3m" 

24 

25# currency -> friendly FRED series key (data/reference/fred_series/<key>.csv) 

26CURRENCY_RATE_SERIES = { 

27 "USD": "treasury_3mo", 

28 "EUR": "interbank_3m_eur", 

29 "GBP": "interbank_3m_gbp", 

30 "JPY": "interbank_3m_jpy", 

31 "AUD": "interbank_3m_aud", 

32 "CAD": "interbank_3m_cad", 

33 "CHF": "interbank_3m_chf", 

34 "NZD": "interbank_3m_nzd", 

35} 

36# Days between an observation's date and the day it may be used. The OECD monthly series is dated the 1st and published 

37# in the following month; 45 days is the conservative figure the plan fixed. The daily Treasury bill is a same-day mark. 

38FX_RATE_PUBLICATION_LAG_DAYS = {"treasury_3mo": 0, **{key: 45 for currency, key in CURRENCY_RATE_SERIES.items() if currency != "USD"}} 

39 

40 

41def split_forex_ticker(ticker: str) -> tuple[str, str] | None: 

42 """('EUR', 'USD') for 'EURUSD'; None unless the ticker is two known 3-letter currency codes.""" 

43 text = str(ticker).upper() 

44 if len(text) != 6: 

45 return None 

46 base, quote = text[:3], text[3:] 

47 return (base, quote) if base in CURRENCY_RATE_SERIES and quote in CURRENCY_RATE_SERIES else None 

48 

49 

50def fx_carry_3m( 

51 ticker: str, 

52 asset_class: str | None, 

53 current_date: date, 

54 fred_series: dict[str, list[dict]], 

55 publication_lag_days: dict[str, int] | None = None, 

56) -> float: 

57 """r_base - r_quote at `current_date` (percentage points), 0.0 for a non-forex asset, an unparseable pair, or when 

58 either rate has no published observation yet (a missing rate must read as "no carry", never as a guessed rate).""" 

59 if asset_class != "forex": 

60 return 0.0 

61 pair = split_forex_ticker(ticker) 

62 if pair is None: 

63 return 0.0 

64 lags = {**FX_RATE_PUBLICATION_LAG_DAYS, **(publication_lag_days or {})} 

65 rates = [] 

66 for currency in pair: 

67 key = CURRENCY_RATE_SERIES[currency] 

68 rate = series_value_asof(fred_series.get(key, []), current_date, lags.get(key, 0)) 

69 if rate is None: 

70 return 0.0 

71 rates.append(rate) 

72 return float(rates[0] - rates[1])