# ML Stock Trading Bot v8

Autonomous ML-based stock trading system running on Oracle Cloud.

## Repository Structure

```
StockTrading.py          Main trading bot script
watcher_service.sh       Systemd service -- keeps bot alive, executes remote commands
watcher_helper.py        Pushes command results back to GitHub
restart_bot.sh           Manual restart helper
setup.sh                 Initial VM setup script
trading-watcher.service  Systemd unit file
github_watcher.sh        Alternative GitHub polling script
commands/                Remote command queue (watcher reads from here)
feedback/                Command results (watcher writes here)
logs/                    Auto-backed-up log files (trading, debug, hourly report)
_archive/                Non-trading projects -- NOT synced to VM (blacklisted)
```

## VM Folder Structure

```
~/trading/
  StockTrading.py    Main script
  trading.log        Nohup output
  data/              Databases (trading_data_*.db, sentiment_data.db)
  logs/              All log files (debug.log, hourly_report.log, TRADE_LOG.csv, etc.)
  models/            Saved ML model files (.pkl)
  charts/            Hourly accuracy charts (.png)
  backup/            Local file backups
  ops/               Operational scripts (copies of watcher_service.sh etc.)
  commands/          Bridge inbox
  feedback/          Bridge outbox
  venv/              Python virtual environment
```

## Quick Commands

```bash
# Check if bot is running
pgrep -fa StockTrading

# Watch live log
tail -f ~/trading/trading.log

# Check resource usage
grep 'RESOURCE |' ~/trading/logs/debug.log | tail -10

# Full trading mode
python3 StockTrading.py

# Data collection only (test pipelines without trading)
python3 StockTrading.py --collect-only
python3 StockTrading.py --collect-only --sources alpaca,finnhub
```

## Priority System

| Priority | Threads | Behavior |
|---|---|---|
| P0 Critical | Alpaca WebSocket | Never throttled, nice=-5 |
| P1 High | Finnhub, all Sentiment | Never throttled, nice=-5 |
| P2 Normal | Feature rebuild, trading | Never throttled, nice=0 |
| P3 Low | Charts, reports | Throttled at 90% load, nice=10 |
| P4 Background | Backup, state save | Paused at 95% load, subprocess (GIL-free), nice=19 |
