n8n, Alpaca, Polygon, NewsAPI Integration Automated News Sentiment Analysis Trading Bot: Real-time Market Reaction Investment Strategy
By utilizing real-time news data to analyze stock market sentiment and building a bot that automatically executes trades based on those results, you can enhance your investment decisions. Use n8n to visually configure complex workflows and integrate external services like Alpaca, Polygon, and NewsAPI to implement real-time data-driven automated investment strategies.
1. The Challenge / Context
The stock market is influenced by various factors, and information such as news articles, in particular, significantly impacts investor sentiment. Traditional investment methods require considerable time and effort to read and analyze news articles directly to make investment decisions. Furthermore, human subjective judgment can lead to emotional errors. It is now time for an automated system that can efficiently process real-time news data and make investment decisions based on objective criteria.
2. Deep Dive: n8n
n8n is a node-based low-code platform that helps users build automated workflows by connecting various applications and services. Users can connect nodes through a drag-and-drop interface and perform various tasks such as data transformation, API calls, and conditional branching within each node. Key features of n8n include:
- Visual Workflow Design: You can visually configure complex logic without coding.
- Extensive Integration Capabilities: Provides over 200 built-in nodes and can integrate with almost any API via the HTTP Request node.
- Flexible Data Processing: Easily transform and manipulate JSON data.
- Self-Hosting Support: Can be installed on a cloud or your own server to ensure data security and control.
3. Step-by-Step Guide / Implementation
Now, let's look at how to integrate Alpaca, Polygon, and NewsAPI using n8n and build a news sentiment analysis-based trading bot, step by step.
Step 1: n8n Installation and Setup
There are several ways to install n8n. You can install it via Docker, npm, or cloud services. Here, we will explain how to use Docker as an example.
# Download Docker image
docker pull n8nio/n8n
# Run container
docker run -d -p 5678:5678 -v ~/.n8n:/home/node/.n8n n8nio/n8n
Executing the command above will run the n8n container in the background. You can access the n8n interface by connecting to http://localhost:5678 via your web browser.
Step 2: NewsAPI Integration and News Data Collection
NewsAPI provides an API that allows you to collect news articles from various news sources. After signing up for NewsAPI and obtaining an API key, connect to NewsAPI using an HTTP Request node in n8n.
// Example HTTP Request node settings (JavaScript expression)
{
"method": "GET",
"url": "https://newsapi.org/v2/everything",
"qs": {
"q": "주식", // Search query
"apiKey": "YOUR_NEWSAPI_KEY", // Issued API key
"sortBy": "publishedAt",
"pageSize": 10
},
"headerParameters": [],
"bodyParameters": []
}
The setting above is a request to search for news articles with the keyword "주식" (stock) and retrieve the 10 most recent articles. You must replace YOUR_NEWSAPI_KEY with your actual API key.
Step 3: Sentiment Analysis API Integration and Sentiment Score Extraction
Pass the title or content of the collected news articles to a sentiment analysis API to extract sentiment scores. Here, we show an example using the Hugging Face Inference API. You need to sign up for Hugging Face and obtain an API key. We will use the "nlptown/bert-base-multilingual-uncased-sentiment" model for sentiment analysis.
// Example HTTP Request node settings (JavaScript expression)
{
"method": "POST",
"url": "https://api-inference.huggingface.co/models/nlptown/bert-base-multilingual-uncased-sentiment",
"headerParameters": [
{
"name": "Authorization",
"value": "Bearer YOUR_HUGGINGFACE_API_KEY" // Issued API key
}
],
"bodyParameters": [
{
"name": "inputs",
"value": "{{ $json.articles[0].title }}" // News title
}
],
"json": true
}
The setting above passes the news article title to the Hugging Face Inference API and receives the sentiment analysis result in JSON format. You must replace YOUR_HUGGINGFACE_API_KEY with your actual API key. The sentiment analysis result is returned in a star rating format, where a higher star rating indicates more positive sentiment. The method for extracting sentiment scores may vary depending on the structure of the result JSON.
Step 4: Polygon API Integration and Stock Data Collection
Polygon API provides real-time stock data. After signing up for Polygon and obtaining an API key, connect to the Polygon API using an HTTP Request node in n8n.
// Example HTTP Request node settings (JavaScript expression)
{
"method": "GET",
"url": "https://api.polygon.io/v2/last/trade/AAPL", // Latest trade information for Apple stock
"qs": {
"apiKey": "YOUR_POLYGON_API_KEY" // Issued API key
}
}
The setting above is a request to retrieve the latest trade information for Apple stock (AAPL). You must replace YOUR_POLYGON_API_KEY with your actual API key.
Step 5: Alpaca API Integration and Automated Trading Execution
Alpaca API provides an API that allows you to automate stock trading. After signing up for Alpaca and obtaining an API key, connect to the Alpaca API using an HTTP Request node in n8n.
// Example HTTP Request node settings (JavaScript expression) - Buy order
{
"method": "POST",
"url": "https://paper-api.alpaca.markets/v2/orders", // Paper Trading API endpoint (use a different endpoint for live trading)
"headerParameters": [
{
"name": "APCA-API-KEY-ID",
"value": "YOUR_ALPACA_API_KEY_ID" // Issued API Key ID
},
{
"name": "APCA-API-SECRET-KEY",
"value": "YOUR_ALPACA_API_SECRET_KEY" // Issued API Key Secret
}
],
"bodyParameters": [
{
"name": "symbol",
"value": "AAPL" // Apple stock
},
{
"name": "qty",
"value": "1" // Quantity to buy
},
{
"name": "side",
"value": "buy" // Buy
},
{
"name": "type",
"value": "market" // Market order
},
{
"name": "time_in_force",
"value": "gtc" // Good 'Til Canceled
}
],
"json": true
}
The setting above is an order to buy 1 share of Apple stock (AAPL) at market price. You


