Leveraging AI and the yfinance python module to investigate the stock market
This Python script combines real-time market data with AI-powered analysis to provide comprehensive stock assessments and trading recommendations. It leverages Yahoo Finance for market data and Claude (Anthropic's AI) for intelligent analysis.
- Real-time stock data fetching using yfinance
- Technical analysis including:
- Moving averages (50-day, 200-day SMA)
- RSI (Relative Strength Index)
- Volume analysis
- Volatility metrics
- Fundamental analysis including:
- Market cap
- P/E ratio
- Profit margins
- Revenue growth
- Debt/Equity ratios
- Market context analysis:
- Sector performance
- Industry positioning
- Beta and relative strength vs S&P500
- AI-powered analysis providing:
- Technical trend interpretation
- Fundamental value assessment
- Market condition evaluation
- Clear BUY/HOLD/SELL recommendations
pip install yfinance anthropic numpy pandas
- Get an API key from Anthropic (https://www.anthropic.com/)
- Create a file named
key.txt
in the project directory - Paste your Anthropic API key in
key.txt
python yai.py
When prompted, enter a stock symbol (e.g., AAPL, MSFT, GOOGL).
The script provides:
- Detailed technical analysis
- Fundamental analysis
- Market context analysis
- Color-coded final recommendation (🟢 BUY, 🟡 HOLD, 🔴 SELL)
- Analysis cost based on API usage
Example output:
📊 Analysis Report: AAPL
==================================================
📍 Technical Analysis
------------------------------
[Technical analysis details...]
📍 Fundamental Analysis
------------------------------
[Fundamental analysis details...]
📍 Market Analysis
------------------------------
[Market analysis details...]
🎯 FINAL RECOMMENDATION
==================================================
Signal: BUY
Rationale: [AI-generated explanation]
💰 Analysis Cost: ~$0.30
Never commit your key.txt
file to version control. Make sure it's listed in your .gitignore
:
key.txt
Feel free to open issues or submit pull requests with improvements.
MIT License - See LICENSE file for details
This tool is for educational purposes only. Always do your own research and consult with financial advisors before making investment decisions. The recommendations provided are based on automated analysis and should not be the sole basis for trading decisions.