This is a transformer model to predict stock price movements using sequences of changes in a list of stocks. The alphabet encodes changes. The default model uses configuration in config_template.yaml.
Choose your learning path:
- Read PROJECT_SUMMARY.md (5 min overview)
- Follow QUICKSTART.md (setup & first run)
- Try EXAMPLES.md (11 practical examples)
pip install -r requirements.txt
python test_installation.py # Verify setup
python train_model.py --db-password YOUR_PASSWORD # Run with defaults- Full technical docs: See README.md
- File structure: See STRUCTURE.txt
- Configuration: See config_template.yaml
- Source code: See src/ directory
Trains a transformer model to predict the next "word" in a sequence of stock price changes:
- Input: List of stocks (e.g., ["AAPL", "MSFT", "GOOGL"])
- Process: Encode price changes as letters (a-g for 7 delta levels)
- Example Word: "acgaeb" = 6 stocks with specific price movements
- Model Task: Given 4-16 words, predict the next word
- Output: Predictions of next period's price movements
pytink/
βββ README.md β Full technical documentation
βββ QUICKSTART.md β Setup & usage guide
βββ PROJECT_SUMMARY.md β Overview & architecture
βββ EXAMPLES.md β 11 usage examples
βββ STRUCTURE.txt β File descriptions
βββ INDEX.md β You are here
β
βββ requirements.txt β Install with: pip install -r requirements.txt
βββ config_template.py β Configuration templates
βββ test_installation.py β Verify installation
βββ train_model.py β Training CLI interface
βββ inference.py β Evaluate trained models
β
βββ src/ β Core Python modules
β βββ database.py β MySQL interface
β βββ processor.py β Data processing & encoding
β βββ model.py β PyTorch models
β βββ analysis.py β Visualization tools
β βββ __init__.py β Package init
β
βββ tests/ β Unit & integration tests
βββ test_database.py
βββ test_processor.py
βββ test_model.py
βββ test_integration.py
βββ test_inference.py
cd ~/pytink
pip install -r requirements.txt
python train_model.py --db-password YOUR_PASSWORDpython inference.py --db-password YOUR_PASSWORD --model-dir models/TICKER-LIST/TIMESTAMP/cd ~/pytink
python test_installation.pypytest tests/ -v| File | Purpose | Read Time |
|---|---|---|
| PROJECT_SUMMARY.md | Overview, architecture, features | 5 min |
| QUICKSTART.md | Setup, basic usage, troubleshooting | 10 min |
| README.md | Complete technical reference | 20 min |
| EXAMPLES.md | 11 practical code examples | 15 min |
| STRUCTURE.txt | File descriptions & statistics | 5 min |
from database import StockDatabase
db = StockDatabase()
db.connect()
stocks = db.get_random_stocks(count=10)
quotes = db.get_quotes_for_stocks(stock_ids)from processor import PriceProcessor
processor = PriceProcessor(interval_minutes=15)
words = processor.extract_words(quotes, stock_ids)
unique_count, unique_words = processor.count_unique_words(words)from model import StockWordDataset, StockTransformerModel
dataset = StockWordDataset(words, vocab, context_window_size=4)
model = StockTransformerModel(vocab_size=len(vocab))
predictions = model.predict(input_ids)from analysis import plot_training_loss, analyze_prediction_quality
plot_training_loss(history)
analyze_prediction_quality(predictions)Price changes are encoded as letters a-g:
| Letter | Delta | Change |
|---|---|---|
| a | -0.01 | β 1% or more |
| b | -0.005 | β 0.5% |
| c | -0.001 | β 0.1% |
| d | 0.00 | β 0% |
| e | +0.001 | β 0.1% |
| f | +0.005 | β 0.5% |
| g | +0.01 | β 1% or more |
Example: "acgaeb" with 6 stocks = AAPLβ1%, AALβ0.1%, MSFTβ1%, GOOGLβ1%, AMZNβ0.1%, TSLAβ0.5%
MySQL Database
β (fetch historical prices)
Raw Quote Data (timestamps + prices)
β (align timestamps, calculate deltas)
Encoded "Words" (sequences like "acgaeb")
β (create training pairs)
PyTorch Dataset
β (train transformer)
Trained Model
β (evaluate performance)
Loss / Accuracy / Perplexity metrics
- Python: 3.8+
- Database: MySQL 5.7+ (local, port 3306)
- RAM: 4GB minimum (8GB+ recommended)
- GPU: Optional (training faster with CUDA)
- Disk: ~500MB for dependencies
- Read PROJECT_SUMMARY.md
- Run
python test_installation.py - Run
python train_model.py - Review EXAMPLES.md (Example 1-3)
- Review README.md
- Run
pytest tests/ - Modify parameters in QUICKSTART.md
- Try EXAMPLES.md (Example 4-7)
- Study source code in src/
- Implement custom modifications
- Review EXAMPLES.md (Example 8-11)
- Experiment with config_template.py
Q: What's a "word" in this context? A: A sequence of letters representing price changes for all stocks in one time period. Example: "acgaeb" is a 6-stock word.
Q: How many "words" will there be? A: Depends on data. Usually 100-10,000 unique words. With 10 stocks, max theoretical is 9^10 (3.5B), but real data has far fewer.
Q: Can I use different stocks?
A: Yes! Use db.get_random_stocks(count=X) or query specific tickers in database.py.
Q: How long does training take? A: ~1-5 minutes per epoch (10 epochs default), depending on data size and hardware.
Q: How do I save the trained model? A: See EXAMPLES.md (Example 10) for model saving/loading code.
Q: Can I use this for real trading? A: This is a research/educational project. Don't use for real trading without extensive testing.
Q: What if I don't have MySQL? A: The database module handles connections. You'll need MySQL running on localhost:3306 with the "tinker" database.
- Setup: QUICKSTART.md
- Examples: EXAMPLES.md
- Technical Docs: README.md
- Source Code: src/
- Configuration: config_template.py
- Project Overview: PROJECT_SUMMARY.md
- File Structure: STRUCTURE.txt
Verify installation before starting:
python test_installation.pyThis checks:
- β Python packages
- β Project modules
- β Database connection
- β PyTorch setup
- β Model creation
- β Data processing
| File | Type | Purpose |
|---|---|---|
| train_model.py | Script | CLI interface |
| test_installation.py | Script | Installation verification |
| config_template.py | Config | Configuration templates |
| requirements.txt | Config | Dependencies |
| src/database.py | Code | Database access |
| src/processor.py | Code | Data processing |
| src/model.py | Code | PyTorch models |
| src/analysis.py | Code | Visualization |
| tests/ | Tests | Unit & integration tests (pytest) |
| README.md | Docs | Full technical reference |
| QUICKSTART.md | Docs | Setup & usage guide |
| EXAMPLES.md | Docs | 11 code examples |
| PROJECT_SUMMARY.md | Docs | Project overview |
| STRUCTURE.txt | Docs | File descriptions |
- Now: Read PROJECT_SUMMARY.md (5 min)
- Then: Follow QUICKSTART.md (10 min)
- Test: Run
python test_installation.py(1 min) - Try: Run
python train_model.py(5-10 min) - Explore: Check EXAMPLES.md for more use cases
- Installation issues: See QUICKSTART.md Troubleshooting
- Usage questions: See EXAMPLES.md
- Technical details: See README.md
- Configuration: See config_template.py
Educational project for stock price prediction research.
Version: 0.1.0
Created: December 29, 2025
Status: Complete & Ready to Use β