{
  "id": "CLFMiunuAfiKdbZB",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Instruction Email \u2192 Structured Trade Capture",
  "tags": [],
  "nodes": [
    {
      "id": "4b89707a-f532-4407-9916-4416dff8096b",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -128,
        208
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "87ef0715-f17c-4cd9-a96d-b5dfe8c9ba34",
      "name": "Watch for New Trade Instructions",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -400,
        0
      ],
      "parameters": {
        "filters": {
          "q": "subject:\"Trade Instruction\""
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "45aa066d-bf8a-4d9e-8b03-71637550e26b",
      "name": "Map Email Body & Sender",
      "type": "n8n-nodes-base.set",
      "position": [
        -192,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "e301a030-efc7-4560-a70d-f4f2224c3923",
              "name": "email_content",
              "type": "string",
              "value": "={{ $json.snippet }}"
            },
            {
              "id": "64ca98a1-412e-4392-8d23-5c7217b4c147",
              "name": "sender_info",
              "type": "string",
              "value": "={{ $json.From }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "3c7d8ce0-fb60-4ac7-aa3e-225ebdca221f",
      "name": "Extract Trade Details (Gemini AI)",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        16,
        0
      ],
      "parameters": {
        "text": "=You are a Middle Office Trade Capture Assistant. Your task is to extract trade details from the provided email text.\n\nInstructions:\n\nExtract: Asset Name, Ticker, Quantity (number only), Price (number only), and Client Name.\n\nIf the price is \"Market Order,\" return the \"Targeting\" price as a number.\n\nFormat the output as a clean JSON object.\n\nInput Text: > {{ $json.email_content }}",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 3.1
    },
    {
      "id": "623ea09b-54b4-447f-a7e7-556222abd366",
      "name": "Enforce Trade Data Schema",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        192,
        208
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"asset\": \"Apple Inc. (AAPL)\",\n  \"quantity\": 150,\n  \"price\": 185.00,\n  \"client\": \"Alpha Asset Management\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "2112c673-13f8-4376-b39c-25cd35a947e0",
      "name": "Verify Trade Data Validity",
      "type": "n8n-nodes-base.if",
      "position": [
        368,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "ea372277-6ebc-41fa-a450-b780bb022592",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.output.quantity }}",
              "rightValue": 0
            },
            {
              "id": "881c76a2-5e5f-4442-bc63-b567b321bdd2",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.output.asset }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "7aafaa0d-8c36-4f68-ab00-48be16f5f22d",
      "name": "Log Trade to Master Ledger (Google Sheets)",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        560,
        -144
      ],
      "parameters": {
        "columns": {
          "value": {
            "Asset": "={{ $json.output.asset }}",
            "Price": "={{ $json.output.price }}",
            "Client": "={{ $json.output.client }}",
            "Quantity": "={{ $json.output.quantity }}",
            "Timestamp": "={{ $now.toLocaleString() }}"
          },
          "schema": [
            {
              "id": "Asset",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Asset",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Quantity",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Quantity",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Price",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Price",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Client",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Client",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Timestamp",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 767020110,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1a-h9uNZC6y11nhwTXyGm-Wu8vIIWolLFu2NqOdr49dc/edit#gid=767020110",
          "cachedResultName": "\u0936\u0940\u091f2"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1a-h9uNZC6y11nhwTXyGm-Wu8vIIWolLFu2NqOdr49dc",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1a-h9uNZC6y11nhwTXyGm-Wu8vIIWolLFu2NqOdr49dc/edit?usp=drivesdk",
          "cachedResultName": "Knowledge Base Builder."
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "0da81866-9f7e-4515-b2c7-438440a0aa09",
      "name": "Send Confirmation to Client",
      "type": "n8n-nodes-base.gmail",
      "position": [
        752,
        -272
      ],
      "parameters": {
        "message": "=\"Hello, your trade instruction for {{ $node[\"Extract Trade Details (Gemini AI)\"].json.output.quantity }} shares of {{ $node[\"Extract Trade Details (Gemini AI)\"].json.output.asset }} has been successfully captured and logged at {{ $now.toLocaleString() }}.\"",
        "options": {},
        "emailType": "text",
        "messageId": "={{ $('Watch for New Trade Instructions').item.json.id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "017c08d9-3255-48bb-8026-ebf8ed66df01",
      "name": "Notify Ops Team (Success)",
      "type": "n8n-nodes-base.slack",
      "position": [
        768,
        -80
      ],
      "parameters": {
        "text": "=New Trade Captured:{{ $('Extract Trade Details (Gemini AI)').item.json.output }}.json.output.client }} bought {{ $node[\"Extract Trade Details (Gemini AI)\"].json.output.quantity }} of {{ $node[\"Extract Trade Details (Gemini AI)\"].json.output.asset }} @ {{ $node[\"Extract Trade Details (Gemini AI)\"].json.output.price }}.\"",
        "user": {
          "__rl": true,
          "mode": "id",
          "value": "U0AP17N3QRY"
        },
        "select": "user",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.4
    },
    {
      "id": "bd738550-dab8-4d29-8bef-1fd9a3a3a384",
      "name": "Alert Ops Team (Extraction Failed)",
      "type": "n8n-nodes-base.slack",
      "position": [
        768,
        112
      ],
      "parameters": {
        "text": "=CRITICAL: Trade Capture Failed\nSource: {{ $('Watch for New Trade Instructions').item.json.From }}\nIssue: AI could not extract valid trade fields (Asset/Qty/Price).\nManual Action Required.",
        "user": {
          "__rl": true,
          "mode": "id",
          "value": "U0AP17N3QRY"
        },
        "select": "user",
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.4
    },
    {
      "id": "498f26b9-b970-479d-a38d-71c450ef5eee",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -960,
        -432
      ],
      "parameters": {
        "width": 512,
        "height": 480,
        "content": "## Instruction Email \u2192 Structured Trade Capture\n\n## How it works:\nThis workflow automates the extraction of financial trade data from incoming emails using Gemini AI. It cleans the input, uses a structured AI agent to identify key trade fields (Asset, Qty, Price, Client), and validates the data. Valid trades are logged to a master ledger, while ambiguous data is sent to a manual correction queue.\n\n## Setup Steps:\n\nCredentials: Connect your Google (Gmail/Sheets), Slack, and Gemini API accounts.\n\nGmail: Create a label named \"Trade Instruction\" or update the trigger filter.\n\nSheets: Ensure your Google Sheet has headers: Asset, Quantity, Price, Client, and Timestamp.\n\nSlack: Select the specific channel for success and error alerts."
      },
      "typeVersion": 1
    },
    {
      "id": "b6921706-cd5e-4757-94ad-e8eb742f3502",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -416,
        -224
      ],
      "parameters": {
        "color": 7,
        "width": 720,
        "height": 400,
        "content": "## Phase 1: Intake & Intelligence \n(Group Trigger, Edit, AI, Model, Parser)\n\nPhase 1: Data Extraction\nCaptures new emails and uses Gemini AI with a Structured Output Parser to turn messy text into clean trade data. This ensures the output always matches the required spreadsheet columns."
      },
      "typeVersion": 1
    },
    {
      "id": "f439b95d-2902-48b9-8529-933780b2b807",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        320,
        -416
      ],
      "parameters": {
        "color": 7,
        "width": 608,
        "height": 512,
        "content": "## Phase 2: Logic & Execution \n(Group If, Sheets, Reply, Slack Success)\n\nPhase 2: Validation & Processing\nFilters data based on safety rules (e.g., Quantity > 0). If valid, it appends the trade to the ledger and notifies both the client and the team."
      },
      "typeVersion": 1
    },
    {
      "id": "6371e0e7-4816-47be-96c9-c7f963b00440",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        128
      ],
      "parameters": {
        "color": 7,
        "width": 512,
        "height": 336,
        "content": "\n\n\n\n\n\n\n\n\n\n3: Exception Alerting\n\nPhase 3: Manual Intervention Alert > If the AI cannot validate the Asset or Quantity, the workflow routes to this branch. It sends a critical Slack notification detailing the source and issue for immediate manual review."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "48c8a9b1-68bd-400e-a833-235bd480ccc8",
  "connections": {
    "Map Email Body & Sender": {
      "main": [
        [
          {
            "node": "Extract Trade Details (Gemini AI)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Extract Trade Details (Gemini AI)",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Enforce Trade Data Schema": {
      "ai_outputParser": [
        [
          {
            "node": "Extract Trade Details (Gemini AI)",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Notify Ops Team (Success)": {
      "main": [
        []
      ]
    },
    "Verify Trade Data Validity": {
      "main": [
        [
          {
            "node": "Log Trade to Master Ledger (Google Sheets)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Alert Ops Team (Extraction Failed)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Watch for New Trade Instructions": {
      "main": [
        [
          {
            "node": "Map Email Body & Sender",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Trade Details (Gemini AI)": {
      "main": [
        [
          {
            "node": "Verify Trade Data Validity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Trade to Master Ledger (Google Sheets)": {
      "main": [
        [
          {
            "node": "Notify Ops Team (Success)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send Confirmation to Client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}