Thanks to visit codestin.com
Credit goes to github.com

Skip to content

kamaja23/trading_llm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TradeBot

Version 1.2 - AMD RX 7800 XT ROCm support

A proof-of-concept implementation demonstrating that a small LLM can learn to predict trading actions (BUY/SELL/HOLD) from tokenized market data sequences.

πŸ†• What's New in v1.1

  • βœ… Fixed: NaN handling errors in indicator calculations
  • βœ… Fixed: Deprecated transformers API compatibility
  • βœ… Added: Accelerate library support for modern training
  • βœ… Added: Comprehensive .gitignore for model files
  • βœ… Optimized: AMD RX 7800 XT support with ROCm and FP16 mixed precision
  • βœ… Improved: Better error messages and debugging

See CHANGELOG.md for complete details.

Project Goal

Demonstrate technical feasibility of using an LLM to learn trading patterns by:

  1. Converting historical price data into a token-based trading language
  2. Fine-tuning a small language model (distilgpt2) on these sequences
  3. Testing if the model can predict trading actions better than random chance

This is a research/educational project, not investment advice.

Architecture Overview

Historical Price Data (SPY via configured market data provider)
    ↓
Token Generator (converts OHLCV β†’ trading language tokens)
    ↓
Training Sequences (e.g., "<SYM_SPY> <TF_DAILY> ST_UpTrend Hi_Volume BUY")
    ↓
Fine-tuned distilgpt2 model
    ↓
Predictions (next token = BUY/SELL/HOLD)

Trading Language Definition

Each sequence is composed of discrete tokens representing market state:

  • Symbol: <SYM_SPY>, <SYM_QQQ>, etc.
  • Timeframe: <TF_DAILY>, <TF_WEEKLY>, <TF_60MIN>
  • State Trend: ST_UpTrend, ST_DownTrend, ST_Flat
  • Volume: Hi_Volume, Lo_Volume, Avg_Volume
  • Indicators: HA_UpCross, HA_DownCross, STO_Cross, STO_NoCross
  • Actions: BUY, SELL, HOLD

Example sequence:

<SYM_SPY> <TF_DAILY> ST_DownTrend Hi_Volume HA_UpCross STO_Cross BUY

Installation

Quick Start (AMD RX 7800 XT with ROCm)

# 1. Install PyTorch with ROCm
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7

# 2. Install dependencies
pip install -r requirements.txt

# 3. Verify GPU
python -c "import torch; print(f'ROCm: {torch.cuda.is_available()}')"

See RX7800XT_SETUP.md for detailed setup guide.

Docker

# Build the image
docker build -t tradebot .

# Run the Streamlit app (mount your trained model)
docker run -p 8501:8501 \
  -v /path/to/your/models:/app/models \
  tradebot

# Run with live market data (set API keys via -e)
docker run -p 8501:8501 \
  -v /path/to/your/models:/app/models \
  -e ALPHA_VANTAGE_API_KEY=your_key \
  tradebot

# Run a different command (train, data gen, test, etc.)
docker run --rm \
  -v /path/to/your/models:/app/models \
  -v /path/to/your/data:/app/data \
  tradebot \
  python src/02_train_model.py

Note: The image does not bundle model or data files β€” they are mounted as volumes.

Usage

Step 1: Generate Training Data

python src/01_generate_training_data.py

This downloads SPY historical data and generates token sequences. Set ALPHA_VANTAGE_API_KEY or STOOQ_API_KEY to use live data; otherwise the generator falls back to the included sample data.

For the Streamlit demo, add a live data key in .env:

cp .env.example .env
# Edit .env and set ALPHA_VANTAGE_API_KEY
.venv/bin/streamlit run app.py

Step 2: Train the Model

python src/02_train_model.py

Fine-tunes distilgpt2 on the trading sequences.

Step 3: Test the Model

python src/03_test_model.py

Evaluates model accuracy on held-out test sequences.

Step 4: Interactive Inference

python src/04_interactive_inference.py

Test the model with custom market state inputs.

Project Structure

tradebot/
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/              # Downloaded price data
β”‚   β”œβ”€β”€ processed/        # Token sequences
β”‚   └── train_test_split/ # Training/test datasets
β”œβ”€β”€ models/
β”‚   └── tradebot/         # Saved model checkpoints
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ 01_generate_training_data.py
β”‚   β”œβ”€β”€ 02_train_model.py
β”‚   β”œβ”€β”€ 03_test_model.py
β”‚   └── 04_interactive_inference.py
└── utils/
    β”œβ”€β”€ data_generator.py
    β”œβ”€β”€ token_definitions.py
    └── indicators.py

Success Metrics (Phase 1)

  • Primary: Model predicts correct action token >50% of the time (better than random 33%)
  • Secondary: Model learns symbol/timeframe context (different predictions for same pattern under different symbols)

Limitations & Risks

  1. Correlation β‰  Causation: Model learns statistical patterns, not market dynamics
  2. Overfitting: May memorize historical sequences without generalizing
  3. No Risk Management: Predictions are binary (BUY/SELL), no position sizing or stop losses
  4. Lookback Bias: All training data is "from the future" relative to prediction point
  5. No Transaction Costs: Ignores fees, slippage, and execution delays

Next Steps (Beyond Hello World)

  • Add more sophisticated indicators (RSI, MACD, Bollinger Bands)
  • Implement proper backtesting with transaction costs
  • Test on multiple symbols and timeframes
  • Paper trading with live data
  • Explore reinforcement learning approach (reward = profit)
  • Adapt to prediction markets (Polymarket)

Timeline

  • Data Generation: 30 minutes
  • Model Training: 1-2 hours (CPU) / 15-30 minutes (GPU)
  • Testing & Evaluation: 30 minutes
  • Total: ~3-4 hours for complete Hello World

License

Educational/Research use only. Not financial advice.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors