{
  "name": "Crypto Price Tracker",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "*/15 * * * *"
            }
          ]
        }
      },
      "id": "cron-trigger",
      "name": "Every 15 Minutes",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.coingecko.com/api/v3/simple/price",
        "options": {
          "qs": {
            "ids": "bitcoin,ethereum,cardano,solana,polkadot,chainlink,avalanche-2,polygon,cosmos,algorand",
            "vs_currencies": "usd",
            "include_24hr_change": "true",
            "include_24hr_vol": "true",
            "include_market_cap": "true"
          }
        }
      },
      "id": "get-crypto-prices",
      "name": "Get Crypto Prices",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Process cryptocurrency data\nconst cryptoData = $input.first().json;\nconst processedData = [];\n\n// Map of coin IDs to names\nconst coinNames = {\n  'bitcoin': 'Bitcoin',\n  'ethereum': 'Ethereum',\n  'cardano': 'Cardano',\n  'solana': 'Solana',\n  'polkadot': 'Polkadot',\n  'chainlink': 'Chainlink',\n  'avalanche-2': 'Avalanche',\n  'polygon': 'Polygon',\n  'cosmos': 'Cosmos',\n  'algorand': 'Algorand'\n};\n\nfor (const [coinId, data] of Object.entries(cryptoData)) {\n  const processed = {\n    coinId,\n    name: coinNames[coinId] || coinId,\n    price: data.usd,\n    change24h: data.usd_24h_change,\n    volume24h: data.usd_24h_vol,\n    marketCap: data.usd_market_cap,\n    timestamp: new Date().toISOString(),\n    // Calculate additional metrics\n    priceChangePercent: data.usd_24h_change,\n    isPositiveChange: data.usd_24h_change > 0,\n    volumeRank: 0, // Will be calculated later\n    marketCapRank: 0 // Will be calculated later\n  };\n  \n  processedData.push(processed);\n}\n\n// Sort by market cap and assign ranks\nprocessedData.sort((a, b) => b.marketCap - a.marketCap);\nprocessedData.forEach((coin, index) => {\n  coin.marketCapRank = index + 1;\n});\n\n// Sort by volume and assign ranks\nprocessedData.sort((a, b) => b.volume24h - a.volume24h);\nprocessedData.forEach((coin, index) => {\n  coin.volumeRank = index + 1;\n});\n\nreturn processedData.map(coin => ({ json: coin }));"
      },
      "id": "process-crypto-data",
      "name": "Process Crypto Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "crypto_prices",
        "columns": "coin_id, name, price, change_24h, volume_24h, market_cap, timestamp, price_change_percent, is_positive_change, volume_rank, market_cap_rank",
        "values": "={{ $json.coinId }}, {{ $json.name }}, {{ $json.price }}, {{ $json.change24h }}, {{ $json.volume24h }}, {{ $json.marketCap }}, {{ $json.timestamp }}, {{ $json.priceChangePercent }}, {{ $json.isPositiveChange }}, {{ $json.volumeRank }}, {{ $json.marketCapRank }}"
      },
      "id": "save-crypto-prices",
      "name": "Save Crypto Prices",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        900,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Calculate market summary\nconst prices = $input.all().map(item => item.json);\n\nconst summary = {\n  totalMarketCap: prices.reduce((sum, coin) => sum + coin.marketCap, 0),\n  totalVolume: prices.reduce((sum, coin) => sum + coin.volume24h, 0),\n  averageChange: prices.reduce((sum, coin) => sum + coin.change24h, 0) / prices.length,\n  positiveChanges: prices.filter(coin => coin.isPositiveChange).length,\n  negativeChanges: prices.filter(coin => !coin.isPositiveChange).length,\n  topGainer: prices.reduce((max, coin) => coin.change24h > max.change24h ? coin : max, prices[0]),\n  topLoser: prices.reduce((min, coin) => coin.change24h < min.change24h ? coin : min, prices[0]),\n  timestamp: new Date().toISOString()\n};\n\nreturn { json: summary };"
      },
      "id": "calculate-summary",
      "name": "Calculate Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        400
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "crypto_summary",
        "columns": "timestamp, total_market_cap, total_volume, average_change, positive_changes, negative_changes, top_gainer, top_gainer_change, top_loser, top_loser_change",
        "values": "={{ $json.timestamp }}, {{ $json.totalMarketCap }}, {{ $json.totalVolume }}, {{ $json.averageChange }}, {{ $json.positiveChanges }}, {{ $json.negativeChanges }}, {{ $json.topGainer.name }}, {{ $json.topGainer.change24h }}, {{ $json.topLoser.name }}, {{ $json.topLoser.change24h }}"
      },
      "id": "save-summary",
      "name": "Save Summary",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1120,
        400
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-1",
              "leftValue": "={{ $json.averageChange }}",
              "rightValue": 5,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-market-trend",
      "name": "Check Market Trend",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1340,
        400
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "n8n",
          "mode": "name"
        },
        "text": "\ud83d\ude80 Crypto Market Update - Bullish Trend",
        "otherOptions": {
          "attachments": [
            {
              "color": "good",
              "fields": [
                {
                  "title": "Total Market Cap",
                  "value": "${{ Math.round($json.totalMarketCap / 1000000000) }}B",
                  "short": true
                },
                {
                  "title": "Total Volume",
                  "value": "${{ Math.round($json.totalVolume / 1000000000) }}B",
                  "short": true
                },
                {
                  "title": "Average Change",
                  "value": "{{ $json.averageChange.toFixed(2) }}%",
                  "short": true
                },
                {
                  "title": "Positive Changes",
                  "value": "{{ $json.positiveChanges }}/{{ $json.positiveChanges + $json.negativeChanges }}",
                  "short": true
                },
                {
                  "title": "Top Gainer",
                  "value": "{{ $json.topGainer.name }}: {{ $json.topGainer.change24h.toFixed(2) }}%",
                  "short": false
                }
              ]
            }
          ]
        }
      },
      "id": "bullish-notification",
      "name": "Bullish Notification",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1560,
        200
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "n8n",
          "mode": "name"
        },
        "text": "\ud83d\udcc9 Crypto Market Update - Bearish Trend",
        "otherOptions": {
          "attachments": [
            {
              "color": "warning",
              "fields": [
                {
                  "title": "Total Market Cap",
                  "value": "${{ Math.round($json.totalMarketCap / 1000000000) }}B",
                  "short": true
                },
                {
                  "title": "Total Volume",
                  "value": "${{ Math.round($json.totalVolume / 1000000000) }}B",
                  "short": true
                },
                {
                  "title": "Average Change",
                  "value": "{{ $json.averageChange.toFixed(2) }}%",
                  "short": true
                },
                {
                  "title": "Negative Changes",
                  "value": "{{ $json.negativeChanges }}/{{ $json.positiveChanges + $json.negativeChanges }}",
                  "short": true
                },
                {
                  "title": "Top Loser",
                  "value": "{{ $json.topLoser.name }}: {{ $json.topLoser.change24h.toFixed(2) }}%",
                  "short": false
                }
              ]
            }
          ]
        }
      },
      "id": "bearish-notification",
      "name": "Bearish Notification",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1560,
        600
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every 15 Minutes": {
      "main": [
        [
          {
            "node": "Get Crypto Prices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Crypto Prices": {
      "main": [
        [
          {
            "node": "Process Crypto Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Crypto Data": {
      "main": [
        [
          {
            "node": "Save Crypto Prices",
            "type": "main",
            "index": 0
          },
          {
            "node": "Calculate Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Summary": {
      "main": [
        [
          {
            "node": "Save Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Summary": {
      "main": [
        [
          {
            "node": "Check Market Trend",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Market Trend": {
      "main": [
        [
          {
            "node": "Bullish Notification",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Bearish Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "createdAt": "2024-01-01T00:00:00.000Z",
      "updatedAt": "2024-01-01T00:00:00.000Z",
      "id": "crypto",
      "name": "crypto"
    }
  ],
  "triggerCount": 1,
  "updatedAt": "2024-01-01T00:00:00.000Z",
  "versionId": "1"
}