feat: Add per-symbol trading controls for SOL and ETH

- Add SymbolSettings interface with enabled/positionSize/leverage fields
- Implement per-symbol ENV variables (SOLANA_*, ETHEREUM_*)
- Add SOL and ETH sections to settings UI with enable/disable toggles
- Add symbol-specific test buttons (SOL LONG/SHORT, ETH LONG/SHORT)
- Update execute and test endpoints to check symbol enabled status
- Add real-time risk/reward calculator per symbol
- Rename 'Position Sizing' to 'Global Fallback' for clarity
- Fix position manager P&L calculation for externally closed positions
- Fix zero P&L bug affecting 12 historical trades
- Add SQL scripts for recalculating historical P&L data
- Move archive TypeScript files to .archive to fix build

Defaults:
- SOL: 10 base × 10x leverage = 100 notional (profit trading)
- ETH:  base × 1x leverage =  notional (data collection)
- Global: 10 × 10x for BTC and other symbols

Configuration priority: Per-symbol ENV > Market config > Global ENV > Defaults
This commit is contained in:
mindesbunister
2025-11-03 10:28:48 +01:00
parent aa8e9f130a
commit 881a99242d
17 changed files with 1996 additions and 108 deletions

View File

@@ -0,0 +1,248 @@
# Per-Symbol Settings Quick Reference
## Access Settings UI
```
http://localhost:3001/settings
```
## Symbol Sections
### 💎 Solana (SOL-PERP)
- **Toggle**: Enable/disable SOL trading
- **Position Size**: Base USD amount (default: 210)
- **Leverage**: Multiplier 1-20x (default: 10x)
- **Notional**: $210 × 10x = $2100 position
- **Use Case**: Primary profit generation
### ⚡ Ethereum (ETH-PERP)
- **Toggle**: Enable/disable ETH trading
- **Position Size**: Base USD amount (default: 4)
- **Leverage**: Multiplier 1-20x (default: 1x)
- **Notional**: $4 × 1x = $4 position
- **Use Case**: Data collection with minimal risk
- **Note**: Drift minimum is ~$38-40 (0.01 ETH)
### 💰 Global Fallback
- **Applies To**: BTC-PERP and any future symbols
- **Position Size**: Default: 54
- **Leverage**: Default: 10x
## Environment Variables
```bash
# SOL Settings
SOLANA_ENABLED=true
SOLANA_POSITION_SIZE=210
SOLANA_LEVERAGE=10
# ETH Settings
ETHEREUM_ENABLED=true
ETHEREUM_POSITION_SIZE=4
ETHEREUM_LEVERAGE=1
# Global Fallback (BTC, etc.)
MAX_POSITION_SIZE_USD=54
LEVERAGE=10
```
## Common Scenarios
### Scenario 1: Disable ETH Trading
1. Go to Settings UI
2. Toggle off "Enable Ethereum Trading"
3. Click "Save Settings"
4. Click "Restart Bot"
5. All ETH signals will now be rejected
### Scenario 2: Increase SOL Position Size
1. Go to Settings UI
2. Adjust "SOL Position Size" slider or input
3. Adjust "SOL Leverage" if needed
4. Review Risk/Reward calculator
5. Click "Save Settings"
6. Click "Restart Bot"
### Scenario 3: Test Single Symbol
1. Go to Settings UI
2. Click "💎 Test SOL LONG" or "⚡ Test ETH LONG"
3. Confirm warning dialog
4. Watch for success/error message
5. Check Position Manager logs
### Scenario 4: Minimal Risk on Both
```bash
SOLANA_ENABLED=true
SOLANA_POSITION_SIZE=4
SOLANA_LEVERAGE=1
ETHEREUM_ENABLED=true
ETHEREUM_POSITION_SIZE=4
ETHEREUM_LEVERAGE=1
```
## Test Buttons
### 💎 SOL Test Buttons
- **Test SOL LONG**: Opens long position with SOL settings
- **Test SOL SHORT**: Opens short position with SOL settings
- Disabled when `SOLANA_ENABLED=false`
### ⚡ ETH Test Buttons
- **Test ETH LONG**: Opens long position with ETH settings
- **Test ETH SHORT**: Opens short position with ETH settings
- Disabled when `ETHEREUM_ENABLED=false`
## Checking Current Settings
### Via UI
```
http://localhost:3001/settings
```
### Via API
```bash
curl http://localhost:3001/api/settings | jq
```
### Via Container Logs
```bash
docker logs trading-bot-v4 | grep "Symbol-specific sizing"
```
### Via Environment
```bash
docker exec trading-bot-v4 printenv | grep -E "SOLANA|ETHEREUM|POSITION_SIZE|LEVERAGE"
```
## Priority Order
When bot receives signal, it checks in this order:
1.**Per-symbol ENV** (`SOLANA_POSITION_SIZE`) - highest priority
2. Market-specific config (code level)
3. Global ENV (`MAX_POSITION_SIZE_USD`) - fallback
4. Default config (code level) - last resort
## Risk Calculator
Each symbol section shows:
- **Max Loss**: Base × Leverage × |SL%|
- **Full Win**: TP1 gain + TP2 gain
- **R:R Ratio**: How much you win vs how much you risk
### Example: SOL with $210 × 10x
- SL: -1.5% → Max Loss: $31.50
- TP1: +0.7% (50% position) → $7.35
- TP2: +1.5% (50% position) → $15.75
- Full Win: $23.10
- R:R: 1:0.73
## Monitoring Per-Symbol Trading
### Check if Symbol Enabled
```bash
# Look for "Symbol trading disabled" errors
docker logs trading-bot-v4 | grep "trading disabled"
```
### Check Position Sizes
```bash
# Look for symbol-specific sizing logs
docker logs trading-bot-v4 | grep "Symbol-specific sizing"
```
### Recent ETH Trades
```bash
docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -c "
SELECT
entry_time::timestamp,
symbol,
direction,
base_position_size,
leverage,
ROUND(position_size, 2) as notional,
ROUND(realized_pnl, 2) as pnl
FROM trades
WHERE symbol = 'ETH-PERP'
AND test_trade = false
ORDER BY entry_time DESC
LIMIT 10;
"
```
### Recent SOL Trades
```bash
docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -c "
SELECT
entry_time::timestamp,
symbol,
direction,
base_position_size,
leverage,
ROUND(position_size, 2) as notional,
ROUND(realized_pnl, 2) as pnl
FROM trades
WHERE symbol = 'SOL-PERP'
AND test_trade = false
ORDER BY entry_time DESC
LIMIT 10;
"
```
## Troubleshooting
### "Symbol trading disabled" Error
**Cause**: Symbol is toggled off in settings
**Solution**:
1. Check settings UI - is toggle on?
2. Check ENV: `docker exec trading-bot-v4 printenv | grep ENABLED`
3. If needed, set `SOLANA_ENABLED=true` or `ETHEREUM_ENABLED=true`
4. Restart bot
### ETH Trades Using $540 Instead of $4
**Cause**: Global ENV override
**Solution**:
1. Check: `docker exec trading-bot-v4 printenv | grep ETHEREUM`
2. Should see: `ETHEREUM_POSITION_SIZE=4`
3. If not, update settings UI and restart
4. Verify logs show: `ETH Position size: $4`
### SOL Trades Using Wrong Size
**Cause**: Global ENV override
**Solution**:
1. Check: `docker exec trading-bot-v4 printenv | grep SOLANA`
2. Should see: `SOLANA_POSITION_SIZE=210`
3. If not, update settings UI and restart
### Changes Not Applied After Save
**Cause**: Bot not restarted
**Solution**:
1. Settings page shows: "Click Restart Bot to apply changes"
2. Must click "🔄 Restart Bot" button
3. Wait ~10 seconds for restart
4. Verify with `docker logs trading-bot-v4`
## Best Practices
1. **Test First**: Use test buttons before enabling live trading
2. **Check Risk**: Review Risk/Reward calculator before changing sizes
3. **Save + Restart**: Always restart after saving settings
4. **Monitor Logs**: Watch logs for first few trades after changes
5. **Verify Sizes**: Check database to confirm actual executed sizes
6. **One at a Time**: Change one symbol setting at a time for easier debugging
## Quick Commands
```bash
# Full system restart
docker compose restart trading-bot
# View real-time logs
docker logs -f trading-bot-v4
# Check ENV variables
docker exec trading-bot-v4 printenv | grep -E "POSITION|LEVERAGE|ENABLED"
# Test settings API
curl http://localhost:3001/api/settings | jq
# Check recent trades by symbol
docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -c \
"SELECT symbol, COUNT(*), ROUND(SUM(realized_pnl),2) FROM trades WHERE test_trade=false GROUP BY symbol;"
```

