""" Astrological feature extraction for financial time series. Computes geocentric ecliptic longitudes, aspects, lunar phases, and retrogrades for each date using PyEphem. """ from __future__ import annotations from datetime import date, timedelta import ephem import numpy as np # Planets used in financial astrology PLANETS = { "Sun": ephem.Sun, "Moon": ephem.Moon, "Mercury": ephem.Mercury, "Venus": ephem.Venus, "Mars": ephem.Mars, "Jupiter": ephem.Jupiter, "Saturn": ephem.Saturn, "Uranus": ephem.Uranus, "Neptune": ephem.Neptune, "Pluto": ephem.Pluto, } # Classical aspects (angle, orb in degrees) ASPECTS = { "conjunction": (0, 8), "sextile": (60, 6), "square": (90, 8), "trine": (120, 8), "opposition": (180, 8), } # Zodiac signs (each 30 degrees) SIGNS = [ "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces", ] def _ecliptic_longitude(body: ephem.Body, d: ephem.Date) -> float: """Compute geocentric ecliptic longitude in degrees [0, 360).""" body.compute(d) ecl = ephem.Ecliptic(body) return np.degrees(float(ecl.lon)) % 360.0 def _is_retrograde(body_cls, d: ephem.Date) -> bool: """Check if a planet is retrograde (longitude decreasing day-to-day).""" if body_cls in (ephem.Sun, ephem.Moon): return False # Sun and Moon never retrograde b1 = body_cls() b2 = body_cls() lon1 = _ecliptic_longitude(b1, d) lon2 = _ecliptic_longitude(b2, d + 1) # Handle wrap-around (e.g., 359 -> 1 is forward, not backward) diff = (lon2 - lon1 + 180) % 360 - 180 return diff < 0 def _lunar_phase_angle(d: ephem.Date) -> float: """Compute lunar phase as angle [0, 360). 0=New, 180=Full.""" sun = ephem.Sun() moon = ephem.Moon() sun_lon = _ecliptic_longitude(sun, d) moon_lon = _ecliptic_longitude(moon, d) return (moon_lon - sun_lon) % 360.0 def _check_aspect(lon1: float, lon2: float, angle: float, orb: float) -> float: """Check if two longitudes form an aspect. Returns closeness (0=exact, 1=at orb edge), or -1 if no aspect.""" diff = abs((lon1 - lon2 + 180) % 360 - 180) deviation = abs(diff - angle) if deviation <= orb: return 1.0 - deviation / orb # 1.0 = exact, 0.0 = at edge return -1.0 def compute_features_for_date(d: date) -> dict[str, float]: """Compute all astrological features for a single date. Returns dict with feature names as keys. """ ed = ephem.Date(d.strftime("%Y/%m/%d")) features: dict[str, float] = {} # 1. Planetary longitudes (normalized to [0, 1]) longitudes: dict[str, float] = {} for name, cls in PLANETS.items(): body = cls() lon = _ecliptic_longitude(body, ed) longitudes[name] = lon features[f"lon_{name}"] = lon / 360.0 # 2. Zodiac sign as integer (0-11) — for each planet for name, lon in longitudes.items(): features[f"sign_{name}"] = int(lon / 30.0) # 3. Retrogrades (binary) for name, cls in PLANETS.items(): features[f"retro_{name}"] = float(_is_retrograde(cls, ed)) # 4. Lunar phase angle and derived features phase_angle = _lunar_phase_angle(ed) features["lunar_phase_angle"] = phase_angle / 360.0 features["lunar_phase_sin"] = np.sin(np.radians(phase_angle)) features["lunar_phase_cos"] = np.cos(np.radians(phase_angle)) # Lunar quarter (0=New, 1=First Quarter, 2=Full, 3=Last Quarter) features["lunar_quarter"] = int(phase_angle / 90.0) # 5. Aspects between all planet pairs planet_names = list(PLANETS.keys()) for i in range(len(planet_names)): for j in range(i + 1, len(planet_names)): p1, p2 = planet_names[i], planet_names[j] lon1, lon2 = longitudes[p1], longitudes[p2] for asp_name, (angle, orb) in ASPECTS.items(): closeness = _check_aspect(lon1, lon2, angle, orb) key = f"asp_{p1}_{p2}_{asp_name}" # Binary: is aspect active? features[key] = 1.0 if closeness >= 0 else 0.0 # 6. Planetary speed features (daily motion in degrees) for name, cls in PLANETS.items(): if cls in (ephem.Sun, ephem.Moon): continue b1, b2 = cls(), cls() lon1 = _ecliptic_longitude(b1, ed) lon2 = _ecliptic_longitude(b2, ed + 1) speed = (lon2 - lon1 + 180) % 360 - 180 features[f"speed_{name}"] = speed return features def compute_features_batch(dates: list[date]) -> np.ndarray: """Compute astrological features for a batch of dates. Returns: (n_dates, n_features) array, and list of feature names. """ all_features = [compute_features_for_date(d) for d in dates] feature_names = list(all_features[0].keys()) matrix = np.array([[f[k] for k in feature_names] for f in all_features]) return matrix, feature_names