{
  "id": "65dLT87vPoyQexup",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Automated Daily Crypto Market Report - Top Gainers & Losers",
  "tags": [],
  "nodes": [
    {
      "id": "d4575dcd-57c9-4ade-9746-be644aba5443",
      "name": "Daily Crypto Trigger\t",
      "type": "n8n-nodes-base.cron",
      "position": [
        0,
        240
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": 0
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "39d4dd2e-6ec8-4970-b5b1-8e9c896529ef",
      "name": "Set Configuration Variables\t",
      "type": "n8n-nodes-base.set",
      "position": [
        224,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "symbols-assignment",
              "name": "vs_currency",
              "type": "string",
              "value": "usd"
            },
            {
              "id": "whatsapp-number",
              "name": "whatsapp_number",
              "type": "string",
              "value": "+1234567890"
            },
            {
              "id": "email-recipient",
              "name": "email_recipient",
              "type": "string",
              "value": "user@example.com"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "eef4d478-03bd-438b-88c9-83244dd4e1d0",
      "name": "Fetch Crypto Data from CoinGecko\t",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        448,
        240
      ],
      "parameters": {
        "url": "=https://api.coingecko.com/api/v3/coins/markets?vs_currency={{ $json.vs_currency }}&order=market_cap_desc&per_page=100&page=1&sparkline=false&locale=en&price_change_percentage=24h",
        "options": {
          "timeout": 30000
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "29507472-9f45-4854-a25c-e0cb35136fa9",
      "name": "Process Crypto Movements\t",
      "type": "n8n-nodes-base.code",
      "position": [
        672,
        240
      ],
      "parameters": {
        "jsCode": "// Process crypto data and calculate movements\nlet processedStocks = [];\n\n// Log the raw input for debugging\nconsole.log('Raw API Input:', JSON.stringify($input.all(), null, 2));\n\n// Aggregate data from all input items\n$input.all().forEach(item => {\n  const stockData = item.json;\n  if (stockData && stockData.name && stockData.price_change_percentage_24h !== undefined) {\n    const percentChange = parseFloat(stockData.price_change_percentage_24h);\n    const price = parseFloat(stockData.current_price);\n    const change = parseFloat(stockData.price_change_24h);\n    \n    processedStocks.push({\n      name: stockData.name,\n      symbol: stockData.symbol.toUpperCase(),\n      price: isNaN(price) ? null : price,\n      change: isNaN(change) ? null : change,\n      percent_change: isNaN(percentChange) ? null : percentChange,\n      volume: stockData.total_volume ? parseInt(stockData.total_volume) : null,\n      high: stockData.high_24h ? parseFloat(stockData.high_24h) : null,\n      low: stockData.low_24h ? parseFloat(stockData.low_24h) : null,\n      movement_type: percentChange > 0 ? 'gain' : 'loss',\n      abs_percent_change: Math.abs(percentChange)\n    });\n  }\n});\n\n// Log processed stocks for debugging\nconsole.log('Processed Stocks:', JSON.stringify(processedStocks, null, 2));\n\n// Sort by absolute percentage change (biggest movers)\nprocessedStocks.sort((a, b) => b.abs_percent_change - a.abs_percent_change);\n\n// Take top 100 biggest movers, gainers, and losers\nconst topMovers = processedStocks.slice(0, 100);\nconst gainers = processedStocks.filter(stock => stock.percent_change > 0).slice(0, 100);\nconst losers = processedStocks.filter(stock => stock.percent_change < 0).slice(0, 100);\n\n// Set current date and time in IST (02:33 PM IST, August 22, 2025)\nconst currentDate = new Date('2025-08-22T14:33:00+05:30').toLocaleString('en-IN', {\n \n  year: 'numeric',\n  month: 'long',\n  day: 'numeric'\n});\n\nreturn [{\n  json: {\n    date: currentDate, // Reflects 02:33 PM IST, August 22, 2025\n    total_stocks: processedStocks.length,\n    top_gainers: gainers,\n    top_losers: losers,\n    biggest_movers: topMovers\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "65d484f2-0da1-4fba-86b3-eef7d8c59bb4",
      "name": "Format WhatsApp Message\t",
      "type": "n8n-nodes-base.code",
      "position": [
        896,
        144
      ],
      "parameters": {
        "jsCode": "// Generate WhatsApp message\n const data = $input.first().json;\n\nlet message = `\ud83d\udcc8 *DAILY CRYPTO MARKET REPORT*\\n`;\nmessage += `\ud83d\udcc5 Date: ${data.date}\\n`;\nmessage += `\ud83d\udcca Analyzed: ${data.total_stocks} cryptos\\n\\n`;\n\n// Top Gainers\nmessage += `\ud83d\udfe2 *TOP GAINERS*\\n`;\nmessage += `\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\\n`;\ndata.top_gainers.forEach((stock, index) => {\n  message += `${index + 1}. ${stock.symbol}\\n`;\n  message += `   \ud83d\udcb0 $${stock.price?.toFixed(2) || 'N/A'}\\n`;\n  message += `   \ud83d\udcc8 +${stock.percent_change?.toFixed(2)}% (+$${stock.change?.toFixed(2)})\\n\\n`;\n});\n\n// Top Losers\nmessage += `\ud83d\udd34 *TOP LOSERS*\\n`;\nmessage += `\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\\n`;\ndata.top_losers.forEach((stock, index) => {\n  message += `${index + 1}. ${stock.symbol}\\n`;\n  message += `   \ud83d\udcb0 $${stock.price?.toFixed(2) || 'N/A'}\\n`;\n  message += `   \ud83d\udcc9 ${stock.percent_change?.toFixed(2)}% ($${stock.change?.toFixed(2)})\\n\\n`;\n});\n\nmessage += `\\n\u26a1 Generated by Crypto Alert Bot`;\n\nreturn [{ json: { whatsapp_message: message } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "337719ad-85c1-49bc-8bf9-b578f8718211",
      "name": "Format Email Content\t",
      "type": "n8n-nodes-base.code",
      "position": [
        896,
        336
      ],
      "parameters": {
        "jsCode": "// Generate HTML Email content\n const data = $input.first().json;\n\nlet htmlContent = `\n<!DOCTYPE html>\n<html>\n<head>\n    <style>\n        body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }\n        .container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }\n        .header { text-align: center; border-bottom: 2px solid #007bff; padding-bottom: 15px; margin-bottom: 20px; }\n        .section { margin-bottom: 30px; }\n        .stock-table { width: 100%; border-collapse: collapse; margin-top: 10px; }\n        .stock-table th, .stock-table td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }\n        .stock-table th { background-color: #f8f9fa; font-weight: bold; }\n        .gain { color: #28a745; font-weight: bold; }\n        .loss { color: #dc3545; font-weight: bold; }\n        .symbol { font-weight: bold; color: #007bff; }\n        .stats { display: flex; justify-content: space-around; background: #f8f9fa; padding: 15px; border-radius: 5px; margin-bottom: 20px; }\n        .stat-item { text-align: center; }\n        .stat-number { font-size: 24px; font-weight: bold; color: #007bff; }\n    </style>\n</head>\n<body>\n    <div class=\"container\">\n        <div class=\"header\">\n            <h1>\ud83d\udcc8 Daily Crypto Market Report</h1>\n            <p><strong>Date:</strong> ${data.date}</p>\n        </div>\n        \n        <div class=\"stats\">\n            <div class=\"stat-item\">\n                <div class=\"stat-number\">${data.total_stocks}</div>\n                <div>Cryptos Analyzed</div>\n            </div>\n            <div class=\"stat-item\">\n                <div class=\"stat-number\">${data.top_gainers.length}</div>\n                <div>Top Gainers</div>\n            </div>\n            <div class=\"stat-item\">\n                <div class=\"stat-number\">${data.top_losers.length}</div>\n                <div>Top Losers</div>\n            </div>\n        </div>\n\n        <div class=\"section\">\n            <h2 style=\"color: #28a745;\">\ud83d\udfe2 Top Gainers</h2>\n            <table class=\"stock-table\">\n                <thead>\n                    <tr>\n                        <th>Rank</th>\n                        <th>Symbol</th>\n                        <th>Price</th>\n                        <th>Change</th>\n                        <th>% Change</th>\n                        <th>Volume</th>\n                    </tr>\n                </thead>\n                <tbody>`;\n\ndata.top_gainers.forEach((stock, index) => {\n    htmlContent += `\n                    <tr>\n                        <td>${index + 1}</td>\n                        <td class=\"symbol\">${stock.symbol}</td>\n                        <td>$${stock.price?.toFixed(2) || 'N/A'}</td>\n                        <td class=\"gain\">+$${stock.change?.toFixed(2) || 'N/A'}</td>\n                        <td class=\"gain\">+${stock.percent_change?.toFixed(2)}%</td>\n                        <td>${stock.volume?.toLocaleString() || 'N/A'}</td>\n                    </tr>`;\n});\n\nhtmlContent += `\n                </tbody>\n            </table>\n        </div>\n\n        <div class=\"section\">\n            <h2 style=\"color: #dc3545;\">\ud83d\udd34 Top Losers</h2>\n            <table class=\"stock-table\">\n                <thead>\n                    <tr>\n                        <th>Rank</th>\n                        <th>Symbol</th>\n                        <th>Price</th>\n                        <th>Change</th>\n                        <th>% Change</th>\n                        <th>Volume</th>\n                    </tr>\n                </thead>\n                <tbody>`;\n\ndata.top_losers.forEach((stock, index) => {\n    htmlContent += `\n                    <tr>\n                        <td>${index + 1}</td>\n                        <td class=\"symbol\">${stock.symbol}</td>\n                        <td>$${stock.price?.toFixed(2) || 'N/A'}</td>\n                        <td class=\"loss\">$${stock.change?.toFixed(2) || 'N/A'}</td>\n                        <td class=\"loss\">${stock.percent_change?.toFixed(2)}%</td>\n                        <td>${stock.volume?.toLocaleString() || 'N/A'}</td>\n                    </tr>`;\n});\n\nhtmlContent += `\n                </tbody>\n            </table>\n        </div>\n        \n        <div style=\"text-align: center; margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd; color: #666;\">\n            <p>\u26a1 Generated automatically by Crypto Alert System</p>\n            <p><small>Data provided by CoinGecko API</small></p>\n        </div>\n    </div>\n</body>\n</html>`;\n\n const plainText = `\nDAILY CRYPTO MARKET REPORT\\n========================\\nDate: ${data.date}\\nCryptos Analyzed: ${data.total_stocks}\\n\\nTOP GAINERS:\\n-----------\\n${data.top_gainers.map((stock, i) => `${i+1}. ${stock.symbol}: $${stock.price?.toFixed(2)} (+${stock.percent_change?.toFixed(2)}%)`).join('\\n')}\\n\\nTOP LOSERS:\\n----------\\n${data.top_losers.map((stock, i) => `${i+1}. ${stock.symbol}: $${stock.price?.toFixed(2)} (${stock.percent_change?.toFixed(2)}%)`).join('\\n')}\\n\\nGenerated by Crypto Alert System`;\n\nreturn [{\n    json: {\n        email_html: htmlContent,\n        email_text: plainText,\n        email_subject: `\ud83d\udcc8 Daily Crypto Report - ${data.date} | Top Movers Alert`\n    }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "e66985b9-91e8-4345-994d-9713557bc7cd",
      "name": "Send Email Alert\t",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        1120,
        336
      ],
      "parameters": {
        "html": "={{ $json.email_html }}",
        "options": {},
        "subject": "={{ $json.email_subject }}",
        "toEmail": "={{ $('Set Configuration Variables\t').item.json.email_recipient }}",
        "fromEmail": "user@example.com",
        "emailFormat": "html"
      },
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "7c7f1532-ef42-4537-b9c6-5fa1834dd832",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 180,
        "height": 200,
        "content": "Triggers daily at 00:00 UTC."
      },
      "typeVersion": 1
    },
    {
      "id": "60622a0b-df55-46f3-8da7-961be1d89e61",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        224,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 180,
        "height": 200,
        "content": "Configure phone numbers, and email addresses here"
      },
      "typeVersion": 1
    },
    {
      "id": "1a47e027-c1d2-47aa-8083-b6a8dc033b09",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        448,
        0
      ],
      "parameters": {
        "color": 3,
        "width": 180,
        "height": 200,
        "content": "Fetches 24h data for top 100 cryptos using CoinGecko API"
      },
      "typeVersion": 1
    },
    {
      "id": "45c9aadb-c889-4aff-bffa-8a3940d1815d",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 180,
        "height": 200,
        "content": "Processes and ranks cryptos by biggest 24h movements"
      },
      "typeVersion": 1
    },
    {
      "id": "dd16268b-0178-4416-ae19-bb8e4cc6b34c",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1088,
        0
      ],
      "parameters": {
        "color": 6,
        "width": 200,
        "height": 200,
        "content": "Sends alerts via WhatsApp/Telegram and Email with formatted reports"
      },
      "typeVersion": 1
    },
    {
      "id": "bd9244db-2652-491e-9437-7b497098ee04",
      "name": "Send message",
      "type": "n8n-nodes-base.whatsApp",
      "position": [
        1120,
        144
      ],
      "parameters": {
        "textBody": "={{ $json.whatsapp_message }}",
        "operation": "send",
        "phoneNumberId": "=+919876543234",
        "additionalFields": {},
        "recipientPhoneNumber": "={{ $('Set Configuration Variables\t').item.json.whatsapp_number }}"
      },
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "666f2f29-90e3-4f93-9267-a32bb6e03fc3",
  "connections": {
    "Daily Crypto Trigger\t": {
      "main": [
        [
          {
            "node": "Set Configuration Variables\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Email Content\t": {
      "main": [
        [
          {
            "node": "Send Email Alert\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format WhatsApp Message\t": {
      "main": [
        [
          {
            "node": "Send message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Crypto Movements\t": {
      "main": [
        [
          {
            "node": "Format WhatsApp Message\t",
            "type": "main",
            "index": 0
          },
          {
            "node": "Format Email Content\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Configuration Variables\t": {
      "main": [
        [
          {
            "node": "Fetch Crypto Data from CoinGecko\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Crypto Data from CoinGecko\t": {
      "main": [
        [
          {
            "node": "Process Crypto Movements\t",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}