Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

APIFeaturedFinance

Can news headlines and emotions affect stock market prices?

What is the role of news in influencing stock prices? 

Stock prices will rise or fall depending on the latest news. This process is known as price discovery. Traders will analyse the fresh information and determine how the stock market will react.

Is it possible to create a trading bot that can use news headlines to predict stock prices?

Using three APIs from APILayer, you can build apps that can check the headlines of financial news, evaluate how the headlines may make investors feel, and look for possible correlations for how the daily news headlines may positively or negatively influence stock prices.

Correlation is a term used in statistics to describe how two variables move together. When two things change in the same way, we say that they are positively correlated. If they move in opposite directions, there is a negative correlation between them.

In this article you will learn how to combine the capabilities of three APIs to analyse the impact of news on stock prices.

With these three APIs and some basic Python programming, you can get financial data and news to start making observations and find correlations between emotions and stock prices.

Setup

At the end of this tutorial, you will get the full Python code. In the next steps of the tutorial, you will learn which parts of the code are needed to get results from APIs.

Step 1 – Retrieving financial news headlines from Finance News API

To analyse the impact of financial news on trader emotions, first you will need Finance News API

With this API, you can get headlines from sources that report on financial news. You can get more accurate results by using the company name, ticker tags, and any other relevant keywords you want to filter by.

For example, do news stories about new jobs or products lead to higher share prices? Do bad keywords in headlines cause stock prices to move in the wrong direction? The Finance News API is very flexible and lets you filter for specific keyword news. If you want, you can also filter in or filter out certain news publications from your search results. This is the research you’ll need to do before you can make a great bot that makes really accurate predictions based on what the news headlines say and what prices do next.

The Python code you’ll use to get news about a certain company will look something like the image below. Check out the API documentation on Finance News API to learn more about what it can do. You can tell it which keywords and ticker symbols to look for when filtering news, and you have to give it a date or range of dates. In the example Python code below, the code is part of a Python function that gets a date and pulls out all the financial news titles about the “Pultegroup” and puts them in a string called “mytext.”

Step 2 – retrieving the emotional score of financial news headlines

Next, use APILayer’s Text to Emotion API to look at each trading day’s news headlines and see how they make people feel.

When the Text to Emotion API checks all of the news titles in the string mytext, it will give the text a score based on how it makes the reader feel.

    {

    “Happy”: 0.14,

    “Angry”: 0.14,

    “Surprise”: 0.07,

    “Sad”: 0.0,

    “Fear”: 0.64

     }

In the example code below, the emotion scores are saved to lists that will later be imported into a Pandas dataframe.

Step 3 – Using Marketstack.com to retrieve historical stock price data

With marketstack.com, you can get information about the past for more than 170,000 stock tickers. You can find information from as long ago as 30 years ago. This is where you can start to get price data, which you can then compare to the emotional score data of the day’s news headlines.

To get prices, you don’t need much code. To get a price from the marketstack API, you have to put a ticker symbol, a date, and your API key into a URL. The URL gives the result in JSON format, which can be queried with Python and other programming languages.

When you run the Python code you will get a result that looks a lot like the one below. Where you can get the opening and closing prices of the market, as well as other information like the number of stocks sold and the day’s highest and lowest prices.

 

{‘open’: 40.36, ‘high’: 41.1, ‘low’: 40.0, ‘close’: 40.59, ‘volume’: 2195000.0, ‘adj_high’: None, ‘adj_low’: None, ‘adj_close’: 40.59, ‘adj_open’: None, ‘adj_volume’: None, ‘split_factor’: 1.0, ‘dividend’: 0.0, ‘symbol’: ‘PHM’, ‘exchange’: ‘XNYS’, ‘date’: ‘2022-06-27T00:00:00+0000’}

 

Step 4 – Put the extracted data into a dataframe

When API result data is retrieved for each date, it can be put into lists and then each list can be passed to a column in a dataframe.

