Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

N8N Algo Trading Workflow Automation using OpenAlgo

4 min read

Are you a trader looking to automate your strategies, but intimidated by complex coding? Do you wish you could easily trigger orders based on specific times, integrate with other platforms, and streamline your entire trading process? The combination of OpenAlgo and n8n makes this a reality, even without writing a single line of code!

This blog post will introduce you to these two powerful tools and demonstrate a basic example: creating a time-based straddle order workflow.

What is OpenAlgo?

OpenAlgo is a personal algo trading platform that allows you to connect your trading strategies from various sources (like Amibroker, TradingView, Python, Excel, or Google Sheets) to top Indian brokers. It provides a robust infrastructure for automating order placement, managing positions, and analyzing your trades.

What is n8n?

n8n (pronounced “n-eight-n”) is a workflow automation tool that lets you connect different applications and services without needing to write any custom code. It’s a “low-code” or “no-code” platform, meaning you can create complex workflows visually, using a drag-and-drop interface.

Key Benefits of n8n for Traders:

  • No-Code Automation: Build workflows without extensive programming knowledge.
  • Cross-Platform: n8n can be installed on Windows, Linux, and macOS. You can even self-host it on your own servers for complete control.
  • Open Source: n8n has a free, open-source version for personal use, giving you transparency and flexibility. There is also a cloud-based version.
  • Extensive Integrations: n8n connects with hundreds of applications, including trading platforms, brokers, databases, messaging services (like Telegram and Slack), and even AI models.
  • Flexibility: Create custom workflows, from simple tasks (like sending order notifications) to complex strategies involving multiple steps and conditions.
  • Scheduled Actions: Trigger workflows based on time (e.g., place orders at specific times), events (e.g., when a price alert is triggered), or webhooks.

How OpenAlgo + n8n Transforms Trading Automation

By combining OpenAlgo and n8n, you can:

  1. Schedule Order Placement: Execute trades automatically at pre-defined times, perfect for strategies like time-based straddles or market-on-open orders.
  2. Integrate with External Data: Fetch data from APIs (e.g., real-time market data) and use it within your trading logic.
  3. Create Notifications: Send alerts via Telegram, Slack, or email when orders are placed, filled, or when specific conditions are met.
  4. Process and Transform Data: Clean, format, and analyze market data before making trading decisions.
  5. Build Complex Workflows: Chain multiple actions together (e.g., check a condition, place an order, send a notification, update a spreadsheet).

Pre-Requisites

Before you start, you’ll need the following:

  1. OpenAlgo: Install and configure OpenAlgo, ensuring your broker is connected. You can find instructions on the OpenAlgo website.
  2. Node.js: n8n relies on Node.js, so you’ll need to install it. Download the LTS (Long-Term Support) version from the official Node.js website.
  3. n8n: We’ll be using the open-source, self-hosted version of n8n in this example.
  4. Broker API Key: You’ll need your broker’s API key and API secret (from OpenAlgo).
  5. Basic Understanding of Cron Expressions: Cron expressions are used to define schedules. While n8n provides a helpful interface, a basic understanding is useful.

Building a Simple Time-Based Straddle with n8n and OpenAlgo

Let’s walk through the process of creating a workflow that places a NIFTY straddle order at a specific time.

Step 1: Install Node.js

Download and install the LTS version of Node.js from the official website. After installation, open your command prompt (or terminal) and verify the installation by typing:

node -v
npm -v

You should see the version numbers of Node.js and npm (Node Package Manager) displayed.

Step 2: Install n8n

Open your command prompt (as an administrator) and run the following command:

npm install -g n8n

This command installs n8n globally on your system.

Step 3: Start n8n

Once installed, start n8n by running:

n8n

This will start the n8n server. You’ll see logs indicating that n8n is running, and it will provide a URL (usually http://localhost:5678) to access the editor.

Step 4: Create Your n8n Workflow

  1. Open a web browser and go to the URL provided by n8n (e.g., http://localhost:5678).
  2. Click on “Create Workflow.”
  3. Name your workflow (e.g., “Time Based Straddle – OpenAlgo”).

Step 5: Add a Schedule Trigger

  1. Click the “+” button to add the first step.
  2. Search for “Schedule Trigger” and select it.
  3. In the “Trigger Interval” section, choose “Custom (Cron).”
  4. Enter a Cron expression to define when the workflow should run. For example, to run at 11:41 AM every day, you would use:
    <strong>0 41 11 * * *</strong>
    • 0: Seconds (optional)
    • 41: Minute
    • 11: Hour (24-hour format)
    • *: Day of the month (any day)
    • *: Month (any month)
    • *: Day of the week (any day)
  5. Important: go to triple dot menu in the top bar. Go to settings and change the Time Zone from default (America/NewYork) to Asia/Kolkata

Step 6: Add an HTTP Request Node (for the Call Option)

  1. Click the “+” button connected to the Schedule Trigger.
  2. Search for “HTTP Request” and select it.
  3. Configure the node:
    • Method: Set to “POST”.
    • URL: Enter your OpenAlgo API endpoint for placing orders. This is typically something like: http://127.0.0.1:5000/api/v1/placeorder.
    • Authentication: Set to “None” (for this example, we’re using the API key in the body).
    • Send Body: Enable this option.
    • Body Content Type: Select “JSON”.
    • Specify Body: Select “Using JSON”.
  4. In the JSON section, paste the following, replacing "your_app_apikey" with your actual OpenAlgo API key and modifying the symbol for your desired call option:
{
    "apikey": "your_app_apikey",
    "strategy": "n8n",
    "symbol": "NIFTY06MAR2522100CE",
    "action": "SELL",
    "exchange": "NFO",
    "pricetype": "MARKET",
    "product": "MIS",
    "quantity": "75"
}

Step 7: Duplicate the HTTP Request Node (for the Put Option)

  1. Right-click on the “HTTP Request” node.
  2. Select “Duplicate”.
  3. Connect the Schedule Trigger to the newly duplicated node.
  4. Double-click the duplicated node.
  5. Change the symbol to the corresponding put option (e.g., NIFTY06MAR2522100PE). All other settings remain the same.

Step 8: Save and Activate

  1. Give a name to your workflow at the top.
  2. Click the “Save” button in the top right corner.
  3. Click the “Inactive” toggle in the top right corner to change it to “Active.”

Step 9: Test Your Workflow (Optional)

  • Test Workflow Button: You can manually test the workflow by clicking the “Test workflow” button at the bottom of the canvas. This will trigger the workflow immediately. Important: If you’re in Live Mode in OpenAlgo, this will place real orders! Use Analyze Mode in OpenAlgo for testing.
  • Wait for the Schedule: If you activate the workflow, it will automatically run at the time specified in your Schedule Trigger.

Monitoring Your Orders

After the workflow runs, you can check your OpenAlgo Order Book to see the placed orders.

Important Considerations:

  • Error Handling: In a real-world scenario, you would want to add error handling to your workflow. n8n provides nodes for catching errors and taking appropriate actions (e.g., sending notifications, retrying).
  • Live vs. Analyze Mode: Always test your workflows thoroughly in OpenAlgo’s Analyze Mode before switching to Live Mode.
  • Advanced Logic: This is a very basic example. You can extend the workflow to include more complex logic, data transformations, and integrations with other services.

This comprehensive guide provides a foundational understanding of how to install and use n8n in combination with OpenAlgo to automate trading tasks. You are also able to find detailed documentation of OpenAlgo API here

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More