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