Commit Graph

33 Commits

Author SHA1 Message Date
mindesbunister
0558111ded feat: Update Money Machine workflow for multi-timeframe data collection
- Replaced Parse Signal node with enhanced version (regex supports buy 5, buy 15, buy 1h, buy 4h, buy D)
- Updated Execute Trade jsonBody to include all metrics: atr, adx, rsi, volumeRatio, pricePosition, indicatorVersion
- Removed 5min Chart Only filter - execute endpoint now handles routing automatically
- Flow: Webhook → Parse Signal Enhanced → Execute Trade → Telegram
- 5min signals execute trades, 15m/1h/4h/daily save to BlockedSignal for analysis
- User will manually update n8n nodes after JSON changes
2025-11-19 10:42:47 +01:00
mindesbunister
f65af9530f feat: Enhanced timeframe extraction for multi-timeframe data collection
Updated n8n Parse Signal Enhanced to support multiple timeframe formats:
- 5m, 15m timeframes (buy 5, buy 15)
- Hourly: buy 60, buy 1h → 60
- 4-hour: buy 240, buy 4h → 240
- Daily: buy D, buy 1d → D
- Weekly/Monthly: buy W, buy M

Fixes:
- Default timeframe changed from '15' to '5' (5min is production)
- Added indicator version extraction (IND:v8)
- Proper conversion of hour/day notation to minutes
- Case-insensitive matching for D/W/M

Related: Multi-timeframe data collection system (execute endpoint saves
non-5min signals to BlockedSignal for cross-timeframe analysis). Now
15min signals from TradingView will be properly parsed and saved.

Files:
- workflows/trading/parse_signal_enhanced.json (updated regex + conversion)
- .github/copilot-instructions.md (documented supported formats)
2025-11-19 10:33:12 +01:00
mindesbunister
067b0f7e6f feat: Add momentum confirmation to v8 - filter rapid flip-flops
V8 improvements to handle small bounces in strong trends:
- Increased flip threshold: 0.5% → 0.8% (stronger move required)
- Added confirmBars: 2 bars required after threshold breach
- Momentum tracking: Counts consecutive bars in new direction
- Anti-whipsaw: Flip only after 3 consecutive bars (confirmBars+1) beyond threshold

Example: In downtrend, price must close >0.8% above line for 3 bars before flipping bullish
This filters 1-2 bar bounces that don't change the macro trend

Result: Far fewer false reversals during strong directional moves
2025-11-18 11:23:17 +01:00
mindesbunister
039363d5b5 fix: Update v8 indicator version tracking and comments
- Changed indicatorVer from 'v6' to 'v8' for database tracking
- Updated comments to reflect sticky trend logic
- Ready for TradingView alert setup
2025-11-18 11:14:32 +01:00
mindesbunister
c9ba24f6eb feat: Create v8 indicator with sticky trend detection
V8 Changes (fundamental improvements, not just filter toggles):
- Increased ATR multipliers across all timeframes (stickier line)
  - Minutes: 3.3→3.8
  - Hours: 3.0→3.5
  - Daily: 2.8→3.2
  - Weekly: 2.5→3.0
- NEW flip threshold (0.5%): Requires price to move beyond line by % before color changes
- Entry buffer enabled by default (0.20 ATR vs 0.15)
- ADX minimum increased to 18 (from 14) for stronger trend requirement
- Core HalfTrend logic modified to require threshold breach before flip

Goal: Reduce flip-flops while maintaining high-quality trend signals
Test against v6 (filtered) and v7 (pure flips) for comparison
2025-11-18 11:11:52 +01:00
mindesbunister
cc0d754b9a feat: Add Money Line v7 with pure line flip signals
- Created moneyline_v7_line_flips.pinescript
- Signals fire on EVERY line color change (red↔green)
- All filters removed - line flip IS the signal
- confirmBars = 0 for immediate flip bar signals
- Risk management via ATR-based TP/SL (bot-side)

- Restored v6 to original filtered mode
- v6: Conservative with all filters (ADX, volume, RSI, etc)
- v7: Aggressive pure flips for maximum accuracy

Rationale: Chart analysis shows ~100% accuracy on line flips
Every green section = price up, every red section = price down
ATR-based stops handle risk, no need for entry filters
2025-11-18 10:57:37 +01:00
mindesbunister
9dfc6da449 feat: Optimize v6 indicator settings for better signal quality
- Moved confirmBars to separate 'Signal Timing' section (no longer under optional filters)
- Made timing setting always-active status clear in UI with improved tooltip
- Updated default settings based on backtesting analysis:
  * confirmBars: 0 → 1 (wait one bar for confirmation, reduces false signals)
  * ADX filter: Enabled by default (was disabled)
  * ADX Length: 14 → 16 (better trend detection)
  * ADX minimum: 20 → 14 (catches trends earlier)
  * Volume filter: Enabled by default (was disabled)
  * Volume max: 3.0x → 3.5x (allows bigger breakout moves)
  * RSI filter: Enabled by default (was disabled)
  * RSI long minimum: 45 → 35 (catches momentum earlier)
  * RSI short maximum: 55 → 70 (CRITICAL: allows shorts during breakdowns)

Why these changes matter:
- Old RSI short max of 55-61 blocked profitable breakdown entries (RSI 62-70 range)
- ADX 21 minimum missed early trend starts, lowering to 14 catches them
- Volume max 3.5x allows high-volume breakouts (was blocking with 3.0x)
- confirmBars=1 reduces wick flips while still catching good moves

Expected impact: Should fix v6 underperformance (-$47.70 over 25 trades)
Target: Positive P&L and 50%+ win rate over next 20-30 trades

Files modified:
- workflows/trading/moneyline_v6_improved.pinescript
2025-11-17 09:57:44 +01:00
mindesbunister
abf982d645 feat: add indicator version parsing to n8n workflow
Updated Parse Signal Enhanced node to extract indicator version from alerts:
- Parses 'IND:v6' field from TradingView alert messages
- Defaults to 'v5' if version field not present (backward compatible)
- Passes indicatorVersion to Execute Trade endpoint

Updated Execute Trade1 node to include indicatorVersion in API payload:
- Added indicatorVersion field to JSON body
- Backend can now track which indicator version generated each signal

Backward Compatible:
- Old alerts without IND: field will default to 'v5'
- System works with or without version field
- No breaking changes to existing alerts

This enables version comparison analytics (v5 vs v6 performance) while
maintaining compatibility with any alerts that don't include the version.
2025-11-12 07:52:37 +01:00
mindesbunister
0700daf8ff feat: add indicator version tracking system
Database changes:
- Added indicatorVersion field to Trade table
- Added indicatorVersion field to BlockedSignal table
- Tracks which Pine Script version (v5, v6, etc.) generated each signal

Pine Script changes:
- v6 now includes '| IND:v6' in alert messages
- Enables differentiation between v5 and v6 signals in database

Documentation:
- Created INDICATOR_VERSION_TRACKING.md with full implementation guide
- Includes n8n workflow update instructions
- Includes SQL analysis queries for v5 vs v6 comparison
- Includes rollback plan if needed

Next steps (manual):
1. Update n8n workflow Parse Signal Enhanced node to extract IND field
2. Update n8n HTTP requests to pass indicatorVersion
3. Update API endpoints to accept and save indicatorVersion
4. Rebuild Docker container

Benefits:
- Compare v5 vs v6 Pine Script effectiveness
- Track which version generated winning/losing trades
- Validate that v6 price position filter reduces blocked signals
- Data-driven decisions on Pine Script improvements
2025-11-11 12:53:33 +01:00
mindesbunister
871d82a64a feat: add Pine Script v6 with improved signal quality filters
New v6 improvements:
- Fixed price position calculation: 100-bar range (was 20-bar)
- Added price position filter: prevents chasing extremes (85% max for longs, 15% min for shorts)
- Added volume filter: optional range check (0.7-3.0x average)
- Added RSI momentum filter: optional directional confirmation
- All new filters toggleable with sensible defaults

Key changes:
- Price position filter ENABLED by default (prevents flip-flop losses)
- Volume and RSI filters DISABLED by default (test incrementally)
- Aligns TradingView filtering with bot's 5-metric scoring system
- Reduces signals sent to bot that would be blocked anyway

Rationale:
Database analysis showed range extreme entries (9-94%) caused flip-flop losses.
V6 filters these at source instead of blocking in bot after webhook call.

Testing approach:
1. Phase 1: Price position filter only (test 5-10 signals)
2. Phase 2: Add volume filter if needed
3. Phase 3: Add RSI filter as last resort
2025-11-11 12:32:26 +01:00
mindesbunister
c3a053df63 CRITICAL FIX: Use ?? instead of || for tp2SizePercent to allow 0 value
BUG FOUND:
Line 558: tp2SizePercent: config.takeProfit2SizePercent || 100

When config.takeProfit2SizePercent = 0 (TP2-as-runner system), JavaScript's ||
operator treats 0 as falsy and falls back to 100, causing TP2 to close 100%
of remaining position instead of activating trailing stop.

