DEV Community

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

Posted on • Updated on

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.

Top comments (0)