All Toolsโ€บNews Sentiment Trading Bot
๐Ÿ”ง AI-Powered Trading AutomationApril 26, 2026โœ… Tests passing

News Sentiment Trading Bot

This tool creates an automation pipeline for executing trades based on sentiment analysis of financial news. It fetches news headlines, evaluates sentiment scores, and triggers buy/sell actions based on predefined thresholds. This tool helps automate trading strategies for AI developers and quantitative traders.

What It Does

  • Fetches the latest business news headlines using the News API.
  • Analyzes the sentiment of each headline using Hugging Face's Transformers library.
  • Executes buy or sell trades on the Alpaca trading platform based on sentiment scores.

Installation

1. Clone the repository:

git clone <repository_url>
   cd <repository_directory>

2. Install the required dependencies:

pip install -r requirements.txt

Usage

Run the script with the following command-line arguments:

python news_sentiment_trading_bot.py \
    --news_api_key <YOUR_NEWS_API_KEY> \
    --alpaca_api_key <YOUR_ALPACA_API_KEY> \
    --alpaca_api_secret <YOUR_ALPACA_API_SECRET> \
    --buy_threshold <BUY_THRESHOLD> \
    --sell_threshold <SELL_THRESHOLD>

Arguments

  • --news_api_key: API key for the News API.
  • --alpaca_api_key: API key for the Alpaca trading API.
  • --alpaca_api_secret: API secret for the Alpaca trading API.
  • --buy_threshold: Sentiment score threshold for executing buy orders.
  • --sell_threshold: Sentiment score threshold for executing sell orders.

Source Code

import argparse
import logging
from newsapi import NewsApiClient
from transformers import pipeline
from alpaca_trade_api import REST
import pandas as pd

def fetch_news(api_key):
    try:
        newsapi = NewsApiClient(api_key=api_key)
        headlines = newsapi.get_top_headlines(category='business', language='en', page_size=10)
        articles = headlines.get('articles', [])
        return [article['title'] for article in articles]
    except Exception as e:
        logging.error(f"Error fetching news: {e}")
        return []

def analyze_sentiment(headlines):
    try:
        sentiment_analyzer = pipeline('sentiment-analysis')
        results = []
        for headline in headlines:
            sentiment = sentiment_analyzer(headline)[0]
            score = sentiment['score'] if sentiment['label'].upper() == 'POSITIVE' else -sentiment['score']
            results.append((headline, score))
        return results
    except Exception as e:
        logging.error(f"Error analyzing sentiment: {e}")
        return []

def execute_trades(api_key, api_secret, buy_threshold, sell_threshold, sentiment_scores):
    try:
        alpaca = REST(api_key, api_secret, base_url='https://paper-api.alpaca.markets')
        for headline, score in sentiment_scores:
            if score >= buy_threshold:
                alpaca.submit_order(symbol='AAPL', qty=1, side='buy', type='market', time_in_force='gtc')
                logging.info(f"Executed BUY order for AAPL based on headline: {headline}")
            elif score <= sell_threshold:
                alpaca.submit_order(symbol='AAPL', qty=1, side='sell', type='market', time_in_force='gtc')
                logging.info(f"Executed SELL order for AAPL based on headline: {headline}")
    except Exception as e:
        logging.error(f"Error executing trades: {e}")

def main():
    parser = argparse.ArgumentParser(description="News Sentiment Trading Bot")
    parser.add_argument('--news_api_key', required=True, help="API key for News API")
    parser.add_argument('--alpaca_api_key', required=True, help="API key for Alpaca trading API")
    parser.add_argument('--alpaca_api_secret', required=True, help="API secret for Alpaca trading API")
    parser.add_argument('--buy_threshold', type=float, required=True, help="Sentiment score threshold for buying")
    parser.add_argument('--sell_threshold', type=float, required=True, help="Sentiment score threshold for selling")

    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO)

    headlines = fetch_news(args.news_api_key)
    if not headlines:
        logging.error("No headlines fetched. Exiting.")
        return

    sentiment_scores = analyze_sentiment(headlines)
    if not sentiment_scores:
        logging.error("No sentiment scores generated. Exiting.")
        return

    execute_trades(args.alpaca_api_key, args.alpaca_api_secret, args.buy_threshold, args.sell_threshold, sentiment_scores)

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
news_sentiment_trading_bot
Category
AI-Powered Trading Automation
Generated
April 26, 2026
Tests
Passing โœ…
Fix Loops
3

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-04-26/news_sentiment_trading_bot
cd generated_tools/2026-04-26/news_sentiment_trading_bot
pip install -r requirements.txt 2>/dev/null || true
python news_sentiment_trading_bot.py
News Sentiment Trading Bot โ€” AI Tools by AutoAIForge