IMPACT:
- On-chain orders placed correctly (line 481 uses ?? correctly)
- Position Manager reads from DB and expects TP2 to close position
- Result: User sees TWO take-profit orders instead of runner system

FIX:
Changed both tp1SizePercent and tp2SizePercent to use ?? operator:
- tp1SizePercent: config.takeProfit1SizePercent ?? 75
- tp2SizePercent: config.takeProfit2SizePercent ?? 0

This allows 0 value to be saved correctly for TP2-as-runner system.

VERIFICATION NEEDED:
Current open SHORT position in database has tp2SizePercent=100 from before
this fix. Next trade will use correct runner system.
2025-11-10 19:46:03 +01:00
mindesbunister
d2fbd125a0 fix: Make minSignalQualityScore configurable via settings + anti-chop improvements
CRITICAL BUG FIX:
- Settings page saved MIN_SIGNAL_QUALITY_SCORE to .env but check-risk had hardcoded value
- Now reads from config.minSignalQualityScore (defaults to 65, editable via /settings)
- Prevents settings changes from being ignored after restart

ANTI-CHOP FILTER FIXES:
- Fixed volume breakout bonus conflicting with anti-chop filter
- Volume breakout now requires ADX > 18 (trending market)
- Prevents high volume + low ADX from getting rewarded instead of penalized
- Anti-chop filter now properly blocks whipsaw traps at score 60

TESTING INFRASTRUCTURE:
- Added backtest script showing +17.1% P&L improvement (saved $242 in losses)
- Added test-signals.sh for comprehensive signal quality validation
- Added test-recent-signals.sh for analyzing actual trading session signals
- All tests passing: timeframe awareness, anti-chop, score thresholds

CHANGES:
- config/trading.ts: Added minSignalQualityScore to interface and defaults
- app/api/trading/check-risk/route.ts: Use config value instead of hardcoded 65
- lib/trading/signal-quality.ts: Fixed volume breakout bonus logic
- .env: Added MIN_SIGNAL_QUALITY_SCORE=65
- scripts/: Added comprehensive testing tools

BACKTEST RESULTS (Last 30 trades):
- Old system (score ≥60): $1,412.79 P&L
- New system (score ≥65 + anti-chop): $1,654.79 P&L
- Improvement: +$242.00 (+17.1%)
- Blocked 5 losing trades, missed 0 winners
2025-11-10 11:22:52 +01:00
mindesbunister
4b11186d16 Fix: Add timeframe-aware signal quality scoring for 5min charts
PROBLEM:
- Long signal (ADX 15.7, ATR 0.35%) blocked with score 45/100
- Missed major +3% runup, lost -2 on short that didn't flip
- Scoring logic treated all timeframes identically (daily chart thresholds)

ROOT CAUSE:
- ADX < 18 always scored -15 points regardless of timeframe
- 5min charts naturally have lower ADX (12-22 healthy range)
- copilot-instructions mentioned timeframe awareness but wasn't implemented

FIX:
- Add timeframe parameter to RiskCheckRequest interface
- Update scoreSignalQuality() with timeframe-aware ADX thresholds:
  * 5min/15min: ADX 12-22 healthy (+5), <12 weak (-15), >22 strong (+15)
  * Higher TF: ADX 18-25 healthy (+5), <18 weak (-15), >25 strong (+15)
- Pass timeframe from n8n workflow through check-risk and execute
- Update both Check Risk nodes in Money Machine workflow

IMPACT:
Your blocked signal (ADX 15.7 on 5min) now scores:
- Was: 50 + 5 - 15 + 0 + 0 + 5 = 45 (BLOCKED)
- Now: 50 + 5 + 5 + 0 + 0 + 5 = 65 (PASSES)

This 20-point improvement from timeframe awareness would have caught the runup.
2025-11-10 07:34:21 +01:00
mindesbunister
22195ed34c Fix P&L calculation and signal flip detection
- Fix external closure P&L using tp1Hit flag instead of currentSize
- Add direction change detection to prevent false TP1 on signal flips
- Signal flips now recorded with accurate P&L as 'manual' exits
- Add retry logic with exponential backoff for Solana RPC rate limits
- Create /api/trading/cancel-orders endpoint for manual cleanup
- Improves data integrity for win/loss statistics
2025-11-09 17:59:50 +01:00
mindesbunister
fdbb474e68 fix(n8n): CRITICAL - Add quality score validation to old workflow path
PROBLEM:
- Trades with quality score 35 and 45 were executed (threshold: 60)
- Position opened without risk management after signal flips
- "Parse Signal" node didn't extract ATR/ADX/RSI/volumeRatio/pricePosition
- "Check Risk" node only sent symbol+direction, skipped quality validation
- "Execute Trade" node didn't forward metrics to backend

