Financial News Sentiment Analysis with Chat GPT

Future
3 min readDec 17, 2022

--

Photo by Lucas Hoang on Unsplash

To showcase the effectiveness of Chat GPT, the following blog will be co-generated by Chat GPT. Codes are not generated by Chat GPT.

In the world of financial instrument trading, news sentiment analysis can be a powerful tool for traders. News sentiment analysis is the process of using news data to identify trends and potential opportunities in the markets. By analyzing how news affects markets, traders can gain an insight into the sentiment of the markets and make informed trading decisions. News sentiment analysis has become increasingly important in recent years, as the amount of available news data has grown exponentially. With the help of technology, traders can now access news from a variety of sources, including news outlets, blogs, social media, and more. By analyzing news sentiment, traders can identify which news stories are likely to have a positive or negative impact on the markets. In addition to identifying trends, news sentiment analysis can also be used to spot potential trading opportunities. By monitoring news sentiment, traders can identify when the sentiment of a particular instrument is likely to change. For example, if a news story is released that is expected to have a positive impact on a certain asset, traders may look to buy that asset in anticipation of a rally. Similarly, if a news story is released that is expected to have a negative impact, traders may look to short sell the asset in anticipation of a sell off. By using news sentiment analysis, traders can gain an edge in the markets and improve their trading performance. By staying up to date with the latest news, traders can make informed decisions and identify potential trading opportunities. In order to make the most of news sentiment analysis, traders should be sure to use reliable sources of news data and monitor the news closely.

We will make use of Alpaca to show how we can use Chat GPT for news sentiment analysis.

Install the dependency libraries

pip install alpaca-trade-api
pip install openai

Import dependencies and set key variables

import os
import openai
from alpaca_trade_api import REST, Stream

#Note, to use chat GPT, its API key has to be named in this format.
openai.api_key="INSERT OPENAI API KEY"
alpaca_api_key="INSERT ALPACA API KEY"
alpaca_api_secret="INSERT ALPACA API SECRET"

Set up Alpaca

restAPI = REST(alpaca_api_key, alpaca_api_secret)
news = restAPI.get_news("TSLA", "2022-01-01", "2022-12-15")
stream = Stream(alpaca_api_key, alpaca_api_secret)

Set up Chat GPT model

def run_model(news_text):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Give a normalised number for the sentiment \n " + news_text,
temperature=0,
max_tokens=3000,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
return response

For the prompt in chat GPT model, I have found this to return the best result for sentiment analysis. You can change the prompt to different phrase and experiment with it.

Set up Alpaca streaming client

async def news_feed(news):
threshold = 0.5

#Form a simple news feed
simple_news_feed = news.summary + news.headline

#Run the news feed in the model
response = run_model(simple_news_feed)

#Buy if the sentiment is above threshold
if response['choices'][0]['text'] > threshold:
restAPI.submit_order('TSLA', 100)

#Sell if the sentiment is below threshold
elif response['choices'][0]['text'] < -threshold:
restAPI.submit_order('TSLA', -100)

stream.subscribe_news(news_feed, "Tesla")

stream.run()

Assuming that we are trading Tesla. We first define the threshold which this bot will trade. For example, we had chosen 0.5. When chat GPT analysed the news and give a rating of above 0.5 or below -0.5, we will buy/sell accordingly.

Disclaimer: This article is purely for reference and education. Do not use this as investment advice. Backtest the strategy with your desired financial instrument before executing with live account.

--

--

Future
Future

Written by Future

Looking towards the Future

No responses yet