Skip to content

Latest commit

ย 

History

20 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Financial Analysis Model Suite

A comprehensive machine learning pipeline combining sentiment analysis and technical analysis for robust financial decision-making and swing trading.

๐Ÿ“Š Project Overview

This project combines two complementary models for comprehensive financial analysis:

๐Ÿง  Sentiment Model (sentiment model/)

  • Unified investor sentiment signals from multiple data sources
  • Kaggle Twitter dataset as core foundation
  • FinancialBERT sentiment analysis for news headlines
  • Google Trends alignment for interest tracking
  • Machine learning training and backtesting capabilities

๐Ÿ“ˆ Price Model (price model/)

  • Candlestick pattern recognition for technical analysis
  • Optimized for 17.1 GB RAM with memory management
  • Progressive training with 4-stage optimization
  • Real-time trading signals with risk management
  • Swing trading focus (1-3 day holding periods)

๐ŸŽฏ Quick Start

Option 1: Interactive Launcher (Recommended)

cd "price model"
python launch.py

Option 2: Direct Commands

# Build dataset
python main.py --mode data

# Progressive training (optimized)
python main.py --mode progressive --experiment-name my_experiment

# Predict patterns
python main.py --mode predict --ticker AAPL

# Generate trading signals
python main.py --mode signals --portfolio AAPL MSFT GOOGL

# Complete demo
python main.py --mode demo

๐Ÿ“Š Optimized Configuration

Stage Batch Size Memory Usage Improvement
Stage 1 512 0.06 GB 2x larger
Stage 2 256 0.05 GB 4x larger
Stage 3 128 0.05 GB 4x larger
Stage 4 64 0.05 GB 4x larger

๐Ÿ› ๏ธ Available Modes

--mode data

  • Build and prepare dataset
  • Memory-optimized data loading
  • Automatic feature normalization

--mode train

  • Standard single-stage training
  • Optimized batch size (512)
  • Enhanced logging and monitoring

--mode progressive

  • 4-stage progressive training
  • Optimized batch sizes for each stage
  • Memory management between stages
  • Automatic model checkpointing

--mode predict

  • Pattern prediction for any ticker
  • Automatic model selection
  • Confidence scoring

--mode signals

  • Generate trading signals for portfolios
  • Multi-stock analysis
  • Signal strength ranking

--mode demo

  • Complete pipeline demonstration
  • Data โ†’ Training โ†’ Prediction โ†’ Signals

--mode all

  • Run complete pipeline
  • End-to-end processing

๐Ÿง  Sentiment Model Features

Data Sources

  • Kaggle Twitter Dataset: Pre-existing high-quality Twitter sentiment data (core)
  • NewsAPI: Real-time news headlines with FinancialBERT sentiment analysis
  • Google Trends: Interest scores aligned with Twitter dataset dates/tickers
  • Market Data: Stock prices for target variable creation

FinancialBERT Integration

  • Domain-specific sentiment analysis for financial text
  • Model: ahmedrachid/FinancialBERT-Sentiment-Analysis
  • Outputs: sentiment scores (-1 to 1), labels, and confidence scores

Dataset Alignment

  • All data sources aligned by date and ticker
  • Kaggle dataset serves as the foundation
  • Supporting features appended without disrupting original schema

Feature Engineering

  • Lagged features (1, 3, 7 days)
  • Moving averages (3, 7, 14 days)
  • Momentum indicators
  • Volatility measures

๐Ÿ“ˆ Price Model Features

Swing Trading Focus

  • 1-3 day holding periods - Perfect for swing trading
  • 65 features per timestep - Comprehensive technical analysis
  • Binary classification - Buy/Sell signals with confidence scores
  • Risk management - Stop loss and take profit recommendations

Smart Data Collection

  • Incremental updates - Only collect recent data for existing tickers
  • Graceful interruption - Press Ctrl+C to stop and save progress
  • Backup protection - Automatic backup files during processing
  • S&P 500 coverage - 500 stocks with quality filtering
  • Integrated quality fixes - Automatic NaN handling, outlier detection, class balancing

Advanced AI Model

  • CNN architecture - Deep learning for pattern recognition
  • Attention mechanism - Focus on important features
  • Residual connections - Better gradient flow
  • Progressive training - Two-stage training for better performance

