How to analyse daily news sentiment for cryptocurrency with Python

Do you trade the news?

If you’re a cryptocurrency trader, you’ve probably traded the news at least a few times. I know I did, with various degrees of success. The issue I noticed with this strategy is that, more often than not, by the time you become aware of a big piece of news for any given cryptocurrency, there are many other players who capitalised on the news, opened positions and made their moves. So unless you’re constantly checking for them, you won’t make the best possible trade. 

Script overview

Automating the news scouting

The script that I’ll be taking you through in this article is designed to perform a contextual web search by taking in keyword inputs and returning news results to you. This way you’ll have access to the latest news published on a cryptocurrency of your choice. The API I used to search for relevant cryptocurrency news is the contextual web search hosted on RapidAPI. It’s free with 100 requests a day, and offers paid plans if you need more than that.

Capturing the sentiment

The second part of this script takes the news headlines and analyses the sentiment in order to determine whether the news are positive, neutral or negative. In theory, this can save a lot of time as opposed to manually reading each headline and deciding whether a trade should be placed or not. The sentiment can then be used in automated decision making as a buy or sell signal of any coin. The trading logic is not integrated with this bot, but I will add this in a future article. 

Limitations

Before we jump into the code I would like to point out a couple of limitations that you need to be aware of. 

  • There is a hard cap of 100 requests / day from the search API ( can be replaced with a scraper)
  • The Sentiment analysis may not be able to determine just how “big” the news is

With that out of the way, let’s jump into it!

The code

Prerequisites

In order to get this script running you will need the following:

One your have your Rapid API account set-up and have generated the keys for the two APIs above, you’re ready to rock and roll!

Imports

Start by importing the following modules into your Python compiler

Crete user inputs and global variables

Next, we need to define a few variables to store our API keys and to tell out algorithm what to search the web for. 

Have a quick look at the sentiment_key and websearch_key variables. I used an environment variable that I created on my computer in order to safely pull it into my code. If you’re not planning to share the code you can simply define your keys as sentiment_key = ‘YOUR_KEY_HERE’

The dictionary crypto_key_pairs contains the symbol name and Keywords for each coin given, for easy integration with the cryptocurrency exchange like Binance. The values are analysed by our script, and the keys can be used by Binance  API to open trades. I will cover the integration with Binance in the next blog, so keep an eye out for that one.

Fetch the news

We are now defining the function responsible for fetching the news based on the parameters we have selected above. The code below loops through each keyword given in the crypto_key_pairs and returns today’s news for each cryptocurrency. There is additional configuration possible for the querystring variable if you want to adjust the number of pages to search as well as the size (in numbers of articles) for each page. In addition to the fromPublishedDate which we already defined, a toPublishedDate can also be given.

querystring = {“q”:str(crypto),“pageNumber”:“1”,“pageSize”:“30”,“autoCorrect”:“true”,“fromPublishedDate”:date_since,“toPublishedDate”:“null”}

Analyse the sentiment for each article

The next function will analyse the sentiment for each article returned and return to us a value of 1 or 0 for each of the 3 sentiment categories supported by the API: positive, neutral, negative. The result is then appended to the news_output variable and returned in a list format like so: news_output[“Bitcoin”][“sentiment”][“positive”] = [1,1,1,1,1]. We can use this output to calculate an average in our last function.

Calculate the overall sentiment of all articles pulled

In the final function we are going to calculate the overall news sentiment for each cryptocurrency in percentages. Remember how each article has a positive, neutral and negative sentiment? By finding out the total number of articles, we can then calculate how much of the overall sentiment is negative, neutral and positive. The formula used to determine this is: the number of positive articles * 100 / the total number of articles. It works in the same way for each of our sentiment categories.

The final if statement will execute the code every 15 minutes using the time.sleep(900) function. You can change this to whatever period you like but remember that the websearch API only has 100 free calls a day.

Additional Resources:

Did you like this article? Subscribe below to be notified when this will be integrated into a fully-working bot!

 

10 thoughts on “How to analyse daily news sentiment for cryptocurrency with Python

  1. Hi,
    I get this errors:

    Traceback (most recent call last):
    File “newstrade2.py”, line 127, in
    calc_sentiment()
    File “newstrade2.py”, line 109, in calc_sentiment
    news_output = analyze_headlines()
    File “newstrade2.py”, line 62, in analyze_headlines
    news_output = get_news_headlines()
    File “newstrade2.py”, line 53, in get_news_headlines
    for news in result[‘value’]:
    KeyError: ‘value’

    My Python version is 3.8.9

  2. I will use Github repo then.
    One thing I am dubious about is that my webesearch API key and API sentiment key are the same.
    I thought the keys should be different.

  3. Yeah, i get the same errors also with github repoTraceback (most recent call last):
    File “newstrade.py”, line 130, in
    calc_sentiment()
    File “newstrade.py”, line 108, in calc_sentiment
    news_output = analyze_headlines()
    File “newstrade.py”, line 61, in analyze_headlines
    news_output = get_news_headlines()
    File “newstrade.py”, line 52, in get_news_headlines
    for news in result[‘value’]:
    KeyError: ‘value’

    what I am doing wrong?

    1. hi, write your api key directly on headers, for some reason, the sentiment_key and the webserach_key variables doesnt work, and yes, both variables are the same “x-rapidapi-key”

  4. I removed calc_sentiment()
    but i get nothing
    user@usernoMacBook-Pro Documents % python trade4.py
    user@usernoMacBook-Pro Documents %

  5. no luck with the Github repo. sorry I am kind of a noob with python.

    (env) user@usernoMacBook-Pro trade % python news-analysis.py
    Traceback (most recent call last):
    File “news-analysis.py”, line 130, in
    calc_sentiment()
    File “news-analysis.py”, line 108, in calc_sentiment
    news_output = analyze_headlines()
    File “news-analysis.py”, line 61, in analyze_headlines
    news_output = get_news_headlines()
    File “news-analysis.py”, line 52, in get_news_headlines
    for news in result[‘value’]:
    KeyError: ‘value’

    1. Hi Andrea, did you manage to solve the issue? If not please see my latest blog, I created a bot using the sentiment analysis from this one.

Leave a Reply

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