View File

@@ -0,0 +1,266 @@
# Per-Symbol Settings Implementation
## Overview
Implemented individual enable/disable toggles and position sizing controls for Solana (SOL-PERP) and Ethereum (ETH-PERP) in the settings UI, allowing independent configuration of each trading pair.
## Date
**November 3, 2024**
## Changes Made
### 1. Configuration System (`config/trading.ts`)
**Added:**
- `SymbolSettings` interface with `enabled`, `positionSize`, and `leverage` fields
- `solana` and `ethereum` optional fields to `TradingConfig` interface
- Per-symbol environment variable support:
- `SOLANA_ENABLED` (default: true)
- `SOLANA_POSITION_SIZE` (default: 210)
- `SOLANA_LEVERAGE` (default: 10)
- `ETHEREUM_ENABLED` (default: true)
- `ETHEREUM_POSITION_SIZE` (default: 4)
- `ETHEREUM_LEVERAGE` (default: 1)
**Modified:**
- `getPositionSizeForSymbol()` now returns `{ size, leverage, enabled }`
- Symbol-specific settings take priority over global fallback settings
- Default SOL config: $210 base × 10x = $2100 notional
- Default ETH config: $4 base × 1x = $4 notional (close to Drift's $38-40 minimum)
### 2. Settings UI (`app/settings/page.tsx`)
**Added:**
- New `TradingSettings` interface fields:
- `SOLANA_ENABLED`, `SOLANA_POSITION_SIZE`, `SOLANA_LEVERAGE`
- `ETHEREUM_ENABLED`, `ETHEREUM_POSITION_SIZE`, `ETHEREUM_LEVERAGE`
**New UI Sections:**
1. **"Solana (SOL-PERP)"** section:
- Enable/disable toggle
- Position size input (1-10000 USD)
- Leverage input (1-20x)
- Real-time risk/reward calculator showing max loss, full win, and R:R ratio
2. **"Ethereum (ETH-PERP)"** section:
- Enable/disable toggle
- Position size input (1-10000 USD)
- Leverage input (1-20x)
- Real-time risk/reward calculator
- Note: Drift minimum is ~$38-40 (0.01 ETH)
3. **"Global Position Sizing (Fallback)"** section:
- Renamed from "Position Sizing"
- Clarifies these are defaults for symbols without specific config (e.g., BTC-PERP)
- Yellow warning banner explaining fallback behavior
**Test Buttons:**
- Replaced single LONG/SHORT buttons with symbol-specific tests:
- 💎 Test SOL LONG / 💎 Test SOL SHORT (purple gradient)
- ⚡ Test ETH LONG / ⚡ Test ETH SHORT (blue gradient)
- Buttons disabled when respective symbol trading is disabled
- `testTrade()` function now accepts `symbol` parameter
### 3. Settings API (`app/api/settings/route.ts`)
**GET endpoint:**
- Returns all 6 new per-symbol fields with defaults from ENV
**POST endpoint:**
- Saves per-symbol settings to .env file (implementation via existing `updateEnvFile()`)
### 4. Execute Endpoint (`app/api/trading/execute/route.ts`)
**Added:**
- Symbol enabled check before execution
- Returns 400 error if trading is disabled for the symbol
- Logs enabled status along with position size and leverage
**Example flow:**
```typescript
const { size, leverage, enabled } = getPositionSizeForSymbol(driftSymbol, config)
if (!enabled) {
return NextResponse.json({
success: false,
error: 'Symbol trading disabled',
message: `Trading is currently disabled for ${driftSymbol}...`
}, { status: 400 })
}
```
### 5. Test Endpoint (`app/api/trading/test/route.ts`)
**Added:**
- Symbol enabled check (same as execute endpoint)
- Test trades rejected if symbol is disabled
### 6. Archive Cleanup
**Fixed:**
- Moved `.ts` files from `archive/` to `.archive/` to exclude from TypeScript compilation
- Fixed import paths in archived test files
## Configuration Priority
**Order of precedence:**
1. **Per-symbol ENV vars** (highest priority)
- `SOLANA_POSITION_SIZE`, `SOLANA_LEVERAGE`, `SOLANA_ENABLED`
- `ETHEREUM_POSITION_SIZE`, `ETHEREUM_LEVERAGE`, `ETHEREUM_ENABLED`
2. **Market-specific config** (from `MARKET_CONFIGS` in config/trading.ts)
3. **Global ENV vars** (fallback)
- `MAX_POSITION_SIZE_USD`, `LEVERAGE`
4. **Default config** (lowest priority)
## Use Cases
### Use Case 1: Data Collection on ETH
- Set `ETHEREUM_POSITION_SIZE=4` and `ETHEREUM_LEVERAGE=1`
- Results in $4 notional (minimal risk)
- Collects trade data for strategy optimization
- Note: Actual Drift minimum is ~$38-40, so this will be adjusted up
### Use Case 2: Profit Generation on SOL
- Set `SOLANA_POSITION_SIZE=210` and `SOLANA_LEVERAGE=10`
- Results in $2100 notional position
- Full-scale trading with normal risk
### Use Case 3: Disable ETH Temporarily
- Set `ETHEREUM_ENABLED=false` in settings UI
- All ETH signals from TradingView will be rejected
- SOL trading continues normally
### Use Case 4: Different Risk Profiles
- SOL: High conviction, larger size ($210 × 10x = $2100)
- ETH: Testing strategy, minimal size ($4 × 1x = $4)
- BTC: Falls back to global settings ($54 × 10x = $540)
## Testing Performed
1. ✅ Built successfully with `npm run build`
2. ✅ Docker image built successfully
3. ✅ Container started and restored existing position
4. ✅ No TypeScript errors
5. ✅ Settings UI loads with new sections
6. ✅ Per-symbol test buttons functional
## Next Steps
1. Test symbol enable/disable in production
2. Verify ETH trades use $4 sizing (or Drift's minimum)
3. Confirm SOL trades continue at $210 sizing
4. Monitor that disabled symbols correctly reject signals
5. Update `.env` with desired per-symbol settings
## Breaking Changes
**None** - Fully backward compatible:
- If per-symbol ENV vars not set, falls back to global settings
- All symbols default to `enabled: true`
- Existing ENV vars (`MAX_POSITION_SIZE_USD`, `LEVERAGE`) still work as fallback
## Files Modified
1. `config/trading.ts` - Added SymbolSettings interface and per-symbol ENV support
2. `app/settings/page.tsx` - Added SOL/ETH sections with toggles and test buttons
3. `app/api/settings/route.ts` - Added per-symbol fields to GET/POST
4. `app/api/trading/execute/route.ts` - Added enabled check
5. `app/api/trading/test/route.ts` - Added enabled check
6. `archive/test-drift-v4.ts` - Fixed imports (moved to .archive)
7. `archive/test-position-manager.ts` - Fixed imports (moved to .archive)
## Configuration Reference
### Default Values
```bash
# Solana (SOL-PERP)
SOLANA_ENABLED=true
SOLANA_POSITION_SIZE=210
SOLANA_LEVERAGE=10
# Ethereum (ETH-PERP)
ETHEREUM_ENABLED=true
ETHEREUM_POSITION_SIZE=4
ETHEREUM_LEVERAGE=1
# Global Fallback (BTC, others)
MAX_POSITION_SIZE_USD=54
LEVERAGE=10
```
### Example: Disable ETH, Keep SOL at $2100
```bash
SOLANA_ENABLED=true
SOLANA_POSITION_SIZE=210
SOLANA_LEVERAGE=10
ETHEREUM_ENABLED=false
```
### Example: Both Minimal Sizing
```bash
SOLANA_ENABLED=true
SOLANA_POSITION_SIZE=4
SOLANA_LEVERAGE=1
ETHEREUM_ENABLED=true
ETHEREUM_POSITION_SIZE=4
ETHEREUM_LEVERAGE=1
```
## UI Screenshots
### Settings Page Structure
```
┌─────────────────────────────────────────┐
│ ⚙️ Trading Bot Settings │
├─────────────────────────────────────────┤
│ 📊 Risk Calculator (Global) │
├─────────────────────────────────────────┤
│ 💎 Solana (SOL-PERP) │
│ 🟢 Enable Solana Trading [Toggle] │
│ SOL Position Size: [210] USD │
│ SOL Leverage: [10]x │
│ Risk/Reward: Max Loss $31.50 ... │
├─────────────────────────────────────────┤
│ ⚡ Ethereum (ETH-PERP) │
│ 🟢 Enable Ethereum Trading [Toggle] │
│ ETH Position Size: [4] USD │
│ ETH Leverage: [1]x │
│ Risk/Reward: Max Loss $0.06 ... │
├─────────────────────────────────────────┤
│ 💰 Global Position Sizing (Fallback) │
│ ⚠️ Fallback for BTC and others │
│ Position Size: [54] USD │
│ Leverage: [10]x │
├─────────────────────────────────────────┤
│ ... (other sections) ... │
├─────────────────────────────────────────┤
│ [💾 Save Settings] [🔄 Restart Bot] │
├─────────────────────────────────────────┤
│ 🧪 Test Trades (REAL MONEY) │
│ [💎 Test SOL LONG] [💎 Test SOL SHORT] │
│ [⚡ Test ETH LONG] [⚡ Test ETH SHORT] │
└─────────────────────────────────────────┘
```
## Implementation Notes
### Why Per-Symbol Settings?
1. **ETH for Data Only**: User wants minimal risk on ETH ($4 positions) purely for collecting trade data
2. **SOL for Profits**: User wants normal-sized positions on SOL ($2100) for actual profit generation
3. **Cooldown Independence**: Each symbol has independent cooldown timer (already implemented in previous phase)
4. **Strategy Testing**: Can test different strategies on different symbols simultaneously
### Drift Minimum Constraints
- **SOL**: 0.1 SOL minimum (~$5-15) ✅ Our $210 base exceeds this
- **ETH**: 0.01 ETH minimum (~$38-40) ⚠️ Our $4 target is below this
- **BTC**: 0.0001 BTC minimum (~$10-12) ✅ Our $54 base exceeds this
**Solution for ETH**: The execute endpoint will attempt to open with specified size, and Drift SDK will adjust up to meet minimum. Monitor actual executed sizes in logs.
### Risk/Reward Display
Each symbol section shows real-time risk metrics:
- **Max Loss**: Position size × leverage × |SL%|
- **Full Win**: TP1 gain + TP2 gain
- **R:R Ratio**: Full Win / Max Loss
Example for SOL ($210 × 10x = $2100 notional, -1.5% SL, +0.7% TP1, +1.5% TP2):
- Max Loss: $31.50
- TP1 Gain: $7.35 (50% position)
- TP2 Gain: $15.75 (50% position)
- Full Win: $23.10
- R:R: 1:0.73
## Future Enhancements
1. Add BTC-PERP section (currently uses global fallback)
2. Per-symbol stop loss percentages (currently global)
3. Per-symbol take profit levels (currently global)
4. Import/export symbol configurations
5. Symbol-specific quality score thresholds
6. Historical performance by symbol in analytics dashboard