Trading Signals

  • Real-time predictions - Get BUY/SELL signals for any stock
  • Technical confirmation - RSI, MACD, moving averages
  • Entry/Exit points - Optimal prices with stop loss/take profit
  • Portfolio analysis - Compare multiple stocks at once

๐Ÿ“ Project Structure

Financial Sentiment Model/
โ”œโ”€โ”€ README.md                    # ๐Ÿ“– This comprehensive guide
โ”œโ”€โ”€ sentiment model/             # ๐Ÿง  Sentiment analysis pipeline
โ”‚   โ”œโ”€โ”€ README.md               # ๐Ÿ“– Sentiment model documentation
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ data/              # ๐Ÿ“Š Data ingestion and processing
โ”‚   โ”‚   โ”œโ”€โ”€ features/          # ๐Ÿ”ง Normalization + signal fusion
โ”‚   โ”‚   โ”œโ”€โ”€ models/            # ๐Ÿง  ML training + evaluation
โ”‚   โ”‚   โ””โ”€โ”€ backtest/          # ๐Ÿ“ˆ Backtesting strategies
โ”‚   โ”œโ”€โ”€ data/
โ”‚   โ”‚   โ”œโ”€โ”€ raw/               # ๐Ÿ“Š Raw data (news, trends, market)
โ”‚   โ”‚   โ””โ”€โ”€ processed/         # ๐Ÿ“Š Structured sentiment scores
โ”‚   โ””โ”€โ”€ configs/               # โš™๏ธ Configuration files
โ”œโ”€โ”€ price model/                # ๐Ÿ“ˆ Technical analysis pipeline
โ”‚   โ”œโ”€โ”€ main.py                # ๐ŸŽฏ Main pipeline script
โ”‚   โ”œโ”€โ”€ launch.py              # ๐Ÿš€ Interactive launcher
โ”‚   โ”œโ”€โ”€ memory_optimization.py # ๐Ÿ’พ Memory analysis tool
โ”‚   โ”œโ”€โ”€ README_STREAMLINED.md  # ๐Ÿ“– Streamlined documentation
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ training/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ train.py      # ๐Ÿง  Streamlined training
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ progressive_trainer.py # ๐Ÿš€ Optimized progressive training
โ”‚   โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ dataset.py    # ๐Ÿ’พ Memory-optimized data loading
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ””โ”€โ”€ config.json       # โš™๏ธ Optimized configuration
โ”‚   โ”œโ”€โ”€ models/                # ๐Ÿง  Saved models
โ”‚   โ”œโ”€โ”€ logs/                  # ๐Ÿ“ Training logs
โ”‚   โ””โ”€โ”€ data/                  # ๐Ÿ“Š Dataset files
โ””โ”€โ”€ docs/                      # ๐Ÿ“š Additional documentation

๐ŸŽฎ Usage Examples

Interactive Menu

cd "price model"
python launch.py
# Choose from menu options

Command Line

# Quick progressive training
python main.py --mode progressive

# Custom experiment
python main.py --mode progressive --experiment-name my_experiment

# Predict specific stock
python main.py --mode predict --ticker TSLA

# Portfolio analysis
python main.py --mode signals --portfolio AAPL MSFT GOOGL TSLA AMZN

Sentiment Model Pipeline

cd "sentiment model"

# Complete pipeline
python src/data/backfill_historical_new.py \
    --kaggle_path path/to/kaggle_twitter_sentiment.csv \
    --tickers AAPL MSFT GOOGL TSLA \
    --since 2023-01-01 \
    --until 2023-12-31

# Individual components
python src/data/ingest_kaggle_twitter.py --kaggle_path dataset.csv
python src/data/ingest_news_financialbert.py --tickers AAPL TSLA
python src/data/ingest_trends_aligned.py --tickers AAPL TSLA

โšก Performance Benefits

Price Model Optimizations

  • 2-4x faster training with larger batch sizes
  • Better GPU utilization with optimized memory usage
  • Stable convergence with more stable gradients
  • Memory efficient using only 0.6% of available RAM
  • Automatic cleanup between training stages

