From ad2f169e1646a5aa31aa43db484e1539049a15f2 Mon Sep 17 00:00:00 2001 From: wangyiqian Date: Wed, 31 Aug 2022 19:43:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0BOLL=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stockchart/index/BollCalculator.kt | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/index/BollCalculator.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/index/BollCalculator.kt index 56e7b67..3dd899c 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/index/BollCalculator.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/index/BollCalculator.kt @@ -57,24 +57,19 @@ object BollCalculator: ICalculator { sum += kEntity.getClosePrice() - if (pEnd - pFrom + 1 == n) { - val ma = sum / n - result[mbIdx][kEntityIdx] = ma + var ma = sum / (pEnd - pFrom + 1) + result[mbIdx][kEntityIdx] = ma + if(pEnd - pFrom + 1 == n){ sum -= input[pFrom].getClosePrice() pFrom += 1 - - var squareSum = 0f - for (i in pEnd downTo pEnd - n + 1) { - squareSum += (input[i].getClosePrice() - ma).pow(2) - } - val std = sqrt(squareSum / n) - result[upIdx][kEntityIdx] = ma + k * std - result[dnIdx][kEntityIdx] = ma - k * std - } else { - result[mbIdx][kEntityIdx] = null - result[upIdx][kEntityIdx] = null - result[dnIdx][kEntityIdx] = null } + var squareSum = 0f + for (i in pEnd downTo pFrom) { + squareSum += (input[i].getClosePrice() - ma).pow(2) + } + val std = sqrt(squareSum / n) + result[upIdx][kEntityIdx] = ma + k * std + result[dnIdx][kEntityIdx] = ma - k * std } return result }