Menu Close

JavaScript Finance: Calculate RSI value

To calculate the RSI index value, you need to first compute the average gain and the average loss over a specified number of periods. You can then use these values to calculate the relative strength and the relative strength index (RSI) using the following formula:

RSI = 100 – 100 / (1 + (average gain / average loss))

To compute the average gain and the average loss, you need to first determine the change in price for each period. If the price increases, the change is positive and is added to the total gain. If the price decreases, the change is negative and is added to the total loss. The average gain and average loss are then calculated by dividing the total gain and total loss by the number of periods used to compute the RSI.

For example, if you are calculating the RSI using 14 periods, you would first compute the change in price for each of the last 14 periods. If the price increased by $1 in a period, that would be added to the total gain. If the price decreased by $1 in a period, that would be added to the total loss. Once you have the total gain and total loss, you would then divide each by 14 to get the average gain and average loss. Finally, you would use the formula above to calculate the RSI.

It is important to note that the RSI is an oscillator, meaning that it fluctuates between 0 and 100. The RSI is considered overbought when it is above 70 and oversold when it is below 30. This can be used as a guide to determine potential buying and selling opportunities.

function rsi(prices, period) {
  const gains = [];
  const losses = [];

  for (let i = 1; i < prices.length; i++) {
    const change = prices[i] - prices[i - 1];
    if (change > 0) {
      gains.push(change);
    } else {
      losses.push(change);
    }
  }

  const avgGain = average(gains.slice(0, period));
  const avgLoss = average(losses.slice(0, period).map(Math.abs));
  const rs = avgGain / avgLoss;

  return 100 - (100 / (1 + rs));
}

function average(values) {
  return values.reduce((total, value) => total + value) / values.length;
}

This code calculates the RSI value for the given list of prices over a specified period. It first computes the gains and losses for each price change, and then calculates the average gain and average loss for the specified period. The relative strength (RS) is then calculated as the ratio of the average gain to the average loss. Finally, the RSI value is calculated as 100 minus 100 divided by the sum of 1 and the RS.

To use this code, you can call the rsi function with a list of prices and a period as arguments, for example:

const prices = [100, 105, 110, 115, 120, 130, 135];
const period = 5;
const rsiValue = rsi(prices, period);

This will calculate the RSI value for the given prices list over a period of 5. The resulting rsiValue will be a number between 0 and 100, indicating the relative strength of the stock. A value below 30 is considered oversold, while a value above 70 is considered overbought.

function rsiBuySellDecision(rsi) {
  if (rsi < 30) {
    return 'BUY';
  } else if (rsi > 70) {
    return 'SELL';
  } else {
    return 'HOLD';
  }
}

Note that this is just an example, and the RSI thresholds for making buy or sell decisions may vary depending on the specific strategy being used. It is also worth mentioning that RSI should not be used in isolation, but rather as part of a broader analysis that takes into account other factors such as the stock’s price and volume, as well as market conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *