DEV Community

Cover image for Calculate the Fear and Greed Index with JavaScript
Mustafa Onur Çelik
Mustafa Onur Çelik

Posted on • Edited on

5 1

Calculate the Fear and Greed Index with JavaScript

To calculate the Fear and Greed Index with JavaScript, you can use a combination of seven factors:

  1. Market volatility (VIX): This is a measure of the market's expectation of future volatility.

  2. Put and call options: This is a ratio of the volume of put options to call options, which indicates investor sentiment.

  3. Market breadth: This is a measure of the percentage of stocks that are advancing or declining.

  4. Junk bonds: This is a measure of the demand for high-yield (junk) bonds.

  5. Safe haven demand: This is a measure of the demand for safe haven assets such as gold and Treasury bonds.

  6. Treasury yield: This is a measure of the return on Treasury bonds, which indicates investor confidence in the market.

  7. Stock price to earnings ratio (PE): This is a measure of the value of stocks relative to their earnings.

To calculate the Fear and Greed Index, you need to first calculate the value of each of these factors, and then weight and combine them to get a final score between 0 and 100. A score of 0 indicates extreme fear, and a score of 100 indicates extreme greed.

Here's an example of how you can implement this calculation with JavaScript:

function calculateFearAndGreed(vix, putCallRatio, marketBreadth, junkBonds,
  safeHavenDemand, treasuryYield, peRatio) {
  // Calculate the Fear and Greed Index
  const fearAndGreed = (
    (vix * 0.05) +
    (putCallRatio * 0.1) +
    (marketBreadth * 0.15) +
    (junkBonds * 0.2) +
    (safeHavenDemand * 0.25) +
    (treasuryYield * 0.15) +
    (peRatio * 0.1)
  );

  return fearAndGreed;
}

// Example usage
const vix = 20;
const putCallRatio = 1;
const marketBreadth = 0.8;
const junkBonds = 0.6;
const safeHavenDemand = 0.2;
const treasuryYield = 0.1;
const peRatio = 20;
const fearAndGreed = calculateFearAndGreed(
  vix, putCallRatio, marketBreadth, junkBonds, safeHavenDemand, treasuryYield, peRatio
);
console.log(fearAndGreed); // Output: 50

Enter fullscreen mode Exit fullscreen mode

This code calculates the Fear and Greed Index by applying the appropriate weights to each of the seven factors and combining them to get a final score. You can adjust the weighting factors to suit your needs.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️