DataFrame is a labelled 2-dimensional data structure with columns that could be of different types. You can think of it as a spreadsheet, a SQL table, or a dict of Series objects. 

Correlations between emotions and the marketplace

When the price and emotion score data are in a dataframe, it’s easy to use simple functions in Python libraries like Pandas and Seaborn to do correlation analysis and make correlation graphs.

With a correlation score of 1, there is a perfect match between the two things.

When the correlation score is between 0.5 and 0.7, it means that the variables are only somewhat related to each other. Correlation scores between 0.3 and 0.5 show that the two variables don’t go together very well.

This correlation graph shows that there is a moderate positive correlation between ‘Angry’ and ‘Open Price’, and a strong negative correlation between ‘Fear’ and ‘Open Price’.

The effect of fear on market opening prices

For scores that have a high correlation it is very interesting to analyse them with scatter plots.

There was a very strong negative correlation shown in the correlation graph between fear scores and opening prices.

Below is the code for a scatter plot that can be used to analyse the relationship between fear scores and opening prices. It uses the data that was saved to the lists for ‘Fear’ scores and ‘Opening Prices’.

The effect of anger on market opening prices

Below is the code for a scatter plot that can be used to analyse the relationship between angry scores and opening prices. It uses the data that was saved to the lists for ‘Angry’ scores and ‘Opening Prices’.

There was only a moderate positive correlation between ‘Angry’ and ‘Open Price’. On the days that zero anger was detected in news titles, the stock prices were slightly lower than a day when a small amount of anger was detected in the news titles.

Conclusion

By looking at a small range of news headlines and comparing them to stock prices from the marketstack API and the Text to Emotion API, you can start to see how people feel about the news could affect the opening prices.

If you were to build a more advanced bot that could be used as a predictor for future prices such as EOD prices, you could use the Finance News API to provide you with the URLs of news headlines where the news itself could be analysed using scraping processes, passing the news text to Emotion to Text API, or a Sentiment API.

Different types of stock niches may respond differently to different types of financial news, which is why it is useful to be able to use Finance News API to be able to filter on particular online news resources and keywords. When you use Marketstack API there is a wealth of historical price information that you can compare to previous news sentiment, and this will help you make better future predictions.

The full code

Combine the APIs

 

import json

import requests

 

#iterate dates

#https://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python

 

from datetime import date, timedelta

 

def daterange(start_date, end_date):

    for n in range(int((end_date – start_date).days)):

        yield start_date + timedelta(n)

 

