Your Client Wants to Sentiment Trade Based on Headlines News

Your client (or boss) comes to you and says, “Look, I want to dabble in trading. I don’t know a thing about price action or algorithms, but I’ve seen stocks go wild on news. So, build me a program that will email me when there is bullish or bearish news on any S&P 500 listed company.”

Let’s Get to Building & Solve The Problem 🙂

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 your client (or boss). 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
nltk.download('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("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en")

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():
table=pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
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:
related_news.append(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:
sentiment["bullish"].append(news)
elif score['compound'] < -0.05:
sentiment["bearish"].append(news)
else:
sentiment["neutral"].append(news)
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. This is an efficient way to keep stakeholders informed about the market sentiment of specific companies.

def send_email(email_recipient, bullish_news, bearish_news):
msg = MIMEMultipart()
msg['From'] = 'your_email@example.com'
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('smtp.gmail.com', 587)
server.starttls()
server.login('your_email@example.com', 'YourPassword')
text = msg.as_string()
server.sendmail('your_email@example.com', email_recipient, text)
server.quit()
send_email('example@example.com', sentiments["bullish"], sentiments["bearish"])

NOTE: Please remember to replace ‘your_email@example.com’ 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 your client (or boss) to place their trades. However, if you have ever been on a Zoom call with me, you already know that I place tremendous emphasis on what to do while in the actual trade (defense) just as much as I do on offense (entering trades based on headline news sentiment in this case). So, it may be worth to ask your client (or boss) if they have a trade, risk, and money management strategy when they are in the actual trade, if not, you may be able to help them in that regard as well!

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 https://PinnacleQuant.com/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. 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 private label custom trading system, please visit https://PinnacleQuant.com/consulting.

Happy coding, happy sentiment analyzing, and happy trading!

 
Share with others!