Organization: - Created docs/ with setup/, guides/, history/ subdirectories - Created workflows/ with trading/, analytics/, telegram/, archive/ subdirectories - Created scripts/ with docker/, setup/, testing/ subdirectories - Created tests/ for TypeScript test files - Created archive/ for unused reference files Moved files: - 17 documentation files → docs/ - 16 workflow JSON files → workflows/ - 10 shell scripts → scripts/ - 4 test files → tests/ - 5 unused files → archive/ Updated: - README.md with new file structure and documentation paths Deleted: - data/ (empty directory) - screenshots/ (empty directory) Critical files remain in root: - telegram_command_bot.py (active bot - used by Dockerfile) - watch-restart.sh (systemd service dependency) - All Dockerfiles and docker-compose files - All environment files Validation: Containers running (trading-bot-v4, telegram-trade-bot, postgres) API responding (positions endpoint tested) Telegram bot functional (/status command tested) All critical files present in root No code changes - purely organizational. System continues running without interruption. Recovery: git revert HEAD or git reset --hard cleanup-before
9.0 KiB
n8n Database Integration Setup Guide
Overview
This guide shows you how to connect your n8n instance to the Trading Bot v4 PostgreSQL database for automated analysis and insights.
Database Connection Details
⚠️ IMPORTANT: n8n is on a different Docker network than the trading bot postgres. You MUST use the host machine IP or localhost.
✅ CORRECT Connection (n8n is on different network)
Type: PostgreSQL
Host: host.docker.internal (or your machine's IP like 172.18.0.1)
Port: 5432
Database: trading_bot_v4
User: postgres
Password: postgres
SSL Mode: disable
Alternative: Use localhost with host networking
If host.docker.internal doesn't work, find your docker network gateway:
docker inspect n8n --format '{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}'
# Result: 172.18.0.1 (use this as Host)
Network Details (for reference)
- n8n network:
compose_files_default(172.18.0.0/16) - Trading bot network:
traderv4_trading-net(172.28.0.0/16) - PostgreSQL container:
trading-bot-postgreson traderv4_trading-net (172.28.0.2) - PostgreSQL exposed port: 5432 → localhost:5432
Since they're on different networks, use the host machine as bridge.
Setup Steps
1. Access n8n
Open your browser and navigate to:
http://localhost:8098
2. Create PostgreSQL Credential
- Click on Credentials in the left sidebar
- Click Add Credential
- Search for Postgres and select it
- Fill in the connection details (see above)
- Name it: Trading Bot Database
- Click Test Connection to verify
- Click Save
3. Import Workflows
Four pre-built workflow templates are ready in your workspace:
A. Database Analytics (n8n-database-analytics.json)
Purpose: Query and analyze closed trades with statistical calculations
Features:
- Fetches last 100 closed trades
- Calculates win rate, P&L, profit factor
- Breaks down by symbol, direction, and exit reason
- Identifies best performing setups
To import:
- Go to Workflows → Add Workflow
- Click ... menu → Import from File
- Select
n8n-database-analytics.json - Update PostgreSQL node to use "Trading Bot Database" credential
- Click Save
- Click Execute Workflow to test
B. Daily Trading Report (n8n-daily-report.json)
Purpose: Automated daily summary at midnight (stores in DailyStats table)
Features:
- Runs automatically at 00:05 every day
- Calculates yesterday's performance
- Breaks down by symbol
- Stores in DailyStats table for historical tracking
- Calculates win rate, profit factor, avg hold time
To import:
- Import workflow from file
- Update both PostgreSQL nodes with "Trading Bot Database" credential
- Activate the workflow (toggle in top right)
- Will run automatically at midnight
C. Pattern Analysis (n8n-pattern-analysis.json)
Purpose: Discover which times/conditions produce best results
Features:
- Hourly Analysis: Which hours have best win rate
- Daily Analysis: Which days perform best
- Hold Time Analysis: Optimal position duration
- Generates actionable recommendations
Example insights:
- "Focus trading around 14:00-16:00 (75% win rate)"
- "Trade more on Tuesday, avoid Friday"
- "Target exits in 15-30 min range"
To import:
- Import workflow
- Update all 3 PostgreSQL nodes with credential
- Run manually to see insights
D. Stop Loss Analysis (n8n-stop-loss-analysis.json)
Purpose: Optimize stop loss distances and understand exit patterns
Features:
- Exit reason breakdown (stopped out vs targets hit)
- Stop distance effectiveness (tight vs wide stops)
- Symbol-specific stop performance
- Calculates profit factor (avg win / avg loss)
- Recommendations for stop optimization
Example insights:
- "⚠️ High stop hit rate - consider wider stops"
- "💡 Normal (1-1.5%) stops perform 45% better than tight stops"
- "✅ Risk/reward ratio is positive"
To import:
- Import workflow
- Update all 3 PostgreSQL nodes with credential
- Run manually to analyze
Database Schema Reference
Trade Table (Main table)
Key fields for analysis:
id, symbol, direction, entryPrice, exitPrice, quantity,
notionalSize, realizedPnL, realizedPnLPercent,
entryTime, exitTime, holdTimeSeconds,
stopLossPrice, takeProfitPrice1, takeProfitPrice2,
exitReason, status, isTestTrade
Common Queries
Get all closed trades (last 30 days)
SELECT * FROM "Trade"
WHERE status = 'closed'
AND "isTestTrade" = false
AND "entryTime" >= NOW() - INTERVAL '30 days'
ORDER BY "entryTime" DESC;
Calculate win rate
SELECT
COUNT(*) as total_trades,
COUNT(CASE WHEN "realizedPnL" > 0 THEN 1 END) as wins,
ROUND(COUNT(CASE WHEN "realizedPnL" > 0 THEN 1 END)::numeric / COUNT(*) * 100, 2) as win_rate
FROM "Trade"
WHERE status = 'closed' AND "isTestTrade" = false;
Best performing symbols
SELECT
symbol,
COUNT(*) as trades,
SUM("realizedPnL") as total_pnl,
AVG("realizedPnL") as avg_pnl
FROM "Trade"
WHERE status = 'closed' AND "isTestTrade" = false
GROUP BY symbol
ORDER BY total_pnl DESC;
Workflow Automation Ideas
1. Performance Alerts
Trigger: Schedule (every 6 hours) Query: Check if win rate drops below 50% in last 24h Action: Send Telegram notification to pause trading
2. Best Setup Detector
Trigger: Manual or daily Query: Find symbol + direction + time combinations with >70% win rate Action: Save insights to config for bot to prioritize
3. Drawdown Monitor
Trigger: After each trade (webhook) Query: Calculate rolling 10-trade P&L Action: Auto-reduce position size if in drawdown
4. Exit Optimization
Trigger: Weekly Query: Compare TP1 vs TP2 hit rates and P&L Action: Recommend adjustment to TP levels
Connecting Workflows to Trading Bot
Webhook from Trading Bot to n8n
In your n8n workflow:
- Add Webhook trigger node
- Set HTTP Method: POST
- Note the webhook URL:
http://localhost:8098/webhook/your-unique-id
In trading bot code (e.g., after trade closes):
// Send trade data to n8n for analysis
await fetch('http://localhost:8098/webhook/your-unique-id', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tradeId: trade.id,
symbol: trade.symbol,
pnl: trade.realizedPnL,
exitReason: trade.exitReason
})
});
Update Bot Config from n8n
In your n8n workflow (after analysis):
- Add HTTP Request node
- URL:
http://trading-bot-v4:3000/api/settings - Method: POST
- Body: Updated config based on analysis
Example - adjust stop loss based on analysis:
{
"STOP_LOSS_PERCENT": 1.5,
"USE_DUAL_STOPS": true
}
Advanced Use Cases
Machine Learning Integration
Use n8n to:
- Export trade data to CSV
- Send to Python ML service via HTTP
- Receive predictions
- Update bot configuration
Multi-Timeframe Analysis
Create workflow that:
- Queries trades by hour/day/week
- Identifies patterns at each timeframe
- Generates trading schedule recommendations
Risk Management Automation
Build workflow that:
- Monitors account balance
- Calculates daily/weekly profit target
- Auto-pauses bot after hitting target
- Resumes trading next day/week
Troubleshooting
Connection Refused
- Verify PostgreSQL container is running:
docker ps | grep postgres - Check port mapping:
docker port trading-bot-postgres - Ensure n8n and postgres are on same Docker network
Query Timeouts
- Add indexes to frequently queried columns
- Limit result sets with
LIMITclause - Use date range filters to reduce dataset
Empty Results
- Check
isTestTradefilter (you may want to include test trades for testing) - Verify date range in queries
- Ensure trades have been closed (
status = 'closed')
Testing Your Setup
Quick Test Query
In n8n, create a workflow with:
- Manual Trigger
- PostgreSQL node with query:
SELECT COUNT(*) as total_trades,
MAX("entryTime") as last_trade
FROM "Trade";
- Execute and verify results
Verify Test Trade Flag
SELECT
"isTestTrade",
COUNT(*) as count
FROM "Trade"
GROUP BY "isTestTrade";
Expected output:
false: Production tradestrue: Test trades from /api/trading/test-db
Next Steps
- Import all 4 workflows and test each one
- Activate the Daily Report workflow for automated tracking
- Run Pattern Analysis to discover your best trading times
- Run Stop Loss Analysis to optimize risk management
- Create custom workflows based on your specific needs
Support
If you encounter issues:
- Check n8n logs:
docker logs n8n - Check postgres logs:
docker logs trading-bot-postgres - Test database connection from host:
psql -h localhost -p 5432 -U postgres -d trading_bot_v4 - Verify bot is writing to database: Check
/analyticspage in web UI
Pro Tip: Start with the Pattern Analysis workflow to understand your trading patterns, then use those insights to create automated optimization workflows!