An easy and exciting way to have an edge in the market
Investing in stocks, ETFs, Forex, or cryptocurrencies can sometimes feel like navigating a complex maze. But what if there was a tool that could simplify your decision-making process and improve your chances of success? Enter the Financial Model Prep Sentiment API — a powerful ally for investors, and in this article, we’ll explore how it works and why it’s essential.
Understanding the Financial Model Prep Sentiment API
This API endpoint is like a financial compass that guides you through the market’s ups and downs. It uses its own special magic, called sentiment analysis, to understand how people feel about different financial assets. The best part? It does this not just once in a while but every single minute, keeping you updated in real-time.
Now, here’s the genius part. It takes all the positive and negative feelings it finds and calculates a special score for each hour. This score tells you whether people are generally feeling positive or negative about a specific asset on that day.
Importing Packages
The first and foremost step of setting up the coding environment is to import the required packages. In this article, we are not going to use any fancy packages but rather just two basic and widely used packages which are pandas for data manipulation, and requests for making API calls. The following code will import all the mentioned packages into our Python environment:
import requests
import pandas as pd
If you haven’t installed any of the imported packages, make sure to do so using the pip command in your terminal. Before moving further, to extract the data, we’ll be using the APIs of FinancialModelingPrep. So for the smooth execution of the upcoming code block, you need to have an FMP developer account which you can easily create using the link here.
Fetching Sentiment Data
This is where the magic of sentiment analysis comes in. You want to know how people are feeling about a particular stock, right? The API has got your back:
def extract_sentiments(symbol):
api_key = 'YOUR API KEY'
url = f'https://financialmodelingprep.com/api/v4/historical/social-sentiment?symbol={symbol}&apikey={api_key}'
raw_df = requests.get(url).json()
df = pd.DataFrame(raw_df)
return df
tsla_sentiments = extract_sentiments('TSLA')
tsla_sentiments.tail()
The function extract_sentiments takes a stock code, like “TSLA” for Tesla, and fetches the sentiment score along with various other important information for that stock from the API. This score tells you whether people are generally feeling positive or negative about it. Also, make sure to replace YOUR API KEY with your secret API key which you can obtain after creating an FMP developer account. This is the output dataframe of the above code:
The data represents a snapshot of social media activity and sentiment analysis related to stocks for various timestamps. Monitoring this data can help stakeholders assess market sentiment, track public perception, and potentially make informed trading decisions based on the level of engagement and sentiment trends. It also allows for the identification of key events or discussions that may have influenced the stock’s performance on the given date. Here’s what each column means:
‘symbol’: This column contains the stock symbol or ticker symbol for Tesla, typically “TSLA,” which uniquely identifies the company’s stock on stock exchanges.
‘stocktwitsPosts’: This column contains the number of posts or messages related to Tesla stock on the StockTwits platform, a social media platform for stock trading discussions.
‘twitterPosts’: This column contains the number of tweets or Twitter posts related to Tesla stock on the Twitter platform.
‘stocktwitsComments’: This column contains the number of comments or replies to posts about Tesla stock on StockTwits.
‘twitterComments’: This column contains the number of comments or replies to tweets about Tesla stock on Twitter.
‘stocktwitsLikes’: This column contains the number of likes or upvotes received on posts related to Tesla stock on StockTwits.
‘twitterLikes’: This column contains the number of likes or favorites received on tweets related to Tesla stock on Twitter.
‘stocktwitsImpressions’: This column contains data on the total number of impressions or views of posts related to Tesla stock on StockTwits.
‘twitterImpressions’: This column contains data on the total number of impressions or views of tweets related to Tesla stock on Twitter.
‘stocktwitsSentiment’: This column contains sentiment scores or labels associated with StockTwits posts about Tesla stock, indicating whether the posts are positive, negative, or neutral in sentiment.
‘twitterSentiment’: This column contains sentiment scores or labels associated with tweets about Tesla stock on Twitter.
Fetching the most trending stocks with real-time data
Imagine having a live pulse on the stock market. FMP’s sentiment data API makes it possible:
def extract_trending():
api_key = 'YOUR API KEY'
url = f'https://financialmodelingprep.com/api/v3/stock_market/actives?apikey={api_key}'
raw_df = requests.get(url).json()
df = pd.DataFrame(raw_df)
return df
trending = extract_trending()
trending.head()
The function extract_trending fetches data about 50 trending stocks in real-time. It sends a request to the FMP API, receives stock data, organizes it into a table, and shows the latest stocks in the table. These are the top five trending stocks according to the above code:
Here’s a column-wise explanation of the output:
‘symbol’: This is a short code that represents each stock.
‘name’: The full name of the company or entity behind the stock.
‘change’: How much the stock’s price has changed. It can be a positive number (meaning it went up) or a negative number (meaning it went down).
‘price’: The current price of the stock.
‘changesPercentage’: The percentage (a way to express parts of a whole as a percentage) change in the stock’s price.
Finding Bullish and Bearish stocks using sentiment scores
Another exciting feature of the FMP sentiment data API is its ability to provide bullish stocks and bearish stocks information based on the current market sentiment. Here’s a simple way to use their API to extract such data:
def extract_pattern_stocks(pattern):
api_key = 'YOUR API KEY'
url = f'https://financialmodelingprep.com/api/v4/social-sentiments/change?type={pattern}&source=stocktwits&apikey={api_key}'
response = requests.get(url).json()
df = pd.DataFrame(response)
return df
trending_sentiments = extract_pattern_stocks('bearish')
trending_sentiments.head()
The function extract_pattern_stock takes a parameter named pattern, which specifies whether we want to find bullish or bearish stocks based on sentiment scores. This URL fetches sentiment data from the ‘financialmodelingprep.com’ API using the get function provided by requests package.
The function sends an HTTP GET request to the constructed URL, retrieves the response data in JSON format, and then converts it into a DataFrame using the pandas library. Finally, the function returns this DataFrame containing the 50 most negative stocks in real-time sentiment data:
Similarly, if you want to attain data related to the most bullish stocks according to the market sentiment, you can change the pattern parameter from bearish to bullish. Here’s an example output of the top 50 most bullish stocks:
Conclusion
In the world of finance, the Financial Model Prep’s Sentiment API is like a secret weapon that can make your investment journey smoother and more successful. It’s like having a crystal ball to help you make smarter decisions.
Imagine having the power to spot opportunities, reduce risks, and make informed choices as they happen. The API’s simplicity and real-world applications, as demonstrated in our example, make it a valuable tool for any investor, whether you’re a seasoned pro or just starting out.
Don’t wait! Give it a try and watch your investments thrive. Hope you learned something new and got interesting info from this article. Happy investing!
Comentários