Fetch recent news for a ticker and compute a sentiment score using the OpenAI API.
- Create a local
.envfile (optional) in the project root:
OPENAI_API_KEY=...
# Optional (only needed for --source newsapi; otherwise Google News RSS is used)
NEWSAPI_KEY=...
# Optional
OPENAI_MODEL=gpt-4o-mini
OPENAI_BASE_URL=https://api.openai.com/v1- (Optional) Install a CLI entrypoint:
python3 -m pip install -e .- Run an analysis:
python3 -m stock_sentiment analyze TSLA
python3 -m stock_sentiment analyze TSLA --days 7 --max-articles 50 --format json --include-reasons
python3 -m stock_sentiment analyze TSLA --source google-rssNotes:
- Default output is a single-line text summary; use
--format jsonfor structured output. - By default
--source autouses NewsAPI whenNEWSAPI_KEYis set, otherwise Google News RSS. - In
--source auto, if NewsAPI fails the CLI falls back to Google News RSS. OPENAI_API_KEYis required unless all needed per-article classifications are already cached.- Add
--include-articlesto embed article metadata in JSON output. - Add
--verboseto print per-article sentiment details in text mode. - OpenAI results are cached locally by default (see
--cache-dir,--no-cache,--cache-ttl-hours). - JSON output includes
sourceandlookback_daysfields for downstream systems.
Disclaimer: This tool is for informational purposes only and is not financial advice.
python3 -m unittest discover -s tests -p "test_*.py"