{
  "name": "Leonex — Bridge Intraday (cada 1h, US market hours, modo FAST)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 10-16 * * 1-5"
            }
          ]
        }
      },
      "id": "trigger-cron-intraday",
      "name": "Cron — cada hora 10-16 NY L-V (= 14-20 UTC)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [200, 320],
      "notes": "Dispara cada hora a en punto, de las 10:00 a 16:00 hora NY (= 14:00-20:00 UTC), lunes a viernes. Son 7 disparos al dia. Esto cubre la sesion US (apertura 09:30 NY = 13:30 UTC, cierre 16:00 NY = 20:00 UTC). IMPORTANTE: n8n self-hosted ejecuta crones en el timezone de la instancia (America/New_York en este caso), NO en el timezone del workflow setting. Por eso el cron va en hora NY."
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://leonex.187.127.86.200.nip.io/api/bridge-intraday?mode=execute&fetch=0",
        "options": {
          "redirect": {"redirect": {"followRedirects": true}},
          "response": {"response": {"neverError": true, "responseFormat": "json"}},
          "timeout": 480000
        },
        "sendHeaders": false,
        "sendBody": false
      },
      "id": "call-bridge-intraday",
      "name": "POST /api/bridge-intraday?mode=execute&fetch=0",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 320],
      "notes": "Llama al endpoint del servidor. Comportamiento adaptativo: fetch=0 por defecto (fast 30-90s, usa cache 1h), PERO el server hace auto-promote a fetch=1 si detecta que el cache 1h es del dia anterior (caso tipico del primer cron del dia, porque el daily corrio pre-market y aun no tenia barras del dia actual). En ese caso, descarga incremental 1h/4h y tarda 3-8 min. Timeout 480s (8 min) para acomodar ese caso. URL HARDCODED porque n8n self-hosted bloquea acceso a $env vars. Tras el primer fetch=1 del dia, los disparos siguientes ya vuelven a ser fast (30-90s)."
    },
    {
      "parameters": {
        "conditions": {
          "options": {"caseSensitive": false, "leftValue": "", "typeValidation": "loose"},
          "conditions": [
            {
              "id": "is-409",
              "leftValue": "={{ $json.error }}",
              "rightValue": "refresh_in_progress",
              "operator": {"type": "string", "operation": "equals"}
            }
          ],
          "combinator": "and"
        }
      },
      "id": "check-409",
      "name": "If 409 (lock busy)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [720, 320],
      "notes": "Si el lock esta tomado por otra accion, registramos y seguimos. El siguiente tick lo reintentara."
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "log",
              "value": "=Lock busy at {{ $now.toISO() }}. Holder: {{ $json.lock_status?.holder }} (age {{ $json.lock_status?.age_seconds }}s). Skipping this tick."
            }
          ]
        }
      },
      "id": "log-409",
      "name": "Log lock-busy skip",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, 200]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "log",
              "value": "=Bridge intraday OK at {{ $now.toISO() }}. Mode: {{ $json.mode }} · Steps: {{ ($json.steps || []).length }}"
            }
          ]
        }
      },
      "id": "log-ok",
      "name": "Log success",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, 440]
    }
  ],
  "connections": {
    "Cron — cada hora 10-16 NY L-V (= 14-20 UTC)": {
      "main": [[{"node": "POST /api/bridge-intraday?mode=execute&fetch=0", "type": "main", "index": 0}]]
    },
    "POST /api/bridge-intraday?mode=execute&fetch=0": {
      "main": [[{"node": "If 409 (lock busy)", "type": "main", "index": 0}]]
    },
    "If 409 (lock busy)": {
      "main": [
        [{"node": "Log lock-busy skip", "type": "main", "index": 0}],
        [{"node": "Log success", "type": "main", "index": 0}]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "saveManualExecutions": true,
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "none",
    "timezone": "UTC"
  },
  "tags": ["leonex", "trading", "intraday", "v2-fast"],
  "meta": {
    "instanceId": "leonex-bridge-intraday-v2-fast"
  },
  "pinData": {},
  "staticData": null
}
