chore: Organize workspace structure - move docs, workflows, scripts to subdirectories

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
This commit is contained in:
mindesbunister
2025-10-27 12:59:25 +01:00
parent f8f289232a
commit 14d5de2c64
48 changed files with 37 additions and 14 deletions

557
docs/setup/DOCKER.md Normal file
View File

@@ -0,0 +1,557 @@
# Trading Bot v4 - Docker Deployment Guide
Complete guide for containerized deployment with Docker and Docker Compose.
---
## 📋 Table of Contents
1. [Quick Start](#quick-start)
2. [Production Deployment](#production-deployment)
3. [Development Setup](#development-setup)
4. [Configuration](#configuration)
5. [Docker Commands](#docker-commands)
6. [Troubleshooting](#troubleshooting)
7. [Best Practices](#best-practices)
---
## 🚀 Quick Start
### Prerequisites
```bash
# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Verify installation
docker --version
docker-compose --version
```
### Minimal Setup (Production)
```bash
# 1. Navigate to v4 directory
cd v4
# 2. Create .env file from template
cp .env.example .env
# 3. Edit .env with your credentials
nano .env # or vim, code, etc.
# 4. Build and start
docker-compose up -d
# 5. View logs
docker-compose logs -f trading-bot
# 6. Check status
docker-compose ps
```
---
## 🏭 Production Deployment
### Step 1: Prepare Environment
```bash
cd v4
# Create production .env file
cp .env.example .env
# Edit required fields (minimum required)
DRIFT_WALLET_PRIVATE_KEY=your_base58_private_key
SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_KEY
API_SECRET_KEY=$(openssl rand -hex 32)
PYTH_HERMES_URL=https://hermes.pyth.network
# Trading config (safe defaults)
MAX_POSITION_SIZE_USD=50
LEVERAGE=10
DRY_RUN=false
# Database password (if using PostgreSQL)
POSTGRES_PASSWORD=$(openssl rand -hex 16)
```
### Step 2: Build Image
```bash
# Build with cache
docker-compose build
# Build without cache (clean build)
docker-compose build --no-cache
# Build with progress output
docker-compose build --progress=plain
```
### Step 3: Start Services
```bash
# Start all services in background
docker-compose up -d
# Start specific service
docker-compose up -d trading-bot
# Start with recreation (force restart)
docker-compose up -d --force-recreate
```
### Step 4: Verify Deployment
```bash
# Check running containers
docker-compose ps
# View logs
docker-compose logs -f trading-bot
# Check health
docker-compose exec trading-bot wget -qO- http://localhost:3000/api/health
# Test API
curl -H "Authorization: Bearer YOUR_API_KEY" \
http://localhost:3000/api/trading/positions
```
### Step 5: Monitor
```bash
# Follow logs in real-time
docker-compose logs -f
# View resource usage
docker stats
# Check container details
docker inspect trading-bot-v4
```
---
## 🔧 Development Setup
### Hot Reload Development
```bash
cd v4
# Create dev .env
cp .env.example .env
# Set to devnet for safety
echo "DRIFT_ENV=devnet" >> .env
echo "DRY_RUN=true" >> .env
echo "MAX_POSITION_SIZE_USD=10" >> .env
# Start development container
docker-compose -f docker-compose.dev.yml up
# Rebuild on code changes
docker-compose -f docker-compose.dev.yml up --build
```
### Debug Mode
```bash
# Start with Node.js debugger
docker-compose -f docker-compose.dev.yml up
# Attach debugger in VS Code:
# 1. Open Debug panel (Ctrl+Shift+D)
# 2. Select "Attach to Docker"
# 3. Set breakpoints
# 4. Start debugging
# Or use Chrome DevTools:
# Open: chrome://inspect
# Click: "Configure" → Add localhost:9229
```
### Run Tests in Container
```bash
# Execute tests
docker-compose exec trading-bot npm test
# Run specific test
docker-compose exec trading-bot npx tsx v4/test-price-monitor.ts
# Shell access for manual testing
docker-compose exec trading-bot sh
```
---
## ⚙️ Configuration
### Environment Variables
Create `.env` file in `v4/` directory:
```env
# Required
DRIFT_WALLET_PRIVATE_KEY=your_key
SOLANA_RPC_URL=your_rpc
API_SECRET_KEY=your_secret
# Optional overrides
MAX_POSITION_SIZE_USD=50
LEVERAGE=10
LOG_LEVEL=info
```
See `.env.example` for complete list.
### Docker Compose Override
Create `docker-compose.override.yml` for local customizations:
```yaml
version: '3.9'
services:
trading-bot:
ports:
- "3001:3000" # Use different port
environment:
LOG_LEVEL: debug # More verbose logging
volumes:
- ./custom-config:/app/config # Custom config directory
```
### Resource Limits
Edit `docker-compose.yml`:
```yaml
deploy:
resources:
limits:
cpus: '2' # Max 2 CPU cores
memory: 2G # Max 2GB RAM
reservations:
cpus: '1' # Reserve 1 core
memory: 1G # Reserve 1GB
```
---
## 🎮 Docker Commands Reference
### Container Management
```bash
# Start services
docker-compose up -d
# Stop services
docker-compose stop
# Stop and remove containers
docker-compose down
# Restart specific service
docker-compose restart trading-bot
# View container status
docker-compose ps
# View resource usage
docker stats trading-bot-v4
```
### Logs & Debugging
```bash
# View all logs
docker-compose logs
# Follow logs in real-time
docker-compose logs -f trading-bot
# Last 100 lines
docker-compose logs --tail=100 trading-bot
# Logs since timestamp
docker-compose logs --since 2024-10-23T10:00:00
# Shell access
docker-compose exec trading-bot sh
# Run command in container
docker-compose exec trading-bot node -v
```
### Database Operations
```bash
# Access PostgreSQL CLI
docker-compose exec postgres psql -U postgres -d trading_bot_v4
# Backup database
docker-compose exec postgres pg_dump -U postgres trading_bot_v4 > backup.sql
# Restore database
docker-compose exec -T postgres psql -U postgres trading_bot_v4 < backup.sql
# View database logs
docker-compose logs postgres
```
### Cleanup
```bash
# Stop and remove containers
docker-compose down
# Remove containers and volumes
docker-compose down -v
# Remove everything including images
docker-compose down --rmi all -v
# Clean up unused Docker resources
docker system prune -a
```
### Image Management
```bash
# Build image
docker-compose build
# Rebuild without cache
docker-compose build --no-cache
# Pull latest base images
docker-compose pull
# View images
docker images | grep trading-bot
# Remove old images
docker rmi trading-bot-v4:old
```
---
## 🔍 Troubleshooting
### Container Won't Start
```bash
# Check logs for errors
docker-compose logs trading-bot
# Verify environment variables
docker-compose config
# Check if port is already in use
sudo lsof -i :3000
# Rebuild from scratch
docker-compose down -v
docker-compose build --no-cache
docker-compose up -d
```
### Connection Issues
```bash
# Test internal network
docker-compose exec trading-bot ping postgres
# Check exposed ports
docker-compose port trading-bot 3000
# Verify RPC connection
docker-compose exec trading-bot wget -qO- $SOLANA_RPC_URL
# Test Pyth connection
docker-compose exec trading-bot wget -qO- https://hermes.pyth.network
```
### Performance Issues
```bash
# Check resource usage
docker stats
# View container processes
docker top trading-bot-v4
# Increase resources in docker-compose.yml
deploy:
resources:
limits:
cpus: '2'
memory: 2G
# Restart with new limits
docker-compose up -d
```
### Database Issues
```bash
# Check database health
docker-compose exec postgres pg_isready
# View database connections
docker-compose exec postgres psql -U postgres -c "SELECT * FROM pg_stat_activity"
# Reset database
docker-compose down -v postgres
docker-compose up -d postgres
```
### Permission Issues
```bash
# Fix volume permissions
sudo chown -R 1001:1001 ./logs
sudo chmod -R 755 ./logs
# Run as root (not recommended)
docker-compose run --user root trading-bot sh
```
---
## ✅ Best Practices
### Security
1. **Never commit .env files**
```bash
echo ".env" >> .gitignore
echo ".env.*" >> .gitignore
```
2. **Use secrets for sensitive data**
```yaml
services:
trading-bot:
secrets:
- drift_private_key
secrets:
drift_private_key:
file: ./secrets/drift_key.txt
```
3. **Run as non-root user** (already configured in Dockerfile)
4. **Limit container capabilities**
```yaml
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
```
### Performance
1. **Use multi-stage builds** (already configured)
2. **Mount volumes for persistence**
```yaml
volumes:
- ./logs:/app/logs
- postgres-data:/var/lib/postgresql/data
```
3. **Set resource limits**
```yaml
deploy:
resources:
limits:
memory: 1G
```
4. **Use BuildKit for faster builds**
```bash
DOCKER_BUILDKIT=1 docker-compose build
```
### Monitoring
1. **Health checks** (already configured)
2. **Log rotation**
```yaml
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
```
3. **Metrics collection**
```bash
# Export container metrics
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
```
### Deployment
1. **Use tagged images**
```bash
docker tag trading-bot-v4:latest trading-bot-v4:1.0.0
```
2. **Automated backups**
```bash
# Backup script
docker-compose exec postgres pg_dump -U postgres trading_bot_v4 | \
gzip > backup-$(date +%Y%m%d).sql.gz
```
3. **Blue-green deployment**
```bash
# Start new version on different port
docker-compose -f docker-compose.blue.yml up -d
# Test new version
curl http://localhost:3001/api/health
# Switch traffic (nginx/traefik)
# Stop old version
docker-compose -f docker-compose.green.yml down
```
---
## 🔗 Additional Resources
- **Docker Docs**: https://docs.docker.com
- **Docker Compose**: https://docs.docker.com/compose
- **Node.js Docker**: https://nodejs.org/en/docs/guides/nodejs-docker-webapp
- **Next.js Docker**: https://nextjs.org/docs/deployment#docker-image
---
## 📞 Support
For issues specific to:
- **Docker setup**: Check this guide first
- **Trading bot**: See `../TRADING_BOT_V4_MANUAL.md`
- **Phase 2 features**: See `PHASE_2_COMPLETE.md`
- **Testing**: See `TESTING.md`
---
**Ready to deploy! 🚀**

View File

@@ -0,0 +1,317 @@
# 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:
```bash
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-postgres` on 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
1. Click on **Credentials** in the left sidebar
2. Click **Add Credential**
3. Search for **Postgres** and select it
4. Fill in the connection details (see above)
5. Name it: **Trading Bot Database**
6. Click **Test Connection** to verify
7. 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:**
1. Go to **Workflows****Add Workflow**
2. Click **...** menu → **Import from File**
3. Select `n8n-database-analytics.json`
4. Update PostgreSQL node to use "Trading Bot Database" credential
5. Click **Save**
6. 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:**
1. Import workflow from file
2. Update both PostgreSQL nodes with "Trading Bot Database" credential
3. **Activate** the workflow (toggle in top right)
4. 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:**
1. Import workflow
2. Update all 3 PostgreSQL nodes with credential
3. 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:**
1. Import workflow
2. Update all 3 PostgreSQL nodes with credential
3. Run manually to analyze
## Database Schema Reference
### Trade Table (Main table)
Key fields for analysis:
```sql
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)
```sql
SELECT * FROM "Trade"
WHERE status = 'closed'
AND "isTestTrade" = false
AND "entryTime" >= NOW() - INTERVAL '30 days'
ORDER BY "entryTime" DESC;
```
#### Calculate win rate
```sql
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
```sql
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:
1. Add **Webhook** trigger node
2. Set HTTP Method: POST
3. Note the webhook URL: `http://localhost:8098/webhook/your-unique-id`
In trading bot code (e.g., after trade closes):
```typescript
// 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):
1. Add **HTTP Request** node
2. URL: `http://trading-bot-v4:3000/api/settings`
3. Method: POST
4. Body: Updated config based on analysis
Example - adjust stop loss based on analysis:
```json
{
"STOP_LOSS_PERCENT": 1.5,
"USE_DUAL_STOPS": true
}
```
## Advanced Use Cases
### Machine Learning Integration
Use n8n to:
1. Export trade data to CSV
2. Send to Python ML service via HTTP
3. Receive predictions
4. Update bot configuration
### Multi-Timeframe Analysis
Create workflow that:
1. Queries trades by hour/day/week
2. Identifies patterns at each timeframe
3. Generates trading schedule recommendations
### Risk Management Automation
Build workflow that:
1. Monitors account balance
2. Calculates daily/weekly profit target
3. Auto-pauses bot after hitting target
4. 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 `LIMIT` clause
- Use date range filters to reduce dataset
### Empty Results
- Check `isTestTrade` filter (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:
1. Manual Trigger
2. PostgreSQL node with query:
```sql
SELECT COUNT(*) as total_trades,
MAX("entryTime") as last_trade
FROM "Trade";
```
3. Execute and verify results
### Verify Test Trade Flag
```sql
SELECT
"isTestTrade",
COUNT(*) as count
FROM "Trade"
GROUP BY "isTestTrade";
```
Expected output:
- `false`: Production trades
- `true`: Test trades from /api/trading/test-db
## Next Steps
1. **Import all 4 workflows** and test each one
2. **Activate the Daily Report** workflow for automated tracking
3. **Run Pattern Analysis** to discover your best trading times
4. **Run Stop Loss Analysis** to optimize risk management
5. **Create custom workflows** based on your specific needs
## Support
If you encounter issues:
1. Check n8n logs: `docker logs n8n`
2. Check postgres logs: `docker logs trading-bot-postgres`
3. Test database connection from host: `psql -h localhost -p 5432 -U postgres -d trading_bot_v4`
4. Verify bot is writing to database: Check `/analytics` page 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!

View File

@@ -0,0 +1,213 @@
# n8n Trading Bot v4 Workflow - Setup Instructions
## Step 1: Create API Credential in n8n
1. Go to n8n → **Credentials****New Credential**
2. Search for **"Header Auth"**
3. Configure:
- **Name**: `Trading Bot API Key`
- **Name** (field): `Authorization`
- **Value**: `Bearer 2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb`
4. Click **Save**
## Step 2: Update Your Telegram Credential
Make sure your Telegram Bot credential exists in n8n:
- **Credential Name**: `Telegram Bot`
- **Chat ID**: `579304651` (or change in workflow to your ID)
## Step 3: Import the Workflow
1. Download: `/home/icke/traderv4/n8n-complete-workflow.json`
2. Go to n8n → **Workflows****Import from File**
3. Select the downloaded JSON file
4. Click **Import**
## Step 4: Configure Credentials
After importing, update the credential references:
### For "Check Risk" and "Execute Trade" nodes:
- Click on the node
- Under **Authentication****Credential for Header Auth**
- Select: `Trading Bot API Key` (the one you created in Step 1)
### For all Telegram nodes:
- Click on each Telegram node
- Under **Credential for Telegram API**
- Select your Telegram Bot credential
## Step 5: Test the Webhook
1. **Activate** the workflow
2. Get the webhook URL (shown in the Webhook node)
3. Test with curl:
```bash
curl -X POST "https://your-n8n-instance.com/webhook/tradingview-bot-v4" \\
-H "Content-Type: application/json" \\
-d '{"body": "Buy SOL | Entry: 140.50"}'
```
## Workflow Flow
```
TradingView Alert
[Webhook] Receives signal
[Parse Signal] Extracts data (symbol, direction, timeframe)
[Check Risk] Validates trade (API call)
[Risk Passed?] Decision
↓ ↓
YES NO
↓ ↓
[Execute Trade] [Risk Blocked Message]
[Trade Success?] Decision
↓ ↓
SUCCESS FAILED
↓ ↓
[Success Msg] [Error Msg]
```
## Expected Telegram Notifications
### Success Message:
```
🟢 TRADE OPENED SUCCESSFULLY
Buy SOL | Entry: 140.50
📊 Symbol: SOL-PERP
📈 Direction: LONG
💰 Entry: $140.5000
💵 Size: $500.00
⚡ Leverage: 10x
🎯 Take Profit:
TP1: $141.48 (+0.7%)
TP2: $142.63 (+1.5%)
🛑 Stop Loss:
SL: $138.39 (-1.5%)
📊 Slippage: 0.023%
⏰ 14:32
✅ Position monitored automatically
🤖 Auto-exit at TP/SL levels
```
### Risk Blocked Message:
```
⚠️ TRADE BLOCKED - RISK LIMITS
Buy SOL | Entry: 140.50
📊 Symbol: SOL-PERP
📈 Direction: LONG
🛑 Reason: Daily drawdown limit reached
📝 Details: Check risk management settings
⏰ 14:32
✅ Trade will be allowed when conditions improve
```
### Error Message:
```
🔴 TRADE EXECUTION FAILED
Buy SOL | Entry: 140.50
📊 Symbol: SOL-PERP
📈 Direction: LONG
❌ Error: Drift wallet not configured
⏰ 14:32
⚠️ Check bot logs:
docker logs trading-bot-v4 --tail=50
```
## TradingView Alert Format
Your TradingView alerts should send data in this format:
**Simple format (recommended):**
```
Buy SOL | Entry: 140.50
```
or
```
Sell BTC | Entry: 67890.00
```
The workflow will automatically detect:
- **Symbol**: SOL, BTC, ETH (defaults to SOL if not found)
- **Direction**: Buy/Long → long, Sell/Short → short
- **Timeframe**: Fixed at 5 minutes
## API Endpoints Used
1. **Risk Check**: `http://10.0.0.48:3001/api/trading/check-risk`
- Method: POST
- Body: `{"symbol": "SOL-PERP", "direction": "long"}`
2. **Execute Trade**: `http://10.0.0.48:3001/api/trading/execute`
- Method: POST
- Body: `{"symbol": "SOL-PERP", "direction": "long", "timeframe": "5", "signalStrength": "strong"}`
## Troubleshooting
### "Connection refused" error:
- Check if trading bot is running: `docker ps | grep trading-bot`
- Verify the bot is accessible: `curl http://10.0.0.48:3001/api/trading/positions`
### "Unauthorized" error:
- Check API key credential is set correctly
- Verify the Bearer token format: `Bearer <your-api-key>`
### Telegram not sending:
- Verify your Telegram bot token is valid
- Check chat ID is correct (must be a number)
- Test Telegram node independently
### No response from webhook:
- Make sure workflow is **activated**
- Check webhook path matches your TradingView alert
- Verify n8n is accessible from TradingView
## Quick Commands
```bash
# Check bot status
docker ps | grep trading-bot
# View bot logs
docker logs trading-bot-v4 --tail=50 -f
# Test API directly
curl -X POST http://10.0.0.48:3001/api/trading/check-risk \\
-H "Authorization: Bearer 2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb" \\
-H "Content-Type: application/json" \\
-d '{"symbol":"SOL-PERP","direction":"long"}'
# Check active positions
curl http://localhost:3001/api/trading/positions \\
-H "Authorization: Bearer 2a344f0149442c857fb56c038c0c7d1b113883b830bec792c76f1e0efa15d6bb"
```
## Next Steps
1. ✅ Import workflow
2. ✅ Configure credentials
3. ✅ Activate workflow
4. ⚠️ Configure Drift wallet in trading bot (see main README.md)
5. 🚀 Set up TradingView alerts
6. 💰 Start trading!

View File

@@ -0,0 +1,60 @@
# Settings Management Setup
## Initial Setup
After deploying the trading bot, you need to make the `.env` file writable by the container:
```bash
chmod 666 /home/icke/traderv4/.env
```
This allows the web UI to save settings changes directly to the `.env` file.
## Security Note
The `.env` file contains sensitive information (private keys, API keys). The file permissions of `666` (rw-rw-rw-) allow the containerized application to write settings, but the file is still protected by:
1. File system permissions (only accessible on the host)
2. Docker isolation (container runs as non-root user)
3. Network isolation (not exposed to internet)
For production environments, consider:
- Using secrets management (e.g., Docker secrets, Kubernetes secrets)
- Environment variable injection from secure vaults
- Read-only .env with separate settings database
## How It Works
1. User edits settings in web UI at http://10.0.0.48:3001/settings
2. Click "Save Settings" → API writes to `/app/.env` (mounted from host)
3. Click "Restart Bot" → Watcher restarts container
4. Container reloads settings from updated .env file
5. New configuration takes effect
## Take Profit Configuration
You can now customize BOTH the price level AND the position size for each TP:
### TP1 (Take Profit 1)
- **Price %**: When to trigger the exit (e.g., +0.7% = exit at 0.7% profit)
- **Size %**: What % of position to close (e.g., 60 = close 60% of position)
### TP2 (Take Profit 2)
- **Price %**: When to trigger the second exit (e.g., +1.5% = exit at 1.5% profit)
- **Size %**: What % of REMAINING position to close (e.g., 100 = close rest)
### Example Strategy
**Conservative (70/30 split):**
- TP1: +0.5% price, 70% size → Lock in most profit early
- TP2: +2.0% price, 30% size → Let remainder run
**Aggressive (30/70 split):**
- TP1: +1.0% price, 30% size → Take small profit
- TP2: +3.0% price, 70% size → Let most position run for bigger gains
**Balanced (50/50 split - default):**
- TP1: +0.7% price, 50% size
- TP2: +1.5% price, 50% size
The risk calculator in the web UI will dynamically show expected gains based on your settings.

315
docs/setup/SETUP.md Normal file
View File

@@ -0,0 +1,315 @@
# Trading Bot v4 - Setup Instructions
## <20> Phase Overview
- **Phase 1**: Basic trade execution (✅ COMPLETE)
- **Phase 2**: Automatic exits with real-time monitoring (✅ COMPLETE)
- **Phase 3**: Database, risk manager, notifications (Coming soon)
## <20>🚀 Quick Setup
### 1. Install Dependencies
Since v4 uses the existing project structure, dependencies should already be installed. If not:
```bash
npm install
```
Required packages (should already be in package.json):
- `@solana/web3.js`
- `@coral-xyz/anchor`
- `@drift-labs/sdk`
- `@pythnetwork/price-service-client` (for Phase 2 price monitoring)
### 2. Configure Environment Variables
Add these to your `.env.local`:
```env
# Drift Trading (v4)
DRIFT_WALLET_PRIVATE_KEY=your_base58_private_key_here
DRIFT_ENV=mainnet-beta
API_SECRET_KEY=your_random_secret_for_n8n
# Already configured (from v3)
SOLANA_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_KEY
# Optional: Override default risk parameters
MAX_POSITION_SIZE_USD=1000
LEVERAGE=10
STOP_LOSS_PERCENT=-1.5
TAKE_PROFIT_1_PERCENT=0.7
TAKE_PROFIT_2_PERCENT=1.5
MAX_DAILY_DRAWDOWN=-150
MAX_TRADES_PER_HOUR=6
```
### 3. Get Your Drift Wallet Private Key
```bash
# If using Phantom wallet, export private key from Settings
# Then convert to base58 format (it's usually already in base58)
# Test your key works:
node -e "const {Keypair} = require('@solana/web3.js'); const kp = Keypair.fromSecretKey(Buffer.from('YOUR_KEY', 'base58')); console.log('Wallet:', kp.publicKey.toString())"
```
### 4. Initialize Drift Account
If you haven't already:
1. Go to https://drift.trade
2. Connect your wallet
3. Deposit USDC for trading
4. Initialize your account (one-time setup)
### 5. Test Drift Connection
Create a test script:
```bash
# Create test file
cat > test-drift-v4.ts << 'EOF'
import { initializeDriftService } from './v4/lib/drift/client'
async function test() {
console.log('🧪 Testing Drift connection...')
const drift = await initializeDriftService()
const balance = await drift.getUSDCBalance()
console.log(`💰 USDC Balance: $${balance.toFixed(2)}`)
const positions = await drift.getAllPositions()
console.log(`📊 Active positions: ${positions.length}`)
const health = await drift.getAccountHealth()
console.log(`💊 Free collateral: $${health.freeCollateral.toFixed(2)}`)
await drift.disconnect()
console.log('✅ Test complete!')
}
test().catch(console.error)
EOF
# Run test
npx tsx test-drift-v4.ts
```
### 6. Configure n8n Webhook
1. **Import workflow** from `n8n-workflow-v4.json`
2. **Set environment variables** in n8n:
- `TRADING_BOT_API_URL=https://your-bot-domain.com`
- `API_SECRET_KEY=your_secret_key`
- `TRADINGVIEW_WEBHOOK_SECRET=another_secret`
3. **Activate workflow**
4. **Copy webhook URL**
### 7. Configure TradingView Alert
1. Create alert on your 5-minute chart
2. **Webhook URL**: `https://your-n8n.com/webhook/tradingview-signal?secret=YOUR_SECRET`
3. **Message** (JSON):
```json
{
"action": "{{strategy.order.action}}",
"symbol": "{{ticker}}",
"timeframe": "{{interval}}",
"price": "{{close}}",
"timestamp": "{{timenow}}",
"signal_type": "buy",
"strength": "strong"
}
```
4. Enable **Webhook URL** notification
5. Test alert
### 8. Test Full Flow
```bash
# 1. Start your Next.js server
npm run dev
# 2. Trigger TradingView alert manually
# 3. Check n8n execution logs
# 4. Check your bot API logs
# 5. Check Drift account for position
```
## 🔧 API Endpoints
### Execute Trade
```bash
POST http://localhost:3000/api/trading/execute
Authorization: Bearer YOUR_API_SECRET_KEY
Content-Type: application/json
{
"symbol": "SOLUSDT",
"direction": "long",
"timeframe": "5",
"signalStrength": "strong"
}
```
### Check Risk
```bash
POST http://localhost:3000/api/trading/check-risk
Authorization: Bearer YOUR_API_SECRET_KEY
Content-Type: application/json
{
"symbol": "SOL-PERP",
"direction": "long"
}
```
## 🧪 Testing
### Test with curl (without n8n)
```bash
# Test risk check
curl -X POST http://localhost:3000/api/trading/check-risk \
-H "Authorization: Bearer YOUR_API_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{"symbol":"SOL-PERP","direction":"long"}'
# Test trade execution (CAREFUL - THIS WILL OPEN A REAL POSITION!)
curl -X POST http://localhost:3000/api/trading/execute \
-H "Authorization: Bearer YOUR_API_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"symbol": "SOLUSDT",
"direction": "long",
"timeframe": "5",
"signalStrength": "strong"
}'
```
### Test with Postman
1. Import collection from docs
2. Set environment variables
3. Run tests
## 📁 v4 File Structure
```
v4/
├── config/
│ └── trading.ts # Trading configuration
├── lib/
│ ├── drift/
│ │ ├── client.ts # Drift SDK client ✅
│ │ └── orders.ts # Order execution ✅
│ ├── pyth/
│ │ └── price-monitor.ts # Real-time prices ✅ (Phase 2)
│ └── trading/
│ └── position-manager.ts # Auto-exit logic ✅ (Phase 2)
└── app/
└── api/
└── trading/
├── execute/
│ └── route.ts # Execute trade endpoint ✅
├── check-risk/
│ └── route.ts # Risk check endpoint ✅
└── positions/
└── route.ts # Query positions ✅ (Phase 2)
```
## ✅ Phase 1 Complete
- ✅ Drift Protocol integration
- ✅ Market order execution (open/close)
- ✅ n8n webhook endpoint (execute trade)
- ✅ Basic risk check endpoint
- ✅ Trading configuration
- ✅ TradingView symbol normalization
## ✅ Phase 2 Complete
- ✅ Pyth price monitoring (WebSocket + polling)
- ✅ Position manager (track active trades)
- ✅ Auto-exit logic (TP1/TP2/SL/Emergency)
- ✅ Dynamic SL adjustment (breakeven, profit lock)
- ✅ Multi-position support
- ✅ Positions query endpoint
## 🚧 Coming Next (Phase 3)
- ⏳ Database integration (PostgreSQL/Prisma)
- ⏳ Trade history persistence
- ⏳ Risk manager (daily limits, cooldowns, frequency checks)
- ⏳ Enhanced notifications (Telegram/Discord/Email)
- ⏳ Performance analytics
- ⏳ Web dashboard for monitoring
## 🔐 Security
**IMPORTANT:**
- Never commit `.env.local` to git
- Keep your private key secure
- Use a dedicated trading wallet
- Start with small position sizes
- Test on devnet first if possible
## 💡 Tips
1. **Start small**: Use $100 position size first
2. **Test signals**: Manually trigger alerts to test flow
3. **Monitor closely**: Watch first 5-10 trades carefully
4. **Check Drift UI**: Verify positions at https://drift.trade
5. **Backup strategy**: Have emergency close ready
## 🆘 Troubleshooting
### "Drift service not initialized"
- Make sure DRIFT_WALLET_PRIVATE_KEY is set
- Check wallet has SOL for gas fees
- Verify Drift account is initialized
### "Insufficient collateral"
- Deposit more USDC to Drift account
- Check account health at drift.trade
- Reduce position size
### "Webhook not working"
- Verify n8n workflow is active
- Check API_SECRET_KEY matches
- Test with curl first
### "Order execution failed"
- Check market is active on Drift
- Verify minimum order size
- Check RPC connection
- Review Drift UI for errors
## 📞 Next Steps
1. Test Drift connection
2. Deploy to production
3. Configure n8n webhook
4. Set up TradingView alerts
5. Start with paper trading (small size)
6. Scale up after 10+ successful trades
## 🎓 Resources
- Drift Protocol: https://drift.trade
- Drift Docs: https://docs.drift.trade
- n8n Workflow: See `TRADING_BOT_V4_MANUAL.md`
- Full Manual: See `QUICKSTART_V4.md`
---
**Ready to trade! 🚀**
*Remember: Always start with small position sizes and monitor closely.*

View File

@@ -0,0 +1,151 @@
# Telegram Trading Bot - Quick Reference
## 🚀 Quick Setup (3 steps)
### 1. Import n8n workflow
- Open: http://10.0.0.48:8098
- Import: `telegram-manual-trade-FINAL.json`
- Connect last node to "Check Risk" in Money Machine
- Activate workflow
### 2. Get Telegram Bot Token
Run on your phone:
```
Open Telegram → @BotFather → /newbot → follow instructions
```
You'll get a token like: `123456789:ABCdefGHIjklMNOpqrsTUVwxyz`
### 3. Run setup
```bash
./complete_telegram_setup.sh
```
Paste your bot token when asked.
---
## 📱 Using it on your phone
### Check Position Status
Send `/status` to see your current open position with:
- Real-time P&L (both $ amount and % of account)
- Entry price and current price
- TP1, TP2, and SL levels with status indicators
- Position size and leverage
- Trade age in minutes
Example output:
```
🟢 SOL-PERP 📈 LONG
💰 P&L: $3.50 (+0.70% account)
📊 Price Change: +0.07%
Entry: $142.5000
Current: $142.6000
Targets:
TP1: $143.4975 ⏳
TP2: $144.6375
SL: $140.3625
Position: $500.00 @ 10x
Age: 5 min
```
### Execute Trades
Just send messages to your Telegram chat:
```
buy sol
sell btc
buy eth
sell sol
```
Or use commands:
```
/buySOL
/sellBTC
/buyETH
/status
```
The bot will:
1. ✅ Parse your message
2. ✅ Forward to n8n webhook
3. ✅ n8n sends to your trading bot
4. ✅ Trade executes with risk checks
5. ✅ You get confirmation in Telegram
---
## 🔧 Management
**View logs:**
```bash
docker logs -f telegram-trade-bot
```
**Restart bot:**
```bash
docker restart telegram-trade-bot
```
**Stop bot:**
```bash
docker-compose -f docker-compose.telegram-bot.yml down
```
**Start bot:**
```bash
docker-compose -f docker-compose.telegram-bot.yml --env-file .env.telegram-bot up -d
```
---
## 📋 Configuration Files
- `.env.telegram-bot` - Bot token and webhook URL
- `docker-compose.telegram-bot.yml` - Docker configuration
- `telegram_trade_bot.py` - Bot source code
---
## 🐛 Troubleshooting
**Bot not responding?**
```bash
docker logs telegram-trade-bot
```
**Check if bot is running:**
```bash
docker ps | grep telegram
```
**Test webhook manually:**
```bash
curl -X POST http://10.0.0.48:8098/webhook/manual-trade \
-H "Content-Type: application/json" \
-d '{"text": "buy sol"}'
```
**Check n8n workflow:**
- Is it activated?
- Is the webhook URL correct?
- Is it connected to Check Risk node?
---
## ✅ Supported Commands
From your phone, send any of these:
- `buy sol` / `buy btc` / `buy eth`
- `sell sol` / `sell btc` / `sell eth`
- `long sol` / `short btc` (same as buy/sell)
The bot extracts:
- **Symbol**: SOL, BTC, or ETH (defaults to SOL)
- **Direction**: sell/short = short position, anything else = long position
Commands are case-insensitive: `BUY SOL`, `Buy Sol`, `buy sol` all work!