AI

Building a Crypto Recommendation App with Sentiment Analysis based on Risk Tolerance

Cryptocurrency Trends

In the ever-evolving cryptocurrency market, staying ahead of trends and making informed decisions can be challenging. With countless news articles published daily and market sentiment shifting rapidly, investors need a reliable way to filter relevant information and analyze sentiment effectively.

This blog post explores how to build a cost-effective cryptocurrency recommendation API that fetches real-time news, caches it for optimized performance, and analyzes sentiment to provide actionable insights. By leveraging NewsAPI for news data and incorporating both basic sentiment analysis (using libraries like Sentiment) and advanced sentiment analysis (via Google Cloud Natural Language API), you can create an API that delivers accurate, real-time crypto recommendations tailored to market sentiment.

Whether you’re a developer building tools for crypto investors or a hobbyist experimenting with financial trends, this guide will walk you through the process step by step, ensuring a scalable, low-cost, and high-performance solution.

Synopsis: Building a Smart Crypto Recommendation System

Cryptocurrency markets are driven by real-time trends, news sentiment, and dynamic market movements. Developing a system that can process large volumes of information, extract insights, and provide recommendations is crucial for investors and enthusiasts looking to make informed decisions.

This project focuses on creating a cost-efficient API that:

  1. Fetches cryptocurrency data for thousands of coins from trusted sources to evaluate their market performance and trends.
  2. Retrieves real-time cryptocurrency news to stay updated on the latest developments.
  3. Caches news articles to minimize redundant API calls and reduce costs.
  4. Performs sentiment analysis using both basic and advanced tools to score market sentiment accurately.
  5. Filters and ranks cryptocurrencies based on sentiment scores, user-defined risk appetite, and market trends to provide actionable recommendations.

By integrating coin data, news, and sentiment analysis, this API delivers personalized crypto recommendations tailored to user-defined risk levels. The solution is designed for scalability, real-time updates, and budget-friendly implementation, making it an ideal tool for developers, investors, and crypto enthusiasts.

Setting Up the Project

Ensure you have a Node.js/Next.js environment ready. Install the necessary packages:

npm install node-fetch sentiment node-cache @google-cloud/language

Dependencies:

  • node-fetch: Fetch API for Node.js.
  • sentiment: A simple NLP library for basic sentiment scoring.
  • @google-cloud/language: For advanced sentiment analysis using Google Cloud Natural Language API.
  • node-cache: To cache news articles and reduce API calls.

Fetching Coins Data

When building a cryptocurrency recommendation system, fetching data for thousands of coins is comprehensive but can be computationally expensive and result in excessive API calls, especially when analyzing news sentiment for each coin. To balance cost-efficiency and effectiveness, it’s ideal to fetch data for a limited set of top-performing coins that are most relevant to users. This reduces the number of API calls for fetching news and sentiment analysis while maintaining high-quality recommendations.

Optimized Approach: Fetching the Top 50 Coins

Instead of processing the entire cryptocurrency dataset, we can use CoinGecko’s API to fetch data for the top 50 coins based on market cap. These coins are generally the most actively traded and have the most significant influence on the crypto market, making them a solid starting point for recommendations.

Why Limit to 50 Coins?

  1. Cost Efficiency:

    By focusing on the top 50 coins, you significantly reduce the number of API calls required for news fetching and sentiment analysis. Each API call for news and sentiment involves processing, which can be resource-intensive.

  2. Relevance:

    The top 50 coins cover a substantial portion of the market cap, ensuring that users receive recommendations for well-established or trending cryptocurrencies.

  3. Performance:

    A smaller dataset ensures faster processing and lower latency in API responses.

Implementation:

import fetch from "node-fetch";

const fetchCryptoData = async () => {
  const apiUrl = "https://api.coingecko.com/api/v3/coins/markets";
  const params = new URLSearchParams({
    vs_currency: "usd",
    order: "market_cap_desc",
    per_page: 50,
    page: 1,
    sparkline: false,
  });

  const response = await fetch(`${apiUrl}?${params}`);
  return response.json();
};

