- Add Pyth Network price monitoring (WebSocket + polling fallback) - Add Position Manager with automatic exit logic (TP1/TP2/SL) - Implement dynamic stop-loss adjustment (breakeven + profit lock) - Add real-time P&L tracking and multi-position support - Create comprehensive test suite (3 test scripts) - Add 5 detailed documentation files (2500+ lines) - Update configuration to $50 position size for safe testing - All Phase 2 features complete and tested Core Components: - v4/lib/pyth/price-monitor.ts - Real-time price monitoring - v4/lib/trading/position-manager.ts - Autonomous position management - v4/app/api/trading/positions/route.ts - Query positions endpoint - v4/test-*.ts - Comprehensive testing suite Documentation: - PHASE_2_COMPLETE_REPORT.md - Implementation summary - v4/PHASE_2_SUMMARY.md - Detailed feature overview - v4/TESTING.md - Testing guide - v4/QUICKREF_PHASE2.md - Quick reference - install-phase2.sh - Automated installation script
8.4 KiB
8.4 KiB
n8n Setup Guide for Trading Bot v4
Quick Start
Option 1: n8n Cloud (Easiest)
- Sign up at https://n8n.io/cloud
- Import workflow:
- Go to Workflows → Import from File
- Upload
n8n-workflow-v4.json
- Set environment variables:
- Click Settings → Variables
- Add these variables:
TRADINGVIEW_WEBHOOK_SECRET=your_secret_key_here
API_SECRET_KEY=your_api_key_here
TRADING_BOT_API_URL=https://your-bot-domain.com
TELEGRAM_CHAT_ID=your_telegram_chat_id
DISCORD_WEBHOOK_URL=your_discord_webhook
-
Configure Telegram credentials:
- Go to Credentials → Add Credential
- Select Telegram
- Add your bot token from @BotFather
-
Activate workflow:
- Toggle Active switch to ON
- Copy webhook URL
- Add to TradingView alert
Option 2: Self-Hosted Docker
# Create docker-compose.yml
cat > docker-compose.yml << 'EOF'
version: '3'
services:
n8n:
image: n8nio/n8n
restart: always
ports:
- "5678:5678"
environment:
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=your_password_here
- N8N_HOST=your-domain.com
- N8N_PORT=5678
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://your-domain.com/
- GENERIC_TIMEZONE=America/New_York
volumes:
- ~/.n8n:/home/node/.n8n
EOF
# Start n8n
docker-compose up -d
# Check logs
docker-compose logs -f n8n
Access at: http://localhost:5678
Option 3: npm Global Install
npm install -g n8n
n8n start
Environment Variables Setup
In n8n Cloud/UI
- Go to Settings → Environments
- Add these variables:
| Variable | Value | Description |
|---|---|---|
TRADINGVIEW_WEBHOOK_SECRET |
random_secret_123 |
Secret for TradingView webhooks |
API_SECRET_KEY |
your_api_key |
Auth for your trading bot API |
TRADING_BOT_API_URL |
https://your-bot.com |
Your Next.js bot URL |
TELEGRAM_CHAT_ID |
123456789 |
Your Telegram chat ID |
DISCORD_WEBHOOK_URL |
https://discord.com/api/webhooks/... |
Discord webhook URL |
In Docker
Add to docker-compose.yml under environment::
- TRADINGVIEW_WEBHOOK_SECRET=random_secret_123
- API_SECRET_KEY=your_api_key
- TRADING_BOT_API_URL=https://your-bot.com
- TELEGRAM_CHAT_ID=123456789
- DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/...
In npm Install
Create .env file:
export TRADINGVIEW_WEBHOOK_SECRET=random_secret_123
export API_SECRET_KEY=your_api_key
export TRADING_BOT_API_URL=https://your-bot.com
export TELEGRAM_CHAT_ID=123456789
export DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/...
Then run:
source .env
n8n start
Import Workflow
Method 1: UI Import
- Open n8n
- Click Workflows → Import from File
- Select
n8n-workflow-v4.json - Click Import
Method 2: API Import
curl -X POST http://localhost:5678/rest/workflows/import \
-H "Content-Type: application/json" \
-u admin:your_password \
-d @n8n-workflow-v4.json
Configure Credentials
Telegram Bot
-
Create bot with @BotFather on Telegram:
/newbot Trading Bot V4 trading_bot_v4_bot -
Get bot token from BotFather
-
Get your chat ID:
- Send a message to your bot
- Visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Find
"chat":{"id":123456789}
-
Add credential in n8n:
- Go to Credentials → Add Credential
- Select Telegram
- Paste bot token
- Save
Discord Webhook (Optional)
-
Create webhook in Discord:
- Go to Server Settings → Integrations → Webhooks
- Click New Webhook
- Name: "Trading Bot V4"
- Copy webhook URL
-
Add to n8n:
- Paste URL in
DISCORD_WEBHOOK_URLvariable
- Paste URL in
Test Workflow
Test with Manual Trigger
- Open workflow in n8n
- Click Execute Workflow
- Send test webhook:
curl -X POST https://your-n8n.com/webhook/tradingview-signal?secret=YOUR_SECRET \
-H "Content-Type: application/json" \
-d '{
"action": "buy",
"symbol": "SOLUSDT",
"timeframe": "5",
"price": "100.50",
"timestamp": "2025-10-23T10:00:00Z",
"signal_type": "buy",
"strength": "strong",
"strategy": "5min_scalp_v4"
}'
- Check execution log in n8n
Test from TradingView
- Create alert in TradingView
- Set webhook URL:
https://your-n8n.com/webhook/tradingview-signal?secret=YOUR_SECRET - Trigger alert manually
- Check n8n execution log
Webhook URL Format
Your webhook URL will be:
n8n Cloud:
https://YOUR_USERNAME.app.n8n.cloud/webhook/tradingview-signal?secret=YOUR_SECRET
Self-hosted:
https://your-domain.com/webhook/tradingview-signal?secret=YOUR_SECRET
Local testing:
http://localhost:5678/webhook-test/tradingview-signal?secret=YOUR_SECRET
Monitoring & Debugging
View Execution Logs
- Go to Executions in n8n
- Click on any execution to see:
- Input data
- Output from each node
- Errors
- Execution time
Enable Detailed Logging
Add to docker-compose.yml:
- N8N_LOG_LEVEL=debug
- N8N_LOG_OUTPUT=console
Webhook Testing Tools
Use these to test webhook:
Postman:
POST https://your-n8n.com/webhook/tradingview-signal?secret=YOUR_SECRET
Headers:
Content-Type: application/json
Body:
{
"action": "buy",
"symbol": "SOLUSDT",
"timeframe": "5",
"price": "100.50"
}
curl:
curl -X POST 'https://your-n8n.com/webhook/tradingview-signal?secret=YOUR_SECRET' \
-H 'Content-Type: application/json' \
-d '{"action":"buy","symbol":"SOLUSDT","timeframe":"5","price":"100.50"}'
Common Issues
Webhook Not Receiving Data
Check:
- Workflow is activated (toggle is ON)
- Webhook URL is correct
- Secret parameter is included
- TradingView alert is active
Test:
# Test with curl
curl -v -X POST 'https://your-n8n.com/webhook/tradingview-signal?secret=test123' \
-H 'Content-Type: application/json' \
-d '{"test":"data"}'
Authentication Errors
Check:
API_SECRET_KEYmatches in n8n and Next.js- Authorization header is sent correctly
- Trading bot API is accessible
Test:
# Test API directly
curl -X POST https://your-bot.com/api/trading/check-risk \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"symbol":"SOL-PERP","direction":"long"}'
Telegram Not Sending
Check:
- Bot token is correct
- Chat ID is correct
- You sent a message to bot first
- Bot is not blocked
Test:
# Send test message
curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage" \
-d "chat_id=<YOUR_CHAT_ID>" \
-d "text=Test message"
Security Best Practices
- Use strong secrets: Generate with
openssl rand -hex 32 - Enable HTTPS: Always use HTTPS in production
- Restrict access: Use firewall rules to limit access
- Rotate keys: Change secrets regularly
- Monitor logs: Check for suspicious activity
n8n Advanced Features for Trading Bot
Useful n8n Nodes
- Function Node: Custom JavaScript logic
- HTTP Request: Call external APIs
- Telegram: Send notifications
- Discord: Alternative notifications
- Email: Send email alerts
- Cron: Schedule tasks (daily reports, cleanup)
- If Node: Conditional logic
- Switch Node: Multiple conditions
- Merge Node: Combine data streams
- Set Node: Transform data
Add Daily Report Workflow
Create a separate workflow:
Cron (daily 11:59 PM)
→ HTTP Request (GET /api/trading/daily-stats)
→ Function (format report)
→ Telegram (send summary)
Add Position Monitoring
Create monitoring workflow:
Cron (every 5 minutes)
→ HTTP Request (GET /api/trading/positions)
→ If (positions exist)
→ HTTP Request (check prices)
→ Function (calculate P&L)
→ If (alert condition met)
→ Telegram (send alert)
Next Steps
- ✅ Import workflow to n8n
- ✅ Configure environment variables
- ✅ Set up Telegram bot
- ✅ Test webhook with curl
- ✅ Connect TradingView alert
- ✅ Test full flow
- ✅ Set up monitoring
Resources
- n8n Docs: https://docs.n8n.io
- n8n Community: https://community.n8n.io
- Webhook Testing: https://webhook.site
- TradingView Alerts: https://www.tradingview.com/support/solutions/43000529348
Ready to automate! 🚀