fix for heikin ashi calculation

This commit is contained in:
Julien Gainza 2022-10-09 12:38:34 +02:00
parent 8469b2108e
commit a7fecbeba6

View File

@ -4,23 +4,15 @@ export default function heikinAshiDataset(columns: string[], data: Array<number[
const highIdx = columns.indexOf('high');
const lowIdx = columns.indexOf('low');
return data.map((original, idx, candles) => {
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<number[
candle[highIdx] = high;
candle[lowIdx] = low;
prevCandle = candle.slice();
return candle;
});
}