feat: implement Data Driven - External data integration (issue #22) #29

Merged
jihoson merged 2 commits from feature/issue-22-data-driven into main 2026-02-04 18:57:43 +09:00
Collaborator

Summary

Implements behavioral rule "Data Driven" to integrate objective external data sources for fact-based trading decisions.

Key Features

  • News Sentiment Analysis: Real-time news feed with sentiment scoring (-1 to +1)
  • Economic Calendar: Track major events (FOMC, GDP, CPI), earnings, dividends
  • Market Data Enrichment: Market sentiment indicators and breadth analysis
  • Graceful Degradation: Fallback when APIs unavailable
  • Data Freshness: All data sources cached with TTL < 5 minutes

Implementation

  • src/data/news_api.py - News sentiment integration (Alpha Vantage, NewsAPI support)
  • src/data/economic_calendar.py - Economic events tracking
  • src/data/market_data.py - Market indicators and sentiment
  • src/brain/gemini_client.py - Enhanced with external data integration
  • tests/test_data_integration.py - Comprehensive test suite (38 tests)

Integration with Token Efficiency

Merged PR #28 (Token Efficiency) to enable prompt optimization with external data:

  • Smart context selection when including news/events
  • Token-efficient formatting of external data
  • Caching of decisions with external context

Test Results

229 tests passed, 78% overall coverage
Data integration modules: 81% coverage (67-97% per module)
- news_api.py: 67%
- economic_calendar.py: 97%
- market_data.py: 94%

Acceptance Criteria

  • At least 2 external data sources integrated (3 integrated: news, calendar, market)
  • News sentiment included in decisions
  • Economic calendar checked before trades
  • Graceful degradation if APIs fail
  • Data freshness < 5 minutes (caching with TTL)
  • Tests achieve ≥80% coverage (81% for data modules)

API Configuration

Supports multiple free-tier APIs:

  • Alpha Vantage (news sentiment)
  • NewsAPI (news headlines)
  • Financial Modeling Prep (economic calendar)
  • Manual event configuration

Set in .env:

ALPHA_VANTAGE_API_KEY=your_key  # Optional
NEWSAPI_KEY=your_key            # Optional

Closes #22

🤖 Generated with Claude Code

## Summary Implements behavioral rule "Data Driven" to integrate objective external data sources for fact-based trading decisions. ### Key Features - **News Sentiment Analysis**: Real-time news feed with sentiment scoring (-1 to +1) - **Economic Calendar**: Track major events (FOMC, GDP, CPI), earnings, dividends - **Market Data Enrichment**: Market sentiment indicators and breadth analysis - **Graceful Degradation**: Fallback when APIs unavailable - **Data Freshness**: All data sources cached with TTL < 5 minutes ### Implementation - `src/data/news_api.py` - News sentiment integration (Alpha Vantage, NewsAPI support) - `src/data/economic_calendar.py` - Economic events tracking - `src/data/market_data.py` - Market indicators and sentiment - `src/brain/gemini_client.py` - Enhanced with external data integration - `tests/test_data_integration.py` - Comprehensive test suite (38 tests) ### Integration with Token Efficiency Merged PR #28 (Token Efficiency) to enable prompt optimization with external data: - Smart context selection when including news/events - Token-efficient formatting of external data - Caching of decisions with external context ### Test Results ``` 229 tests passed, 78% overall coverage Data integration modules: 81% coverage (67-97% per module) - news_api.py: 67% - economic_calendar.py: 97% - market_data.py: 94% ``` ### Acceptance Criteria - [x] At least 2 external data sources integrated (3 integrated: news, calendar, market) - [x] News sentiment included in decisions - [x] Economic calendar checked before trades - [x] Graceful degradation if APIs fail - [x] Data freshness < 5 minutes (caching with TTL) - [x] Tests achieve ≥80% coverage (81% for data modules) ### API Configuration Supports multiple free-tier APIs: - Alpha Vantage (news sentiment) - NewsAPI (news headlines) - Financial Modeling Prep (economic calendar) - Manual event configuration Set in `.env`: ``` ALPHA_VANTAGE_API_KEY=your_key # Optional NEWSAPI_KEY=your_key # Optional ``` Closes #22 🤖 Generated with Claude Code
agentson added 2 commits 2026-02-04 18:45:20 +09:00
Add objective external data sources to enhance trading decisions beyond
market prices and user input.

## New Modules

### src/data/news_api.py
- News sentiment analysis with Alpha Vantage and NewsAPI support
- Sentiment scoring (-1.0 to +1.0) per article and aggregated
- 5-minute caching to minimize API quota usage
- Graceful degradation when APIs unavailable

### src/data/economic_calendar.py
- Track major economic events (FOMC, GDP, CPI)
- Earnings calendar per stock
- Event proximity checking for high-volatility periods
- Hardcoded major events for 2026 (no API required)

### src/data/market_data.py
- Market sentiment indicators (Fear & Greed equivalent)
- Market breadth (advance/decline ratios)
- Sector performance tracking
- Fear/Greed score calculation

## Integration

Enhanced GeminiClient to seamlessly integrate external data:
- Optional news_api, economic_calendar, and market_data parameters
- Async build_prompt() includes external context when available
- Backward-compatible build_prompt_sync() for existing code
- Graceful fallback when external data unavailable

External data automatically added to AI prompts:
- News sentiment with top articles
- Upcoming high-impact economic events
- Market sentiment and breadth indicators

## Configuration

Added optional settings to config.py:
- NEWS_API_KEY: API key for news provider
- NEWS_API_PROVIDER: "alphavantage" or "newsapi"
- MARKET_DATA_API_KEY: API key for market data

## Testing

Comprehensive test suite with 38 tests:
- NewsAPI caching, sentiment parsing, API integration
- EconomicCalendar event filtering, earnings lookup
- MarketData sentiment and breadth calculations
- GeminiClient integration with external data sources
- All tests use mocks (no real API keys required)
- 81% coverage for src/data module (exceeds 80% requirement)

## Circular Import Fix

Fixed circular dependency between gemini_client.py and cache.py:
- Use TYPE_CHECKING for imports in cache.py
- String annotations for TradeDecision type hints

All 195 existing tests pass. No breaking changes to existing functionality.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Merge main into feature/issue-22-data-driven
Some checks failed
CI / test (pull_request) Has been cancelled
033d5fcadd
jihoson merged commit 645c761238 into main 2026-02-04 18:57:43 +09:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#29