def extract_news_and_analyse_sentiment(pass_a_date):

    print(‘Analysing ‘, pass_a_date )

    

    global HappyList

    global AngryList

    global SurpriseList

    global SadList

    global FearList

    global OpenList

    global CloseList

    global DifferenceList

    global VolumeList

        

    #YYYY-MM-DD

    date = pass_a_date

    fallback = ‘off’

    keywords = ‘Pultegroup’

    limit = 10

    offset = 0

    sources = ”

    tags = ”

    tickers = ‘phm’

    url = “https://api.apilayer.com/financelayer/news?tickers=” + tickers + “&tags=”+ tags +”&sources=”+ sources +”&offset=”+ str(offset) +”&limit=” + str(limit) +”&keywords=”+ keywords +”&fallback=”+ fallback +”&date=”+ date +””

    payload = {}

    headers= {

      “apikey”: “Put_Your_API_key_here” # API Key for financelayer

    }

 

    response = requests.request(“GET”, url, headers=headers, data = payload)

    status_code = response.status_code

    result = response.text

    result = json.loads(result)

    jdict = result.get(‘data’)

    mytext = “”

    my_url_list = []

 

    for row in jdict:

        title = row[‘title’]

        #print(title)

        mytext = mytext + ” ” + title

        news_url = row[‘url’]

        my_url_list.append(news_url)

 

    #do emotion analysis across all headlines

 

    url = “https://api.apilayer.com/text_to_emotion”

    payload = mytext.encode(“utf-8”)

    headers= {

      “apikey”: “Put_Your_API_key_here” # api key for https://api.apilayer.com/text_to_emotion

    }

 

    response = requests.request(“POST”, url, headers=headers, data = payload)

    status_code = response.status_code

    emotion = response.text

 

    emotion = eval(emotion)

    

    Happy = emotion.get(“Happy”)

    Angry = emotion.get(“Angry”)

    Surprise = emotion.get(“Surprise”)

    Sad = emotion.get(“Sad”)

    Fear = emotion.get(“Fear”)

    

 

    HappyList.append(Happy)

    AngryList.append(Angry)

    SurpriseList.append(Surprise)

    SadList.append(Sad)

    FearList.append(Fear)

    print(emotion)

 

    #get ticker close price and volume of trade

 

    api_key = “Put_Your_API_key_here”

    api_result = requests.get(‘http://api.marketstack.com/v1/tickers/’+ tickers +’/eod/’+ pass_a_date +’?access_key=’+ api_key +”)

    api_response = api_result.json()

    print(api_response)

    print(type(api_response))

    

    openf = api_response.get(‘open’)

    close = api_response.get(‘close’)

    volume = api_response.get(‘volume’)

    DifferenceList.append(close-openf)

    OpenList.append(openf)

    CloseList.append(close)

    VolumeList.append(volume)  

    print(‘close: ‘, api_response.get(‘close’), api_response.get(‘volume’) )

 

HappyList = []

AngryList = []

SurpriseList = []

SadList = []

FearList = []

OpenList = []

CloseList = []

DifferenceList = []

VolumeList = []

DateList = []

 

       

start_date = date(2022, 6, 24)

end_date = date(2022, 7, 3)

for single_date in daterange(start_date, end_date):

    if single_date.weekday() > 4:

        print(‘Given date is weekend.’)

    else:

        print(‘Given data is weekday.’)

        DateList.append(single_date.strftime(“%Y-%m-%d”))

        extract_news_and_analyse_sentiment(single_date.strftime(“%Y-%m-%d”))

    

    

    print(CloseList)

 

Create a dataframe

 

# Import Library

import pandas as pd

 

# Defne Data

timeseries_data = { 

    ‘Date’: DateList, 

    ‘Volume’: VolumeList,

    ‘OpenPrice’ : OpenList,

    ‘ClosePrice’ : CloseList,

    ‘DifPrice’ : DifferenceList,

    ‘Fear’ : FearList,

    ‘Sad’ : SadList,

    ‘Surprise’ : SurpriseList,

    ‘Angry’ : AngryList,

    ‘Happy’ : HappyList

}

 

# Create dataframe

dataframe = pd.DataFrame(timeseries_data,columns=[‘Date’, ‘Volume’, ‘OpenPrice’,’ClosePrice’,’DifPrice’,’Fear’,’Sad’,’Surprise’,’Angry’,’Happy’])

 

# Changing the datatype

dataframe[“Date”] = dataframe[“Date”].astype(“datetime64”)

 

# Setting the Date as index

 

dataframe = dataframe.set_index(“Date”)

dataframe

 

Create a correlation table

 

dataframe.corr()

 

Create a correlation graph

 

import seaborn as sns

Var_Corr = dataframe.corr()

# plot the heatmap and annotation on it

sns.heatmap(Var_Corr, xticklabels=Var_Corr.columns, yticklabels=Var_Corr.columns, annot=True)

 

Create a scatter plot

 

import matplotlib.pyplot as plt

 

x = FearList

y = OpenList

plt.scatter(x, y, c =”blue”)

 

# To show the plot

plt.xlabel(“Fear Score”)

plt.ylabel(“Open Price”)

plt.show()

Related posts
APICurrency

Exchange Rate API Integration in E-Commerce App in 2024

APICurrency

Building a Real-Time Currency Converter in Java Using Currencylayer API

API

10 Best Stocks APIs For Developers

API

Top 10 Flight Search APIs in 2024

Leave a Reply

Your email address will not be published. Required fields are marked *