Your AlphaFlow platform is now fully production-ready with enterprise-grade features for live automated trading!
File: backend/trade_history.py
Features:
- JSON file-based trade database (
trade_history.json) - Comprehensive trade logging with all details
- Performance analytics (win rate, P&L, profit factor)
- Export to CSV for analysis
- Historical queries by strategy, symbol, date range
Every Trade Logged:
- Trade ID, timestamp, strategy
- Symbol, side (buy/sell), shares, price
- Order type, status, commission
- P&L (for sells), entry price, hold duration
- Stop-loss/take-profit levels
- Notes (e.g., "stop_loss_triggered")
- Alpaca order ID
API Endpoints:
GET /api/trades/history- Get trade historyGET /api/trades/performance- Performance statsGET /api/trades/summary- Dashboard summaryGET /api/trades/export/csv- Export to CSV
File: backend/notification_system.py
Notification Channels:
- Console Logging (always enabled)
- Email (SMTP - Gmail, etc.)
- Slack (via webhooks)
Automated Alerts For:
- ✅ Trade executed (buy/sell with P&L)
- ✅ Stop-loss triggered
- ✅ Take-profit triggered
- ✅ Daily loss limit reached
- ✅ Strategy started/stopped
- ✅ Emergency stop executed
- ✅ Trading mode changed (paper ↔ live)
- ✅ System errors
Email Setup (Add to .env):
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your_email@gmail.com
SMTP_PASSWORD=your_app_password
EMAIL_FROM=your_email@gmail.com
EMAIL_TO=recipient1@example.com,recipient2@example.comSlack Setup:
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/WEBHOOK/URLExample Notification:
🕐 2026-01-20 15:30:45
📋 Trade Executed: BUY AAPL
Strategy 'multi_timeframe' executed BUY order for 5 shares of AAPL @ $185.50
📊 Details:
• Strategy: multi_timeframe
• Symbol: AAPL
• Side: BUY
• Shares: 5
• Price: $185.50
• P&L: N/A
File: backend/portfolio_risk.py
Advanced Risk Controls:
Portfolio Heat Tracking:
- Calculates total capital at risk
- Formula:
Sum(position_size × distance_to_stop) - Default limit: 25% of portfolio
- Prevents over-exposure
Correlation Analysis:
- Groups highly correlated assets
- Limits exposure to correlated clusters
- Default: Max 15% in correlated assets (>0.7 correlation)
- Prevents concentration risk
Pre-Trade Risk Checks:
- Validates new positions before entry
- Checks if position would exceed heat limit
- Checks if position would exceed correlation limit
- Returns approval or rejection with reason
Example:
# Check if we can open new position
can_open, reason = portfolio_risk_manager.can_open_new_position(
symbol='AAPL',
position_value=10000,
stop_loss_price=180.00,
entry_price=185.50,
existing_positions=current_positions,
portfolio_value=100000
)
# Returns: (True, "Position approved (Heat: 18.5%, Corr: 12.3%)")| Feature | Status | Description |
|---|---|---|
| Automated Trading | ✅ Ready | 7 strategies execute real trades |
| Position Tracking | ✅ Ready | Real-time P&L, stop-loss monitoring |
| Trade History | ✅ Ready | Full trade logging & analytics |
| Risk Management | ✅ Ready | Daily limits, stop-loss, portfolio heat |
| Email Alerts | ✅ Ready | Trade notifications via email |
| Slack Alerts | ✅ Ready | Notifications to Slack channel |
| Emergency Stop | ✅ Ready | Kill switch stops everything |
| Paper/Live Toggle | ✅ Ready | Switch modes with visual indicator |
| Portfolio Heat | ✅ Ready | Limit total capital at risk |
| Correlation Limits | ✅ Ready | Prevent over-concentration |
| Positions UI | ✅ Ready | Frontend page for positions |
| Trading Mode Indicator | ✅ Ready | Visual paper/live badge |
Update your .env file:
# ==============================================================================
# ALPACA MARKETS API (REQUIRED)
# ==============================================================================
ALPACA_API_KEY=your_live_api_key_here
ALPACA_SECRET_KEY=your_live_secret_key_here
ALPACA_PAPER=false # SET TO FALSE FOR LIVE TRADING
# ==============================================================================
# RISK PARAMETERS
# ==============================================================================
MAX_POSITION_SIZE=10000 # Max $10k per position
MAX_DAILY_LOSS=5000 # Halt trading at $5k daily loss
STOP_LOSS_PERCENT=2.0 # 2% stop-loss
TAKE_PROFIT_PERCENT=5.0 # 5% take-profit
# ==============================================================================
# EMAIL NOTIFICATIONS (RECOMMENDED)
# ==============================================================================
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your_email@gmail.com
SMTP_PASSWORD=your_gmail_app_password
EMAIL_FROM=your_email@gmail.com
EMAIL_TO=your_phone_email@example.com # For SMS via email
# ==============================================================================
# SLACK NOTIFICATIONS (OPTIONAL)
# ==============================================================================
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/WEBHOOK/URLBefore going live, test your notification system:
- Start backend with notifications configured
- Start a strategy in paper mode
- Verify you receive emails/Slack messages for:
- Strategy started
- Trade executed
- Stop-loss triggered (if it happens)
If notifications work, you're ready for live trading!
Recommended Approach:
- Start Small: Use $1,000-$5,000 initial capital
- One Strategy: Start with best-performing strategy only
- One Symbol: Test with a single liquid stock (AAPL, SPY, etc.)
- Monitor Closely: Watch for first few trades
- Verify: Check Alpaca dashboard to confirm orders
- Scale Up: Gradually increase after 1-2 weeks of success
Commands:
# Backend
cd /Volumes/File\ System/Algorithmic\ Trading
source .venv/bin/activate
python3 -m uvicorn backend.main:app --reload
# Frontend
cd frontend
npm run devCheck Trading Mode:
- Open http://localhost:5173
- Look for header badge: "LIVE MODE" (red) = real money
- If you see "PAPER MODE" (blue), go to Settings → Toggle to LIVE
Live Monitoring:
- Positions Page: View all open positions with real-time P&L
- Email/Slack: Receive instant trade notifications
- Alpaca Dashboard: https://app.alpaca.markets/dashboard
- Trade History:
/api/trades/history- all trades logged
Emergency Controls:
- Emergency Stop Button: Red button in header (stops everything)
- Stop Strategy: Click stop button on Strategies page
- Close Position: Click close button on Positions page
| Parameter | Value | Purpose |
|---|---|---|
| Position Size | 1% of portfolio | Max $1,000 per trade on $100k account |
| Stop-Loss | 2x ATR | Automatic exit at 2× Average True Range |
| Daily Loss Limit | 2% | Halt if down 2% in one day |
| Portfolio Heat | 25% max | Max 25% of capital at risk |
| Correlated Exposure | 15% max | Max 15% in correlated assets (>0.7 correlation) |
| Order Type | Market | Guaranteed execution |
| Execution Frequency | 60 seconds | Balance speed vs API rate limits |
If trading with $5,000 capital:
# In .env file
MAX_POSITION_SIZE=500 # 10% of $5k
MAX_DAILY_LOSS=100 # 2% of $5kIf trading with $100,000 capital:
MAX_POSITION_SIZE=5000 # 5% (more conservative)
MAX_DAILY_LOSS=2000 # 2%Subject: [AlphaFlow INFO] Trade Executed: BUY AAPL
Strategy 'multi_timeframe' executed BUY order for 5 shares of AAPL @ $185.50
Details:
• Strategy: multi_timeframe
• Symbol: AAPL
• Side: BUY
• Shares: 5
• Price: $185.50
Subject: [AlphaFlow WARNING] Stop-Loss Triggered: AAPL
Stop-loss triggered for AAPL in strategy 'multi_timeframe'
Details:
• Strategy: multi_timeframe
• Symbol: AAPL
• Entry Price: $185.50
• Stop Price: $180.00
• Loss: $-27.50
Subject: [AlphaFlow CRITICAL] Daily Loss Limit Reached
Daily loss of -2.05% has reached the limit of 2.00%. All trading has been halted.
Details:
• Daily P&L: -2.05%
• Limit: 2.00%
• Action: All strategies stopped
Subject: [AlphaFlow CRITICAL] Trading Mode Changed
Trading mode changed from PAPER to LIVE
Details:
• Previous Mode: PAPER
• New Mode: LIVE
• Warning: REAL MONEY AT RISK
Via API:
# Get recent trades
curl http://localhost:8000/api/trades/history?limit=50
# Get performance stats
curl http://localhost:8000/api/trades/performance
# Get trades for specific strategy
curl http://localhost:8000/api/trades/history?strategy_id=multi_timeframe
# Export to CSV
curl http://localhost:8000/api/trades/export/csv?filename=my_trades.csvPerformance Metrics Included:
- Total trades (wins/losses)
- Win rate (%)
- Total P&L ($)
- Average P&L per trade
- Average win / average loss
- Largest win / largest loss
- Profit factor (gross profit / gross loss)
Trade History Location:
- JSON Database:
trade_history.json - CSV Exports:
trade_history.json in root (runtime)*.csv
- ✅ Tested in paper mode for 2+ weeks successfully
- ✅ All strategies generate correct signals
- ✅ Stop-losses trigger correctly
- ✅ Notifications working (email/Slack)
- ✅ Verified trades appear in Alpaca dashboard
- ✅ Emergency stop button tested
- ✅ Position sizing appropriate for your capital
- ✅ Monitoring plan in place (check daily)
-
⚠️ Starting with small capital ($1k-$5k) -
⚠️ Only running 1-2 best strategies -
⚠️ Have exit plan if not working
Expect Differences from Paper Trading:
- Slippage: Live fills may be worse than paper
- Latency: Orders take time to execute
- Emotions: Harder when real money is at risk
- Market Impact: Large orders move prices
- Costs: Commission, fees, taxes
Typical Performance Adjustment:
- Paper trading return: 15% → Live trading: 10-12%
- Paper win rate: 70% → Live: 60-65%
- Drawdowns usually larger in live trading
1. Not Receiving Notifications
Check email settings:
# Test in Python console
from backend.notification_system import notification_system
notification_system.alert_strategy_started('test', ['AAPL'])
# Should receive email/Slack message2. Orders Not Executing
- Check Alpaca API keys are correct
- Verify market is open (9:30 AM - 4:00 PM ET, Mon-Fri)
- Check account has sufficient buying power
- Review backend logs for errors
3. Emergency Stop Not Working
- Click the red "EMERGENCY STOP" button in header
- Confirm in dialog
- All strategies should stop immediately
- Check
/api/strategies/emergency-stopendpoint
Logs Location:
- Backend logs: Console output
- Trade history:
trade_history.json - System logs: Check terminal output
API Documentation:
- Swagger UI: http://localhost:8000/api/docs
- ReDoc: http://localhost:8000/api/redoc
Your AlphaFlow platform now has:
✅ Enterprise-grade trade logging with full history ✅ Multi-channel notifications (email + Slack + console) ✅ Advanced risk management (heat + correlation limits) ✅ Real-time monitoring with positions page ✅ Emergency controls for quick exits ✅ Production safety features at every level
Start small, monitor closely, and scale gradually!
Last Updated: January 20, 2026 Version: 7.0.0 - Production Ready Status: ✅ LIVE TRADING READY
Good luck and trade safely! 🚀📈💰