From a7fecbeba66beb23e664b83bd79f19e06472b9fd Mon Sep 17 00:00:00 2001 From: Julien Gainza Date: Sun, 9 Oct 2022 12:38:34 +0200 Subject: [PATCH] fix for heikin ashi calculation --- src/shared/heikinashi.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/shared/heikinashi.ts b/src/shared/heikinashi.ts index 4c60974a..c0966f62 100644 --- a/src/shared/heikinashi.ts +++ b/src/shared/heikinashi.ts @@ -4,23 +4,15 @@ export default function heikinAshiDataset(columns: string[], data: Array { + let prevCandle: number[]; + return data.map((original, idx) => { // Prevent mutation of original data const candle = original.slice(); - - if (idx === 0) { - const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4; - const open = (candle[openIdx] + candle[closeIdx]) / 2; - - candle[openIdx] = open; - candle[closeIdx] = close; - - return candle; - } - - const prevCandle = candles[idx - 1]; + const open = + idx === 0 + ? (candle[openIdx] + candle[closeIdx]) / 2 + : (prevCandle[openIdx] + prevCandle[closeIdx]) / 2; const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4; - const open = (prevCandle[openIdx] + prevCandle[closeIdx]) / 2; const high = Math.max(candle[highIdx], candle[openIdx], candle[closeIdx]); const low = Math.min(candle[lowIdx], candle[openIdx], candle[closeIdx]); @@ -29,6 +21,8 @@ export default function heikinAshiDataset(columns: string[], data: Array