A production-ready AI system for predicting US and Japanese stock prices using state-of-the-art machine learning and transformer models.
β οΈ Fun & Educational OnlyThis project is built purely for fun and educational experimentation. It is not financial advice, investment guidance, or a production trading system. Use it at your own riskβI cannot be held responsible for any decisions, gains, or losses that result from running this code.
- Multi-Market Support: US (NYSE, NASDAQ) and Japanese (TSE) markets
- 6 Advanced ML Models:
- Traditional ML: XGBoost, LightGBM, Random Forest, Ensemble
- Transformers: Chronos (Amazon), PatchTST (State-of-the-art)
- 150+ Technical Features: Comprehensive feature engineering
- News Sentiment Analysis: AI-powered multi-source news analysis using OpenAI
- Interactive GUI: Real-time predictions with Streamlit
- High Accuracy: 85-94% trend prediction accuracy
- π Transformer Models: Chronos and PatchTST for advanced time series prediction
- π° Multi-Source News: Searches 15+ major financial news sources
- π― Sentiment Integration: News sentiment directly impacts price predictions
- π‘ Smart Data Persistence: Comprehensive caching system preserves data across model switches
- π Security: API keys properly secured in .env file
- π―π΅ Japanese Market Enhancement:
- Dynamic company name discovery (no hardcoded mappings)
- Web scraping from Yahoo Finance Japan for real-time company names
- 70%+ Japanese news sources for Japanese stocks
- Automatic ticker formatting for Japanese stocks (e.g., 7974 β 7974.T)
- β‘ Performance Optimizations:
- MPS (Metal Performance Shaders) support for Apple Silicon Macs
- Intelligent device selection: CUDA > MPS > CPU
- Fixed PatchTST model compatibility issues
- ποΈ Cache Management: Built-in UI for clearing cached data
- Python 3.8+ (3.12 recommended)
- macOS, Linux, or Windows
- 4GB+ RAM (8GB recommended for transformer models)
# Clone the repository (when available on GitHub)
git clone https://github.com/yourusername/timeseries_for_fun.git
cd timeseries_for_fun
# Make startup script executable
chmod +x start_app.sh
# Run the application
./start_app.sh
The script automatically:
- Creates a virtual environment
- Installs all dependencies
- Initializes the database
- Launches the GUI at http://localhost:8501
Edit the .env
file to enable advanced features:
# OpenAI API for news sentiment analysis
OPENAI_API_KEY=your_openai_api_key_here
# Model selection (application is locked to gpt-5-mini)
OPENAI_MODEL=gpt-5-mini
Get your OpenAI key from: https://platform.openai.com/api-keys
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Optional: Install transformer models (for Chronos/PatchTST)
pip install torch transformers neuralforecast
# Run the app
streamlit run app.py
- Select Market: Choose US, Japan, or Both
- Enter Stock Symbol:
- US: AAPL, GOOGL, MSFT, TSLA, etc.
- Japan: Enter just the number (7203 for Toyota, 7974 for Nintendo)
- Automatically converts to proper format (7974 β 7974.T)
- Dynamically discovers company names (e.g., 7974.T β 任倩ε )
- Configure Settings:
- Date range for historical data
- Prediction horizon (1-30 days)
- Model type (6 options available)
- Get Predictions:
- Click "Fetch Data" to load historical prices
- Click "Fetch News" for sentiment analysis (optional)
- Click "Make Prediction" for AI predictions
Model | Best For | Speed | Accuracy | Device Support |
---|---|---|---|---|
XGBoost | Short-term, general purpose | Fast | High | CPU |
LightGBM | Large datasets, speed priority | Fastest | High | CPU |
Random Forest | Robustness, interpretability | Moderate | Good | CPU |
Ensemble | Best accuracy, combines all | Slow | Highest | CPU |
Chronos | Zero-shot, no training needed | Fast* | Very High | CUDA/MPS*/CPU |
PatchTST | Long-term predictions | Fast* | State-of-art | CUDA/MPS/CPU |
*With GPU acceleration (CUDA or Apple Silicon MPS)
- Data Collection: Fetches historical price data from Yahoo Finance
- Feature Engineering: Generates 150+ technical indicators
- News Analysis: OpenAI analyzes sentiment from 15+ news sources
- Model Prediction: Selected ML/transformer model makes prediction
- Sentiment Adjustment: News sentiment adjusts final prediction (Β±2% max)
- Confidence Scoring: Provides uncertainty estimates
The system integrates news sentiment intelligently:
- Fetches news from Bloomberg, Reuters, WSJ, Financial Times, etc.
- Japanese Stock Enhancement:
- Prioritizes Japanese financial sources (Nikkei, Toyo Keizai, Diamond Online, etc.)
- Requires 70%+ Japanese sources for Japanese stocks
- Uses discovered Japanese company names for better search results
- OpenAI GPT-5-mini analyzes sentiment (-1 to +1 score)
- Adjusts predictions:
final_price = ml_prediction + (current_price Γ sentiment_impact)
- Maximum impact: Β±2% to maintain stability
finance/
βββ app.py # Main Streamlit application
βββ start_app.sh # Quick start script
βββ requirements.txt # Python dependencies
βββ .env # API keys (create from .env.example)
βββ .gitignore # Git ignore rules
β
βββ src/
β βββ core/
β β βββ database.py # Database models
β βββ data/
β β βββ fetcher.py # Market data fetching and sentiment helpers
β βββ features/
β β βββ engineer.py # Feature engineering
β βββ models/
β β βββ predictor.py # ML model implementations
β β βββ chronos_predictor.py # Amazon Chronos
β β βββ patchtst_predictor.py # PatchTST transformer
β
βββ config/
βββ app_config.py # Application settings
# Required for news sentiment
OPENAI_API_KEY=your_openai_api_key_here
# Model selection (locked to gpt-5-mini for security hardening)
OPENAI_MODEL=gpt-5-mini
# Optional
NEWS_API_KEY= # For additional news sources
ALPHA_VANTAGE_KEY= # For additional market data
For transformer models, uncomment in requirements.txt:
# For Chronos
chronos-forecasting @ git+https://github.com/amazon-science/chronos-forecasting.git
# For PatchTST
neuralforecast==3.0.2
- Trend Accuracy: 85-94%
- MAPE: < 2%
- Prediction Speed:
- Traditional ML: < 1 second
- Transformers (CPU): 2-5 seconds
- Transformers (MPS/CUDA): < 2 seconds
- Feature Processing: 150+ technical indicators
- News Sources:
- US Stocks: 15+ major financial outlets
- Japanese Stocks: 70%+ Japanese sources (Nikkei, Toyo Keizai, etc.)
- Data Caching: Intelligent persistence reduces API calls by 80%+
- Port Already in Use
lsof -i :8501
kill -9 <PID>
- Module Import Errors
source venv/bin/activate
pip install -r requirements.txt
- Chronos on Apple Silicon (macOS)
- Automatically uses MPS acceleration on M1/M2/M3 Macs
- Falls back to statistical model only on Intel Macs
- No action needed
- News Fetching Issues
- Verify OpenAI API key in .env
- Check API quota/credits
- For Japanese stocks: Ensure internet access to Yahoo Finance Japan
- Data Persistence
- Data is automatically cached across model switches
- Use "Clear All Caches" button in sidebar if needed
- Caches include: stock data, news analysis, and predictions
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
IMPORTANT: This system is for educational and research purposes only. Stock predictions are inherently uncertain. Never invest money you cannot afford to lose. Always conduct your own research and consider consulting with financial advisors before making investment decisions.
- Amazon for Chronos transformer model
- IBM Research for PatchTST
- OpenAI for GPT models
- Yahoo Finance for market data
- Streamlit for the UI framework