Sentiment Model Features

  • No API Rate Limits: Uses pre-existing Twitter data
  • Domain-Specific Sentiment: FinancialBERT for financial text
  • Reliable Data Sources: Focused on News + Trends
  • Modular Design: Run components independently
  • Feature Rich: Comprehensive sentiment signals
  • Maintainable: Clean, documented codebase

๐Ÿ”ง Memory Optimization Features

  • โœ… Explicit data types (float32) to prevent object arrays
  • โœ… Memory management between training stages
  • โœ… Garbage collection and CUDA cache clearing
  • โœ… Cached data loaders to prevent multiple instances
  • โœ… Optimized batch sizes for 17.1 GB RAM
  • โœ… Robust error handling and graceful failures

๐Ÿ“ˆ Expected Results

Price Model

  • Training Speed: 2-4x faster with optimized batch sizes
  • Memory Usage: Only 0.6% of available RAM per stage
  • Model Quality: Better convergence with larger batches
  • Reliability: Robust error handling and recovery

Sentiment Model

  • Unified Signals: Combined sentiment from multiple sources
  • Domain Expertise: FinancialBERT for financial text analysis
  • Feature Rich: Comprehensive sentiment indicators
  • Scalable: Modular design for easy expansion

๐Ÿšจ Important Notes

Data Collection

  • Internet required: Downloads data from various sources
  • Time intensive: Processing takes 2-4 hours for full datasets
  • Graceful interruption: Press Ctrl+C to stop safely
  • Backup protection: Automatic backup files created
  • Quality improvements: Applied automatically by default

Model Training

  • GPU recommended: CUDA support for faster training
  • Memory intensive: 16GB+ RAM recommended
  • Patience required: Training takes 1-2 hours
  • Early stopping: Prevents overfitting

Trading Signals

  • Not financial advice: Use at your own risk
  • Backtesting recommended: Test on historical data
  • Risk management: Always use stop losses
  • Diversification: Don't put all money in one signal

๐Ÿ” Troubleshooting

Common Issues

Data Collection Fails:

# Check internet connection
# Verify API keys (if using paid data)
# Try incremental mode
python main.py --mode data --incremental

Training Stuck:

# Reduce learning rate
# Increase patience
# Check GPU memory
python main.py --mode train --epochs 30

Low Accuracy:

# Try progressive training
# Increase model capacity
# Check data quality
python main.py --mode train --epochs 75

Performance Optimization

For Faster Training:

  • Use GPU with CUDA
  • Increase batch size (if memory allows)
  • Reduce number of tickers for testing

For Better Accuracy:

  • Use progressive training
  • Increase training epochs
  • Collect more recent data
  • Use quality-improved dataset

๐Ÿ“š References

  • Technical Analysis: TA-Lib library
  • Data Source: Yahoo Finance via yfinance
  • Deep Learning: PyTorch framework
  • S&P 500 Data: DataHub constituents
  • FinancialBERT: Domain-specific sentiment analysis
  • Kaggle Dataset: Twitter sentiment for financial markets

๐Ÿ”„ Recent Updates

Pipeline Organization (Latest)

  • โœ… Reorganized Structure: Moved scripts to appropriate folders
  • โœ… Eliminated Redundancy: Removed duplicate files and scripts
  • โœ… Documentation Consolidation: All docs organized properly
  • โœ… Utility Scripts: Organized utility functions
  • โœ… Quality Integration: Data quality fixes integrated into main pipeline

Memory Optimizations

  • โœ… Optimized Batch Sizes: 512, 256, 128, 64 for 17.1 GB RAM
  • โœ… Memory Management: Between training stages
  • โœ… Data Type Optimization: float32 to prevent object arrays
  • โœ… Cached Data Loaders: Prevent multiple instances

Streamlined Interface

  • โœ… Interactive Launcher: Easy-to-use menu system
  • โœ… Unified Commands: Single main.py handles all modes
  • โœ… Comprehensive Documentation: All information in one place
  • โœ… Error Handling: Robust error recovery

๐Ÿ“„ License

This project is for educational purposes. Use trading signals at your own risk.


๐Ÿš€ Ready to Use

The pipeline is now streamlined and optimized for your system. Simply run:

cd "price model"
python launch.py

Or start with:

python main.py --mode demo

Everything is integrated, optimized, and ready to use! ๐ŸŽฏ

About

training a financial model

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages