Skip to content

Trailing Stop Monitor - Usage Guide

Overview

The Trailing Stop Monitor is a custom Python script that implements automated trailing stop functionality for Schwab positions. Since Schwab's API doesn't support native trailing stop orders, this script monitors your position in real-time and automatically adjusts stop loss orders to protect your gains.

How It Works

  1. Monitors Price: Checks stock price every 30 seconds (configurable)
  2. Tracks Peak: Records the highest price reached
  3. Calculates Stop: Sets stop price at X% below the peak
  4. Auto-Adjusts: When price rises, stop rises (never falls)
  5. Protects Gains: Automatically sells if price drops X% from peak

Installation

The script is already installed at:

/Users/bertfrichot/mem-agent-mcp/tools/trailing_stop_monitor.py

Dependencies (already installed): - schwab-py - Schwab API client - python-dotenv - Environment variable loading

Usage

Basic Command

uv run python3 tools/trailing_stop_monitor.py SYMBOL QUANTITY TRAIL_PERCENT [OPTIONS]

Examples

# Dry run first (test mode, no real orders)
uv run python3 tools/trailing_stop_monitor.py CABA 15 15 --dry-run

# Live mode (places real orders!)
uv run python3 tools/trailing_stop_monitor.py CABA 15 15

What this does: - Monitors CABA position (15 shares) - Sets stop 15% below highest price reached - Current price: $3.02 → Initial stop: ~$2.57 - If CABA rises to $3.50 → Stop rises to $2.98 - If CABA rises to $4.00 → Stop rises to $3.40 - Stop NEVER moves down - Minimum locked profit: $5.35 (+16.2% gain)

2. Custom Check Interval

# Check every 60 seconds instead of 30
uv run python3 tools/trailing_stop_monitor.py CABA 15 15 --interval 60

3. Different Trail Percentages

# Tight 10% trail (less breathing room, more profit protection)
uv run python3 tools/trailing_stop_monitor.py CABA 15 10

# Loose 20% trail (more breathing room, less profit protection)
uv run python3 tools/trailing_stop_monitor.py CABA 15 20

Command Line Options

Option Description Example
--dry-run Test mode, no real orders --dry-run
--interval SECONDS Check interval (default: 30s) --interval 60
--account HASH Schwab account hash --account ABC123...

Running in Background

Option 1: Terminal Session (Simple)

# Start monitoring
uv run python3 tools/trailing_stop_monitor.py CABA 15 15

# Press Ctrl+C to stop
# Start in background
nohup uv run python3 tools/trailing_stop_monitor.py CABA 15 15 > ~/caba_trailing_stop.log 2>&1 &

# Check if running
ps aux | grep trailing_stop_monitor

# View live log
tail -f ~/caba_trailing_stop.log

# Stop monitoring
pkill -f "trailing_stop_monitor.py CABA"

Option 3: tmux/screen Session (Best for Long-Term)

# Create tmux session
tmux new -s caba_trailing_stop

# Inside tmux, run monitor
uv run python3 tools/trailing_stop_monitor.py CABA 15 15

# Detach: Press Ctrl+B, then D
# Reattach later: tmux attach -t caba_trailing_stop
# Kill session: tmux kill-session -t caba_trailing_stop

State Persistence

The script saves its state to:

~/.trailing_stop_CABA.json

This means: - If you stop and restart the script, it remembers the highest price - Stop orders persist even if script crashes - Safe to restart without losing progress

Monitoring & Logs

Real-Time Output

[2025-10-10 09:30:00] Checking CABA...
   Current: $3.02 (+36.65%)
   📈 New high: $3.02 (previous: $2.95)
   📊 Adjusting stop: $2.51 → $2.57 (+$0.06)
   ✅ Cancelled old stop order 1004123456
   ✅ Stop order placed at $2.57 (Order ID: 1004123457)
   Stop: $2.57 (-14.9% cushion, locks +16.2% gain)

What to Watch For

  • "New high" - Price is rising, stop is being adjusted up
  • "Adjusting stop" - Stop order was successfully updated
  • ⚠️ "Position closed" - Stock was sold (stop triggered or manual sale)
  • "Error" - Check connection, API authentication, or market hours

Safety Features

  1. Dry Run Mode: Test without placing real orders
  2. State Persistence: Survives crashes and restarts
  3. Minimum Adjustment: Only adjusts stop if it moves ≥$0.05 (reduces API calls)
  4. Error Handling: Gracefully handles API errors, network issues
  5. Manual Override: You can still manually trade; script detects position closure

Important Notes

Market Hours

  • Script works 24/7, but orders only execute during market hours:
  • Regular: 9:30 AM - 4:00 PM ET, Monday-Friday
  • No trading on market holidays

Schwab API Limitations

  • Schwab API does NOT support trailing stop orders natively
  • This script implements trailing stop logic by:
  • Monitoring price continuously
  • Placing/canceling regular STOP orders
  • Adjusting stops as price rises

Best Practices

  1. Always test with --dry-run first
  2. Check existing stop orders before starting (script manages its own)
  3. Monitor the first hour to ensure it's working correctly
  4. Use tmux/screen for long-running sessions
  5. Set trail % based on stock volatility:
  6. Volatile stocks (biotech): 15-20%
  7. Stable stocks (blue chips): 8-12%

Troubleshooting

"Account hash not found"

# Check if SCHWAB_ACCOUNT_HASH is in .env
grep SCHWAB_ACCOUNT_HASH ~/Claude-Micro-Cap-Experiment/.env

# If missing, get it from Schwab API:
uv run python3 tools/schwab_fetcher.py | grep "Account:"

"Token expired"

# Re-authenticate
uv run python3 tools/schwab_setup.py

"Position not found"

  • Verify you actually own the stock
  • Check spelling of symbol
  • Ensure position hasn't been closed

Script Stops Unexpectedly

  • Check ~/.trailing_stop_SYMBOL.json for saved state
  • Review error logs
  • Ensure stable internet connection
  • Verify Schwab API isn't having issues

Example: CABA 15% Trailing Stop

Current Position

  • Symbol: CABA
  • Quantity: 15 shares
  • Entry: $2.21
  • Current: $3.02
  • Gain: +$12.15 (+36.65%)

Trail Setup (15%)

uv run python3 tools/trailing_stop_monitor.py CABA 15 15

What Happens

CABA Price Stop Price Cushion Locked Profit
$3.02 $2.57 15% $5.35 (+16%)
$3.50 $2.98 15% $11.55 (+35%)
$4.00 $3.40 15% $17.85 (+54%)
$4.50 $3.83 15% $24.30 (+73%)
Drops to $3.40 SELLS @ $3.40 - $17.85 profit locked

Risk/Reward

  • Upside: Unlimited (stop follows price up)
  • Downside: Protected at +16% minimum gain
  • Trade-off: Could get stopped out on -15% dip, even if stock recovers

Advanced: Multiple Positions

To monitor multiple positions simultaneously:

# Terminal 1: CABA
tmux new -s caba
uv run python3 tools/trailing_stop_monitor.py CABA 15 15
# Ctrl+B, D to detach

# Terminal 2: REI
tmux new -s rei
uv run python3 tools/trailing_stop_monitor.py REI 300 12
# Ctrl+B, D to detach

# Terminal 3: NVDA
tmux new -s nvda
uv run python3 tools/trailing_stop_monitor.py NVDA 1 10
# Ctrl+B, D to detach

# View all sessions
tmux ls

# Reattach to any session
tmux attach -t caba

Questions?

  • Script location: /Users/bertfrichot/mem-agent-mcp/tools/trailing_stop_monitor.py
  • State files: ~/.trailing_stop_SYMBOL.json
  • Logs: Check terminal output or redirect to file with > logfile.log 2>&1

Ready to protect your CABA gains! 🚀