{
  "name": "Dobby",
  "nodes": [
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "940ed6ec-f5cc-47d3-9894-8eef9e47b6e0",
              "leftValue": "={{ $json.data[0].code }}",
              "rightValue": 404,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -720,
        -48
      ],
      "id": "c366d720-86e5-4dc9-8d43-1aafb2cfd15b",
      "name": "404"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "940ed6ec-f5cc-47d3-9894-8eef9e47b6e0",
              "leftValue": "={{ $json.data[0].code }}",
              "rightValue": 429,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -496,
        48
      ],
      "id": "cb71977d-e01e-444b-b944-a5d1ca89ab80",
      "name": "429"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 1 * * *"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -736,
        -384
      ],
      "id": "1220642e-d318-4724-af72-bfa9d57bb001",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "url": "https://api.twelvedata.com/time_series?",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "=apikey",
              "value": "={{ $json.apikey1m }}"
            },
            {
              "name": "symbol",
              "value": "={{ $json.symbol }}"
            },
            {
              "name": "interval",
              "value": "1min"
            },
            {
              "name": "outputsize",
              "value": "100"
            },
            {
              "name": "exchange",
              "value": "BINANCE"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -288,
        -672
      ],
      "id": "0a66d987-7f2f-4aac-90fd-5960e83df186",
      "name": "1min"
    },
    {
      "parameters": {
        "numberInputs": 3
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        -64,
        -496
      ],
      "id": "413d6ecc-0d14-4ca1-a935-a165c6db7438",
      "name": "Merge"
    },
    {
      "parameters": {
        "url": "https://api.twelvedata.com/time_series?",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "=apikey",
              "value": "={{ $json.apikey15m }}"
            },
            {
              "name": "symbol",
              "value": "={{ $json.symbol }}"
            },
            {
              "name": "interval",
              "value": "15min"
            },
            {
              "name": "outputsize",
              "value": "100"
            },
            {
              "name": "exchange",
              "value": "BINANCE"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -288,
        -480
      ],
      "id": "44a829fa-40fc-40f7-9c22-39a9802729bc",
      "name": "15min"
    },
    {
      "parameters": {
        "url": "https://api.twelvedata.com/time_series?",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "=apikey",
              "value": "={{ $json.apikey1h }}"
            },
            {
              "name": "symbol",
              "value": "={{ $json.symbol }}"
            },
            {
              "name": "interval",
              "value": "1h"
            },
            {
              "name": "outputsize",
              "value": "100"
            },
            {
              "name": "exchange",
              "value": "BINANCE"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -288,
        -288
      ],
      "id": "94c1245d-bb5e-4c7c-a31c-475fecf67919",
      "name": "1h"
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        160,
        -480
      ],
      "id": "d89aa258-3135-484b-bfbc-7c3a62662d26",
      "name": "Aggregate"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.fireworks.ai/inference/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer fw_3ZN44gAXzejooyEFaJd6wPz2"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n    \"model\": \"accounts/sentientfoundation/models/dobby-unhinged-llama-3-3-70b-new\",\n    \"messages\": [\n      {\n        \"role\": \"user\",\n        \"content\": \"{{ $json.prompt }}\"\n      }\n    ],\n    \"max_tokens\": 512,\n    \"temperature\": 0.6,\n    \"top_p\": 1,\n    \"top_k\": 40,\n    \"presence_penalty\": 0,\n    \"frequency_penalty\": 0\n  }",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -272,
        400
      ],
      "id": "5ac566d0-8723-4819-a3b3-8a5b3a59fa68",
      "name": "Dobby"
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -736,
        -576
      ],
      "id": "94c38636-759b-4cdc-b5aa-7438ccb84e7b",
      "name": "Ticker",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $node[\"Ticker\"].json[\"message\"][\"from\"][\"id\"] }}",
        "text": "={{ $node[\"Dobby\"].json[\"choices\"][\"0\"][\"message\"][\"content\"] }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -48,
        400
      ],
      "id": "ff3d3b7a-c6e5-4bc7-b4d0-cbdb51152192",
      "name": "TG Content",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9d47149c-4ea8-4430-81e2-3ca58dd743a3",
              "name": "=prompt",
              "value": "=\nProvide your answer as plain text with bullet points and clear line breaks.\n\n{{ $json.ticker }}\nTechnical Data (candles):\n{{ JSON.stringify($json.candles1m) }}\n{{ JSON.stringify($json.candles15m) }}\n{{ JSON.stringify($json.candles1h) }}\n\n\nEach candle object has:\n- timeframe: \"1m\", \"15m\" or \"1h\"\n- candles: [openTime, open, high, low, close, volume, ...]\n\nSteps for the agent:\n1. Group candles by timeframe (1m, 15m, 1h).\n2. Use 1m & 15m action + indicators (RSI, MACD, trendlines) for optimal entry.\n3. Confirm trend with 1h data.\n4. Factor in sentiment to refine the Buy/Sell/Hold call.\n\n### Response format (no extra text!):\n###  {{ $json.ticker }}\n\u2022 Technical Recommendation:\n< BUY | SELL | HOLD>\n\u2022 Entry Price: <number>\n\u2022 Stop-Loss: \u2039number>\n\u2022 Target/Exit Price:\n\n=====================",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -48,
        144
      ],
      "id": "012a4bcb-416c-4061-8c86-9e3aa8200a7c",
      "name": "Prompt"
    },
    {
      "parameters": {
        "jsCode": "const prompt = $input.first().json.prompt;\n\n// JSON array'i g\u00fcvenli \u015fekilde bulmak i\u00e7in daha esnek bir regex\nconst match = prompt.match(/\\[\\s*\\{[\\s\\S]*?\\}\\s*\\]/);\n\nif (!match) {\n  throw new Error(\"Prompt i\u00e7inde JSON format\u0131nda bir dizi bulunamad\u0131.\");\n}\n\ntry {\n  const jsonArrayStr = match[0];\n  const parsedArray = JSON.parse(jsonArrayStr);\n\n  return [{ json: { candles: parsedArray } }];\n} catch (err) {\n  throw new Error(\"JSON parse hatas\u0131: \" + err.message);\n}"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -720,
        400
      ],
      "id": "7c051a84-a63a-485e-adc8-79a848eed4e9",
      "name": "Prompt Check"
    },
    {
      "parameters": {
        "jsCode": "const candles1m = $json.candles;\n\n// 15m ve 1h mumlar\u0131, son 15 ve 60 adet 1 dakikal\u0131k mum \u00fczerinden hesaplan\u0131r\nconst groupCandles = (source, interval) => {\n  const grouped = [];\n  for (let i = 0; i < source.length; i += interval) {\n    const slice = source.slice(i, i + interval);\n    if (slice.length === 0) continue;\n    grouped.push({\n      time: slice[0].time,\n      open: slice[0].open,\n      high: Math.max(...slice.map(c => c.high)),\n      low: Math.min(...slice.map(c => c.low)),\n      close: slice[slice.length - 1].close,\n    });\n  }\n  return grouped;\n};\n\nconst candles15m = groupCandles(candles1m, 15);\nconst candles1h = groupCandles(candles1m, 60);\n\n// Formatlama\nconst formatCandles = (candles) =>\n  candles.map(c => \n    `Time: ${c.time}, Open: ${c.open}, High: ${c.high}, Low: ${c.low}, Close: ${c.close}`\n  ).join('\\n');\n\n// \u00d6zetler\nconst summary1m = formatCandles(candles1m.slice(-5)); // son 5 mum\nconst summary15m = formatCandles(candles15m.slice(-3)); // son 3 grup\nconst summary1h = formatCandles(candles1h.slice(-2)); // son 2 grup\n\n// Prompt\nconst finalPrompt = `You are a professional crypto day trader. Analyze the price action across multiple timeframes: 1-minute, 15-minute, and 1-hour candles for BTC. Identify trend direction, momentum, and possible entries or reversals. Your response must be based only on the candle data provided. Do NOT use sentiment or external data. Be clear and concise.\\n\\nUse this format ONLY:\\nTechnical Recommendation: <BUY | SELL | HOLD>\\nEntry Price: <number>\\nStop-Loss: <number>\\nTarget/Exit Price: <number>\\n\\n---\\n1-Minute Candles:\\n${summary1m}\\n\\n15-Minute Candles:\\n${summary15m}\\n\\n1-Hour Candles:\\n${summary1h}`;\n\nconst safePrompt = finalPrompt.replace(/\\n/g, \"\\\\n\");\n\nreturn [{\n  json: {\n    prompt: safePrompt\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -496,
        400
      ],
      "id": "5dfcc499-ab0d-42b8-ba9c-7ad7b02487c9",
      "name": "Prompt Safe"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d738af6d-8610-4f3f-b8af-a6eed2a7dd9f",
              "name": "apikey1m",
              "value": "c725c96196424a09a1273123a7dbbca9",
              "type": "string"
            },
            {
              "id": "a4a7bc9a-d348-4bc0-a8dd-b0ccd3f2e940",
              "name": "=symbol",
              "value": "={{$json.message.text.toUpperCase() }}/USD",
              "type": "string"
            },
            {
              "id": "6742a693-b828-4392-b830-9d795f01dc60",
              "name": "apikey15m",
              "value": "5a1c5d582335446ab97b8c59fe31e52f",
              "type": "string"
            },
            {
              "id": "a88a0395-09f0-488d-ae80-de46ece96243",
              "name": "apikey1h",
              "value": "7565020ce0504c1ca32de3e83e76f8b1",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -512,
        -480
      ],
      "id": "cacf0ce8-65cb-49e9-b6e6-432216d0c49e",
      "name": "User Ticker"
    },
    {
      "parameters": {
        "chatId": "470720210",
        "text": "=INVALID TICKER",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -496,
        -144
      ],
      "id": "ee0464e6-6022-4d3d-a149-f36b2bb7e007",
      "name": "TG 404 Error",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "470720210",
        "text": "=API throttled. 8 requests per minute allowed.",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -272,
        -48
      ],
      "id": "97b9eb54-623c-4e80-a2fb-ab18659f818b",
      "name": "TG 429 Error",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const raw = items[0].json.data;\n\n// \ud83d\udd27 Candle verilerini normalize eden yard\u0131mc\u0131 fonksiyon\nfunction normalize(values) {\n  return values\n    .map(v => ({\n      time: new Date(v.datetime),\n      open: parseFloat(v.open),\n      high: parseFloat(v.high),\n      low: parseFloat(v.low),\n      close: parseFloat(v.close),\n      volume: parseFloat(v.volume ?? 0)\n    }))\n    .sort((a, b) => a.time - b.time);\n}\n\nconst result = {\n  ticker: \"NEAR/USD\",\n  candles1m: [],\n  candles15m: [],\n  candles1h: []\n};\n\n// \ud83d\udd04 T\u00fcm entry'leri d\u00f6n \u2192 her biri farkl\u0131 interval\nfor (const entry of raw) {\n  const interval = entry.meta?.interval;\n  const values = entry.values ?? [];\n\n  if (interval === \"1min\") {\n    result.candles1m = normalize(values);\n  } else if (interval === \"15min\") {\n    result.candles15m = normalize(values);\n  } else if (interval === \"1h\") {\n    result.candles1h = normalize(values);\n  }\n}\n\nreturn [{ json: result }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -272,
        144
      ],
      "id": "334a3a3f-5a3b-4ca1-a4bf-e1879c13d72b",
      "name": "Arrange Data"
    }
  ],
  "connections": {
    "404": {
      "main": [
        [
          {
            "node": "TG 404 Error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "429",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "429": {
      "main": [
        [
          {
            "node": "TG 429 Error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Arrange Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "User Ticker",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1min": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "15min": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "1h": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "404",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dobby": {
      "main": [
        [
          {
            "node": "TG Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ticker": {
      "main": [
        [
          {
            "node": "User Ticker",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TG Content": {
      "main": [
        []
      ]
    },
    "Prompt": {
      "main": [
        [
          {
            "node": "Prompt Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prompt Check": {
      "main": [
        [
          {
            "node": "Prompt Safe",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prompt Safe": {
      "main": [
        [
          {
            "node": "Dobby",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "User Ticker": {
      "main": [
        [
          {
            "node": "15min",
            "type": "main",
            "index": 0
          },
          {
            "node": "1min",
            "type": "main",
            "index": 0
          },
          {
            "node": "1h",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Arrange Data": {
      "main": [
        [
          {
            "node": "Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0b1ff4f6-bf61-4a7e-b18c-14692badbd51",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "kcp54NZr5wJcIqtb",
  "tags": []
}