Files
trading_bot_v3/v4/n8n-workflow-simple.json
mindesbunister 2adab40781 fix: Use IF node typeVersion 1 for better compatibility
- Changed from typeVersion 2 to typeVersion 1
- typeVersion 1 is supported in older n8n versions
- Should fix 'custom node' installation error
2025-10-23 16:25:09 +02:00

301 lines
11 KiB
JSON
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"name": "TradingView → Trading Bot v4 (Simplified)",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "tradingview-webhook",
"responseMode": "onReceived",
"options": {}
},
"id": "webhook-tradingview",
"name": "Webhook - TradingView Alert",
"type": "n8n-nodes-base.webhook",
"typeVersion": 1,
"position": [250, 300],
"webhookId": "tradingview-webhook"
},
{
"parameters": {
"functionCode": "// Parse TradingView alert data\nconst body = $input.item.json.body || $input.item.json;\n\n// Extract data from various TradingView formats\nlet symbol = body.symbol || body.ticker || 'SOLUSDT';\nlet action = (body.action || body.signal_type || body.order_action || '').toLowerCase();\nlet timeframe = body.timeframe || body.interval || '5';\nlet price = parseFloat(body.price || body.close || 0);\nlet timestamp = body.timestamp || body.time || new Date().toISOString();\n\n// Normalize symbol (remove exchanges, convert formats)\nsymbol = symbol.replace(/BINANCE:|COINBASE:|FTX:/gi, '');\nsymbol = symbol.replace(/-PERP$/i, '');\nsymbol = symbol.replace(/USDT$/i, 'USD');\nif (symbol === 'SOL') symbol = 'SOLUSDT';\nif (symbol === 'BTC') symbol = 'BTCUSD';\nif (symbol === 'ETH') symbol = 'ETHUSD';\n\n// Normalize action to long/short\nlet direction = 'long';\nif (action.includes('buy') || action.includes('long')) {\n direction = 'long';\n} else if (action.includes('sell') || action.includes('short')) {\n direction = 'short';\n}\n\n// Normalize timeframe (remove 'm' suffix, convert to minutes)\ntimeframe = timeframe.toString().replace(/m$/i, '');\nif (timeframe === '1h') timeframe = '60';\nif (timeframe === '4h') timeframe = '240';\nif (timeframe === '1d') timeframe = '1440';\n\n// Prepare payload for v4 API\nconst apiPayload = {\n symbol: symbol,\n direction: direction,\n timeframe: timeframe,\n signalStrength: body.strength || 'strong',\n signalPrice: price,\n strategy: body.strategy || 'tradingview_alert'\n};\n\nreturn {\n json: {\n rawAlert: body,\n apiPayload: apiPayload,\n parsedData: {\n symbol: symbol,\n direction: direction,\n timeframe: timeframe,\n price: price,\n timestamp: timestamp\n }\n }\n};"
},
"id": "parse-signal",
"name": "Parse TradingView Signal",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [450, 300]
},
{
"parameters": {
"url": "={{ $env.TRADING_BOT_API_URL }}/api/trading/check-risk",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "=Bearer {{ $env.API_SECRET_KEY }}"
}
]
},
"sendBody": true,
"bodyParameters": {
"parameters": [
{
"name": "symbol",
"value": "={{ $json.apiPayload.symbol }}"
},
{
"name": "direction",
"value": "={{ $json.apiPayload.direction }}"
}
]
},
"options": {
"timeout": 10000
}
},
"id": "check-risk",
"name": "Check Risk Limits",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4,
"position": [650, 300]
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{ $json.allowed }}",
"value2": true
}
]
}
},
"id": "risk-check-condition",
"name": "Risk Check Passed?",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [850, 300]
},
{
"parameters": {
"url": "={{ $env.TRADING_BOT_API_URL }}/api/trading/execute",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "=Bearer {{ $env.API_SECRET_KEY }}"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify($('Parse TradingView Signal').item.json.apiPayload) }}",
"options": {
"timeout": 30000
}
},
"id": "execute-trade",
"name": "Execute Trade on Drift",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4,
"position": [1050, 200]
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{ $json.success }}",
"value2": true
}
]
}
},
"id": "trade-success-condition",
"name": "Trade Executed Successfully?",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [1250, 200]
},
{
"parameters": {
"functionCode": "// Format success message for Telegram\nconst trade = $input.item.json.trade || {};\nconst parsedData = $('Parse TradingView Signal').item.json.parsedData;\n\nconst message = `🟢 TRADE EXECUTED\n\n📊 Symbol: ${trade.symbol || parsedData.symbol}\n📈 Direction: ${(trade.direction || parsedData.direction).toUpperCase()}\n💰 Entry Price: $${(trade.entryPrice || parsedData.price).toFixed(4)}\n💵 Position Size: $${(trade.positionSize || trade.notionalValue || 500).toFixed(2)}\n⚡ Leverage: ${trade.leverage || 10}x\n\n🎯 Targets:\n Stop Loss: $${(trade.stopLoss || 0).toFixed(2)} (${trade.stopLossPercent || '-1.5'}%)\n TP1: $${(trade.takeProfit1 || 0).toFixed(2)} (${trade.tp1Percent || '+0.7'}%)\n TP2: $${(trade.takeProfit2 || 0).toFixed(2)} (${trade.tp2Percent || '+1.5'}%)\n\n📊 Slippage: ${(trade.slippage || 0).toFixed(3)}%\n⏰ Time: ${new Date().toLocaleString()}\n\n✅ Position is now being monitored automatically.\nAuto-exit at TP/SL levels.`;\n\nreturn { json: { message } };"
},
"id": "format-success",
"name": "Format Success Message",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1450, 100]
},
{
"parameters": {
"functionCode": "// Format error message for Telegram\nconst error = $input.item.json.error || 'Unknown error occurred';\nconst parsedData = $('Parse TradingView Signal').item.json.parsedData;\n\nconst message = `🔴 TRADE EXECUTION FAILED\n\n📊 Symbol: ${parsedData.symbol}\n📈 Direction: ${parsedData.direction.toUpperCase()}\n💰 Price: $${parsedData.price.toFixed(4)}\n\n❌ Error: ${error}\n\n⏰ Time: ${new Date().toLocaleString()}\n\n⚠ Please check bot logs and Drift account status.`;\n\nreturn { json: { message } };"
},
"id": "format-error",
"name": "Format Error Message",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1450, 300]
},
{
"parameters": {
"functionCode": "// Format risk blocked message for Telegram\nconst riskCheck = $('Check Risk Limits').item.json;\nconst parsedData = $('Parse TradingView Signal').item.json.parsedData;\n\nconst message = `⚠️ TRADE BLOCKED - RISK LIMITS\n\n📊 Symbol: ${parsedData.symbol}\n📈 Direction: ${parsedData.direction.toUpperCase()}\n💰 Price: $${parsedData.price.toFixed(4)}\n\n🛑 Reason: ${riskCheck.reason || 'Risk limits exceeded'}\n\n📈 Current Stats:\n Trades Today: ${riskCheck.stats?.tradesToday || 0}\n Daily P&L: ${riskCheck.stats?.dailyPnl || 0}%\n Last Trade: ${riskCheck.stats?.lastTradeMinutesAgo || 0} min ago\n\n⏰ Time: ${new Date().toLocaleString()}\n\n✅ Trade will be allowed when risk conditions improve.`;\n\nreturn { json: { message } };"
},
"id": "format-risk-blocked",
"name": "Format Risk Blocked Message",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1050, 400]
},
{
"parameters": {
"chatId": "={{ $env.TELEGRAM_CHAT_ID }}",
"text": "={{ $json.message }}",
"additionalFields": {
"parse_mode": "Markdown"
}
},
"id": "telegram-notification",
"name": "Telegram - Send Notification",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [1650, 200],
"credentials": {
"telegramApi": {
"id": "1",
"name": "Telegram Bot"
}
}
}
],
"connections": {
"Webhook - TradingView Alert": {
"main": [
[
{
"node": "Parse TradingView Signal",
"type": "main",
"index": 0
}
]
]
},
"Parse TradingView Signal": {
"main": [
[
{
"node": "Check Risk Limits",
"type": "main",
"index": 0
}
]
]
},
"Check Risk Limits": {
"main": [
[
{
"node": "Risk Check Passed?",
"type": "main",
"index": 0
}
]
]
},
"Risk Check Passed?": {
"main": [
[
{
"node": "Execute Trade on Drift",
"type": "main",
"index": 0
}
],
[
{
"node": "Format Risk Blocked Message",
"type": "main",
"index": 0
}
]
]
},
"Execute Trade on Drift": {
"main": [
[
{
"node": "Trade Executed Successfully?",
"type": "main",
"index": 0
}
]
]
},
"Trade Executed Successfully?": {
"main": [
[
{
"node": "Format Success Message",
"type": "main",
"index": 0
}
],
[
{
"node": "Format Error Message",
"type": "main",
"index": 0
}
]
]
},
"Format Success Message": {
"main": [
[
{
"node": "Telegram - Send Notification",
"type": "main",
"index": 0
}
]
]
},
"Format Error Message": {
"main": [
[
{
"node": "Telegram - Send Notification",
"type": "main",
"index": 0
}
]
]
},
"Format Risk Blocked Message": {
"main": [
[
{
"node": "Telegram - Send Notification",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {
"executionOrder": "v1"
},
"staticData": null,
"tags": [],
"triggerCount": 1,
"updatedAt": "2025-10-23T00:00:00.000Z",
"versionId": "1"
}