calcRSI static method

void calcRSI(
  1. List<KLineEntity> dataList
)

Implementation

static void calcRSI(List<KLineEntity> dataList) {
  double? rsi;
  double rsiABSEma = 0;
  double rsiMaxEma = 0;
  for (int i = 0; i < dataList.length; i++) {
    KLineEntity entity = dataList[i];
    final double closePrice = entity.close;
    if (i == 0) {
      rsi = 0;
      rsiABSEma = 0;
      rsiMaxEma = 0;
    } else {
      double rMax = max(0, closePrice - dataList[i - 1].close.toDouble());
      double rAbs = (closePrice - dataList[i - 1].close.toDouble()).abs();

      rsiMaxEma = (rMax + (14 - 1) * rsiMaxEma) / 14;
      rsiABSEma = (rAbs + (14 - 1) * rsiABSEma) / 14;
      rsi = (rsiMaxEma / rsiABSEma) * 100;
    }
    if (i < 13) rsi = null;
    if (rsi != null && rsi.isNaN) rsi = null;
    entity.rsi = rsi;
  }
}