Skip to content

Instantly share code, notes, and snippets.

@charlesdarkwind
Last active September 24, 2018 11:38
Show Gist options
  • Save charlesdarkwind/504463750296bc816ea31ad2c1d2e5c1 to your computer and use it in GitHub Desktop.
Save charlesdarkwind/504463750296bc816ea31ad2c1d2e5c1 to your computer and use it in GitHub Desktop.
RSI Javascript relative strenght index node nodejs
const inputs = { rsi: 8 };
main['rsi_' + pair] = generateRSI(pair);
const generateRSI = pair => {
const closes = main.closes[pair];
const len = closes.length;
const arr = [];
let prevAvgGain = 0, prevAvgLoss = 0;
for (let j = 250 + inputs.rsi; j > 250; j--) { // first run
let losses = 0, gains = 0;
const previous = closes[len - (j + 1)]; // -259
const present = closes[len - j]; // -258
if (previous < present) gains += present - previous // If price went up, add gain amount
else if (previous > present) losses += previous - present // If price went down, add loss amount
prevAvgGain = gains / inputs.rsi;
prevAvgLoss = losses / inputs.rsi;
}
for (let i = 250; i > 0; i--) {
let loss = 0, gain = 0;
const previous = closes[len - (i + 1)];
const present = closes[len - i];
if (previous < present) gain = present - previous;
else if (previous > present) loss = previous - present;
prevAvgGain = (prevAvgGain * (inputs.rsi - 1) + gain) / inputs.rsi;
prevAvgLoss = (prevAvgLoss * (inputs.rsi - 1) + loss) / inputs.rsi;
const rs = prevAvgGain / prevAvgLoss;
arr.push(100 - (100 / (1 + rs)));
}
return arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment