I am looking to write an RSI indicator with input as RSI based array of pine script language like below:
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput) down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
where the function rma is defined as:
pine_rma(src, length) => alpha = 1/length sum = 0.0 sum := na(sum[1]) ? ta.sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1])
This RSI calculation is not the same as the theories I learned, is there any way to convert it to mql5 correctly? here is the code I am writing:
`double SimpleMA(double &data[], int period, int shift = 0)
{
if (ArraySize(data) < shift + period) {
Print("Not enough data in data[]");
return(-1);
}
double sum = 0.0;
for (int i = shift; i < shift + period; i++)
{
sum += data[i];
}
return sum / period;
}
double RMA(double &data[], int period, int shift = 0)
{
double alpha = 1.0 / (double)period;
double sum = SimpleMA(data, period, shift); // giả sử bạn đã viết hàm SimpleMA
for(int i = shift + period; i < ArraySize(data); i++)
{
sum = alpha * data[i] + (1 - alpha) * sum;
}
return sum;
}
double CustomRSI(double &haClose[], int period, int shift = 0)
{
if (ArraySize(haClose) < shift + period) {
Print("Not enough data in haClose[]");
return(-1);
}
double gain[], loss[];
ArrayResize(gain, period);
ArrayResize(loss, period);
for (int i = shift; i < shift + period; i++)
{
double change = haClose[i] - haClose[i+1];
if (change > 0) {
gain[i-shift] = change;
loss[i-shift] = 0;
} else {
gain[i-shift] = 0;
loss[i-shift] = -change;
}
}
double avgGain = RMA(gain, period);
double avgLoss = RMA(loss, period);
double rs = (avgGain + avgLoss) == 0 ? 0 : avgGain / avgLoss;
double rsi = 100 - (100 / (1 + rs));
return rsi;
}`
please give me advice. thanks very much!
If you have any ideas please help me.Thanks very much.
Viet Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.