Fetching and Caching News Articles

How the News Fetching and Caching Workflow Works

  1. Caching Setup:

    • The NodeCache library is initialized with a Time-to-Live (TTL) of 24 hours (86400 seconds). This ensures that news articles remain in the cache for a full day before being invalidated.

    • A check period of 10 minutes (600 seconds) ensures that expired cache entries are cleared periodically.

      import NodeCache from 'node-cache';
      
      // Initialize NodeCache (24 hours TTL for news articles)
      const cache = new NodeCache({ stdTTL: 86400, checkperiod: 600 });
      
  2. Fetching News:

    • When a request is made for news about a cryptocurrency, the system first checks if the relevant news is available in the cache.

    • If the cache contains data for the queried cryptocurrency (cacheKey), it returns the cached data immediately.

    • If the cache is empty or expired, the system fetches news from the NewsAPI, processes the articles, and stores them in the cache.

      const fetchNews = async (coinName) => {
        const url = `https://newsapi.org/v2/everything?q=${encodeURIComponent(
          coinName
        )}&language=en&apiKey=${NEWS_API_KEY}`;
      
        const response = await fetch(url);
      
        if (!response.ok) {
          console.error(`Failed to fetch news for ${coinName}`);
          return [];
        }
      
        const data = await response.json();
        return data.articles || [];
      };
      
  3. Caching Logic:

    News articles fetched from the API are cached for 24 hours. This reduces redundant API calls for the same coin within the TTL period

    const fetchNewsWithCache = async (coinName) => {
      const cacheKey = `news_${coinName}`;
    
      if (cache.has(cacheKey)) {
        console.log(`Returning cached news articles for: ${coinName}`);
        return cache.get(cacheKey);
      }
    
      const articles = await fetchNews(coinName);
      cache.set(cacheKey, articles);
    
      return articles;
    };
    

Performing Sentiment Analysis

By analyzing the tone of news articles related to cryptocurrencies, we can assess market sentiment—whether it’s positive, negative, or neutral. This sentiment score is then used to refine recommendations based on the user’s risk appetite.

What is Sentiment Analysis?

Sentiment analysis is the process of using natural language processing (NLP) techniques to determine the emotional tone of a text. In this system, it helps to understand how news articles reflect the market’s perception of a cryptocurrency. For instance:

  • Positive sentiment could indicate bullish trends.
  • Negative sentiment could indicate bearish trends or concerns.
  • Neutral sentiment may suggest stability or a lack of strong opinions.

Steps for Performing Sentiment Analysis

  1. Input Data:

    Use the title and description fields from the fetched news articles as the input for sentiment analysis.

  2. Sentiment Analysis Tool:

    You can use an NLP library (like Sentiment.js) or an external API (like Google Cloud Natural Language API, AWS Comprehend, or Azure Text Analytics) for sentiment analysis.

  3. Sentiment Scoring:

    • Each news article is assigned a sentiment score (positive, neutral, or negative) based on the textual content.
    • Scores can range from -1 to +1 (negative to positive), or a categorical value (positive, neutral, negative).
  4. Aggregate Scores:

    Combine sentiment scores across all articles for a cryptocurrency to derive an average sentiment score

Option 1: Basic Sentiment Analysis(Using an Sentiment.js)

We use sentiment library for basic sentiment analysis

const analyzeSentiment = (articles) => {
  const scores = articles.map(
    (article) => sentiment.analyze(article.title).score
  );

  const averageScore =
    scores.reduce((sum, score) => sum + score, 0) / (scores.length || 1);

  return { scores, averageScore };
};

const enrichWithSentiment = async (coins) => {
  return Promise.all(
    coins.map(async (coin) => {
      const articles = await fetchNewsWithCache(coin.name);
      const { averageScore } = analyzeSentiment(articles);

      return {
        ...coin,
        sentimentScore: averageScore,
      };
    })
  );
};

Option 2: Advanced Sentiment Analysis Using Google Cloud NLP

