calcMA static method

dynamic calcMA(
  1. List<KLineEntity> dataList,
  2. List<int> maDayList
)

Implementation

static calcMA(List<KLineEntity> dataList, List<int> maDayList) {
  List<double> ma = List<double>.filled(maDayList.length, 0);

  if (dataList.isNotEmpty) {
    for (int i = 0; i < dataList.length; i++) {
      KLineEntity entity = dataList[i];
      final closePrice = entity.close;
      entity.maValueList = List<double>.filled(maDayList.length, 0);

      for (int j = 0; j < maDayList.length; j++) {
        ma[j] += closePrice;
        if (i == maDayList[j] - 1) {
          entity.maValueList?[j] = ma[j] / maDayList[j];
        } else if (i >= maDayList[j]) {
          ma[j] -= dataList[i - maDayList[j]].close;
          entity.maValueList?[j] = ma[j] / maDayList[j];
        } else {
          entity.maValueList?[j] = 0;
        }
      }
    }
  }
}