ROOT CAUSE:
n8n workflow had TWO paths:
1. NEW: Parse Signal Enhanced → Check Risk1 → Execute Trade1 (working)
2. OLD: Parse Signal → Check Risk → Execute Trade (broken)

Old path bypassed quality check because check-risk endpoint saw
hasContextMetrics=false and allowed trade without validation.

FIX:
1. Changed "Parse Signal" from 'set' to 'code' node with metric extraction
2. Updated "Check Risk" to send atr/adx/rsi/volumeRatio/pricePosition
3. Updated "Execute Trade" to forward all metrics to backend

IMPACT:
- All trades now validated against quality score threshold (60)
- Low-quality signals properly blocked before execution
- Prevents positions opening without proper risk management

Evidence from database showed 3 trades in 2 hours with scores <60:
- 10:00:31 SOL LONG - Score 35 (phantom detected)
- 09:55:30 SOL SHORT - Score 35 (executed)
- 09:35:14 SOL LONG - Score 45 (executed)

All three should have been blocked. Fix prevents future bypasses.
2025-11-04 11:18:57 +01:00
mindesbunister
881a99242d 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
2025-11-03 10:28:48 +01:00
mindesbunister
da960330f4 fix(n8n): pass quality score from Check Risk to Execute Trade
- Added qualityScore field to Execute Trade node JSON body
- Pulls value from Check Risk response: .item.json.qualityScore
- This enables quality score to be saved in database and displayed on analytics dashboard
2025-11-02 23:51:50 +01:00
mindesbunister
202c44e4bc fix: remove qualityScore from Execute Trade body (causes syntax error)
- Execute Trade node was trying to access qualityScore from Check Risk node
- This caused syntax error in n8n when Check Risk blocks the trade
- Backend API calculates qualityScore from the provided metrics (atr, adx, rsi, etc.)
- No need to pass it explicitly in the request body
2025-11-02 16:47:10 +01:00
mindesbunister
32e88c3823 fix: improve signal quality scoring for volume breakouts
- Lower ATR threshold from 0.6% to 0.15% (allows low volatility breakouts)
- Increase volume bonus: +15 for very strong volume (1.5x+), was +10 for 1.2x+
- Add volume breakout logic: High volume (1.4x+) at 95%+ range gets +5 instead of -15 penalty
- Add volume compensation: +10 bonus when volume >1.8x and ATR <0.6%
- Example: SOL signal with 0.18% ATR, 1.74x volume at 95.6% range now scores 70/100 (PASS) instead of 25/100 (BLOCK)
- This signal moved +0.97% and would have hit TP1 (+1.5%) - proves quality scoring was too conservative
- Changes apply globally to all symbols (SOL, ETH, BTC) using same scoring algorithm
2025-11-02 09:10:03 +01:00
mindesbunister
056440bf8f feat: add quality score display and timezone fixes
- Add qualityScore to ExecuteTradeResponse interface and response object
- Update analytics page to always show Signal Quality card (N/A if unavailable)
- Fix n8n workflow to pass context metrics and qualityScore to execute endpoint
- Fix timezone in Telegram notifications (Europe/Berlin)
- Fix symbol normalization in /api/trading/close endpoint
- Update Drift ETH-PERP minimum order size (0.002 ETH not 0.01)
- Add transaction confirmation to closePosition() to prevent phantom closes
- Add 30-second grace period for new trades in Position Manager
- Fix execution order: database save before Position Manager.addTrade()
- Update copilot instructions with transaction confirmation pattern
2025-11-01 17:00:37 +01:00
mindesbunister
7788327a4e Update Parse Signal Enhanced for new alert format
- Changed regex from /\.P\s+(\d+)/ to /\b(buy|sell)\s+(\d+|D|W|M)\b/i
- Matches new format: 'ETH buy 15' instead of 'SOL buy .P 15'
- Supports all timeframes: 5, 15, 60, D (daily), W (weekly), M (monthly)
- Updated comment to reflect new format example
2025-11-01 11:12:38 +01:00
mindesbunister
eb2fea7bc0 Clean up alert format - remove .P notation
- Changed 'SOL buy .P 15' to 'SOL buy 15' (cleaner format)
- timeframe.period is already dynamic (no conversion needed)
- Works for any timeframe: 5, 15, 60, 240, D, etc.
- Format: 'ETH buy 15' or 'BTC sell 5' or 'SOL buy 60'
2025-11-01 11:09:37 +01:00
mindesbunister
fe0496121c Fix hardcoded SOL symbol in Pine Script alerts
- Use syminfo.ticker to dynamically get symbol name
- Strip USD/USDT/PERP suffixes to get base currency
- Works for ETH, SOL, BTC, and any other symbol
- Alerts now correctly show 'ETH buy' for Ethereum, 'BTC buy' for Bitcoin, etc.