For more accurate results, we can integrate Google Cloud Natural Language API, which provides deeper sentiment insights.

Steps:

  1. Enable the Cloud Natural Language API in Google Cloud Console.

  2. Download your service account credentials JSON file.

  3. Set the environment variable to point to your credentials:

export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/credentials.json"

Implementation:

import fetch from 'node-fetch';

const analyzeSentimentWithGoogle = async (text) => {
    const API_URL = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=YOUR_GOOGLE_API_KEY';
    const body = {
        document: { type: 'PLAIN_TEXT', content: text },
        encodingType: 'UTF8',
    };

    const response = await fetch(API_URL, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(body),
    });

    const result = await response.json();
    return result.documentSentiment.score; // Sentiment score (-1 to +1)
};

// Analyze sentiment for a single article
const analyzeArticleSentiment = async (articles) => {
    return Promise.all(
        articles.map(async (article) => ({
            ...article,
            sentimentScore: await analyzeSentimentWithGoogle(`${article.title} ${article.description}`),
        }))
    );
};

Filter Coins Based on Sentiment Score and Risk Tolerance

Filtering coins ensures that investors receive actionable insights tailored to their risk appetite, while considering market trends and public sentiment. Let’s dive deeper into the mechanics of this process.

Understanding the Inputs for Filtering

  1. Sentiment Score
    • A numeric representation of how positively or negatively the market perceives a cryptocurrency.
    • Calculated from news article headlines using sentiment analysis tools.
    • Positive sentiment suggests optimism in the market, while negative sentiment indicates caution.
  2. Risk Tolerance Levels
    • Defined by the user, ranging from ultra-low risk (Level 1) to ultra-high risk (Level 5).
    • Determines the degree of volatility and market uncertainty the investor is willing to accept.
  3. Market Cap Rank
    • A cryptocurrency’s rank based on its total market capitalization.
    • Higher-ranked coins (e.g., Bitcoin, Ethereum) tend to be less volatile and more stable.
    • Lower-ranked coins may offer higher potential returns but come with increased risk.

Risk Levels and Their Impact on Filtering

The risk tolerance level directly influences two key parameters for filtering:

  1. Market Cap Limit

    This restricts the recommendation pool to coins within a specific rank range, ensuring higher-ranked coins (more stable) are prioritized for lower-risk levels.

  2. Sentiment Threshold

    Sets a minimum sentiment score for filtering coins. Coins with scores below this threshold are excluded. This ensures recommendations align with the user’s risk tolerance by considering both market performance and public perception.

Risk Level Breakdown

Risk Level Market Cap Limit Sentiment Threshold Description
1 (Ultra Low) Top 10 coins Sentiment ≥ 0.5 (Highly positive) Focuses on the most stable coins with strong positive sentiment.
2 (Low) Top 20 coins Sentiment ≥ 0.3 (Moderately positive) Includes slightly less stable coins with positive sentiment.
3 (Medium) Top 40 coins Sentiment ≥ 0.0 (Neutral or better) Expands to a broader range of coins with at least neutral sentiment.
4 (High) No cap on market rank Sentiment ≥ -0.2 (Slightly negative) Includes high-risk coins, tolerating mild negative sentiment.
5 (Ultra high) No cap on market rank Sentiment ≥ -0.5 (Negative) Aggressive approach allowing coins with stronger negative sentiment and very high volatility.

Filtering Logic in Code

The filtering process takes a list of enriched cryptocurrencies and applies the risk level criteria:

