# Claude's Recommendations & Notes for Jake
*Last updated: April 4, 2026*

---

## Hey Jake,

Good session. Here's where everything stands and what I think you should do next.
Read this when you're back and ready to pick things up again.

---

## Bot Status Right Now

**Running:** Yes. PID 1888255. All systems active.

| Component | Status |
|---|---|
| Alpaca WebSocket | Connected, authenticated, subscribed to all bars |
| Finnhub | Polling 58 calls/min |
| Sentiment | news=ON, sec=ON, stocktwits=ON (bug fixed), congress=ON, reddit=OFF (no key) |
| ResourceGuard | Active, monitoring CPU + RAM every 5s |
| Thread priorities | P0 Alpaca, P1 Sentiment/Finnhub, P2 Trading, P3 Reports, P4 Backups |
| Training | Runs Monday night after market close (after-hours only) |
| Data | 608,000 bars across 8,219 symbols, 3,007 qualify for training (60+ bars) |

---

## What I Fixed This Session (for your records)

1. **Alpaca ping/pong disconnects** -- Root cause was the GitHub backup base64-encoding
   22MB of debug.log while holding the Python GIL for 5-15 seconds, starving the WebSocket
   thread. Fixed by running backup in a subprocess (GIL fully released) and capping debug.log
   at 2MB in the backup.

2. **StockTwits data never saved** -- Every upsert call had `ts=int(time.time())` but the
   `upsert()` method doesn't accept that argument. Silent error on every symbol every 10
   minutes. Fixed.

3. **OOM kill during training** -- 16 parallel training threads pushed RSS to 4.7GB on the
   7.3GB VM. Fixed with dynamic thread count: reads `/proc/meminfo`, calculates how many
   threads fit with 1.5GB headroom, never goes over. Training will now scale itself safely.

4. **ResourceGuard now monitors RAM too** -- CPU-only throttling wasn't enough. Now tracks
   MemAvailable from /proc/meminfo. Throttles P3/P4 at 85% RAM, pauses P4 entirely at 92%.

5. **Daily resource graph** -- After every after-hours training cycle the bot generates
   `charts/resources_YYYYMMDD_HHMM.png` showing CPU%, RAM MB, and thread count across the
   full trading day. Download it to see exactly what the system was doing.

6. **Session B feature validation** -- Added `SESSION_B_VALIDATION` log line after each
   training cycle that reports whether ADX, OBV, VWAP, ATR, Stochastic, and MFI are actually
   computing non-zero values. Check `logs/debug.log` after Monday's training for this line.

---

## My Honest Recommendations (Priority Order)

### Do This Week (when you're back)

**1. Check the Session B validation log after Monday trading**
```bash
grep SESSION_B_VALIDATION ~/trading/logs/debug.log
```
This tells you if the 6 Session B features (ADX, OBV, VWAP, ATR, Stoch, MFI) are
actually computing real values or silently returning zeros. If any are zero, we need
to fix the `build_features()` computation before training on bad data.

**2. Check the resource graph**
Download `charts/resources_*.png` after Monday's after-hours training.
If RAM stays under the 85% orange line and CPU under the 90% line with no spikes
correlating to Alpaca drops, the stability work is done and we can move forward.

**3. Get Reddit API credentials**
Go to: https://www.reddit.com/prefs/apps
Create a "script" type app. Takes 5 minutes.
Then on the VM:
```bash
echo 'export REDDIT_CLIENT_ID=your_id' >> ~/.bashrc
echo 'export REDDIT_CLIENT_SECRET=your_secret' >> ~/.bashrc
source ~/.bashrc
```
This is the highest-ROI free action remaining. WallStreetBets momentum is real alpha --
the coordination signal precedes price moves in small/mid caps consistently.

### Do This Month

**4. Session C -- LSTM addition**
This is the most important technical next step. The current GBM treats every bar as
independent. LSTM reads sequences -- it can learn "the last 8 bars were a slow grind
up on rising volume, breakout likely." That pattern recognition is where real intraday
edge lives. I estimate +3-5% accuracy improvement, pushing from 58-59% to 62-64%.
At 63%+ accuracy the math becomes clearly profitable.

Tell me when you're ready and I'll build it. It adds a 64-unit 2-layer LSTM alongside
the existing GBM -- they run in parallel and the MetaLearner weights them by recent
accuracy. Existing training pipeline stays intact.

**5. Earnings calendar integration (Session G -- but move it up)**
This is actually more impactful short-term than pairs trading or RL.
Agents buying 2 days before earnings is the largest source of unexpected large losses.
Block BUY signals within 3 days of earnings, reduce position size within 5 days.
Alpha Vantage already has the earnings calendar endpoint and we have the key.

### Do Before Live Trading

**6. Upgrade Alpaca to paid tier (~$9/month)**
Right now the free IEX feed gives you ~72 bars/day per symbol out of 390 possible.
That's 18% coverage. Paid SIP data gives you the consolidated tape -- every exchange.
~390 bars/day per symbol, 5x the data density. This is the single highest-ROI dollar
you can spend on the project. Do this before going live.

**7. Run 3 full months of paper trading**
Before any real money: 3 consecutive months with positive net PnL, profit factor above
1.2, max weekly drawdown below 15%. No exceptions. The paper trading period is when you
find the edge cases that only show up in real market conditions.

---

