avatarAntonio Blago

Summary

The article discusses the discovery and educational use of an unofficial API for fetching CNN's Fear and Greed Index data, including a demonstration of how to access and visualize the index's historical trends.

Abstract

The author of the article details their journey from scraping the Fear and Greed Index to finding an accessible API provided by CNN for retrieving the index's data. Despite the lack of an official API, the author explains how they found the API endpoint through the developer console, emphasizing that this discovery is legal and for educational purposes. The article includes a step-by-step guide on how to use Python to request the data, convert it into a readable format, and plot it for analysis. The author also mentions informing CNN of the API's exposure and provides links to their personal projects and recommended services.

Opinions

  • The author believes that accessing the API through the developer console is legal and ethical, as it is openly accessible.
  • They suggest that many people would be interested in an official API for the Fear and Greed Index.
  • The author implies that CNN may not be interested in or aware of the exposed API, as they did not respond to the author's email.
  • There is an implication that the Fear and Greed Index correlates with market volatility and can be a useful signal for market entry points.
  • The author promotes their own website, a stock and crypto tracking tool, and a web hosting service, indicating endorsement or personal affiliation.
  • The article encourages reader engagement by inviting claps, shares, comments, and provides links to further financial educational resources.

[LEAKED API] Parsing Information of Fear and Greed Index (cnn.com)

TL;DR: This tutorial is just for educational purposes, not for private usage. Some years ago, I started a private program that was fetching minutely the fear and greed index. I wanted to update it, but instead, I discovered an open API.

Screenshot from cnn.com

Some years ago, I wrote a simple python script to scrape the fear and greed index minutely to see, how the index was correlating to the market. If the index was below 30, it would give me an alert on my smartphone. This program ran on pythonanywhere.com*. Unfortunately, it stopped working because CNN changed their websites.

Here is the program:

Screenshot by author

I discovered that whenever the Fear and Greed Index (orange line) is below 50 the volatility index (dark blue line) is rising and the S&P 500 Index (light blue) is falling, so a good entry for a buy.

Diagram by author

Unfortunatley cnn.com has no official API to get the fear and greed infos from their website, but I am sure, a lot of people would be interested in it.

As I said, I wanted to showcase and update this project, when I discovered the API for the fear and greed index in the developer consoles (on windows F12). Everyone can access this console and it is totally legal. You see in the screenshot below of the developer consoles from https://edition.cnn.com/markets/fear-and-greed, that there is a valid GET request (code 200).

Screenshot by author from cnn.com

If you click on it, highlighted in blue, you see this API behind the GET parameter.

Screenshot by author from cnn.com

If you put this API in a new browser window, a json response is appearing (see below).

Screenshot by author from cnn.com

The earliest date I could retrieve is the 2020–08–03T00:00:00.000Z (1596412800000). So not bad.

Of course, I also informed CNN via email (see below) some weeks ago about my discovery. But they did not answer yet. Maybe, they are not interested.

Screenshot by author

API Request

Just for a showcase, I am showing you how you can retrieve the data via this API. It is only for educational purposes. We are setting up via request library.

pip install requests

Then paste in the URL. We are also setting a header to avoid bot detection.

url = r"https://production.dataviz.cnn.io/index/fearandgreed/graphdata/2020-09-06"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0'}

response = req.get(url, headers=headers)

We retrieve this information:

Screenshot by author

We want to read and store this information in a more readable way as a dataframe. We need the ast library to read the bytes object and convert it to a dictionary.

import ast
byte_obj = response.content

dict_str = byte_obj.decode("UTF-8")
mydata = ast.literal_eval(dict_str)

We can now access the dictionary via the index.

Screenshot by author

We save the historical data in the dictionary to a pandas DataFrame.

historical_fg = pd.DataFrame(mydata["fear_and_greed_historical"]["data"])

historical_fg["x"] = pd.to_datetime(historical_fg["x"], unit='ms')
historical_fg = historical_fg.rename(columns =   
{"x":"Date","y":"value"})
Screenshot by author

We can make a little plot:

import matplotlib.pyplot as plt
def create_plot(data):
    fig, ax = plt.subplots()
    ax.plot(data.Date, data['value'], color="tab:orange",
            label="Fear and Greed Index")
    ax.legend()
    plt.xticks(rotation=45)
create_plot(historical_fg)
Plot by author, data from cnn.com

Nice! That was fun. I hope you liked it and you got some value from it.

👉 Please feel free to clap, share, and comment. Follow me for more content about cryptos, stocks, data analysis, and web development.

A Message from InsiderFinance

Thanks for being a part of our community! Before you go:

Python
Fear And Greed Index
Stock Market
Money
Hacking
Recommended from ReadMedium