This fixes the bug where ETH triggers sent 'SOL buy' alerts.
2025-11-01 11:06:14 +01:00
mindesbunister
8f0aa7223d Fix Format Risk node data references
- Updated to Set node v3.4 with proper assignments format
- Explicitly reference Parse Signal Enhanced for rawMessage
- Use $json for Check Risk output (reason, details, score, reasons)
- Properly formatted message with all data fields populated
- Added seconds to timestamp for better tracking
2025-11-01 11:00:13 +01:00
mindesbunister
c70fe45b15 Fix Format Risk message - clean readable format
- Removed ugly escaped syntax with $('Parse Signal').item.json references
- Use $json directly (cleaner and works correctly)
- Issues now display as bullet points instead of comma-separated
- Proper line breaks and formatting
- Professional looking blocked trade notifications
2025-11-01 10:54:30 +01:00
mindesbunister
a2d7cbcc4c Add detailed blocking reasons to risk check notifications
Enhanced 'Format Risk' node in n8n workflow to display:
- Specific blocking reason (duplicate, drawdown, cooldown, quality, etc.)
- Details about what triggered the block
- Quality score if low quality was the reason
- Quality issues breakdown (ATR too low, weak ADX, etc.)

Example output:
 TRADE BLOCKED
SHORT | ATR:0.30 | ADX:19.1 | RSI:46

 Issues: ATR too low (0.30% - dead market), Moderate trend (ADX 19.1), RSI supports short (46.0)
14:23
2025-10-31 14:34:49 +01:00
mindesbunister
37ce94d8f1 Restore context metrics in execute endpoint and clean up test files 2025-10-31 09:09:26 +01:00
mindesbunister
c88d94d14d Add n8n nodes with signal quality scoring - ready for import 2025-10-30 19:45:24 +01:00
mindesbunister
830468d524 Implement signal quality scoring system
- Updated execute endpoint to store context metrics in database
- Updated CreateTradeParams interface with 5 context metrics
- Updated Prisma schema with rsiAtEntry and pricePositionAtEntry
- Ran migration: add_rsi_and_price_position_metrics
- Complete flow: TradingView → n8n → check-risk (scores) → execute (stores)
2025-10-30 19:31:32 +01:00
mindesbunister
781b88f803 Enhance TradingView indicator with context metrics for signal quality
Added 5 context metrics to alert messages:
- ATR% (volatility as % of price)
- ADX (trend strength)
- RSI (momentum)
- VOL (volume ratio vs 20-bar MA)
- POS (price position in 20-bar range 0-100%)

Changes to Pine Script:
- Always calculate ADX (needed for context even if filter disabled)
- Extract ta.rma() calls outside ternary operators (Pine Script requirement)
- Use alert() instead of alertcondition() for dynamic message support
- Changed to single-line string concatenation for compatibility

Alert message format:
OLD: 'Buy SOL 15 | Profile=Hours ATR=10 Mult=3.0'
NEW: 'SOL buy .P 15 | ATR:1.85 | ADX:28.3 | RSI:62.5 | VOL:1.45 | POS:75.3'

Next: Update n8n to parse these metrics, implement signal quality scoring in bot
2025-10-30 15:53:48 +01:00
mindesbunister
fe4d9bc954 Fix: Calculate P&L correctly for external closures
- Save currentSize before it becomes 0 in external closure detection
- Use sizeBeforeClosure for P&L calculation instead of trade.currentSize
- Prevents /bin/bash.00 P&L for TP2 exits when position closes externally
- Ensures win/loss analytics counts TP trades correctly
2025-10-28 20:10:38 +01:00
mindesbunister
17b0806ff3 feat: Add 15-minute chart filter to n8n workflow
- Extract timeframe from TradingView message format: 'SOLUSDT.P 15'
- Added 'timeframe-filter' IF node after Parse Signal
- Only allows trades on 15-minute chart signals
- Blocks 5-minute and other timeframe signals
- Regex pattern: \.P\s+(\d+) matches '.P 15' format

This prevents bot from trading on wrong timeframe alerts.
2025-10-27 13:11:52 +01:00
mindesbunister
14d5de2c64 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
2025-10-27 12:59:25 +01:00