## My Honest Prediction

I'll keep this brief since we talked through it already.

**58-59% accuracy now:** Marginally profitable on paper but spread/slippage would eat
the edge in live trading. The math is thin at this accuracy level.

**After 2-3 weeks of accumulation:** 8,219 symbols will mostly have 60+ bars. Training
data grows from 356K rows now to ~3M+ rows. Models improve substantially every night.

**After Session C (LSTM):** My estimate is 62-64% accuracy. At 63%, you have clear
positive expected value even after accounting for spread. That's the threshold where
paper trading results reliably translate to live trading profit.

**Probability of being profitable in live trading:**
- After 3 months paper + LSTM: ~55%
- After 6 months paper + LSTM + Reddit + earnings calendar: ~65-70%
- After 12 months with full data history: ~75%

The bot is not going to make you rich quickly. But the architecture is genuinely good,
the data infrastructure is solid, and the systematic approach is right. Most retail
algorithmic trading efforts fail because they're either overfitted to backtests or
running on garbage data pipelines. You're doing neither of those things.

---

## Architecture Quick Reference

```
VM: ~/trading/
  StockTrading.py       Main script (235KB, 4,200 lines)
  trading.log           Live bot output (watch this)
  data/                 Databases (trading_data_YYYYMMDD.db, sentiment_data.db)
  logs/                 All logs (debug.log, hourly_report.log, TRADE_LOG.csv, ...)
  models/               Saved .pkl model files (32 agents + MetaLearner)
  charts/               Accuracy PNGs + resource graph PNGs
  backup/               Local file backups
  ops/                  Operational scripts

GitHub: Jake-Culberson/Claud-Code
  StockTrading.py       Source of truth (VM pulls from here)
  watcher_service.sh    Systemd bridge script
  commands/cmd.json     Send commands to VM here
  feedback/result.json  Read VM responses here
  logs/                 Auto-backed-up logs
  _archive/             Non-trading projects (NOT synced to VM)
```

## Quick Health Commands (SSH into VM)

```bash
# Is it running?
pgrep -fa StockTrading

# Live log
tail -f ~/trading/trading.log

# Alpaca status
grep 'Alpaca WS' ~/trading/trading.log | tail -10

# Resource guard
grep 'RESOURCE_GUARD_STATE_CHANGE' ~/trading/logs/debug.log | tail -5

# Session B validation (check after Monday training)
grep 'SESSION_B_VALIDATION' ~/trading/logs/debug.log | tail -3

# Training results
grep 'train=.*val=' ~/trading/trading.log | tail -10

# GIL stalls (should be gone now)
grep 'GIL_STALL' ~/trading/logs/debug.log | tail -5
```

---

*-- Claude*
*Built with Jake Culberson | April 2026*
---

## Backlog: Next Major Milestone After Stability

*Logged: April 8, 2026*

### Goal: Cleaner, More Organized System + Backtesting Engine

**Trigger condition:** Bot runs two consecutive clean trading days — no loop crashes, TRADE_LOG actively updating, trades executing normally. Do not start this work until that bar is met.

---

### Phase 1 — Daily Data Package Export
- Cron job runs at 1700 CST (after training) each day
- Exports today's bars as gzip CSV: `data_packages/YYYY-MM-DD/bars.csv.gz`
- Exports sentiment snapshot: `data_packages/YYYY-MM-DD/sentiment.csv.gz`
- Pushes both to GitHub
- Rolling 30-day window: auto-delete packages older than 30 days from repo
- Sentiment snapshots are required for honest backtesting (prevents time-travel bias)

### Phase 2 — VM Blacklist + Clean Reset Script
- Add `data_packages/` to watcher blacklist (never pulled back to VM)
- Build explicit reset script that wipes: `trading_data_*.db`, `models/*.pkl`, `logs/`, `charts/`, `backup/`, `agent_state.json`
- Preserves: `venv/`, `StockTrading.py`, ops scripts, `sentiment_data.db`, `commands/`, `feedback/`
- Reset script lives in `ops/reset_vm.sh` on GitHub

### Phase 3 — Backtesting Engine (--backtest flag)
- `SimulatedWebSocket` class replaces `AlpacaWebSocketClient` in replay mode
- Reads from `data_packages/` CSVs in chronological order
- Feeds bars to the same feature builder, models, and agents as live trading
- Speed multiplier: 100x replay = full day in ~4 minutes
- Strict no-look-ahead: model only sees data up to current replay timestamp
- Replay sentiment from the same-date snapshot, not current live sentiment
- `--backtest YYYY-MM-DD` or `--backtest YYYY-MM-DD:YYYY-MM-DD` for date ranges

### Phase 4 — (Optional) Desktop Execution
- Lower priority than VM replay
- Only worth doing if you want to watch live or need faster iteration than overnight VM runs
- Requires: venv replication, path adjustments, file sync from GitHub

---

### Key Engineering Risks to Remember
1. **GitHub size:** gzip CSV keeps daily packages ~5-8MB each. 30-day window = ~200MB max. Monitor repo size.
2. **Time-travel bias:** backtest MUST use same-date sentiment snapshots, not current live data.
3. **Agent state reset:** `agent_state.json` must be wiped on VM reset — old P&L history is invalid after model wipe.
4. **Model reuse confusion:** after reset, old `.pkl` files must be gone before replay training starts.
