AI-Powered Algorithmic Stock Trading and Technical Analysis - n8n Workflow

Automate algorithmic stock trading using this complex n8n workflow. It combines LangChain AI agents, technical analysis, news sentiment, and Alpaca trading tools.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Quantitative traders and finance professionals looking to automate complex strategies.

  • Users familiar with n8n and interested in implementing advanced AI agents (LangChain) for real-time decision-making.

  • Individuals seeking pre-built n8n templates for integrating technical indicators (Bollinger Bands, MACD) and news sentiment into a trading system.

  • Developers wanting to connect n8n with broker APIs like Alpaca (implied by tool names) and PostgreSQL for robust logging.

Overview

Executing profitable, automated trading strategies requires combining technical analysis, fundamental data, and swift execution. This comprehensive n8n workflow solves the difficulty of integrating these disparate systems by leveraging sophisticated AI agents.

Starting with an n8n trigger, the workflow initiates a MAIN AGENT powered by Grok 4 (via OpenRouter), which consults market data (sectors, industries) and proprietary knowledge stored in a Supabase RAG system. The key value proposition is its ability to chain specialized agents: a Strategy Agent for high-level stock picking, a Trend + Technical Agent for deep indicator analysis (fetching Bollinger Bands, MACD, and custom support/resistance levels via HTTP requests and n8n code nodes), and finally, a Trader Agent responsible for managing capital, calculating order sizes, and executing trades via POST/GET endpoints (e.g., Alpaca). This robust n8n node architecture ensures that trade decisions are data-driven, highly optimized, and automatically logged via PostgreSQL.

How it Works

The entire process begins with a Schedule Trigger, ensuring the n8n workflow runs at specific market intervals.


  1. Initial Scan and Filtering: The schedule triggers an HTTP Request to pull a universe of symbols, which are then narrowed down by a Filter n8n node.

  2. Main AI Decision: The filtered data is passed to the MAIN AGENT. This primary agent uses a Grok 4 language model and retrieves contextual data from a Supabase Vector Store (using OpenAI Embeddings) and external HTTP tools providing ranking, sector, and industry data.

  3. Strategic Analysis: The MAIN AGENT calls the Strategy Agent (another dedicated n8n agent) to analyze current cash balances and existing trades.

  4. Technical Deep Dive: The Call trend + technical Agent initiates a critical sub-process. This includes using a Set n8n node to define the target stock and API key, followed by multiple HTTP Request n8n nodes to fetch price history, Bollinger Bands, and MACD indicators. An n8n code node calculates custom support and resistance levels.

  5. News and Sentiment: Concurrently, news data is fetched, processed, and aggregated to provide the AI with up-to-date sentiment.

  6. Final Technical Summary: The gathered data is summarized by an OpenAI n8n node, creating a structured JSON payload for the final agent.

  7. Trade Execution: The data flows to the Trader Agent. This specialized agent uses tools like GET endpoints and POST endpoints (for brokerage API interaction) and a Calculator n8n node to determine position size. If a trade is executed, a PostgresTool n8n node is used to save the order information.

  8. Reporting: Finally, the overall analysis and resulting actions from the MAIN AGENT are formatted using Markdown to HTML and delivered via a Gmail n8n node, completing the powerful automated n8n workflow.

Installation Guide

To deploy this expert n8n workflow, follow these steps:


  1. Import: Copy the entire JSON code and paste it into your n8n instance via the 'New' menu > 'Import from JSON'.

  2. Credentials Setup: This n8n workflow requires credentials for:

OpenRouter: For the Grok 4, Anthropic, and various other LLMs used by the n8n agents.
OpenAI: For the embeddings and potentially the First Technical Analysis node.
Supabase: Credentials for the Vector Store (RAG database).
PostgreSQL: Credentials for the database used by the save order info n8n node and current trades monitoring nodes.
Gmail: For the Send report n8n node.
HTTP Requests: Credentials/API keys for the financial data providers (Alpaca/market data endpoints).

  1. Agent Configuration: Review the MAIN AGENT, Strategy Agent, and Trader Agent n8n nodes. Ensure the tool configurations (e.g., API base URLs in the HTTP Request Tools and Postgres tools) match your trading environment. The parameters within the n8n templates must be updated with your specific data structures and environment variables.

  2. Activate: Enable the Schedule Trigger n8n trigger to start the automated trading sequence.

Node Details

Schedule Trigger (n8n trigger): Initiates the entire n8n workflow on a defined schedule (e.g., every morning before market open).
MAIN AGENT (n8n node): The primary LangChain agent using Grok 4 (via OpenRouter). It orchestrates the strategic decision-making and uses multiple RAG tools and nested workflow calls.
Supabase Vector Store / Embeddings OpenAI: Provides the MAIN AGENT with historical or proprietary market context using Retrieval-Augmented Generation (RAG). This is a critical n8n node for data context.
HTTP Request (Get Price History, Get Bollinger Bands, Get MACD): These n8n nodes fetch the raw technical data required for analysis. This demonstrates how this n8n template integrates external financial APIs.
Code (Calculate Support Resistance, Organizing Data, Warp as JSON): Custom n8n code nodes used to perform complex calculations on raw price data and structure the final payload for the AI agents. Essential for proprietary trading logic.
First Technical Analysis (OpenAI n8n node): Summarizes the raw technical indicators into a clear, actionable analysis for subsequent agents.
Trader Agent (n8n node): The final LangChain agent responsible for checking cash, calculating order size, and using the POST endpoints tool to execute trades via the brokerage API.
Postgres Tool (save order info): Logs all executed trades and relevant metadata to a PostgreSQL database for auditing and monitoring, ensuring this n8n workflow maintains data integrity.


  • Gmail (Send report): Sends a summary report of the agent's findings and actions after the workflow completes, triggered by the final decision path.

Related n8n Workflows

Paid

Nodes: 27 Nodes
Updated: December 26 2025
View all
Created by

Automation expert & n8n power user. I build advanced workflows combining AI, outbound, and business logic. Grab my templates or reach out for custom builds.

Featured*