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. 


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


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!


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!


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

  1. Hi,
    I get this errors:

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

    My Python version is 3.8.9

Leave a Reply

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