How to trade based on News Headlines using NLP? | Quantra Classroom

Today we will discuss how to trade based on news headlines using NLP (Natural Language Processing).??

All the concepts covered in this tutorial are taken from the Quantra courses on?Natural Language Processing in Trading?and?Trading Strategies with News and Tweets. You can take a Free Preview?of these courses and learn all these concepts in detail.?

Note:?The links in this tutorial will be accessible only after logging into?quantra.quantinsti.com.


What is NLP?

Natural Language Processing (NLP), is a branch of artificial intelligence that deals with the interaction between computers and human languages.

It involves analysing and processing natural language data, such as text, speech, and conversation, to derive meaningful insights and automate tasks that would normally require human understanding.

VADER is a popular NLP tool for sentiment analysis.


What is VADER?

Valence?Aware?Dictionary and sEntiment?Reasoner (VADER) is a tool used to generate sentiment scores from the text. It uses a set of rules and a lexicon of words to determine the positivity, negativity, and neutrality of a given piece of text.


Approach

Here's a general approach to trading based on news headlines.

approach to trading based on news headlines

The result of this general approach to trading based on news headlines on Apple stock from Jan 2014 to May 2016 is shown below.

example of apple stock

The Python code for the strategy is available?here.

In this tutorial, we will discuss all the steps and methods involved in creating a trading strategy based on news headlines with the help of NLP.

Step 1:?Collect News Headlines

Step 2:?Generate the Sentiment Score

Step 3:?Generate Trading Signals Based on Sentiment Score


Step 1: Collect News Headlines

To collect news headlines, you could use Python APIs like:

  • Webhose,
  • NewsAPI,
  • News Fetch,
  • GoogleNews, etc.

These Python APIs aggregate news headlines from various media sources.?

python api integrating info from sources

The Python code to fetch news headlines from GoogleNews is available?here.


Step 2: Generate the Sentiment Score

Once you have collected the headlines, you can use VADER, a tool to perform sentiment analysis on each headline.

How does VADER calculate the Sentiment Score?

The way VADER works is by assigning a score to each word in the text based on how positive or negative it is. It then combines these scores to give an overall sentiment score for the text.

For example, if a sentence contains the word "happy," which has a positive score, and the word "sad," which has a negative score, VADER will assign a higher score to the positive word and a lower score to the negative word.?VADER takes into account the intensity of the words and their context to determine the overall sentiment score.

Overall, VADER is a useful tool for quickly analysing the sentiment of large amounts of text, such as news data on financial assets.

How to generate sentiment scores using Python?

NLTK (Natural Language Toolkit) is a popular Python library for NLP tasks. VADER (Valence Aware Dictionary and sEntiment Reasoner) is a sentiment analysis tool that is included in the NLTK library.


Step 3: Generate Trading Signals Based on Sentiment Score

After generating the sentiment scores of the news headlines, the final trading signal can be generated by considering the sentiment score of all news headlines collected over the data.?

To generate trading signals, you can define a threshold for the sentiment score. For example, you can define a threshold for the sentiment score as 0.25, and generate a buy signal if the average sentiment score of news headlines collected over the day is greater than or equal to 0.25.?

Similarly, a sell signal is generated if the average sentiment score of news headlines collected over the day is less than or equal to -0.25. No trade signal will be generated if the score is between?-0.25 and 0.25.

The following graph represents the cumulative returns of the sentiment score-based strategy on AAPL stock between ‘2014-01-28’ and ‘2016-05-30’ using the trading signals mentioned above.

cumulative returns of the sentiment score-based strategy on AAPL stock

The Python code for the strategy is available?here.


To review, the following is the workflow to generate daily trading signals based on news headlines.

workflow to generate daily trading signals based on news headlines
Why limit the trading strategy to just the news headlines?
Can we generate the trading signals based on complete news articles instead of just the news headlines??

The answer is?YES!!

The VADER is limited to generating sentiment scores just short text like news headlines. For longer text like a news article, the article should be broken into smaller parts. This can be done using the ‘sent_tokenize()’ method from NLTK.?

Once the news article is divided into multiple sentences, the ‘polarity_scores’ method of the ‘SentimentIntensityAnalyzer’ class in the NLTK library is used to generate the sentiment score of each sentence.

The average sentiment scores of all these sentences of a news article will be the final sentiment score of each news article.?

Once the final sentiment score of each news article is calculated, define the sentiment class of each news article.

  • The sentiment class of a news article is 1 if the final sentiment score is greater than or equal to 0.25.
  • The sentiment class of a news article is -1 if the final sentiment score is less than or equal to -0.25.?
  • Similarly, the sentiment class of a news article is 0 if the final sentiment score is less than 0.25 and greater than -0.25.?

Once the sentiment classes of each news article have been classified, find the average sentiment class of news articles collected over the day.

  • A buy signal is generated if the average sentiment class is greater than or equal to 0.25
  • A sell signal is generated if the average sentiment class is less than or equal to -0.25.?
  • No trade signal is generated if the average sentiment class is less than 0.25 and greater than -0.25.?

The Python code to generate sentiment scores for an entire news article is available?here.


To review, the following is the workflow to generate daily trading signals based on news articles collected over the day.?

workflow to generate daily trading signals based on news articles collected over the day

So, we discussed how to enter a trade based on the sentiment score generated on the news data.


When to exit the trade?

There are several possible exit strategies that you could consider for your trading strategy based on sentiment scores of news data generated by NLP and VADER. Here are a few options:

  • Time-based exit:?Set a fixed holding period for your trades to capture short-term movements in the market, such as a few days or a week. This would allow you to capture short-term movements in the market based on changes in sentiment.
  • Profit-based exit:?Set a profit target for each trade, based on your expected returns from the sentiment analysis. Once the price reaches this target, you would exit the trade and take your profits.
  • Stop-loss exit:?Set a stop-loss level for each trade, based on your risk tolerance. This would limit the losses if the sentiment reverses.
  • Reversal-based exit:?Use a reversal signal to exit your trades when sentiment changes from positive to negative or vice versa, to capture trends in the market and avoid losses due to sudden sentiment changes.

Learn more about different methods to analyse the sentiment of news data and design trading strategies by following the steps below.


What to do next??


We'd love to have you join our community of learners and traders.

Connect with us?to discuss more. We'd be happy to help!


IMPORTANT DISCLAIMER: This article is for educational purposes only and is not a solicitation or recommendation to buy or sell any securities. Investing in stocks involves risks and you should seek the advice of a licensed financial advisor before making any investment decisions. Your investment decisions are solely your responsibility. The information provided is based on publicly available data and our own analysis, and we do not guarantee its accuracy or completeness. By no means is this article the licensed equity analysts or financial advisors and it should not be construed as professional advice or a recommendation to buy or sell any securities.

Puneet Jindal

Top Voice | Training Datasets and workflows for AI Agents

1 年

How do we ensure that VADER represents highly accurate sentiment score? It can't beat a consensus based sentiment analysis pipeline. Such kind of pipeline can be built with tools like Labellerr(https://www.labellerr.com)

回复

要查看或添加评论,请登录

Quantra的更多文章

社区洞察

其他会员也浏览了