This is python code
PYTHON
import pandas as pd
def get_rsi(ohlcv, period, st):
ohlcv["close"] = ohlcv["close"]
delta = ohlcv["close"].diff()
up, down = delta.copy(), delta.copy()
up[up < 0] = 0
down[down > 0] = 0
_gain = up.ewm(com=(period - 1), min_periods=period).mean()
_loss = down.abs().ewm(com=(period - 1), min_periods=period).mean()
RS = _gain / _loss
return float(pd.Series(100 - (100 / (1 + RS)), name="RSI").iloc[st])
JAVASCRIPT
export function getRSI(ohlcv, period) {
if (ohlcv.length < period + 1) {
throw new Error("Data length must be greater than period");
}
let gains = [];
let losses = [];
for (let i = 1; i < ohlcv.length; i++) {
let diff = ohlcv[i].close - ohlcv[i - 1].close;
if (diff > 0) {
gains.push(diff);
losses.push(0);
} else if (diff < 0) {
gains.push(0);
losses.push(Math.abs(diff));
} else {
gains.push(0);
losses.push(0);
}
}
let avgGain = gains.slice(0, period).reduce((acc, cur) => acc + cur, 0) / period;
let avgLoss = losses.slice(0, period).reduce((acc, cur) => acc + cur, 0) / period;
let RS = avgGain / avgLoss;
let RSI = 100 - 100 / (1 + RS);
return RSI;
}
do you know the reason..?
I tried to test it
But RSI results were different
there is no Pandas for Javascript.
The JavaScript code generated through ChatGPT isn’t effective. Could you please help me understand the reason behind it?