{
  "id": "7pcXnXi0xHY1p56a",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Switch Gainium bots to risk-off mode using the Fear and Greed Index",
  "tags": [],
  "nodes": [
    {
      "id": "72715969-8ac8-42c9-ad28-b2f1bb73fb5e",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        576,
        768
      ],
      "parameters": {
        "width": 480,
        "height": 896,
        "content": "## Switch Gainium bots to risk-off mode using the Fear and Greed Index\n\n### How it works\n\nThis workflow runs every day at 08:00, loads the configured Gainium bot details, and fetches the current Crypto Fear and Greed Index. It parses the index into a market regime, then stops the Gainium bot during extreme fear, starts it during extreme greed, or sends a neutral no-change notification otherwise. Telegram alerts are sent for successful branch outcomes and for fetch, bot-action, or workflow-level failures.\n\n### Setup steps\n\n- Configure the schedule trigger timezone and confirm the daily 08:00 run time is appropriate.\n- Set the Gainium bot_id, bot_type, and paper_trading values in the Config node for the target bot.\n- Add valid Gainium credentials to the Gainium nodes and verify the bot start/stop operations match the intended account and environment.\n- Add Telegram credentials and set telegram_chat_id so all notification and alert nodes can send messages.\n- Ensure outbound HTTP access is allowed to https://api.alternative.me/fng/.\n\n### Customization\n\nAdjust the fear/greed threshold logic in the Classify Regime code node, change the schedule frequency, or modify the Telegram message text to include additional bot or index details."
      },
      "typeVersion": 1
    },
    {
      "id": "30b9cc3c-fae1-4d49-a3af-1b6e2251297c",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1136,
        1328
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 320,
        "content": "## Daily setup\n\nStarts the workflow on the daily schedule and injects the bot and Telegram configuration values used by the rest of the run."
      },
      "typeVersion": 1
    },
    {
      "id": "1fc576cf-b4aa-4bf9-a5dc-60d11fbdb53f",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1600,
        1328
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 320,
        "content": "## Fetch and classify index\n\nRetrieves the Fear and Greed Index from the external API and parses the returned value into a market regime for downstream decisions."
      },
      "typeVersion": 1
    },
    {
      "id": "cd9cd6c0-0c00-4185-aa46-1fbc836d5194",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2080,
        1328
      ],
      "parameters": {
        "color": 7,
        "width": 672,
        "height": 624,
        "content": "## Route market regime\n\nBranches the workflow based on the classified regime: extreme fear goes to risk-off handling, extreme greed goes to risk-on handling, and non-extreme conditions send a neutral no-change Telegram update."
      },
      "typeVersion": 1
    },
    {
      "id": "d5399163-0d27-4212-a50f-ef3caf1f51af",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2336,
        768
      ],
      "parameters": {
        "color": 7,
        "width": 640,
        "height": 528,
        "content": "## Control Gainium bot\n\nExecutes the Gainium bot stop/start actions for risk-off or risk-on modes, sends success notifications, and reports bot action failures from the same action cluster."
      },
      "typeVersion": 1
    },
    {
      "id": "0ff7ec62-d932-4fc0-9d56-68d34766a61e",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1440,
        1696
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 480,
        "content": "## Error alerting\n\nHandles the lower alerting cluster, including a direct alert when the index request fails and a global workflow error trigger that sends Telegram error notifications."
      },
      "typeVersion": 1
    },
    {
      "id": "250fbb37-7175-4558-903b-7dbf057a1ab9",
      "name": "When Daily at 08:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        1184,
        1488
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "40a33a33-e0f2-48e0-aa03-e239f5a703f0",
      "name": "Set Bot Configuration",
      "type": "n8n-nodes-base.set",
      "position": [
        1408,
        1488
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "c1",
              "name": "bot_id",
              "type": "string",
              "value": "PUT_YOUR_DCA_BOT_ID_HERE"
            },
            {
              "id": "c2",
              "name": "bot_type",
              "type": "string",
              "value": "dca"
            },
            {
              "id": "c3",
              "name": "paper_trading",
              "type": "boolean",
              "value": true
            },
            {
              "id": "c4",
              "name": "telegram_chat_id",
              "type": "string",
              "value": "PUT_YOUR_TELEGRAM_CHAT_ID"
            },
            {
              "id": "c5",
              "name": "fear_threshold",
              "type": "number",
              "value": 25
            },
            {
              "id": "c6",
              "name": "greed_threshold",
              "type": "number",
              "value": 75
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "eb5d2681-d950-4c60-8d41-10cd410abe78",
      "name": "Fetch Fear & Greed Index",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueErrorOutput",
      "position": [
        1648,
        1488
      ],
      "parameters": {
        "url": "https://api.alternative.me/fng/",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "1"
            },
            {
              "name": "format",
              "value": "json"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "c8c54244-a6d5-4776-bd98-d89d6509ce64",
      "name": "Classify Market Regime",
      "type": "n8n-nodes-base.code",
      "position": [
        1872,
        1488
      ],
      "parameters": {
        "jsCode": "// Parse the Fear & Greed Index value and classify the market regime.\n// API shape: { data: [ { value: \"24\", value_classification: \"Extreme Fear\", timestamp: \"...\" } ] }\nconst cfg = $('Set Bot Configuration').first().json;\nconst fearT = Number(cfg.fear_threshold);\nconst greedT = Number(cfg.greed_threshold);\n\nconst body = $input.first().json;\nconst entry = Array.isArray(body?.data) ? body.data[0] : (body?.data || body);\nconst value = Number(entry?.value);\n\nif (!Number.isFinite(value)) {\n  throw new Error('Could not read Fear & Greed value from API response \u2014 check the endpoint shape.');\n}\n\nlet regime, action;\nif (value <= fearT) {\n  regime = 'risk_off';      // extreme fear -> stop bot\n  action = 'stop';\n} else if (value >= greedT) {\n  regime = 'risk_on';       // extreme greed -> start bot\n  action = 'start';\n} else {\n  regime = 'neutral';       // do nothing\n  action = 'none';\n}\n\nconst ts = Number(entry?.timestamp);\nreturn [{ json: {\n  value,\n  classification: entry?.value_classification || '',\n  regime,\n  action,\n  fear_threshold: fearT,\n  greed_threshold: greedT,\n  index_time: Number.isFinite(ts) ? new Date(ts * 1000).toISOString() : null\n} }];"
      },
      "typeVersion": 2
    },
    {
      "id": "ee2e5106-dee8-4fef-a5c4-ca12c606bdeb",
      "name": "If Extreme Fear",
      "type": "n8n-nodes-base.if",
      "position": [
        2128,
        1488
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cond-fear",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.regime }}",
              "rightValue": "risk_off"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "f51732b5-3f20-4103-9d96-eab309e03edf",
      "name": "If Extreme Greed",
      "type": "n8n-nodes-base.if",
      "position": [
        2352,
        1664
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cond-greed",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.regime }}",
              "rightValue": "risk_on"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "ac15bbd6-874c-4aa0-87b6-51d87e9dd485",
      "name": "Deactivate Bot Risk-Off",
      "type": "n8n-nodes-gainium.Gainium",
      "onError": "continueErrorOutput",
      "position": [
        2384,
        928
      ],
      "parameters": {
        "botId": "={{ $('Set Bot Configuration').item.json.bot_id }}",
        "botType": "={{ $('Set Bot Configuration').item.json.bot_type }}",
        "operation": "stop",
        "paperContext": "={{ $('Set Bot Configuration').item.json.paper_trading }}"
      },
      "typeVersion": 2
    },
    {
      "id": "b132e510-0d4e-4d67-8f9a-d3926ef31262",
      "name": "Activate Bot Risk-On",
      "type": "n8n-nodes-gainium.Gainium",
      "onError": "continueErrorOutput",
      "position": [
        2608,
        1136
      ],
      "parameters": {
        "botId": "={{ $('Set Bot Configuration').item.json.bot_id }}",
        "botType": "={{ $('Set Bot Configuration').item.json.bot_type }}",
        "operation": "start",
        "paperContext": "={{ $('Set Bot Configuration').item.json.paper_trading }}"
      },
      "typeVersion": 2
    },
    {
      "id": "4c32e282-0b22-4ca1-b6da-952bb15a7723",
      "name": "Notify Risk-Off Mode",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2608,
        928
      ],
      "parameters": {
        "text": "=\ud83d\uded1 *Risk-off \u2014 bot stopped*\n\n*Fear & Greed:* {{ $('Classify Market Regime').item.json.value }} ({{ $('Classify Market Regime').item.json.classification }})\n*Threshold:* \u2264 {{ $('Classify Market Regime').item.json.fear_threshold }}\n*Bot:* {{ $('Set Bot Configuration').item.json.bot_id }}\n*Mode:* {{ $('Set Bot Configuration').item.json.paper_trading ? '\ud83e\uddea paper' : '\ud83d\udd34 live' }}\n*Time:* {{ $now.format('MMM d, yyyy HH:mm') }}\n\nExtreme fear detected \u2014 the bot was stopped (open deals left untouched).",
        "chatId": "={{ $('Set Bot Configuration').item.json.telegram_chat_id }}",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "0b9990f2-fe4d-4ea3-8cce-2c8d5a2f1209",
      "name": "Notify Risk-On Mode",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2832,
        1136
      ],
      "parameters": {
        "text": "=\ud83d\udfe2 *Risk-on \u2014 bot started*\n\n*Fear & Greed:* {{ $('Classify Market Regime').item.json.value }} ({{ $('Classify Market Regime').item.json.classification }})\n*Threshold:* \u2265 {{ $('Classify Market Regime').item.json.greed_threshold }}\n*Bot:* {{ $('Set Bot Configuration').item.json.bot_id }}\n*Mode:* {{ $('Set Bot Configuration').item.json.paper_trading ? '\ud83e\uddea paper' : '\ud83d\udd34 live' }}\n*Time:* {{ $now.format('MMM d, yyyy HH:mm') }}\n\nExtreme greed detected \u2014 the bot was started.",
        "chatId": "={{ $('Set Bot Configuration').item.json.telegram_chat_id }}",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d9ec6112-16c8-4ab4-9ae5-a543db881ca5",
      "name": "Notify Neutral Mode",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2608,
        1792
      ],
      "parameters": {
        "text": "=\u26aa *Neutral \u2014 no change*\n\n*Fear & Greed:* {{ $('Classify Market Regime').item.json.value }} ({{ $('Classify Market Regime').item.json.classification }})\n*Band:* {{ $('Classify Market Regime').item.json.fear_threshold }} \u2013 {{ $('Classify Market Regime').item.json.greed_threshold }}\n*Bot:* {{ $('Set Bot Configuration').item.json.bot_id }}\n*Time:* {{ $now.format('MMM d, yyyy HH:mm') }}\n\nThe index sits in the neutral band \u2014 the bot was left as-is.",
        "chatId": "={{ $('Set Bot Configuration').item.json.telegram_chat_id }}",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ee6b2ebf-b933-41df-a15c-d8b3534efb2f",
      "name": "Alert Bot Action Failed",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2832,
        960
      ],
      "parameters": {
        "text": "=\ud83d\udea8 *Gainium bot action failed*\n\n*Error:* {{ $json.error?.message || $json.error || 'unknown' }}\n*Time:* {{ $now.format('MMM d, yyyy HH:mm') }}\n\nThe regime switch could not start/stop the bot. Check your Gainium API credential and bot id.",
        "chatId": "PUT_YOUR_TELEGRAM_CHAT_ID",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "423f8578-f835-4a08-9380-f5be34528b10",
      "name": "Alert Index Fetch Failed",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1712,
        1872
      ],
      "parameters": {
        "text": "=\ud83d\udea8 *Fear & Greed Index fetch failed*\n\n*Error:* {{ $json.error?.message || $json.error || 'unknown' }}\n*Time:* {{ $now.format('MMM d, yyyy HH:mm') }}\n\nNo regime switch was performed. Check the alternative.me API / network.",
        "chatId": "PUT_YOUR_TELEGRAM_CHAT_ID",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "30d54a89-0a7b-47e6-9578-82ca4df0d634",
      "name": "On Global Error",
      "type": "n8n-nodes-base.errorTrigger",
      "position": [
        1488,
        2016
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "b52fa0d4-3211-4400-bf7e-871d405ab01c",
      "name": "Alert Workflow Error",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1712,
        2016
      ],
      "parameters": {
        "text": "=\ud83d\udea8 *Gainium risk-switch workflow error*\n\n*Workflow:* {{ $workflow.name }}\n*Failed node:* {{ $execution.lastNodeExecuted }}\n*Execution:* {{ $execution.id }}\n*Time:* {{ $now.format('MMM d, yyyy HH:mm') }}\n\nCheck the n8n execution log.",
        "chatId": "PUT_YOUR_TELEGRAM_CHAT_ID",
        "additionalFields": {
          "parse_mode": "Markdown",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "5da5c4ff-99ca-4da9-b6f0-76ed9675375c",
  "connections": {
    "If Extreme Fear": {
      "main": [
        [
          {
            "node": "Deactivate Bot Risk-Off",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If Extreme Greed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On Global Error": {
      "main": [
        [
          {
            "node": "Alert Workflow Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Extreme Greed": {
      "main": [
        [
          {
            "node": "Activate Bot Risk-On",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Neutral Mode",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Daily at 08:00": {
      "main": [
        [
          {
            "node": "Set Bot Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Activate Bot Risk-On": {
      "main": [
        [
          {
            "node": "Notify Risk-On Mode",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert Bot Action Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Bot Configuration": {
      "main": [
        [
          {
            "node": "Fetch Fear & Greed Index",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Market Regime": {
      "main": [
        [
          {
            "node": "If Extreme Fear",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Deactivate Bot Risk-Off": {
      "main": [
        [
          {
            "node": "Notify Risk-Off Mode",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert Bot Action Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Fear & Greed Index": {
      "main": [
        [
          {
            "node": "Classify Market Regime",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert Index Fetch Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}