const filterRecommendations = (coins, riskLevel) => {
  let marketCapLimit, sentimentThreshold;

  switch (riskLevel) {
    case 1: // Ultra low risk
      marketCapLimit = 10;
      sentimentThreshold = 0.5;
      break;
    case 2: // Low risk
      marketCapLimit = 20;
      sentimentThreshold = 0.3;
      break;
    case 3: // Medium risk
      marketCapLimit = 40;
      sentimentThreshold = 0;
      break;
    case 4: // High risk
      marketCapLimit = Infinity;
      sentimentThreshold = -0.2;
      break;
    case 5: // Ultra high risk
      marketCapLimit = Infinity;
      sentimentThreshold = -0.5;
      break;
    default:
      throw new Error("Invalid risk level. Must be between 1 and 5.");
  }

  return coins.filter(
    (coin) =>
      coin.market_cap_rank <= marketCapLimit &&
      coin.sentimentScore >= sentimentThreshold
  );
};

Why This Filtering Process Matters

  1. Aligns Recommendations with Investor Goals

    Tailors coin selection to match risk tolerance, ensuring users receive suggestions aligned with their investment preferences.

  2. Combines Sentiment and Stability

    Balances technical indicators (market cap) with qualitative factors (sentiment analysis) for a well-rounded recommendation.

  3. Encourages Informed Decision-Making

    Ensures that users are not only aware of potential rewards but also cognizant of the risks involved.

Combining Everything: A Unified API Route

To streamline the entire process of fetching cryptocurrency data, retrieving news, performing sentiment analysis, and delivering recommendations based on user risk appetite, we combine all these components into a single, unified API route. This approach simplifies the system’s architecture, reduces redundant processing, and ensures seamless integration.

Unified Workflow

The unified API route handles the following tasks:

  1. Fetch Cryptocurrency Data: Retrieve the top 50 cryptocurrencies by market cap using CoinGecko’s API.
  2. Fetch and Cache News: Fetch news articles for each cryptocurrency, caching the results to optimize performance.
  3. Perform Sentiment Analysis: Analyze the sentiment of the news articles to understand market trends for each cryptocurrency.
  4. Filter Recommendations: Use a risk-based filtering algorithm to suggest cryptocurrencies aligned with the user’s risk appetite.

Implementation:

export default async (req, res) => {
  if (req.method !== "GET") {
    return res.status(405).json({ error: "Method not allowed" });
  }

  const riskLevel = parseInt(req.query.riskLevel, 10);

  if (isNaN(riskLevel) || riskLevel < 1 || riskLevel > 5) {
    return res.status(400).json({
      error: "Invalid risk level. Please provide a value between 1 and 5.",
    });
  }

  try {
    const coins = await fetchCryptoData();
    const enrichedCoins = await enrichWithSentiment(coins);
    const recommendations = filterRecommendations(enrichedCoins, riskLevel);

    res.status(200).json({
      recommendations: recommendations.slice(0, 10),
    });
  } catch (error) {
    console.error("Error fetching cryptocurrency data:", error);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

Conclusion

Building a cryptocurrency recommendation system that considers market sentiment and user risk appetite is a powerful way to deliver personalized insights in the fast-paced world of digital assets. By leveraging APIs like CoinGecko for real-time cryptocurrency data, NewsAPI for fetching relevant news, and sentiment analysis tools like Sentiment.js or Google Natural Language API, we can create a robust system that helps users make informed investment decisions.

Caching mechanisms ensure efficient use of resources, while modular design allows for easy scalability and adaptability as the market evolves. The unified API approach not only simplifies development but also ensures that users receive timely and accurate recommendations tailored to their preferences.

This solution demonstrates the synergy of modern web technologies and data-driven approaches in delivering impactful, cost-effective applications. By integrating real-time data analysis with actionable insights, we empower users to navigate the complexities of the cryptocurrency market with confidence.

Disclaimer

This cryptocurrency recommendation system is intended for educational and informational purposes only. It does not constitute financial advice. Cryptocurrencies are inherently volatile, and investment decisions should always be based on thorough research and consultation with a financial advisor. The system provides insights based on sentiment and trends but cannot predict future market performance. Use the recommendations responsibly and at your own discretion.

Github Repository

https://github.com/Hivekind/stock-crypto-recommendation

Need help building your product?

Reach out to us by filling out the form on our contact page. If you need an NDA, just let us know, and we’ll gladly provide one!

Top software development company Malaysia awards
Loading...