From 7362e8d5e24679a732dcf2d5a1120367db65a77c Mon Sep 17 00:00:00 2001 From: Lemuel Date: Fri, 11 Mar 2022 18:57:27 +0800 Subject: [PATCH] move heikin ashi formatter to its own file --- src/components/charts/CandleChart.vue | 4 +-- src/shared/formatters.ts | 36 --------------------------- src/shared/heikinashi.ts | 34 +++++++++++++++++++++++++ src/types/types.ts | 2 +- 4 files changed, 37 insertions(+), 39 deletions(-) create mode 100644 src/shared/heikinashi.ts diff --git a/src/components/charts/CandleChart.vue b/src/components/charts/CandleChart.vue index e49d4ae0..b25a0933 100644 --- a/src/components/charts/CandleChart.vue +++ b/src/components/charts/CandleChart.vue @@ -9,7 +9,7 @@ import { Component, Vue, Prop, Watch } from 'vue-property-decorator'; import { Trade, PairHistory, PlotConfig } from '@/types'; import randomColor from '@/shared/randomColor'; import { roundTimeframe } from '@/shared/timemath'; -import { heikinAshiDataset } from '@/shared/formatters'; +import heikinashi from '@/shared/heikinashi'; import ECharts from 'vue-echarts'; import { use } from 'echarts/core'; @@ -353,7 +353,7 @@ export default class CandleChart extends Vue { const options: EChartsOption = { dataset: { source: this.heikinAshi - ? heikinAshiDataset(this.datasetColumns, this.dataset.data) + ? heikinashi(this.datasetColumns, this.dataset.data) : this.dataset.data, }, grid: [ diff --git a/src/shared/formatters.ts b/src/shared/formatters.ts index 2419a8dc..e140b7b4 100644 --- a/src/shared/formatters.ts +++ b/src/shared/formatters.ts @@ -112,41 +112,6 @@ export function humanizeDurationFromSeconds(duration: number): string { return humanizeDuration(duration * 1000); } -export function heikinAshiDataset(columns: string[], data: Array) { - const openIdx = columns.indexOf('open'); - const closeIdx = columns.indexOf('close'); - const highIdx = columns.indexOf('high'); - const lowIdx = columns.indexOf('low'); - - // Prevent mutation of original data - const dataCopy = data.map((original) => original.slice()); - - return dataCopy.map((candle, idx, candles) => { - 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 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]); - - candle[openIdx] = open; - candle[closeIdx] = close; - candle[highIdx] = high; - candle[lowIdx] = low; - - return candle; - }); -} - export default { formatPrice, formatPercent, @@ -155,5 +120,4 @@ export default { timestampToDateString, dateStringToTimeRange, setTimezone, - heikinAshiDataset, }; diff --git a/src/shared/heikinashi.ts b/src/shared/heikinashi.ts new file mode 100644 index 00000000..f0607f06 --- /dev/null +++ b/src/shared/heikinashi.ts @@ -0,0 +1,34 @@ +export default function heikinAshiDataset(columns: string[], data: Array) { + const openIdx = columns.indexOf('open'); + const closeIdx = columns.indexOf('close'); + const highIdx = columns.indexOf('high'); + const lowIdx = columns.indexOf('low'); + + // Prevent mutation of original data + const dataCopy = data.map((original) => original.slice()); + + return dataCopy.map((candle, idx, candles) => { + 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 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]); + + candle[openIdx] = open; + candle[closeIdx] = close; + candle[highIdx] = high; + candle[lowIdx] = low; + + return candle; + }); +} diff --git a/src/types/types.ts b/src/types/types.ts index 4abc3e52..5f33a560 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -186,7 +186,7 @@ export interface PairHistory { timeframe: string; timeframe_ms: number; columns: string[]; - data: number[]; + data: Array; length: number; /** Number of buy signals in this response */ buy_signals: number;