Build Your Own Headline News Bot for Stocks Your Watching

Need an Edge in Trading?

Would you like to trade based on headline news? Many algorithms thrive on this strategy, especially in the equities market. This guide will equip you with the tools to build your very own S&P 500 headline news sentiment analyzer. This software will analyze the emotional tone of financial news headlines (bullish, bearish, or neutral) and automatically email the analysis to you. This way, you’re always informed and can at your discretion make trading decisions based on the latest news. The entire system uses Python and open-source tools, meaning it’s completely free to use!

Let’s Get to Building:)

In this write-up, I’m going to show you how you can easily use Python to grab headlines from Google News RSS, perform sentiment analysis, and generate an email digest of your findings. I’ve even made it so you can copy and paste my code with the greatest of ease! Ready? Fire up your favorite IDE, and let’s dive into precisely what you need to do. I’m going to walk you through how to categorize news related to publicly traded companies as bearish or bullish, and send the categorized headlines via email to yourself. This is a pretty cool project combining web scraping, natural language processing (NLP), and email handling in Python (yes, we can always do more, for example we can add machine learning to the process, but for this write-up let’s stick to the basics). First, let’s grab some libraries:

#Libraries needed: feedparser, nltk, yfinance, smtplib
import feedparser
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import yfinance as yf
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#Initial setup'vader_lexicon')

Parsing RSS Feed from Google News

Let’s grab the RSS feed from Google News. We will use the feedparser library in Python, which simplifies the parsing process for us. 

def parse_rss_feed(url):
return feedparser.parse(url)

news_feed = parse_rss_feed("")

Extract and Filter News Headlines

After parsing the feed, we need to identify and filter news articles related to our companies of interest. Your client wanted to focus on S&P 500 companies.  Let’s use the Yahoo Finance Python module “yfinance.” We’ll grab all 500 tickers in two shakes of a lamb’s tail!

#We need to add pandas library for data manipulation
import pandas as pd

#Let's grab all SP 500 companies
def get_sp500_tickers():
df = table[0]
sp500_tickers = df['Symbol'].tolist()
return sp500_tickers
companies = get_sp500_tickers()

#Extract and filter the news
def get_related_news(news_feed, companies):
related_news = []
for entry in news_feed.entries:
for symbol in companies:
if symbol in entry.title:
return related_news

related_news = get_related_news(news_feed, companies)

Now That We Have Our Ingredients, Let’s Mix Things Up

Let’s use the Natural Language Toolkit (NLTK) library’s Vader module to do the headline sentiment analysis for us. Vader is a lexicon and rule-based sentiment analysis tool that is specifically designed for sentiments expressed in social media – I just love the Python community!

def analyze_sentiment(news_list):
sia = SentimentIntensityAnalyzer()
sentiment = {"bullish": [], "neutral": [], "bearish": []}
for news in news_list:
score = sia.polarity_scores(news)
if score['compound'] > 0.05:
elif score['compound'] < -0.05:
return sentiment

Sentiments = analyze_sentiment(related_news)

Emailing the Results

Finally, after all the hard work, we’ll send an email with the bearish or bullish headlines. This part involves constructing an email with the headlines as the body, then using Python’s built-in smtplib and email.mime to send it out.

def send_email(email_recipient, bullish_news, bearish_news):
msg = MIMEMultipart()
msg['From'] = ''
msg['To'] = email_recipient
msg['Subject'] = 'Market Sentiment News Digest'
body = 'Bullish News:\n' + '\n'.join(bullish_news) + '\n\nBearish News:\n' + '\n'.join(bearish_news)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('', 587)
server.login('', 'YourPassword')
text = msg.as_string()
server.sendmail('', email_recipient, text)
send_email('', sentiments["bullish"], sentiments["bearish"])

NOTE: Please remember to replace ‘’ and ‘YourPassword’ with your actual email address and password. However, since the email and password are sensitive, you need to do this in a safe way, which would entail using some sort of secure credential storage solution, but that would be outside the scope of this article. However, if you need professional development help in that area, please feel free to contact me below.

Congrats, You Just Did Sentiment Analysis on News Headlines

Voila! You just scoured the news for headlines, and when you found ones that were related to any of the S&P 500 companies, you sent an email breaking down the sentiment, whether it was bullish or bearish. Now, it is up to you to determine what to do with this news.

In the next write-up, I’ll likely write about using machine learning to help us further in our sentiment analysis, so stay tuned! The above NLTK sentiment analysis is quite simplistic, so adding some machine learning pizzazz may be in order.

To learn more or to make general inquiries, please contact

About The Author

This blog was authored by Raffi Sosikian. Raffi is a highly proficient software engineer, and enterprise architect. He has an MBA, holds a Series 3 license, and is the principal at Pinnacle Quant, LLC, CTA, a boutique commodity trading advisory firm that specializes in building both custom trading systems (including private label for small funds to brand as their own software), as well as in-house pre-built quant and price action based automated trading systems, such as Price Action Pivoter™. To learn more about Raffi, to request a live Zoom demo for his pre-built trading systems, or if you need help in building your own custom trading system, please visit

Happy coding, happy sentiment analyzing, and happy trading!

Share the stats